From c1cb455c7aa94d436f1cae20786ac071a0ca0c29 Mon Sep 17 00:00:00 2001 From: BFlat Date: Fri, 9 Oct 2026 08:08:01 +0700 Subject: [PATCH] Fix #1381: selecting type on Wayland no longer deletes it (#1421) Wayland input methods send an empty preedit (and at times an empty commit) whenever the caret rectangle moves, as it does while Cmd+A, Shift-arrows or a drag select text. With nothing being composed, ime_update took that as "replace the selection with nothing" and deleted the selected text. An empty preedit or commit now changes nothing unless a composition is in progress; a real composition still replaces the selection. Claude-Session: https://claude.ai/code/session_01UHgFEHEsefoQoBParH47JA Co-authored-by: Claude --- crates/ui-egui/src/type_tool.rs | 6 ++++++ crates/ui-egui/src/type_tool_tests.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs index 607cd12dccf..25555e9ea04 100644 --- a/crates/ui-egui/src/type_tool.rs +++ b/crates/ui-egui/src/type_tool.rs @@ -330,6 +330,12 @@ fn ime_update(app: &mut PhotocraftApp, s: &str, commit: bool) { let Some(text) = current_text(app, LayerId(ed.layer)) else { return }; let n = text.chars().count(); let s = s.replace("\r\n", "\n").replace('\r', "\n"); + // An empty preedit or commit while nothing is being composed changes nothing. Wayland input + // methods send one on every state change (the caret rectangle moving as a selection grows), + // and it used to replace the selection, deleting the text as it was selected (#1381). + if s.is_empty() && ed.preedit.is_none() { + return; + } let (start, end) = match ed.preedit { Some((p, l)) if p.saturating_add(l) <= n => (p, p + l), _ => (ed.caret.min(ed.anchor).min(n), ed.caret.max(ed.anchor).min(n)), diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rs index 65e87cebd82..89d6e557f28 100644 --- a/crates/ui-egui/src/type_tool_tests.rs +++ b/crates/ui-egui/src/type_tool_tests.rs @@ -459,6 +459,32 @@ fn command_t_while_typing_toggles_the_character_panel() { assert_ne!(crate::view_cmds::checked(h.state(), "window.panel.character"), before, "the Character panel toggled"); } +/// #1381: on Wayland the input method sends an empty preedit (and sometimes an empty commit) +/// whenever the caret rectangle moves, e.g. while ⌘A, ⇧-arrows or a drag select text. With +/// nothing being composed that must leave the text and the selection alone; a real composition +/// still replaces the selection, and clearing it removes only the composed text. +#[test] +fn empty_ime_events_never_delete_the_selection() { + let mut app = new_app(); + let id = LayerId(app.run("type.create", json!({"text": "Hello", "size": 40, "x": 300, "y": 420})).unwrap()["layer"].as_u64().unwrap()); + app.ui.text_edit = + Some(crate::state::TextEdit { layer: id.0, caret: 5, anchor: 0, session: "s".into(), created: false, dragging: false, resize: None, preedit: None }); + let mut h = harness(1.0, app); + let ime = |h: &mut Harness<'static, PhotocraftApp>, e: egui::ImeEvent| { + h.event(egui::Event::Ime(e)); + h.run_steps(1); + }; + ime(&mut h, egui::ImeEvent::Preedit { text: String::new(), active_range_chars: None }); + ime(&mut h, egui::ImeEvent::Commit(String::new())); + assert_eq!(text(h.state(), id).text, "Hello", "empty IME events keep the text"); + assert_eq!(selection(&h), (0, 5), "and the selection"); + // Composing replaces the selection; cancelling the composition leaves the rest. + ime(&mut h, egui::ImeEvent::Preedit { text: "か".into(), active_range_chars: None }); + assert_eq!(text(h.state(), id).text, "か"); + ime(&mut h, egui::ImeEvent::Preedit { text: String::new(), active_range_chars: None }); + assert_eq!(text(h.state(), id).text, ""); +} + fn type_command() -> Modifiers { Modifiers { command: true, ctrl: true, ..Modifiers::NONE } }