editor: Scale minimap width to editor width (#32317)

This commit is contained in:
Evan Simkowitz 2025-06-19 04:24:06 -07:00 committed by GitHub
parent dec7baeb97
commit e202981f0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 70 additions and 22 deletions

View file

@ -435,7 +435,9 @@
// 1. `null` to inherit the editor `current_line_highlight` setting (default) // 1. `null` to inherit the editor `current_line_highlight` setting (default)
// 2. "line" or "all" to highlight the current line in the minimap. // 2. "line" or "all" to highlight the current line in the minimap.
// 3. "gutter" or "none" to not highlight the current line in the minimap. // 3. "gutter" or "none" to not highlight the current line in the minimap.
"current_line_highlight": null "current_line_highlight": null,
// Maximum number of columns to display in the minimap.
"max_width_columns": 80
}, },
// Enable middle-click paste on Linux. // Enable middle-click paste on Linux.
"middle_click_paste": true, "middle_click_paste": true,

View file

@ -1,3 +1,6 @@
use core::num;
use std::num::NonZeroU32;
use gpui::App; use gpui::App;
use language::CursorShape; use language::CursorShape;
use project::project_settings::DiagnosticSeverity; use project::project_settings::DiagnosticSeverity;
@ -150,6 +153,7 @@ pub struct Minimap {
pub thumb: MinimapThumb, pub thumb: MinimapThumb,
pub thumb_border: MinimapThumbBorder, pub thumb_border: MinimapThumbBorder,
pub current_line_highlight: Option<CurrentLineHighlight>, pub current_line_highlight: Option<CurrentLineHighlight>,
pub max_width_columns: num::NonZeroU32,
} }
impl Minimap { impl Minimap {
@ -632,6 +636,11 @@ pub struct MinimapContent {
/// ///
/// Default: inherits editor line highlights setting /// Default: inherits editor line highlights setting
pub current_line_highlight: Option<Option<CurrentLineHighlight>>, pub current_line_highlight: Option<Option<CurrentLineHighlight>>,
/// Maximum number of columns to display in the minimap.
///
/// Default: 80
pub max_width_columns: Option<num::NonZeroU32>,
} }
/// Forcefully enable or disable the scrollbar for each axis /// Forcefully enable or disable the scrollbar for each axis
@ -854,6 +863,8 @@ impl Settings for EditorSettings {
let mut minimap = MinimapContent::default(); let mut minimap = MinimapContent::default();
let minimap_enabled = vscode.read_bool("editor.minimap.enabled").unwrap_or(true); let minimap_enabled = vscode.read_bool("editor.minimap.enabled").unwrap_or(true);
let autohide = vscode.read_bool("editor.minimap.autohide"); let autohide = vscode.read_bool("editor.minimap.autohide");
let mut max_width_columns: Option<u32> = None;
vscode.u32_setting("editor.minimap.maxColumn", &mut max_width_columns);
if minimap_enabled { if minimap_enabled {
if let Some(false) = autohide { if let Some(false) = autohide {
minimap.show = Some(ShowMinimap::Always); minimap.show = Some(ShowMinimap::Always);
@ -863,6 +874,9 @@ impl Settings for EditorSettings {
} else { } else {
minimap.show = Some(ShowMinimap::Never); minimap.show = Some(ShowMinimap::Never);
} }
if let Some(max_width_columns) = max_width_columns {
minimap.max_width_columns = NonZeroU32::new(max_width_columns);
}
vscode.enum_setting( vscode.enum_setting(
"editor.minimap.showSlider", "editor.minimap.showSlider",

View file

@ -16,9 +16,9 @@ use crate::{
HighlightedChunk, ToDisplayPoint, HighlightedChunk, ToDisplayPoint,
}, },
editor_settings::{ editor_settings::{
CurrentLineHighlight, DocumentColorsRenderMode, DoubleClickInMultibuffer, MinimapThumb, CurrentLineHighlight, DocumentColorsRenderMode, DoubleClickInMultibuffer, Minimap,
MinimapThumbBorder, ScrollBeyondLastLine, ScrollbarAxes, ScrollbarDiagnostics, ShowMinimap, MinimapThumb, MinimapThumbBorder, ScrollBeyondLastLine, ScrollbarAxes,
ShowScrollbar, ScrollbarDiagnostics, ShowMinimap, ShowScrollbar,
}, },
git::blame::{BlameRenderer, GitBlame, GlobalBlameRenderer}, git::blame::{BlameRenderer, GitBlame, GlobalBlameRenderer},
hover_popover::{ hover_popover::{
@ -1908,6 +1908,40 @@ impl EditorElement {
text_style.line_height_in_pixels(rem_size) text_style.line_height_in_pixels(rem_size)
} }
fn get_minimap_width(
&self,
minimap_settings: &Minimap,
scrollbars_shown: bool,
text_width: Pixels,
em_width: Pixels,
font_size: Pixels,
rem_size: Pixels,
cx: &App,
) -> Option<Pixels> {
if minimap_settings.show == ShowMinimap::Auto && !scrollbars_shown {
return None;
}
let minimap_font_size = self.editor.read_with(cx, |editor, cx| {
editor.minimap().map(|minimap_editor| {
minimap_editor
.read(cx)
.text_style_refinement
.as_ref()
.and_then(|refinement| refinement.font_size)
.unwrap_or(MINIMAP_FONT_SIZE)
})
})?;
let minimap_em_width = em_width * (minimap_font_size.to_pixels(rem_size) / font_size);
let minimap_width = (text_width * MinimapLayout::MINIMAP_WIDTH_PCT)
.min(minimap_em_width * minimap_settings.max_width_columns.get() as f32);
(minimap_width >= minimap_em_width * MinimapLayout::MINIMAP_MIN_WIDTH_COLUMNS)
.then_some(minimap_width)
}
fn prepaint_crease_toggles( fn prepaint_crease_toggles(
&self, &self,
crease_toggles: &mut [Option<AnyElement>], crease_toggles: &mut [Option<AnyElement>],
@ -7829,9 +7863,10 @@ impl Element for EditorElement {
}); });
let style = self.style.clone(); let style = self.style.clone();
let rem_size = window.rem_size();
let font_id = window.text_system().resolve_font(&style.text.font()); let font_id = window.text_system().resolve_font(&style.text.font());
let font_size = style.text.font_size.to_pixels(window.rem_size()); let font_size = style.text.font_size.to_pixels(rem_size);
let line_height = style.text.line_height_in_pixels(window.rem_size()); let line_height = style.text.line_height_in_pixels(rem_size);
let em_width = window.text_system().em_width(font_id, font_size).unwrap(); let em_width = window.text_system().em_width(font_id, font_size).unwrap();
let em_advance = window.text_system().em_advance(font_id, font_size).unwrap(); let em_advance = window.text_system().em_advance(font_id, font_size).unwrap();
let glyph_grid_cell = size(em_advance, line_height); let glyph_grid_cell = size(em_advance, line_height);
@ -7859,27 +7894,21 @@ impl Element for EditorElement {
.then_some(style.scrollbar_width) .then_some(style.scrollbar_width)
.unwrap_or_default(); .unwrap_or_default();
let minimap_width = self let minimap_width = self
.editor .get_minimap_width(
.read(cx) &settings.minimap,
.minimap() scrollbars_shown,
.is_some() text_width,
.then(|| match settings.minimap.show { em_width,
ShowMinimap::Auto => { font_size,
scrollbars_shown.then_some(MinimapLayout::MINIMAP_WIDTH) rem_size,
} cx,
_ => Some(MinimapLayout::MINIMAP_WIDTH), )
})
.flatten()
.filter(|minimap_width| {
text_width - vertical_scrollbar_width - *minimap_width > *minimap_width
})
.unwrap_or_default(); .unwrap_or_default();
let right_margin = minimap_width + vertical_scrollbar_width; let right_margin = minimap_width + vertical_scrollbar_width;
let editor_width = let editor_width =
text_width - gutter_dimensions.margin - 2 * em_width - right_margin; text_width - gutter_dimensions.margin - 2 * em_width - right_margin;
let editor_margins = EditorMargins { let editor_margins = EditorMargins {
gutter: gutter_dimensions, gutter: gutter_dimensions,
right: right_margin, right: right_margin,
@ -9474,7 +9503,10 @@ struct MinimapLayout {
} }
impl MinimapLayout { impl MinimapLayout {
const MINIMAP_WIDTH: Pixels = px(100.); /// The minimum width of the minimap in columns. If the minimap is smaller than this, it will be hidden.
const MINIMAP_MIN_WIDTH_COLUMNS: f32 = 20.;
/// The minimap width as a percentage of the editor width.
const MINIMAP_WIDTH_PCT: f32 = 0.15;
/// Calculates the scroll top offset the minimap editor has to have based on the /// Calculates the scroll top offset the minimap editor has to have based on the
/// current scroll progress. /// current scroll progress.
fn calculate_minimap_top_offset( fn calculate_minimap_top_offset(