From e592e35d80ef7c0df3c025fd784e1d006e7bbebd Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 23 Oct 2026 05:29:11 +0000 Subject: [PATCH] W27: Wayland set_ime_surrounding_text sends nothing when unchanged Each commit makes the compositor send done, and input-method-v2 compositors ignore input-method commits against an older serial, so an app calling it every frame could lose typed text. --- src/platform/wayland.rs | 3 ++- src/platform_impl/linux/wayland/window/state.rs | 8 ++++++++ tests/ime_patches.rs | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/platform/wayland.rs b/src/platform/wayland.rs index 4ba74ede..b078a78e 100644 --- a/src/platform/wayland.rs +++ b/src/platform/wayland.rs @@ -165,7 +165,8 @@ pub trait WindowExtWayland { /// `anchor`; without a selection they are equal. Leave the preedit out of `text`. Offsets /// are clamped to the text and moved back to a character boundary, and text longer than /// 4000 bytes is cut around the cursor and selection. Call it again whenever the text, cursor - /// or selection changes. Disallowing IME forgets the text. X11 ignores it. + /// or selection changes; a call with the same text, cursor and anchor as the last one sends + /// nothing. Disallowing IME forgets the text. X11 ignores it. fn set_ime_surrounding_text(&self, text: &str, cursor: usize, anchor: usize); /// The surrounding text to delete before inserting the [`Ime::Commit`] being handled. /// diff --git a/src/platform_impl/linux/wayland/window/state.rs b/src/platform_impl/linux/wayland/window/state.rs index cc1ffbc1..3139e3d5 100644 --- a/src/platform_impl/linux/wayland/window/state.rs +++ b/src/platform_impl/linux/wayland/window/state.rs @@ -1066,6 +1066,14 @@ impl WindowState { pub fn set_ime_surrounding_text(&mut self, text: &str, cursor: usize, anchor: usize) { let (text, cursor, anchor) = surrounding_text_excerpt(text, cursor, anchor); let (cursor, anchor) = (cursor as i32, anchor as i32); + // Each commit makes the compositor send `done`, and input-method-v2 compositors ignore + // input-method commits made against an older serial, so an app calling this every frame + // would lose typed text. + let unchanged = + |(t, c, a): &(String, i32, i32)| (t.as_str(), *c, *a) == (text, cursor, anchor); + if self.ime_surrounding_text.as_ref().is_some_and(unchanged) { + return; + } if self.ime_allowed { for text_input in &self.text_inputs { text_input.set_surrounding_text(text.to_owned(), cursor, anchor); diff --git a/tests/ime_patches.rs b/tests/ime_patches.rs index 6380a832..cad563bb 100644 --- a/tests/ime_patches.rs +++ b/tests/ime_patches.rs @@ -170,6 +170,18 @@ fn wayland_surrounding_text_deletion_goes_with_a_commit() { ); } +#[test] +fn wayland_unchanged_surrounding_text_sends_nothing() { + // Each text-input commit makes the compositor send `done`, and input-method-v2 compositors + // ignore input-method commits against an older serial, so an app that sets the surrounding + // text every frame would lose typed text. + let state = source("linux/wayland/window/state.rs"); + let set = section(&state, "pub fn set_ime_surrounding_text(", "\n }\n"); + let early_return = set.find("return;").expect("unchanged surrounding text is sent again"); + let commit = set.find("text_input.commit();").expect("surrounding text isn't committed"); + assert!(early_return < commit, "unchanged surrounding text is sent again"); +} + #[test] fn windows_reset_ime_cancels_the_composition() { // `CPS_CANCEL` discards the composition string; `CPS_COMPLETE` would commit it instead. The