Skip to content

Commit d2975ca

Browse files
committed
add Input Method Editor support (#7325)
# Objective - Fix #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 85989f6 commit d2975ca

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

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
}

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
}

0 commit comments

Comments
 (0)