Skip to content

Commit 74851a4

Browse files
committed
Wire up ReceivedImeText
1 parent e8d14a4 commit 74851a4

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ features = [
104104
'console',
105105
"AddEventListenerOptions",
106106
'CssStyleDeclaration',
107+
'CompositionEvent',
107108
'BeforeUnloadEvent',
108109
'Document',
109110
'DomRect',

src/platform_impl/web/event_loop/window_target.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ impl<T> WindowTarget<T> {
112112
})
113113
});
114114

115-
// TODO: What to do here?
116-
// let runner = self.runner.clone();
117-
// canvas.on_received_character(move |char_code| {
118-
// runner.send_event(Event::WindowEvent {
119-
// window_id: WindowId(id),
120-
// event: WindowEvent::ReceivedCharacter(char_code),
121-
// });
122-
// });
115+
let runner = self.runner.clone();
116+
canvas.on_composition_end(move |data| {
117+
if let Some(data) = data {
118+
runner.send_event(Event::WindowEvent {
119+
window_id: WindowId(id),
120+
event: WindowEvent::ReceivedImeText(data),
121+
});
122+
}
123+
});
123124

124125
let runner = self.runner.clone();
125126
canvas.on_cursor_leave(move |pointer_id| {

src/platform_impl/web/web_sys/canvas.rs

+15-33
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::rc::Rc;
1212

1313
use wasm_bindgen::{closure::Closure, JsCast};
1414
use web_sys::{
15-
AddEventListenerOptions, Event, FocusEvent, HtmlCanvasElement, KeyboardEvent,
15+
AddEventListenerOptions, CompositionEvent, Event, FocusEvent, HtmlCanvasElement, KeyboardEvent,
1616
MediaQueryListEvent, MouseEvent, WheelEvent,
1717
};
1818

@@ -26,7 +26,7 @@ pub struct Canvas {
2626
on_blur: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
2727
on_keyboard_release: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
2828
on_keyboard_press: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
29-
on_received_character: Option<EventListenerHandle<dyn FnMut(KeyboardEvent)>>,
29+
on_composition_end: Option<EventListenerHandle<dyn FnMut(CompositionEvent)>>,
3030
on_mouse_wheel: Option<EventListenerHandle<dyn FnMut(WheelEvent)>>,
3131
on_fullscreen_change: Option<EventListenerHandle<dyn FnMut(Event)>>,
3232
on_dark_mode: Option<MediaQueryListHandle>,
@@ -82,7 +82,7 @@ impl Canvas {
8282
on_focus: None,
8383
on_keyboard_release: None,
8484
on_keyboard_press: None,
85-
on_received_character: None,
85+
on_composition_end: None,
8686
on_mouse_wheel: None,
8787
on_fullscreen_change: None,
8888
on_dark_mode: None,
@@ -175,17 +175,7 @@ impl Canvas {
175175
self.on_keyboard_press = Some(self.common.add_user_event(
176176
"keydown",
177177
move |event: KeyboardEvent| {
178-
// event.prevent_default() would suppress subsequent on_received_character() calls. That
179-
// supression is correct for key sequences like Tab/Shift-Tab, Ctrl+R, PgUp/Down to
180-
// scroll, etc. We should not do it for key sequences that result in meaningful character
181-
// input though.
182-
let event_key = &event.key();
183-
let is_key_string = event_key.len() == 1 || !event_key.is_ascii();
184-
let is_shortcut_modifiers =
185-
(event.ctrl_key() || event.alt_key()) && !event.get_modifier_state("AltGr");
186-
if !is_key_string || is_shortcut_modifiers {
187-
event.prevent_default();
188-
}
178+
event.prevent_default();
189179
handler(
190180
event::key_code(&event),
191181
event::key(&event),
@@ -197,24 +187,17 @@ impl Canvas {
197187
));
198188
}
199189

200-
// pub fn on_received_character<F>(&mut self, mut handler: F)
201-
// where
202-
// F: 'static + FnMut(char),
203-
// {
204-
// // TODO: Use `beforeinput`.
205-
// //
206-
// // The `keypress` event is deprecated, but there does not seem to be a
207-
// // viable/compatible alternative as of now. `beforeinput` is still widely
208-
// // unsupported.
209-
// self.on_received_character = Some(self.common.add_user_event(
210-
// "keypress",
211-
// move |event: KeyboardEvent| {
212-
// // Supress further handling to stop keys like the space key from scrolling the page.
213-
// event.prevent_default();
214-
// handler(event::codepoint(&event));
215-
// },
216-
// ));
217-
// }
190+
pub fn on_composition_end<F>(&mut self, mut handler: F)
191+
where
192+
F: 'static + FnMut(Option<String>),
193+
{
194+
self.on_composition_end = Some(self.common.add_user_event(
195+
"compositionend",
196+
move |event: CompositionEvent| {
197+
handler(event.data());
198+
},
199+
));
200+
}
218201

219202
pub fn on_cursor_leave<F>(&mut self, handler: F)
220203
where
@@ -313,7 +296,6 @@ impl Canvas {
313296
self.on_blur = None;
314297
self.on_keyboard_release = None;
315298
self.on_keyboard_press = None;
316-
self.on_received_character = None;
317299
self.on_mouse_wheel = None;
318300
self.on_fullscreen_change = None;
319301
self.on_dark_mode = None;

0 commit comments

Comments
 (0)