Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix standalone modifier sequences not being flushed after timeout (#55800)
kaedeairbc7041c600e3
diff --git a/crates/gpui/src/platform/keystroke.rs b/crates/gpui/src/platform/keystroke.rsindex aa89e62f3cd..b995508cc3f 100644--- a/crates/gpui/src/platform/keystroke.rs+++ b/crates/gpui/src/platform/keystroke.rs@@ -288,6 +288,14 @@ impl Keystroke {}self}++ /// Returns whether the key itself is a modifier+ pub fn is_modifier_key(&self) -> bool {+ matches!(+ self.key.as_str(),+ "shift" | "control" | "alt" | "platform" | "function"+ )+ }}impl KeybindingKeystroke {diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex 78738238c71..ff1ba124993 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -6214,9 +6214,11 @@ impl Window {accepts});+ let standalone_modifier = matches!(currently_pending.keystrokes.as_slice(), [keystroke] if keystroke.is_modifier_key());let needs_timeout = previous_timeout.is_some()|| match_result.pending_has_binding- || text_input_requires_timeout;+ || text_input_requires_timeout+ || standalone_modifier;currently_pending.timeout = if needs_timeout {match previous_timeout {Some(mut timeout) if timeout.is_paused() => {@@ -9986,6 +9988,122 @@ mod tests {.collect()}+ actions!(window_test, [StandaloneModifierAction]);++ struct StandaloneModifierView {+ focus_handle: FocusHandle,+ action_count: usize,+ }++ impl Render for StandaloneModifierView {+ fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {+ div()+ .key_context("StandaloneModifier")+ .track_focus(&self.focus_handle)+ .on_action(cx.listener(|this, _: &StandaloneModifierAction, _, _| {+ this.action_count += 1;+ }))+ }+ }++ #[gpui::test]+ fn test_standalone_modifier_sequence_timeout_and_recovery(cx: &mut TestAppContext) {+ use crate::{KeyBinding, Modifiers};++ cx.update(|cx| {+ cx.bind_keys([KeyBinding::new(+ "shift shift",+ StandaloneModifierAction,+ Some("StandaloneModifier"),+ )]);+ });+ let (view, cx) = cx.add_window_view(|window, cx| {+ let focus_handle = cx.focus_handle();+ window.focus(&focus_handle, cx);+ StandaloneModifierView {+ focus_handle,+ action_count: 0,+ }+ });+ let shift = Modifiers {+ shift: true,+ ..Modifiers::default()+ };+ cx.simulate_modifiers_change(shift);+ cx.simulate_modifiers_change(Modifiers::default());+ cx.update(|window, _| {+ assert_eq!(+ window.pending_input_keystrokes(),+ Some([Keystroke::parse("shift").expect("valid keystroke")].as_slice())+ );+ });+ cx.background_executor.advance_clock(Duration::from_secs(2));+ cx.run_until_parked();+ cx.update(|window, cx| {+ assert!(!window.has_pending_keystrokes());+ assert_eq!(view.read(cx).action_count, 0);+ });++ cx.simulate_modifiers_change(shift);+ cx.simulate_modifiers_change(Modifiers::default());+ cx.update(|window, cx| {+ assert!(window.has_pending_keystrokes());+ assert_eq!(view.read(cx).action_count, 0);+ });++ cx.simulate_modifiers_change(shift);+ cx.simulate_modifiers_change(Modifiers::default());+ cx.update(|window, cx| {+ assert!(!window.has_pending_keystrokes());+ assert_eq!(view.read(cx).action_count, 1);+ });+ }++ #[gpui::test]+ fn test_standalone_modifier_sequence_dispatches_before_timeout(cx: &mut TestAppContext) {+ use crate::{KeyBinding, Modifiers};++ cx.update(|cx| {+ cx.bind_keys([KeyBinding::new(+ "shift shift",+ StandaloneModifierAction,+ Some("StandaloneModifier"),+ )]);+ });+ let (view, cx) = cx.add_window_view(|window, cx| {+ let focus_handle = cx.focus_handle();+ window.focus(&focus_handle, cx);+ StandaloneModifierView {+ focus_handle,+ action_count: 0,+ }+ });+ let shift = Modifiers {+ shift: true,+ ..Modifiers::default()+ };+ cx.simulate_modifiers_change(shift);+ cx.simulate_modifiers_change(Modifiers::default());+ cx.background_executor+ .advance_clock(Duration::from_millis(500));+ cx.update(|window, cx| {+ assert!(window.has_pending_keystrokes());+ assert_eq!(view.read(cx).action_count, 0);+ });+ cx.simulate_modifiers_change(shift);+ cx.simulate_modifiers_change(Modifiers::default());+ cx.update(|window, cx| {+ assert!(!window.has_pending_keystrokes());+ assert_eq!(view.read(cx).action_count, 1);+ });+ cx.background_executor.advance_clock(Duration::from_secs(2));+ cx.run_until_parked();+ cx.update(|window, cx| {+ assert!(!window.has_pending_keystrokes());+ assert_eq!(view.read(cx).action_count, 1);+ });+ }+fn dispatch_touch<T: 'static>(window: crate::WindowHandle<T>,cx: &mut TestAppContext,