From 889382cba181b8b57d4c26a50a479aa145ba0fbf Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 14 Oct 2025 21:27:23 +0800 Subject: [PATCH] input: Grouping input history to avoid IME placeholder. (#1375) --- crates/ui/src/history.rs | 14 +++++++++++++- crates/ui/src/input/state.rs | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/ui/src/history.rs b/crates/ui/src/history.rs index 399fbf00..355d80ec 100644 --- a/crates/ui/src/history.rs +++ b/crates/ui/src/history.rs @@ -27,6 +27,7 @@ pub struct History { pub(crate) ignore: bool, max_undo: usize, group_interval: Option, + grouping: bool, unique: bool, } @@ -43,6 +44,7 @@ where version: 0, max_undo: 1000, group_interval: None, + grouping: false, unique: false, } } @@ -66,10 +68,20 @@ where self } + /// Start grouping changes, this will prevent the version from being incremented until `end_grouping` is called. + pub fn start_grouping(&mut self) { + self.grouping = true; + } + + /// End grouping changes, this will allow the version to be incremented again. + pub fn end_grouping(&mut self) { + self.grouping = false; + } + /// Increment the version number if the last change was made more than `GROUP_INTERVAL` milliseconds ago. fn inc_version(&mut self) -> usize { let t = Instant::now(); - if Some(self.last_changed_at.elapsed()) > self.group_interval { + if !self.grouping && Some(self.last_changed_at.elapsed()) > self.group_interval { self.version += 1; } diff --git a/crates/ui/src/input/state.rs b/crates/ui/src/input/state.rs index 11d10405..fd3ba5ce 100644 --- a/crates/ui/src/input/state.rs +++ b/crates/ui/src/input/state.rs @@ -1579,7 +1579,6 @@ impl InputState { } let old_text = text.slice(range.clone()).to_string(); - let new_range = range.start..range.start + new_text.len(); self.history @@ -2150,6 +2149,7 @@ impl EntityInputHandler for InputState { } self.push_history(&old_text, &range, &new_text); + self.history.end_grouping(); if let Some(diagnostics) = self.mode.diagnostics_mut() { diagnostics.reset(&self.text) } @@ -2205,7 +2205,6 @@ impl EntityInputHandler for InputState { } } - self.push_history(&old_text, &range, new_text); if let Some(diagnostics) = self.mode.diagnostics_mut() { diagnostics.reset(&self.text) } @@ -2228,7 +2227,8 @@ impl EntityInputHandler for InputState { .into(); } self.mode.update_auto_grow(&self.text_wrapper); - cx.emit(InputEvent::Change); + self.history.start_grouping(); + self.push_history(&old_text, &range, new_text); cx.notify(); }