Skip to content

Commit 5210d9e

Browse files
Tidier multibuffer (zed-industries#26954)
Makes multibuffer headers less close to the top of the file. Moves multibuffer line numbers one em to the right to make space for the expand excerpt button on large line numbers. Release Notes: - N/A --------- Co-authored-by: Danilo Leal <[email protected]>
1 parent 1139904 commit 5210d9e

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

crates/editor/src/editor.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ pub(crate) const SCROLL_CENTER_TOP_BOTTOM_DEBOUNCE_TIMEOUT: Duration = Duration:
211211

212212
pub(crate) const EDIT_PREDICTION_KEY_CONTEXT: &str = "edit_prediction";
213213
pub(crate) const EDIT_PREDICTION_CONFLICT_KEY_CONTEXT: &str = "edit_prediction_conflict";
214+
pub(crate) const MIN_LINE_NUMBER_DIGITS: u32 = 4;
214215

215216
const COLUMNAR_SELECTION_MODIFIERS: Modifiers = Modifiers {
216217
alt: true,
@@ -17618,7 +17619,7 @@ impl EditorSnapshot {
1761817619
.unwrap_or(gutter_settings.line_numbers);
1761917620
let line_gutter_width = if show_line_numbers {
1762017621
// Avoid flicker-like gutter resizes when the line number gains another digit and only resize the gutter on files with N*10^5 lines.
17621-
let min_width_for_number_on_gutter = em_advance * 4.0;
17622+
let min_width_for_number_on_gutter = em_advance * MIN_LINE_NUMBER_DIGITS as f32;
1762217623
max_line_number_width.max(min_width_for_number_on_gutter)
1762317624
} else {
1762417625
0.0.into()
@@ -17647,8 +17648,12 @@ impl EditorSnapshot {
1764717648
em_advance * max_char_count
1764817649
});
1764917650

17651+
let is_singleton = self.buffer_snapshot.is_singleton();
17652+
1765017653
let mut left_padding = git_blame_entries_width.unwrap_or(Pixels::ZERO);
17651-
left_padding += if show_code_actions || show_runnables {
17654+
left_padding += if !is_singleton {
17655+
em_width * 4.0
17656+
} else if show_code_actions || show_runnables {
1765217657
em_width * 3.0
1765317658
} else if show_git_gutter && show_line_numbers {
1765417659
em_width * 2.0
@@ -17658,9 +17663,11 @@ impl EditorSnapshot {
1765817663
px(0.)
1765917664
};
1766017665

17661-
let right_padding = if gutter_settings.folds && show_line_numbers {
17666+
let shows_folds = is_singleton && gutter_settings.folds;
17667+
17668+
let right_padding = if shows_folds && show_line_numbers {
1766217669
em_width * 4.0
17663-
} else if gutter_settings.folds {
17670+
} else if shows_folds || (!is_singleton && show_line_numbers) {
1766417671
em_width * 3.0
1766517672
} else if show_line_numbers {
1766617673
em_width

crates/editor/src/element.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
InlineCompletion, JumpData, LineDown, LineHighlight, LineUp, OpenExcerpts, PageDown, PageUp,
2424
Point, RowExt, RowRangeExt, SelectPhase, SelectedTextHighlight, Selection, SoftWrap,
2525
StickyHeaderExcerpt, ToPoint, ToggleFold, COLUMNAR_SELECTION_MODIFIERS, CURSORS_VISIBLE_FOR,
26-
FILE_HEADER_HEIGHT, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN,
26+
FILE_HEADER_HEIGHT, GIT_BLAME_MAX_AUTHOR_CHARS_DISPLAYED, MAX_LINE_LEN, MIN_LINE_NUMBER_DIGITS,
2727
MULTI_BUFFER_EXCERPT_HEADER_HEIGHT,
2828
};
2929
use buffer_diff::{DiffHunkStatus, DiffHunkStatusKind};
@@ -2119,9 +2119,11 @@ impl EditorElement {
21192119
})
21202120
}
21212121

2122-
fn layout_excerpt_gutter(
2122+
fn layout_expand_toggles(
21232123
&self,
21242124
gutter_hitbox: &Hitbox,
2125+
gutter_dimensions: GutterDimensions,
2126+
em_width: Pixels,
21252127
line_height: Pixels,
21262128
scroll_position: gpui::Point<f32>,
21272129
buffer_rows: &[RowInfo],
@@ -2130,19 +2132,17 @@ impl EditorElement {
21302132
) -> Vec<Option<(AnyElement, gpui::Point<Pixels>)>> {
21312133
let editor_font_size = self.style.text.font_size.to_pixels(window.rem_size()) * 1.2;
21322134

2133-
let icon_size = editor_font_size.round();
2134-
let button_h_padding = ((icon_size - px(1.0)) / 2.0).round() - px(2.0);
2135-
21362135
let scroll_top = scroll_position.y * line_height;
21372136

2138-
let max_line_number_length = 1 + self
2137+
let max_line_number_length = self
21392138
.editor
21402139
.read(cx)
21412140
.buffer()
21422141
.read(cx)
21432142
.snapshot(cx)
21442143
.widest_line_number()
2145-
.ilog10();
2144+
.ilog10()
2145+
+ 1;
21462146

21472147
let elements = buffer_rows
21482148
.into_iter()
@@ -2159,20 +2159,27 @@ impl EditorElement {
21592159
ExpandExcerptDirection::UpAndDown => IconName::ExpandVertical,
21602160
};
21612161

2162+
let git_gutter_width = Self::gutter_strip_width(line_height);
2163+
let available_width = gutter_dimensions.left_padding - git_gutter_width;
2164+
21622165
let editor = self.editor.clone();
2163-
let is_wide = max_line_number_length > 3
2166+
let is_wide = max_line_number_length >= MIN_LINE_NUMBER_DIGITS
21642167
&& row_info
21652168
.buffer_row
2166-
.is_some_and(|row| (row + 1).ilog10() + 1 == max_line_number_length);
2169+
.is_some_and(|row| (row + 1).ilog10() + 1 == max_line_number_length)
2170+
|| gutter_dimensions.right_padding == px(0.);
2171+
2172+
let width = if is_wide {
2173+
available_width - px(2.)
2174+
} else {
2175+
available_width + em_width - px(2.)
2176+
};
21672177

21682178
let toggle = IconButton::new(("expand", ix), icon_name)
21692179
.icon_color(Color::Custom(cx.theme().colors().editor_line_number))
21702180
.selected_icon_color(Color::Custom(cx.theme().colors().editor_foreground))
21712181
.icon_size(IconSize::Custom(rems(editor_font_size / window.rem_size())))
2172-
.width((icon_size + button_h_padding * 2).into())
2173-
.when(is_wide, |el| {
2174-
el.width((icon_size + button_h_padding).into())
2175-
})
2182+
.width(width.into())
21762183
.on_click(move |_, _, cx| {
21772184
editor.update(cx, |editor, cx| {
21782185
editor.expand_excerpt(excerpt_id, direction, cx);
@@ -2185,7 +2192,7 @@ impl EditorElement {
21852192
.into_any_element();
21862193

21872194
let position = point(
2188-
px(1.),
2195+
git_gutter_width + px(1.),
21892196
ix as f32 * line_height - (scroll_top % line_height) + px(1.),
21902197
);
21912198
let origin = gutter_hitbox.origin + position;
@@ -2709,7 +2716,7 @@ impl EditorElement {
27092716
for_excerpt: &ExcerptInfo,
27102717
is_folded: bool,
27112718
is_selected: bool,
2712-
_is_sticky: bool,
2719+
is_sticky: bool,
27132720
jump_data: JumpData,
27142721
window: &mut Window,
27152722
cx: &mut App,
@@ -2744,8 +2751,7 @@ impl EditorElement {
27442751
let colors = cx.theme().colors();
27452752

27462753
div()
2747-
.px_2()
2748-
.pt_2()
2754+
.p_1()
27492755
.w_full()
27502756
.h(FILE_HEADER_HEIGHT as f32 * window.line_height())
27512757
.child(
@@ -2756,7 +2762,7 @@ impl EditorElement {
27562762
.pl_0p5()
27572763
.pr_5()
27582764
.rounded_sm()
2759-
// .when(is_sticky, |el| el.shadow_md())
2765+
.when(is_sticky, |el| el.shadow_md())
27602766
.border_1()
27612767
.map(|div| {
27622768
let border_color = if is_selected
@@ -2784,7 +2790,7 @@ impl EditorElement {
27842790
ButtonLike::new("toggle-buffer-fold")
27852791
.style(ui::ButtonStyle::Transparent)
27862792
.size(ButtonSize::Large)
2787-
.width(px(30.).into())
2793+
.width(px(24.).into())
27882794
.children(toggle_chevron_icon)
27892795
.tooltip({
27902796
let focus_handle = focus_handle.clone();
@@ -4413,6 +4419,10 @@ impl EditorElement {
44134419
});
44144420
}
44154421

4422+
fn gutter_strip_width(line_height: Pixels) -> Pixels {
4423+
(0.275 * line_height).floor()
4424+
}
4425+
44164426
fn diff_hunk_bounds(
44174427
snapshot: &EditorSnapshot,
44184428
line_height: Pixels,
@@ -4421,7 +4431,7 @@ impl EditorElement {
44214431
) -> Bounds<Pixels> {
44224432
let scroll_position = snapshot.scroll_position();
44234433
let scroll_top = scroll_position.y * line_height;
4424-
let gutter_strip_width = (0.275 * line_height).floor();
4434+
let gutter_strip_width = Self::gutter_strip_width(line_height);
44254435

44264436
match hunk {
44274437
DisplayDiffHunk::Folded { display_row, .. } => {
@@ -6887,8 +6897,10 @@ impl Element for EditorElement {
68876897

68886898
let mut expand_toggles =
68896899
window.with_element_namespace("expand_toggles", |window| {
6890-
self.layout_excerpt_gutter(
6900+
self.layout_expand_toggles(
68916901
&gutter_hitbox,
6902+
gutter_dimensions,
6903+
em_width,
68926904
line_height,
68936905
scroll_position,
68946906
&row_infos,

0 commit comments

Comments
 (0)