Skip to content

Commit 076ce77

Browse files
mockersfItsDoot
authored andcommitted
add Input Method Editor support (bevyengine#7325)
# Objective - Fix bevyengine#7315 - Add IME support ## Solution - Add two new fields to `Window`, to control if IME is enabled and the candidate box position This allows the use of dead keys which are needed in French, or the full IME experience to type using Pinyin I also added a basic general text input example that can handle IME input. https://user-images.githubusercontent.com/8672791/213941353-5ed73a73-5dd1-4e66-a7d6-a69b49694c52.mp4
1 parent 9bb1c67 commit 076ce77

File tree

8 files changed

+316
-5
lines changed

8 files changed

+316
-5
lines changed

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,16 @@ description = "Prints out all touch inputs"
11471147
category = "Input"
11481148
wasm = false
11491149

1150+
[[example]]
1151+
name = "text_input"
1152+
path = "examples/input/text_input.rs"
1153+
1154+
[package.metadata.example.text_input]
1155+
name = "Text Input"
1156+
description = "Simple text input with IME support"
1157+
category = "Input"
1158+
wasm = false
1159+
11501160
# Reflection
11511161
[[example]]
11521162
name = "reflection"

crates/bevy_window/src/event.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,52 @@ pub struct ReceivedCharacter {
153153
pub char: char,
154154
}
155155

156+
/// A Input Method Editor event.
157+
///
158+
/// This event is the translated version of the `WindowEvent::Ime` from the `winit` crate.
159+
///
160+
/// It is only sent if IME was enabled on the window with [`Window::ime_enabled`](crate::window::Window::ime_enabled).
161+
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
162+
#[reflect(Debug, PartialEq)]
163+
#[cfg_attr(
164+
feature = "serialize",
165+
derive(serde::Serialize, serde::Deserialize),
166+
reflect(Serialize, Deserialize)
167+
)]
168+
pub enum Ime {
169+
/// Notifies when a new composing text should be set at the cursor position.
170+
Preedit {
171+
/// Window that received the event.
172+
window: Entity,
173+
/// Current value.
174+
value: String,
175+
/// Cursor begin and end position.
176+
///
177+
/// `None` indicated the cursor should be hidden
178+
cursor: Option<(usize, usize)>,
179+
},
180+
/// Notifies when text should be inserted into the editor widget.
181+
Commit {
182+
/// Window that received the event.
183+
window: Entity,
184+
/// Input string
185+
value: String,
186+
},
187+
/// Notifies when the IME was enabled.
188+
///
189+
/// After this event, you will receive events `Ime::Preedit` and `Ime::Commit`,
190+
/// and stop receiving events [`ReceivedCharacter`].
191+
Enabled {
192+
/// Window that received the event.
193+
window: Entity,
194+
},
195+
/// Notifies when the IME was disabled.
196+
Disabled {
197+
/// Window that received the event.
198+
window: Entity,
199+
},
200+
}
201+
156202
/// An event that indicates a window has received or lost focus.
157203
#[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)]
158204
#[reflect(Debug, PartialEq)]

crates/bevy_window/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub use window::*;
1515
pub mod prelude {
1616
#[doc(hidden)]
1717
pub use crate::{
18-
CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, MonitorSelection,
18+
CursorEntered, CursorIcon, CursorLeft, CursorMoved, FileDragAndDrop, Ime, MonitorSelection,
1919
ReceivedCharacter, Window, WindowMoved, WindowPlugin, WindowPosition,
2020
WindowResizeConstraints,
2121
};
@@ -79,6 +79,7 @@ impl Plugin for WindowPlugin {
7979
.add_event::<CursorEntered>()
8080
.add_event::<CursorLeft>()
8181
.add_event::<ReceivedCharacter>()
82+
.add_event::<Ime>()
8283
.add_event::<WindowFocused>()
8384
.add_event::<WindowScaleFactorChanged>()
8485
.add_event::<WindowBackendScaleFactorChanged>()

crates/bevy_window/src/window.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ pub struct Window {
167167
pub prevent_default_event_handling: bool,
168168
/// Stores internal state that isn't directly accessible.
169169
pub internal: InternalWindowState,
170+
/// Should the window use Input Method Editor?
171+
///
172+
/// If enabled, the window will receive [`Ime`](crate::Ime) events instead of
173+
/// [`ReceivedCharacter`](crate::ReceivedCharacter) or
174+
/// [`KeyboardInput`](bevy_input::keyboard::KeyboardInput).
175+
///
176+
/// IME should be enabled during text input, but not when you expect to get the exact key pressed.
177+
///
178+
/// ## Platform-specific
179+
///
180+
/// - iOS / Android / Web: Unsupported.
181+
pub ime_enabled: bool,
182+
/// Sets location of IME candidate box in client area coordinates relative to the top left.
183+
///
184+
/// ## Platform-specific
185+
///
186+
/// - iOS / Android / Web: Unsupported.
187+
pub ime_position: Vec2,
170188
}
171189

172190
impl Default for Window {
@@ -181,6 +199,8 @@ impl Default for Window {
181199
internal: Default::default(),
182200
composite_alpha_mode: Default::default(),
183201
resize_constraints: Default::default(),
202+
ime_enabled: Default::default(),
203+
ime_position: Default::default(),
184204
resizable: true,
185205
decorations: true,
186206
transparent: false,

crates/bevy_winit/src/lib.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ use bevy_utils::{
2525
Instant,
2626
};
2727
use bevy_window::{
28-
CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ModifiesWindows, ReceivedCharacter,
29-
RequestRedraw, Window, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated,
30-
WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged,
28+
CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, Ime, ModifiesWindows,
29+
ReceivedCharacter, RequestRedraw, Window, WindowBackendScaleFactorChanged,
30+
WindowCloseRequested, WindowCreated, WindowFocused, WindowMoved, WindowResized,
31+
WindowScaleFactorChanged,
3132
};
3233

3334
use winit::{
@@ -170,6 +171,7 @@ struct InputEvents<'w> {
170171
mouse_button_input: EventWriter<'w, MouseButtonInput>,
171172
mouse_wheel_input: EventWriter<'w, MouseWheel>,
172173
touch_input: EventWriter<'w, TouchInput>,
174+
ime_input: EventWriter<'w, Ime>,
173175
}
174176

175177
#[derive(SystemParam)]
@@ -555,6 +557,25 @@ pub fn winit_runner(mut app: App) {
555557
position,
556558
});
557559
}
560+
WindowEvent::Ime(event) => match event {
561+
event::Ime::Preedit(value, cursor) => {
562+
input_events.ime_input.send(Ime::Preedit {
563+
window: window_entity,
564+
value,
565+
cursor,
566+
});
567+
}
568+
event::Ime::Commit(value) => input_events.ime_input.send(Ime::Commit {
569+
window: window_entity,
570+
value,
571+
}),
572+
event::Ime::Enabled => input_events.ime_input.send(Ime::Enabled {
573+
window: window_entity,
574+
}),
575+
event::Ime::Disabled => input_events.ime_input.send(Ime::Disabled {
576+
window: window_entity,
577+
}),
578+
},
558579
_ => {}
559580
}
560581
}

crates/bevy_winit/src/system.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bevy_window::{RawHandleWrapper, Window, WindowClosed, WindowCreated};
1313
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
1414

1515
use winit::{
16-
dpi::{LogicalSize, PhysicalPosition, PhysicalSize},
16+
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize},
1717
event_loop::EventLoopWindowTarget,
1818
};
1919

@@ -278,6 +278,17 @@ pub(crate) fn changed_window(
278278
);
279279
}
280280

281+
if window.ime_enabled != previous.ime_enabled {
282+
winit_window.set_ime_allowed(window.ime_enabled);
283+
}
284+
285+
if window.ime_position != previous.ime_position {
286+
winit_window.set_ime_position(LogicalPosition::new(
287+
window.ime_position.x,
288+
window.ime_position.y,
289+
));
290+
}
291+
281292
info.previous = window.clone();
282293
}
283294
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ Example | Description
234234
[Mouse Grab](../examples/input/mouse_grab.rs) | Demonstrates how to grab the mouse, locking the cursor to the app's screen
235235
[Mouse Input](../examples/input/mouse_input.rs) | Demonstrates handling a mouse button press/release
236236
[Mouse Input Events](../examples/input/mouse_input_events.rs) | Prints out all mouse events (buttons, movement, etc.)
237+
[Text Input](../examples/input/text_input.rs) | Simple text input with IME support
237238
[Touch Input](../examples/input/touch_input.rs) | Displays touch presses, releases, and cancels
238239
[Touch Input Events](../examples/input/touch_input_events.rs) | Prints out all touch inputs
239240

0 commit comments

Comments
 (0)