Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - add Input Method Editor support #7325

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions crates/bevy_window/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,52 @@ pub struct ReceivedCharacter {
pub char: char,
}

/// A Input Method Editor event.
///
/// This event is the translated version of the `WindowEvent::Ime` from the `winit` crate.
///
/// It is only sent if IME was enabled on the window with [`Window::ime_enabled`](crate::window::Window::ime_enabled).
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
#[reflect(Debug, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub enum Ime {
/// Notifies when a new composing text should be set at the cursor position.
Preedit {
/// Window that received the event.
window: Entity,
/// Current value.
value: String,
/// Cursor begin and end position.
///
/// `None` indicated the cursor should be hidden
cursor: Option<(usize, usize)>,
},
/// Notifies when text should be inserted into the editor widget.
Commit {
/// Window that received the event.
window: Entity,
/// Input string
value: String,
},
/// Notifies when the IME was enabled.
///
/// After this event, you will receive events `Ime::Preedit` and `Ime::Commit`,
/// and stop receiving events [`ReceivedCharacter`].
Enabled {
/// Window that received the event.
window: Entity,
},
/// Notifies when the IME was disabled.
Disabled {
/// Window that received the event.
window: Entity,
},
}

/// An event that indicates a window has received or lost focus.
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
#[reflect(Debug, PartialEq)]
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub use window::*;
pub mod prelude {
#[doc(hidden)]
pub use crate::{
CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, MonitorSelection,
CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, Ime, MonitorSelection,
ReceivedCharacter, Window, WindowMoved, WindowPlugin, WindowPosition,
WindowResizeConstraints,
};
Expand Down Expand Up @@ -79,6 +79,7 @@ impl Plugin for WindowPlugin {
.add_event::<CursorEntered>()
.add_event::<CursorLeft>()
.add_event::<ReceivedCharacter>()
.add_event::<Ime>()
.add_event::<WindowFocused>()
.add_event::<WindowScaleFactorChanged>()
.add_event::<WindowBackendScaleFactorChanged>()
Expand Down
20 changes: 20 additions & 0 deletions crates/bevy_window/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ pub struct Window {
pub fit_canvas_to_parent: bool,
/// Stores internal state that isn't directly accessible.
pub internal: InternalWindowState,
/// Should the window use Input Method Editor?
///
/// If enabled, the window will receive [`Ime`](crate::Ime) events instead of
/// [`ReceivedCharacter`](crate::ReceivedCharacter) or
/// [`KeyboardInput`](bevy_input::keyboard::KeyboardInput).
///
/// IME should be enabled during text input, but not when you expect to get the exact key pressed.
///
/// ## Platform-specific
///
/// - iOS / Android / Web: Unsupported.
pub ime_enabled: bool,
mockersf marked this conversation as resolved.
Show resolved Hide resolved
/// Sets location of IME candidate box in client area coordinates relative to the top left.
///
/// ## Platform-specific
///
/// - iOS / Android / Web: Unsupported.
pub ime_position: Vec2,
}

impl Default for Window {
Expand All @@ -174,6 +192,8 @@ impl Default for Window {
internal: Default::default(),
composite_alpha_mode: Default::default(),
resize_constraints: Default::default(),
ime_enabled: Default::default(),
ime_position: Default::default(),
resizable: true,
decorations: true,
transparent: false,
Expand Down
27 changes: 24 additions & 3 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ use bevy_utils::{
Instant,
};
use bevy_window::{
CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ModifiesWindows, ReceivedCharacter,
RequestRedraw, Window, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated,
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged,
CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime, ModifiesWindows,
ReceivedCharacter, RequestRedraw, Window, WindowBackendScaleFactorChanged,
WindowCloseRequested, WindowCreated, WindowFocused, WindowMoved, WindowResized,
WindowScaleFactorChanged,
};

use winit::{
Expand Down Expand Up @@ -170,6 +171,7 @@ struct InputEvents<'w> {
mouse_button_input: EventWriter<'w, MouseButtonInput>,
mouse_wheel_input: EventWriter<'w, MouseWheel>,
touch_input: EventWriter<'w, TouchInput>,
ime_input: EventWriter<'w, Ime>,
}

#[derive(SystemParam)]
Expand Down Expand Up @@ -555,6 +557,25 @@ pub fn winit_runner(mut app: App) {
position,
});
}
WindowEvent::Ime(event) => match event {
event::Ime::Preedit(value, cursor) => {
input_events.ime_input.send(Ime::Preedit {
window: window_entity,
value,
cursor,
});
}
event::Ime::Commit(value) => input_events.ime_input.send(Ime::Commit {
window: window_entity,
value,
}),
event::Ime::Enabled => input_events.ime_input.send(Ime::Enabled {
window: window_entity,
}),
event::Ime::Disabled => input_events.ime_input.send(Ime::Disabled {
window: window_entity,
}),
},
_ => {}
}
}
Expand Down
13 changes: 12 additions & 1 deletion crates/bevy_winit/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_window::{RawHandleWrapper, Window, WindowClosed, WindowCreated};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};

use winit::{
dpi::{LogicalSize, PhysicalPosition, PhysicalSize},
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize},
event_loop::EventLoopWindowTarget,
};

Expand Down Expand Up @@ -278,6 +278,17 @@ pub(crate) fn changed_window(
);
}

if window.ime_enabled != previous.ime_enabled {
winit_window.set_ime_allowed(window.ime_enabled);
}

if window.ime_position != previous.ime_position {
winit_window.set_ime_position(LogicalPosition::new(
window.ime_position.x,
window.ime_position.y,
));
}

info.previous = window.clone();
}
}
Expand Down