Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
editor: Update gutter hover tooltip on modifier changes (#58880)
saberoueslatie1ec575d3f78
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rsindex 36b5209a5c0..913ab20c4c1 100644--- a/crates/editor/src/editor.rs+++ b/crates/editor/src/editor.rs@@ -254,9 +254,8 @@ use theme::{};use theme_settings::{ThemeSettings, observe_buffer_font_size_adjustment};use ui::{- Avatar, ButtonSize, ButtonStyle, ContextMenu, Disclosure, IconButton, IconButtonShape,- IconName, IconSize, Indicator, Key, Tooltip, h_flex, prelude::*, scrollbars::ScrollbarAutoHide,- utils::WithRemSize,+ Avatar, ContextMenu, Disclosure, IconButtonShape, Indicator, Key, KeyBinding, Tooltip,+ prelude::*, scrollbars::ScrollbarAutoHide, tooltip_container, utils::WithRemSize,};use ui_input::ErasedEditor;use util::{RangeExt, ResultExt, TryFutureExt, maybe, post_inc};@@ -1637,6 +1636,97 @@ pub struct RewrapOptions {pub line_length: Option<usize>,}+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+enum GutterButtonIntent {+ SetBookmark,+ SetBreakpoint,+}++impl GutterButtonIntent {+ fn as_str(&self) -> &'static str {+ match self {+ Self::SetBookmark => "Set Bookmark",+ Self::SetBreakpoint => "Set Breakpoint",+ }+ }++ fn icon(&self) -> ui::IconName {+ match self {+ Self::SetBookmark => ui::IconName::Bookmark,+ Self::SetBreakpoint => ui::IconName::DebugBreakpoint,+ }+ }++ fn color(&self) -> Color {+ match self {+ Self::SetBookmark => Color::Info,+ Self::SetBreakpoint => Color::Hint,+ }+ }++ fn action(&self) -> &'static dyn Action {+ match self {+ Self::SetBookmark => &ToggleBookmark,+ Self::SetBreakpoint => &ToggleBreakpoint,+ }+ }+}++struct GutterButtonTooltip {+ primary: GutterButtonIntent,+ secondary: GutterButtonIntent,+ focus_handle: FocusHandle,+}++impl GutterButtonTooltip {+ fn active_intent(&self, modifiers: Modifiers) -> GutterButtonIntent {+ if modifiers.secondary() {+ self.secondary+ } else {+ self.primary+ }+ }++ fn meta_text(&self, intent: GutterButtonIntent) -> String {+ const RIGHT_CLICK_HINT: &str = "right-click for more options";++ if self.primary == self.secondary {+ return RIGHT_CLICK_HINT.to_string();+ }+ let modifier_as_text = gpui::Keystroke {+ modifiers: Modifiers::secondary_key(),+ ..Default::default()+ };+ let other = match intent {+ GutterButtonIntent::SetBookmark => "breakpoint",+ GutterButtonIntent::SetBreakpoint => "bookmark",+ };+ format!("{modifier_as_text}-click to add a {other}\n{RIGHT_CLICK_HINT}")+ }+}++impl Render for GutterButtonTooltip {+ fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {+ let intent = self.active_intent(window.modifiers());+ let key_binding = KeyBinding::for_action_in(intent.action(), &self.focus_handle, cx);+ let meta_text = self.meta_text(intent);++ tooltip_container(cx, move |this, _| {+ this.child(+ h_flex()+ .justify_between()+ .child(intent.as_str())+ .child(key_binding),+ )+ .child(+ Label::new(meta_text)+ .size(LabelSize::Small)+ .color(Color::Muted),+ )+ })+ }+}+impl Editor {pub fn single_line(window: &mut Window, cx: &mut Context<Self>) -> Self {let buffer = cx.new(|cx| Buffer::local("", cx));@@ -4404,61 +4494,20 @@ impl Editor {window: &mut Window,cx: &mut Context<Self>,) -> IconButton {- #[derive(Clone, Copy)]- enum Intent {- SetBookmark,- SetBreakpoint,- }-- impl Intent {- fn as_str(&self) -> &'static str {- match self {- Intent::SetBookmark => "Set bookmark",- Intent::SetBreakpoint => "Set breakpoint",- }- }-- fn icon(&self) -> ui::IconName {- match self {- Intent::SetBookmark => ui::IconName::Bookmark,- Intent::SetBreakpoint => ui::IconName::DebugBreakpoint,- }- }-- fn color(&self) -> Color {- match self {- Intent::SetBookmark => Color::Info,- Intent::SetBreakpoint => Color::Hint,- }- }-- fn secondary_and_options(&self) -> String {- let alt_as_text = gpui::Keystroke {- modifiers: Modifiers::secondary_key(),- ..Default::default()- };- match self {- Intent::SetBookmark => format!(- "{alt_as_text}-click to add a breakpoint\nright-click for more options"- ),- Intent::SetBreakpoint => format!(- "{alt_as_text}-click to add a bookmark\nright-click for more options"- ),- }- }- }-let gutter_settings = EditorSettings::get_global(cx).gutter;let show_bookmarks = self.show_bookmarks.unwrap_or(gutter_settings.bookmarks);let show_breakpoints = self.show_breakpoints.unwrap_or(gutter_settings.breakpoints);let [primary, secondary] = match [show_breakpoints, show_bookmarks] {- [true, true] => [Intent::SetBreakpoint, Intent::SetBookmark],- [true, false] => [Intent::SetBreakpoint; 2],- [false, true] => [Intent::SetBookmark; 2],+ [true, true] => [+ GutterButtonIntent::SetBreakpoint,+ GutterButtonIntent::SetBookmark,+ ],+ [true, false] => [GutterButtonIntent::SetBreakpoint; 2],+ [false, true] => [GutterButtonIntent::SetBookmark; 2],[false, false] => {log::error!("Trying to place gutter_hover without anything enabled!!");- [Intent::SetBookmark; 2]+ [GutterButtonIntent::SetBookmark; 2]}};@@ -4485,8 +4534,8 @@ impl Editor {};match intent {- Intent::SetBookmark => editor.toggle_bookmark_at_row(row, cx),- Intent::SetBreakpoint => editor.edit_breakpoint_at_anchor(+ GutterButtonIntent::SetBookmark => editor.toggle_bookmark_at_row(row, cx),+ GutterButtonIntent::SetBreakpoint => editor.edit_breakpoint_at_anchor(position,Breakpoint::new_standard(),BreakpointEditAction::Toggle,@@ -4500,13 +4549,12 @@ impl Editor {})).when(!has_context_menu, |button| {button.tooltip(move |_window, cx| {- Tooltip::with_meta_in(- intent.as_str(),- Some(&ToggleBreakpoint),- intent.secondary_and_options(),- &focus_handle,- cx,- )+ cx.new(|_| GutterButtonTooltip {+ primary,+ secondary,+ focus_handle: focus_handle.clone(),+ })+ .into()})})}diff --git a/crates/editor/src/editor_tests.rs b/crates/editor/src/editor_tests.rsindex 4acdae8d281..181a4d447ba 100644--- a/crates/editor/src/editor_tests.rs+++ b/crates/editor/src/editor_tests.rs@@ -28678,6 +28678,45 @@ fn add_log_breakpoint_at_cursor();}+#[gpui::test]+fn test_gutter_button_tooltip_updates_intent_with_secondary_modifier(cx: &mut TestAppContext) {+ init_test(cx, |_| {});++ let focus_handle = cx.update(|cx| cx.focus_handle());+ let tooltip = GutterButtonTooltip {+ primary: GutterButtonIntent::SetBreakpoint,+ secondary: GutterButtonIntent::SetBookmark,+ focus_handle,+ };++ let primary_intent = tooltip.active_intent(Modifiers::none());+ assert_eq!(primary_intent, GutterButtonIntent::SetBreakpoint);+ assert!(primary_intent.action().as_any().is::<ToggleBreakpoint>());++ let secondary_intent = tooltip.active_intent(Modifiers::secondary_key());+ assert_eq!(secondary_intent, GutterButtonIntent::SetBookmark);+ assert!(secondary_intent.action().as_any().is::<ToggleBookmark>());++ // When both features are enabled, the meta text advertises the+ // modifier-click alternative.+ let meta = tooltip.meta_text(primary_intent);+ assert!(meta.contains("-click to add a bookmark"), "got: {meta}");+ assert!(meta.contains("right-click for more options"));+ let meta = tooltip.meta_text(secondary_intent);+ assert!(meta.contains("-click to add a breakpoint"), "got: {meta}");++ // When only one feature is enabled (primary == secondary), a+ // modifier-click repeats the primary action, so the tooltip must not+ // advertise it.+ let single_feature_tooltip = GutterButtonTooltip {+ primary: GutterButtonIntent::SetBreakpoint,+ secondary: GutterButtonIntent::SetBreakpoint,+ focus_handle: tooltip.focus_handle,+ };+ let meta = single_feature_tooltip.meta_text(GutterButtonIntent::SetBreakpoint);+ assert_eq!(meta, "right-click for more options");+}+#[gpui::test]async fn test_breakpoint_toggling(cx: &mut TestAppContext) {init_test(cx, |_| {});