From 871630606f3a5f0385b2a3eb45a15386713054dc Mon Sep 17 00:00:00 2001 From: Kenzie Raditya Tirtarahardja Date: Fri, 30 May 2025 10:06:15 +0800 Subject: [PATCH] Send synthetic keydown/keyup at ime_insert_text (#37175) At `egl/app_state.rs`, send keydown and keyup with PROCESS KEY when inserting text. This fixes OHOS input event, but maybe also for Android in the future (if it implements `ime_insert_text`). We will get input event since keydown is dispatched (https://github.com/servo/servo/pull/37078). This implementation is similar to [Chromium's](https://source.chromium.org/chromium/chromium/src/+/main:content/public/android/java/src/org/chromium/content/browser/input/ImeAdapterImpl.java;drc=404e8d654e8b26336d2cb103b9c21faecbf7f73a;bpv=1;bpt=1;l=851?gsn=sendCompositionToNative&gs=KYTHE%3A%2F%2Fkythe%3A%2F%2Fchromium.googlesource.com%2Fcodesearch%2Fchromium%2Fsrc%2F%2Fmain%3Flang%3Djava%3Fpath%3Dorg.chromium.content.browser.input.ImeAdapterImpl%23d840961d441fd4bb569f9689c86da91fb714c0c153366e3198a85e9c7a098dce) Android key event. Testing: manually checked on OHOS device For: https://github.com/servo/servo/issues/36259, but only in OHOS Signed-off-by: PotatoCP Co-authored-by: PotatoCP Co-authored-by: stevennovaryo --- components/script/textinput.rs | 3 +++ ports/servoshell/egl/app_state.rs | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 73f6159a9a0..6213b1ef511 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -998,6 +998,9 @@ impl TextInput { self.insert_string(c.as_str()); return KeyReaction::DispatchInput; } + if matches!(key, Key::Process) { + return KeyReaction::DispatchInput; + } KeyReaction::Nothing }) .unwrap() diff --git a/ports/servoshell/egl/app_state.rs b/ports/servoshell/egl/app_state.rs index 486b01060e8..c0890e2d36a 100644 --- a/ports/servoshell/egl/app_state.rs +++ b/ports/servoshell/egl/app_state.rs @@ -620,11 +620,27 @@ impl RunningAppState { } pub fn ime_insert_text(&self, text: String) { - self.active_webview() - .notify_input_event(InputEvent::Ime(ImeEvent::Composition(CompositionEvent { + // In OHOS, we get empty text after the intended text. + if text.is_empty() { + return; + } + let active_webview = self.active_webview(); + active_webview.notify_input_event(InputEvent::Keyboard(KeyboardEvent { + state: KeyState::Down, + key: Key::Process, + ..KeyboardEvent::default() + })); + active_webview.notify_input_event(InputEvent::Ime(ImeEvent::Composition( + CompositionEvent { state: CompositionState::End, data: text, - }))); + }, + ))); + active_webview.notify_input_event(InputEvent::Keyboard(KeyboardEvent { + state: KeyState::Up, + key: Key::Process, + ..KeyboardEvent::default() + })); self.perform_updates(); }