Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

editor: Fix movement mismatch between minimap thumb and cursor (#62087)

Yue Fei90731bdec056
diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs
index 91f06b0e2ff..56663881ff0 100644
--- a/crates/editor/src/element.rs
+++ b/crates/editor/src/element.rs
@@ -1672,6 +1672,12 @@ impl EditorElement {
)
.with_thumb_state(thumb_state);
+ let thumb_pixels_per_editor_line = MinimapLayout::thumb_pixels_per_editor_line(
+ &layout,
+ total_editor_lines,
+ minimap_line_height,
+ );
+
minimap_editor.update(cx, |editor, cx| {
editor.set_scroll_position(point(0., minimap_scroll_top), window, cx)
});
@@ -1703,6 +1709,7 @@ impl EditorElement {
minimap_line_height,
minimap_scroll_top,
max_scroll_top: total_editor_lines,
+ thumb_pixels_per_editor_line,
})
}
@@ -6694,18 +6701,14 @@ impl EditorElement {
}
let minimap_axis = ScrollbarAxis::Vertical;
- let pixels_per_line = Pixels::from(
- ScrollPixelOffset::from(minimap_hitbox.size.height) / layout.max_scroll_top,
- )
- .min(layout.minimap_line_height);
-
- let mut mouse_position = window.mouse_position();
window.on_mouse_event({
let editor = self.editor.clone();
let minimap_hitbox = minimap_hitbox.clone();
+ let mut mouse_position = window.mouse_position();
+
move |event: &MouseMoveEvent, phase, window, cx| {
if phase == DispatchPhase::Capture {
return;
@@ -6717,9 +6720,10 @@ impl EditorElement {
{
let old_position = mouse_position.along(minimap_axis);
let new_position = event.position.along(minimap_axis);
- if (minimap_hitbox.origin.along(minimap_axis)
- ..minimap_hitbox.bottom_right().along(minimap_axis))
- .contains(&old_position)
+ if let Some(pixels_per_line) = layout.thumb_pixels_per_editor_line
+ && (minimap_hitbox.origin.along(minimap_axis)
+ ..minimap_hitbox.bottom_right().along(minimap_axis))
+ .contains(&old_position)
{
let position =
editor.scroll_position(cx).apply_along(minimap_axis, |p| {
@@ -10634,6 +10638,7 @@ struct MinimapLayout {
pub minimap_line_height: Pixels,
pub thumb_border_style: MinimapThumbBorder,
pub max_scroll_top: ScrollOffset,
+ pub thumb_pixels_per_editor_line: Option<Pixels>,
}
impl MinimapLayout {
@@ -10657,6 +10662,35 @@ impl MinimapLayout {
scroll_percentage * (document_lines - visible_minimap_lines).max(0.)
}
}
+
+ /// How far the thumb moves on screen per scrolled editor line.
+ ///
+ /// The thumb moves along its oversized track, but the minimap content scrolls underneath
+ /// it at the same time (see [`Self::calculate_minimap_top_offset`]), so what we actually
+ /// see is the difference of the two. Returns `None` if the thumb can't move at all, e.g.
+ /// because the whole document fits into the editor or the thumb covers the entire minimap
+ /// (extremely unlikely, but may happen for very small editors that we currently allow)
+ fn thumb_pixels_per_editor_line(
+ thumb_layout: &ScrollbarLayout,
+ document_lines: f64,
+ minimap_line_height: Pixels,
+ ) -> Option<Pixels> {
+ let visible_editor_lines =
+ thumb_layout.visible_range.end - thumb_layout.visible_range.start;
+ let visible_minimap_lines = (thumb_layout.hitbox.size.height / minimap_line_height) as f64;
+ let scrollable_editor_lines = document_lines - visible_editor_lines;
+ if scrollable_editor_lines <= 0. {
+ return None;
+ }
+
+ let minimap_scroll_per_editor_line =
+ (document_lines - visible_minimap_lines).max(0.) / scrollable_editor_lines;
+ let pixels_per_line = Pixels::from(
+ ScrollOffset::from(thumb_layout.text_unit_size)
+ - minimap_scroll_per_editor_line * ScrollOffset::from(minimap_line_height),
+ );
+ (pixels_per_line > Pixels::ZERO).then_some(pixels_per_line)
+ }
}
struct CreaseTrailerLayout {
@@ -11344,6 +11378,152 @@ mod tests {
use text::PointUtf16;
use util::test::sample_text;
+ struct MinimapGeometry {
+ document_lines: f64,
+ visible_editor_lines: f64,
+ minimap_line_height: Pixels,
+ minimap_height: Pixels,
+ }
+
+ impl MinimapGeometry {
+ fn visible_minimap_lines(&self) -> f64 {
+ (self.minimap_height / self.minimap_line_height) as f64
+ }
+
+ fn thumb_layout(&self, scroll_position: f64) -> ScrollbarLayout {
+ let bounds = Bounds::new(point(px(0.), px(0.)), size(px(100.), self.minimap_height));
+ let hitbox = Hitbox {
+ id: gpui::HitboxId::placeholder(),
+ bounds,
+ content_mask: gpui::ContentMask { bounds },
+ behavior: HitboxBehavior::Normal,
+ };
+ let minimap_scroll_top = MinimapLayout::calculate_minimap_top_offset(
+ self.document_lines,
+ self.visible_editor_lines,
+ self.visible_minimap_lines(),
+ scroll_position,
+ );
+ ScrollbarLayout::for_minimap(
+ hitbox,
+ self.visible_editor_lines,
+ self.document_lines,
+ self.minimap_line_height,
+ scroll_position,
+ minimap_scroll_top,
+ true,
+ )
+ }
+
+ fn thumb_movement_for_drag(&self, scroll_position: f64, mouse_delta: Pixels) -> Pixels {
+ let layout = self.thumb_layout(scroll_position);
+ let pixels_per_line = self
+ .thumb_pixels_per_editor_line(&layout)
+ .expect("thumb should be draggable");
+
+ let new_scroll_position =
+ scroll_position + ScrollPixelOffset::from(mouse_delta / pixels_per_line);
+
+ let old_top = layout.thumb_bounds.expect("thumb should be shown").origin.y;
+ let new_top = self
+ .thumb_layout(new_scroll_position)
+ .thumb_bounds
+ .expect("thumb should be shown")
+ .origin
+ .y;
+ new_top - old_top
+ }
+
+ fn thumb_pixels_per_editor_line(&self, layout: &ScrollbarLayout) -> Option<Pixels> {
+ MinimapLayout::thumb_pixels_per_editor_line(
+ layout,
+ self.document_lines,
+ self.minimap_line_height,
+ )
+ }
+
+ #[track_caller]
+ fn assert_thumb_follows_drag(&self, scroll_position: f64) {
+ let mouse_delta = px(10.);
+ let movement = self.thumb_movement_for_drag(scroll_position, mouse_delta);
+ assert!(
+ (movement - mouse_delta).abs() < px(0.01),
+ "dragging by {mouse_delta:?} from scroll position {scroll_position} moved the thumb by {movement:?}",
+ );
+ }
+ }
+
+ #[test]
+ fn test_minimap_thumb_follows_drag_in_short_document() {
+ let geometry = MinimapGeometry {
+ document_lines: 100.,
+ visible_editor_lines: 40.,
+ minimap_line_height: px(2.),
+ minimap_height: px(800.),
+ };
+ geometry.assert_thumb_follows_drag(0.);
+ geometry.assert_thumb_follows_drag(30.);
+ }
+
+ #[test]
+ fn test_minimap_thumb_follows_drag_in_long_document() {
+ let geometry = MinimapGeometry {
+ document_lines: 2000.,
+ visible_editor_lines: 40.,
+ minimap_line_height: px(2.),
+ minimap_height: px(800.),
+ };
+ geometry.assert_thumb_follows_drag(0.);
+ geometry.assert_thumb_follows_drag(500.);
+ }
+
+ #[test]
+ fn test_minimap_thumb_follows_drag_with_minimum_thumb_size() {
+ // 10 lines at 2px each would only be a 20px thumb, so this gets bumped up to
+ // `ScrollbarLayout::MIN_THUMB_SIZE`
+ let short_document = MinimapGeometry {
+ document_lines: 80.,
+ visible_editor_lines: 10.,
+ minimap_line_height: px(2.),
+ minimap_height: px(200.),
+ };
+ short_document.assert_thumb_follows_drag(0.);
+ short_document.assert_thumb_follows_drag(30.);
+
+ let long_document = MinimapGeometry {
+ document_lines: 2000.,
+ ..short_document
+ };
+ long_document.assert_thumb_follows_drag(0.);
+ long_document.assert_thumb_follows_drag(500.);
+ }
+
+ #[test]
+ fn test_minimap_thumb_not_draggable_without_room_to_move() {
+ let fits_in_editor = MinimapGeometry {
+ document_lines: 30.,
+ visible_editor_lines: 40.,
+ minimap_line_height: px(2.),
+ minimap_height: px(800.),
+ };
+ assert_eq!(
+ fits_in_editor.thumb_pixels_per_editor_line(&fits_in_editor.thumb_layout(0.)),
+ None
+ );
+
+ // 10 lines at 2px each is a 20px track, so the thumb (at least 25px) already covers all of it
+ let thumb_fills_track = MinimapGeometry {
+ document_lines: 10.,
+ visible_editor_lines: 5.,
+ minimap_line_height: px(2.),
+ minimap_height: px(800.),
+ };
+ assert_eq!(
+ thumb_fills_track.thumb_pixels_per_editor_line(&thumb_fills_track.thumb_layout(0.)),
+ None
+ );
+ }
+
enum PrimaryNavigationOverlay {}
const PRIMARY_NAVIGATION_OVERLAY_KEY: NavigationOverlayKey =