From 1610d05526bb57bc44835a0aa370cee8ebe7b687 Mon Sep 17 00:00:00 2001 From: Junkui Zhang <364772080@qq.com> Date: Wed, 9 Jul 2025 19:29:32 +0800 Subject: [PATCH] checkpoint --- crates/editor/src/editor.rs | 2 +- crates/editor/src/element.rs | 14 +++++----- crates/gpui/src/platform/keystroke.rs | 10 +++++++ crates/settings_ui/src/keybindings.rs | 27 ++++++++++++------- crates/ui/src/components/keybinding.rs | 37 +++++++++++++++++--------- crates/vim/src/mode_indicator.rs | 2 ++ 6 files changed, 62 insertions(+), 30 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index c5fe0db74cedca342aee3199d97ca27d9efd19c3..7ffd0af7618da335145fbf48bf6b6d871651231c 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -9000,7 +9000,7 @@ impl Editor { max_width: Pixels, cursor_point: Point, style: &EditorStyle, - accept_keystroke: Option<&gpui::Keystroke>, + accept_keystroke: Option<&gpui::KeybindingKeystroke>, _window: &Window, cx: &mut Context, ) -> Option { diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 8a5bfb3babdc2a1f3b2594e6b97c8b7d2751e180..4e19d8b11ce4fadf1958247c06fbe0d157b9ea66 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -42,12 +42,12 @@ use gpui::{ Action, Along, AnyElement, App, AppContext, AvailableSpace, Axis as ScrollbarAxis, BorderStyle, Bounds, ClickEvent, ContentMask, Context, Corner, Corners, CursorStyle, DispatchPhase, Edges, Element, ElementInputHandler, Entity, Focusable as _, FontId, GlobalElementId, Hitbox, - HitboxBehavior, Hsla, InteractiveElement, IntoElement, IsZero, Keystroke, Length, - ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, PaintQuad, - ParentElement, Pixels, ScrollDelta, ScrollHandle, ScrollWheelEvent, ShapedLine, SharedString, - Size, StatefulInteractiveElement, Style, Styled, TextRun, TextStyleRefinement, WeakEntity, - Window, anchored, deferred, div, fill, linear_color_stop, linear_gradient, outline, point, px, - quad, relative, size, solid_background, transparent_black, + HitboxBehavior, Hsla, InteractiveElement, IntoElement, IsZero, KeybindingKeystroke, Keystroke, + Length, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, + PaintQuad, ParentElement, Pixels, ScrollDelta, ScrollHandle, ScrollWheelEvent, ShapedLine, + SharedString, Size, StatefulInteractiveElement, Style, Styled, TextRun, TextStyleRefinement, + WeakEntity, Window, anchored, deferred, div, fill, linear_color_stop, linear_gradient, outline, + point, px, quad, relative, size, solid_background, transparent_black, }; use itertools::Itertools; use language::language_settings::{ @@ -6994,7 +6994,7 @@ fn header_jump_data( pub struct AcceptEditPredictionBinding(pub(crate) Option); impl AcceptEditPredictionBinding { - pub fn keystroke(&self) -> Option<&Keystroke> { + pub fn keystroke(&self) -> Option<&KeybindingKeystroke> { if let Some(binding) = self.0.as_ref() { match &binding.keystrokes() { [keystroke, ..] => Some(keystroke), diff --git a/crates/gpui/src/platform/keystroke.rs b/crates/gpui/src/platform/keystroke.rs index 485a5545d48a943a8f37eb39afe002e28c9b439d..30e065bb0c5a11baacffcde472ef6b47c5d81468 100644 --- a/crates/gpui/src/platform/keystroke.rs +++ b/crates/gpui/src/platform/keystroke.rs @@ -272,6 +272,16 @@ impl Keystroke { } self } + + /// TODO: + pub fn into_keybinding_keystroke(self) -> KeybindingKeystroke { + let (key, modifiers) = temp_keyboard_mapper(self.key.clone(), self.modifiers); + KeybindingKeystroke { + inner: self, + modifiers, + key, + } + } } impl KeybindingKeystroke { diff --git a/crates/settings_ui/src/keybindings.rs b/crates/settings_ui/src/keybindings.rs index 951dd77541b8c20d6ef40f321a799e0ac7c091f7..253f97d53e91ab7f45472de48d334b05abe726f4 100644 --- a/crates/settings_ui/src/keybindings.rs +++ b/crates/settings_ui/src/keybindings.rs @@ -11,8 +11,8 @@ use fs::Fs; use fuzzy::{StringMatch, StringMatchCandidate}; use gpui::{ AppContext as _, AsyncApp, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, - Global, KeyContext, Keystroke, ModifiersChangedEvent, ScrollStrategy, StyledText, Subscription, - WeakEntity, actions, div, transparent_black, + Global, KeyContext, KeybindingKeystroke, Keystroke, ModifiersChangedEvent, ScrollStrategy, + StyledText, Subscription, WeakEntity, actions, div, transparent_black, }; use language::{Language, LanguageConfig, ToOffset as _}; use settings::{BaseKeymap, KeybindSource, KeymapFile, SettingsAssets}; @@ -274,7 +274,7 @@ impl KeymapEditor { for key_binding in key_bindings { let source = key_binding.meta().map(settings::KeybindSource::from_meta); - let keystroke_text = ui::text_for_keystrokes(key_binding.keystrokes(), cx); + let keystroke_text = ui::text_for_keybinding_keystrokes(key_binding.keystrokes(), cx); let ui_key_binding = Some( ui::KeyBinding::new_from_gpui(key_binding.clone(), cx) .vim_mode(source == Some(settings::KeybindSource::Vim)), @@ -1143,7 +1143,7 @@ async fn load_rust_language(workspace: WeakEntity, cx: &mut AsyncApp) async fn save_keybinding_update( existing: ProcessedKeybinding, - new_keystrokes: &[Keystroke], + new_keystrokes: &[KeybindingKeystroke], new_context: Option<&str>, fs: &Arc, tab_size: usize, @@ -1280,15 +1280,21 @@ impl KeystrokeInput { cx.notify(); } - fn keystrokes(&self) -> &[Keystroke] { - if self + fn keystrokes(&self) -> Vec { + let keystrokes = if self .keystrokes .last() .map_or(false, |last| last.key.is_empty()) { - return &self.keystrokes[..self.keystrokes.len() - 1]; - } - return &self.keystrokes; + &self.keystrokes[..self.keystrokes.len() - 1] + } else { + &self.keystrokes + }; + keystrokes + .to_vec() + .into_iter() + .map(|keystroke| keystroke.into_keybinding_keystroke()) + .collect() } } @@ -1332,7 +1338,8 @@ impl Render for KeystrokeInput { .gap(ui::DynamicSpacing::Base04.rems(cx)) .children(self.keystrokes.iter().map(|keystroke| { h_flex().children(ui::render_keystroke( - keystroke, + &keystroke.modifiers, + &keystroke.key, None, Some(rems(0.875).into()), ui::PlatformStyle::platform(), diff --git a/crates/ui/src/components/keybinding.rs b/crates/ui/src/components/keybinding.rs index 8812711cb21fbc1ec8d72afaa4906a6d52ecc8c2..898c7e5cbb510f57ec80d6e2ae4333d7a4cb24c0 100644 --- a/crates/ui/src/components/keybinding.rs +++ b/crates/ui/src/components/keybinding.rs @@ -138,7 +138,8 @@ impl RenderOnce for KeyBinding { .rounded_xs() .text_color(cx.theme().colors().text_muted) .children(render_keystroke( - keystroke, + &keystroke.modifiers, + &keystroke.key, color, self.size, self.platform_style, @@ -149,7 +150,8 @@ impl RenderOnce for KeyBinding { } pub fn render_keystroke( - keystroke: &KeybindingKeystroke, + modifiers: &Modifiers, + key: &str, color: Option, size: impl Into>, platform_style: PlatformStyle, @@ -164,12 +166,7 @@ pub fn render_keystroke( if use_text { let element = Key::new( - keystroke_text( - &keystroke.modifiers, - &keystroke.key, - platform_style, - vim_mode, - ), + keystroke_text(modifiers, key, platform_style, vim_mode), color, ) .size(size) @@ -178,13 +175,13 @@ pub fn render_keystroke( } else { let mut elements = Vec::new(); elements.extend(render_modifiers( - &keystroke.modifiers, + modifiers, platform_style, color, size, true, )); - elements.push(render_key(&keystroke.key, color, platform_style, size)); + elements.push(render_key(key, color, platform_style, size)); elements } } @@ -387,10 +384,26 @@ impl KeyIcon { /// Returns a textual representation of the key binding for the given [`Action`]. pub fn text_for_action(action: &dyn Action, window: &Window, cx: &App) -> Option { let key_binding = window.highest_precedence_binding_for_action(action)?; - Some(text_for_keystrokes(key_binding.keystrokes(), cx)) + Some(text_for_keybinding_keystrokes(key_binding.keystrokes(), cx)) +} + +pub fn text_for_keybinding_keystrokes(keystrokes: &[KeybindingKeystroke], cx: &App) -> String { + let platform_style = PlatformStyle::platform(); + let vim_enabled = cx.try_global::().is_some(); + keystrokes + .iter() + .map(|keystroke| { + keystroke_text( + &keystroke.modifiers, + &keystroke.key, + platform_style, + vim_enabled, + ) + }) + .join(" ") } -pub fn text_for_keystrokes(keystrokes: &[KeybindingKeystroke], cx: &App) -> String { +pub fn text_for_keystrokes(keystrokes: &[Keystroke], cx: &App) -> String { let platform_style = PlatformStyle::platform(); let vim_enabled = cx.try_global::().is_some(); keystrokes diff --git a/crates/vim/src/mode_indicator.rs b/crates/vim/src/mode_indicator.rs index d54b270074f1928b6308fbfef1c6c3a04f1b851b..d12b4a71bb647045085931f1857c200bd41c7cfd 100644 --- a/crates/vim/src/mode_indicator.rs +++ b/crates/vim/src/mode_indicator.rs @@ -50,6 +50,8 @@ impl ModeIndicator { } } + // TODO: + // what's this? fn update_pending_keys(&mut self, window: &mut Window, cx: &App) { self.pending_keys = window .pending_input_keystrokes()