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 right-click context menus failing to appear at pixel-snapped scroll positions (#61348)

ᴀᴍᴛᴏᴀᴇʀ547a6cd599a1
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 5203bcbf1b5..e1f08e230f3 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -165,7 +165,7 @@ use gpui::{
Action, Animation, AnimationExt, AnyElement, App, AppContext, AsyncWindowContext,
AvailableSpace, Background, Bounds, ClickEvent, ClipboardEntry, ClipboardItem, Context,
DispatchPhase, Edges, Entity, EntityId, EntityInputHandler, EventEmitter, FocusHandle,
- FocusOutEvent, Focusable, FontId, FontStyle, FontWeight, Global, HighlightStyle, Hsla,
+ FocusOutEvent, Focusable, FontId, FontStyle, FontWeight, Global, HighlightStyle, Hsla, IsZero,
KeyContext, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, PaintQuad, ParentElement,
Pixels, PressureStage, Render, ScrollHandle, SharedString, SharedUri, Size, Stateful, Styled,
Subscription, Task, TextRun, TextStyle, TextStyleRefinement, UTF16Selection, UnderlineStyle,
@@ -10602,10 +10602,14 @@ impl Editor {
) -> Option<gpui::Point<Pixels>> {
let line_height = self.style(cx).text.line_height_in_pixels(window.rem_size());
let text_layout_details = self.text_layout_details(window, cx);
- let scroll_top = text_layout_details
+ let mut scroll_top = text_layout_details
.scroll_anchor
.scroll_position(editor_snapshot)
.y;
+ if !line_height.is_zero() {
+ scroll_top =
+ window.pixel_snap_f64(scroll_top * f64::from(line_height)) / f64::from(line_height);
+ }
if source.row().as_f64() < scroll_top.floor() {
return None;
diff --git a/crates/editor/src/mouse_context_menu.rs b/crates/editor/src/mouse_context_menu.rs
index 0c4cbb3667a..5963dcd557a 100644
--- a/crates/editor/src/mouse_context_menu.rs
+++ b/crates/editor/src/mouse_context_menu.rs
@@ -352,7 +352,12 @@ pub fn deploy_context_menu(
#[cfg(test)]
mod tests {
use super::*;
- use crate::{editor_tests::init_test, test::editor_lsp_test_context::EditorLspTestContext};
+ use crate::{
+ editor_tests::init_test,
+ test::{
+ editor_lsp_test_context::EditorLspTestContext, editor_test_context::EditorTestContext,
+ },
+ };
use indoc::indoc;
#[gpui::test]
@@ -400,4 +405,48 @@ mod tests {
"});
cx.editor(|editor, _window, _app| assert!(editor.mouse_context_menu.is_some()));
}
+
+ #[gpui::test]
+ async fn test_mouse_context_menu_at_pixel_snapped_scroll_position(
+ cx: &mut gpui::TestAppContext,
+ ) {
+ init_test(cx, |_| {});
+
+ let mut cx = EditorTestContext::new(cx).await;
+ cx.set_state(&format!("ˇ{}", "aaaaa\n".repeat(100)));
+ cx.update(|window, _| window.set_scale_factor(1.25));
+ cx.update_editor(|editor, _, cx| {
+ editor.set_text_style_refinement(gpui::TextStyleRefinement {
+ font_size: Some(gpui::px(14.).into()),
+ line_height: Some(gpui::relative(1.3)),
+ ..Default::default()
+ });
+ cx.notify();
+ });
+ cx.run_until_parked();
+
+ cx.update_editor(|editor, window, cx| {
+ assert_eq!(window.scale_factor(), 1.25);
+ let line_height = editor
+ .style(cx)
+ .text
+ .line_height_in_pixels(window.rem_size());
+ assert_eq!(line_height, gpui::px(18.));
+ assert!(window.pixel_snap_f64(f64::from(line_height)) / f64::from(line_height) < 1.);
+ editor.set_scroll_position(gpui::point(0., 1.), window, cx);
+ assert_eq!(editor.snapshot(window, cx).scroll_position().y, 1.);
+
+ deploy_context_menu(
+ editor,
+ Some(gpui::point(gpui::px(200.), gpui::px(200.))),
+ DisplayPoint::new(crate::display_map::DisplayRow(5), 0),
+ window,
+ cx,
+ );
+ assert!(editor.mouse_context_menu.is_some());
+ });
+ cx.run_until_parked();
+
+ assert!(cx.debug_bounds("MENU_ITEM-Copy").is_some());
+ }
}
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index cab88b718b5..312d797876b 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -2574,6 +2574,13 @@ impl Window {
self.scale_factor
}
+ /// Overrides the display scale factor for tests.
+ #[cfg(any(test, feature = "test-support"))]
+ pub fn set_scale_factor(&mut self, scale_factor: f32) {
+ self.scale_factor = scale_factor;
+ self.refresh();
+ }
+
/// The size of an em for the base font of the application. Adjusting this value allows the
/// UI to scale, just like zooming a web page.
pub fn rem_size(&self) -> Pixels {