From 1ecd00a113a473bb5b506c707559fb0786353e04 Mon Sep 17 00:00:00 2001 From: Finn Evers Date: Fri, 9 May 2025 07:32:31 +0200 Subject: [PATCH] editor: Ensure minimap is shown when `show_minimap` is toggled to `true` (#30326) Follow-up of #30285 This PR ensures the action added in the linked PR also works when the user does not have the minimap enabled via settings. Currently, the toggle only works when the user has already enabled the minimap in their settings. This happens because in https://github.com/zed-industries/zed/blob/b4fbb9bc085733d2f771cd440ff57305cf6eebeb/crates/editor/src/element.rs#L7160-L7164 as well as https://github.com/zed-industries/zed/blob/b4fbb9bc085733d2f771cd440ff57305cf6eebeb/crates/editor/src/element.rs#L1542 we check for the user configuration before reserving space for the minimap as well as layouting it and because in https://github.com/zed-industries/zed/blob/b4fbb9bc085733d2f771cd440ff57305cf6eebeb/crates/editor/src/editor.rs#L16404 with https://github.com/zed-industries/zed/blob/b4fbb9bc085733d2f771cd440ff57305cf6eebeb/crates/editor/src/editor_settings.rs#L132-L134 we would not even create a minimap when the user disabled it via their settings. --- This PR fixes this by ensuring a minimap is created on the toggle issue as well as lifting some of the restrictions. Since we are always only returning a minimap in https://github.com/zed-industries/zed/blob/b4fbb9bc085733d2f771cd440ff57305cf6eebeb/crates/editor/src/editor.rs#L16443-L16445 when `show_minimap` is set to `true`, we can assume in the rendering code that if a minimap is present, it should be layouted and rendered no matter if `ShowMinimap` is currently set to `Never`. We can do this since `show_minimap` always reflects the current user configuration, see https://github.com/zed-industries/zed/blob/b4fbb9bc085733d2f771cd440ff57305cf6eebeb/crates/editor/src/editor.rs#L18163-L18164 I also removed the minimap deletion/recreation on the toggling of `show_minimap`, since this is not really needed - once we have stored a minimap editor within the editor, `show_minimap` is sufficient to ensure that it is only shown when the user requests it. Notice that we still will never create a minimap unless neccesary. Lastly, I updated the `supports_minimap` check to account for the fact that the minimap is currently disabled entirely for multibuffers. --- One thing I ~~did not tackle here~~ tackled in the second commit is that due to `show_minimap` now being exposed to the user, it is possible to enable the minimap for all full mode editors, e.g. the agent text thread editor grafik which should most likely not be possible when the minimap is programmatically disabled. Release Notes: - N/A --- crates/editor/src/editor.rs | 82 +++++++++++++++++++------- crates/editor/src/editor_settings.rs | 7 +++ crates/editor/src/element.rs | 5 +- crates/zed/src/zed/quick_action_bar.rs | 2 +- 4 files changed, 72 insertions(+), 24 deletions(-) diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 1d37d68a3f39109cb358b61d4d29285353f76fe0..c70c06766b68df55d25a0f5b35e0000b07291449 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -137,7 +137,7 @@ pub use proposed_changes_editor::{ ProposedChangeLocation, ProposedChangesEditor, ProposedChangesEditorToolbar, }; use smallvec::smallvec; -use std::{cell::OnceCell, iter::Peekable}; +use std::{cell::OnceCell, iter::Peekable, ops::Not}; use task::{ResolvedTask, RunnableTag, TaskTemplate, TaskVariables}; pub use lsp::CompletionContext; @@ -184,7 +184,7 @@ use std::{ cmp::{self, Ordering, Reverse}, mem, num::NonZeroU32, - ops::{ControlFlow, Deref, DerefMut, Not as _, Range, RangeInclusive}, + ops::{ControlFlow, Deref, DerefMut, Range, RangeInclusive}, path::{Path, PathBuf}, rc::Rc, time::{Duration, Instant}, @@ -711,6 +711,43 @@ impl ScrollbarMarkerState { } } +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum MinimapVisibility { + Disabled, + Enabled(bool), +} + +impl MinimapVisibility { + fn for_mode(mode: &EditorMode, cx: &App) -> Self { + if mode.is_full() { + Self::Enabled(EditorSettings::get_global(cx).minimap.minimap_enabled()) + } else { + Self::Disabled + } + } + + fn disabled(&self) -> bool { + match *self { + Self::Disabled => true, + _ => false, + } + } + + fn visible(&self) -> bool { + match *self { + Self::Enabled(visible) => visible, + _ => false, + } + } + + fn toggle_visibility(&self) -> Self { + match *self { + Self::Enabled(visible) => Self::Enabled(!visible), + Self::Disabled => Self::Disabled, + } + } +} + #[derive(Clone, Debug)] struct RunnableTasks { templates: Vec<(TaskSourceKind, TaskTemplate)>, @@ -883,7 +920,7 @@ pub struct Editor { show_breadcrumbs: bool, show_gutter: bool, show_scrollbars: bool, - show_minimap: bool, + minimap_visibility: MinimapVisibility, disable_expand_excerpt_buttons: bool, show_line_numbers: Option, use_relative_line_numbers: Option, @@ -1721,7 +1758,7 @@ impl Editor { blink_manager: blink_manager.clone(), show_local_selections: true, show_scrollbars: full_mode, - show_minimap: full_mode, + minimap_visibility: MinimapVisibility::for_mode(&mode, cx), show_breadcrumbs: EditorSettings::get_global(cx).toolbar.breadcrumbs, show_gutter: mode.is_full(), show_line_numbers: None, @@ -6138,8 +6175,8 @@ impl Editor { } } - pub fn supports_minimap(&self) -> bool { - self.mode.is_full() + pub fn supports_minimap(&self, cx: &App) -> bool { + !self.minimap_visibility.disabled() && self.is_singleton(cx) } fn edit_predictions_enabled_in_buffer( @@ -15169,8 +15206,8 @@ impl Editor { window: &mut Window, cx: &mut Context, ) { - if self.supports_minimap() { - self.set_show_minimap(!self.show_minimap, window, cx); + if self.supports_minimap(cx) { + self.set_minimap_visibility(self.minimap_visibility.toggle_visibility(), window, cx); } } @@ -16441,7 +16478,9 @@ impl Editor { } pub fn minimap(&self) -> Option<&Entity> { - self.minimap.as_ref().filter(|_| self.show_minimap) + self.minimap + .as_ref() + .filter(|_| self.minimap_visibility.visible()) } pub fn wrap_guides(&self, cx: &App) -> SmallVec<[(usize, bool); 2]> { @@ -16638,27 +16677,26 @@ impl Editor { cx.notify(); } - pub fn set_show_minimap( + pub fn set_minimap_visibility( &mut self, - show_minimap: bool, + minimap_visibility: MinimapVisibility, window: &mut Window, cx: &mut Context, ) { - if self.show_minimap != show_minimap { - self.show_minimap = show_minimap; - if show_minimap { + if self.minimap_visibility != minimap_visibility { + if minimap_visibility.visible() && self.minimap.is_none() { let minimap_settings = EditorSettings::get_global(cx).minimap; - self.minimap = self.create_minimap(minimap_settings, window, cx); - } else { - self.minimap = None; + self.minimap = + self.create_minimap(minimap_settings.with_show_override(), window, cx); } + self.minimap_visibility = minimap_visibility; cx.notify(); } } pub fn disable_scrollbars_and_minimap(&mut self, window: &mut Window, cx: &mut Context) { self.set_show_scrollbars(false, cx); - self.set_show_minimap(false, window, cx); + self.set_minimap_visibility(MinimapVisibility::Disabled, window, cx); } pub fn set_show_line_numbers(&mut self, show_line_numbers: bool, cx: &mut Context) { @@ -18160,8 +18198,12 @@ impl Editor { } let minimap_settings = EditorSettings::get_global(cx).minimap; - if self.show_minimap != minimap_settings.minimap_enabled() { - self.set_show_minimap(!self.show_minimap, window, cx); + if self.minimap_visibility.visible() != minimap_settings.minimap_enabled() { + self.set_minimap_visibility( + self.minimap_visibility.toggle_visibility(), + window, + cx, + ); } else if let Some(minimap_entity) = self.minimap.as_ref() { minimap_entity.update(cx, |minimap_editor, cx| { minimap_editor.update_minimap_configuration(minimap_settings, cx) diff --git a/crates/editor/src/editor_settings.rs b/crates/editor/src/editor_settings.rs index f4c0b3a755b1c1d4cd68ace0415f2b0935b85f69..9ee398f82712e40ec1306c09a70b3489635e4905 100644 --- a/crates/editor/src/editor_settings.rs +++ b/crates/editor/src/editor_settings.rs @@ -132,6 +132,13 @@ impl Minimap { pub fn minimap_enabled(&self) -> bool { self.show != ShowMinimap::Never } + + pub fn with_show_override(self) -> Self { + Self { + show: ShowMinimap::Always, + ..self + } + } } #[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)] diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index a9a40d20c712ae453b1ae929b46ff977e03f68a8..58c9b41434108fd92dad872d3b3154aabd67927c 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1539,7 +1539,7 @@ impl EditorElement { || minimap_width.is_zero() || matches!( minimap_settings.show, - ShowMinimap::Never | ShowMinimap::Auto if scrollbar_layout.is_none_or(|layout| !layout.visible) + ShowMinimap::Auto if scrollbar_layout.is_none_or(|layout| !layout.visible) ) { return None; @@ -7161,11 +7161,10 @@ impl Element for EditorElement { .editor .read_with(cx, |editor, _| editor.minimap().is_some()) .then(|| match settings.minimap.show { - ShowMinimap::Never => None, - ShowMinimap::Always => Some(MinimapLayout::MINIMAP_WIDTH), ShowMinimap::Auto => { scrollbars_shown.then_some(MinimapLayout::MINIMAP_WIDTH) } + _ => Some(MinimapLayout::MINIMAP_WIDTH), }) .flatten() .filter(|minimap_width| { diff --git a/crates/zed/src/zed/quick_action_bar.rs b/crates/zed/src/zed/quick_action_bar.rs index d618d29af30c45eb41a922a4f0cc78d7ca7885bb..26947ceb7e26b29c881d5aa927bc148783ae04ec 100644 --- a/crates/zed/src/zed/quick_action_bar.rs +++ b/crates/zed/src/zed/quick_action_bar.rs @@ -105,7 +105,7 @@ impl Render for QuickActionBar { let show_edit_predictions = editor_value.edit_predictions_enabled(); let edit_predictions_enabled_at_cursor = editor_value.edit_predictions_enabled_at_cursor(cx); - let supports_minimap = editor_value.supports_minimap(); + let supports_minimap = editor_value.supports_minimap(cx); let minimap_enabled = supports_minimap && editor_value.minimap().is_some(); let focus_handle = editor_value.focus_handle(cx);