Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -10570,10 +10570,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;
Expand Down
51 changes: 50 additions & 1 deletion crates/editor/src/mouse_context_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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());
}
}
7 changes: 7 additions & 0 deletions crates/gpui/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,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 {
Expand Down
Loading