mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
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 <[email protected]>
This commit is contained in:
1 parent
c84ec1b43a
commit
c1cb455c7a
2 files changed
+32
No files matched your search
@@ -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)),
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user