editor: Use em_advance
everywhere for horizontal scroll position computations (#33514)
Closes #33472 This PR fixes some regressions that were introduced in https://github.com/zed-industries/zed/pull/32558, which updated the editor scrolling to use `em_advance` instead of `em_width` for the horizontal scroll position calculation. However, not all occurrences were updated, which caused issues with wrap guides and some small stuttering with horizontal autoscroll whilst typing/navigating with the keyboard. Release Notes: - Fixed an issue where horizontal autoscrolling would stutter and indent guides would drift when scrolling horizontally.
This commit is contained in:
parent
e6bc1308af
commit
4c2415b338
3 changed files with 29 additions and 18 deletions
|
@ -1215,6 +1215,12 @@ impl GutterDimensions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct CharacterDimensions {
|
||||||
|
em_width: Pixels,
|
||||||
|
em_advance: Pixels,
|
||||||
|
line_height: Pixels,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RemoteSelection {
|
pub struct RemoteSelection {
|
||||||
pub replica_id: ReplicaId,
|
pub replica_id: ReplicaId,
|
||||||
|
@ -20520,15 +20526,20 @@ impl Editor {
|
||||||
.and_then(|item| item.to_any_mut()?.downcast_mut::<T>())
|
.and_then(|item| item.to_any_mut()?.downcast_mut::<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn character_size(&self, window: &mut Window) -> gpui::Size<Pixels> {
|
fn character_dimensions(&self, window: &mut Window) -> CharacterDimensions {
|
||||||
let text_layout_details = self.text_layout_details(window);
|
let text_layout_details = self.text_layout_details(window);
|
||||||
let style = &text_layout_details.editor_style;
|
let style = &text_layout_details.editor_style;
|
||||||
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(window.rem_size());
|
||||||
let line_height = style.text.line_height_in_pixels(window.rem_size());
|
let line_height = style.text.line_height_in_pixels(window.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();
|
||||||
|
|
||||||
gpui::Size::new(em_width, line_height)
|
CharacterDimensions {
|
||||||
|
em_width,
|
||||||
|
em_advance,
|
||||||
|
line_height,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wait_for_diff_to_load(&self) -> Option<Shared<Task<()>>> {
|
pub fn wait_for_diff_to_load(&self) -> Option<Shared<Task<()>>> {
|
||||||
|
@ -22542,19 +22553,19 @@ impl EntityInputHandler for Editor {
|
||||||
cx: &mut Context<Self>,
|
cx: &mut Context<Self>,
|
||||||
) -> Option<gpui::Bounds<Pixels>> {
|
) -> Option<gpui::Bounds<Pixels>> {
|
||||||
let text_layout_details = self.text_layout_details(window);
|
let text_layout_details = self.text_layout_details(window);
|
||||||
let gpui::Size {
|
let CharacterDimensions {
|
||||||
width: em_width,
|
em_width,
|
||||||
height: line_height,
|
em_advance,
|
||||||
} = self.character_size(window);
|
line_height,
|
||||||
|
} = self.character_dimensions(window);
|
||||||
|
|
||||||
let snapshot = self.snapshot(window, cx);
|
let snapshot = self.snapshot(window, cx);
|
||||||
let scroll_position = snapshot.scroll_position();
|
let scroll_position = snapshot.scroll_position();
|
||||||
let scroll_left = scroll_position.x * em_width;
|
let scroll_left = scroll_position.x * em_advance;
|
||||||
|
|
||||||
let start = OffsetUtf16(range_utf16.start).to_display_point(&snapshot);
|
let start = OffsetUtf16(range_utf16.start).to_display_point(&snapshot);
|
||||||
let x = snapshot.x_for_display_point(start, &text_layout_details) - scroll_left
|
let x = snapshot.x_for_display_point(start, &text_layout_details) - scroll_left
|
||||||
+ self.gutter_dimensions.width
|
+ self.gutter_dimensions.full_width();
|
||||||
+ self.gutter_dimensions.margin;
|
|
||||||
let y = line_height * (start.row().as_f32() - scroll_position.y);
|
let y = line_height * (start.row().as_f32() - scroll_position.y);
|
||||||
|
|
||||||
Some(Bounds {
|
Some(Bounds {
|
||||||
|
|
|
@ -5238,8 +5238,8 @@ impl EditorElement {
|
||||||
paint_highlight(range.start, range.end, color, edges);
|
paint_highlight(range.start, range.end, color, edges);
|
||||||
}
|
}
|
||||||
|
|
||||||
let scroll_left =
|
let scroll_left = layout.position_map.snapshot.scroll_position().x
|
||||||
layout.position_map.snapshot.scroll_position().x * layout.position_map.em_width;
|
* layout.position_map.em_advance;
|
||||||
|
|
||||||
for (wrap_position, active) in layout.wrap_guides.iter() {
|
for (wrap_position, active) in layout.wrap_guides.iter() {
|
||||||
let x = (layout.position_map.text_hitbox.origin.x
|
let x = (layout.position_map.text_hitbox.origin.x
|
||||||
|
@ -6676,7 +6676,7 @@ impl EditorElement {
|
||||||
let position_map: &PositionMap = &position_map;
|
let position_map: &PositionMap = &position_map;
|
||||||
|
|
||||||
let line_height = position_map.line_height;
|
let line_height = position_map.line_height;
|
||||||
let max_glyph_width = position_map.em_width;
|
let max_glyph_advance = position_map.em_advance;
|
||||||
let (delta, axis) = match delta {
|
let (delta, axis) = match delta {
|
||||||
gpui::ScrollDelta::Pixels(mut pixels) => {
|
gpui::ScrollDelta::Pixels(mut pixels) => {
|
||||||
//Trackpad
|
//Trackpad
|
||||||
|
@ -6687,15 +6687,15 @@ impl EditorElement {
|
||||||
gpui::ScrollDelta::Lines(lines) => {
|
gpui::ScrollDelta::Lines(lines) => {
|
||||||
//Not trackpad
|
//Not trackpad
|
||||||
let pixels =
|
let pixels =
|
||||||
point(lines.x * max_glyph_width, lines.y * line_height);
|
point(lines.x * max_glyph_advance, lines.y * line_height);
|
||||||
(pixels, None)
|
(pixels, None)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let current_scroll_position = position_map.snapshot.scroll_position();
|
let current_scroll_position = position_map.snapshot.scroll_position();
|
||||||
let x = (current_scroll_position.x * max_glyph_width
|
let x = (current_scroll_position.x * max_glyph_advance
|
||||||
- (delta.x * scroll_sensitivity))
|
- (delta.x * scroll_sensitivity))
|
||||||
/ max_glyph_width;
|
/ max_glyph_advance;
|
||||||
let y = (current_scroll_position.y * line_height
|
let y = (current_scroll_position.y * line_height
|
||||||
- (delta.y * scroll_sensitivity))
|
- (delta.y * scroll_sensitivity))
|
||||||
/ line_height;
|
/ line_height;
|
||||||
|
@ -8591,7 +8591,7 @@ impl Element for EditorElement {
|
||||||
start_row,
|
start_row,
|
||||||
editor_content_width,
|
editor_content_width,
|
||||||
scroll_width,
|
scroll_width,
|
||||||
em_width,
|
em_advance,
|
||||||
&line_layouts,
|
&line_layouts,
|
||||||
cx,
|
cx,
|
||||||
)
|
)
|
||||||
|
|
|
@ -275,10 +275,10 @@ pub fn deploy_context_menu(
|
||||||
cx,
|
cx,
|
||||||
),
|
),
|
||||||
None => {
|
None => {
|
||||||
let character_size = editor.character_size(window);
|
let character_size = editor.character_dimensions(window);
|
||||||
let menu_position = MenuPosition::PinnedToEditor {
|
let menu_position = MenuPosition::PinnedToEditor {
|
||||||
source: source_anchor,
|
source: source_anchor,
|
||||||
offset: gpui::point(character_size.width, character_size.height),
|
offset: gpui::point(character_size.em_width, character_size.line_height),
|
||||||
};
|
};
|
||||||
Some(MouseContextMenu::new(
|
Some(MouseContextMenu::new(
|
||||||
editor,
|
editor,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue