From f169f8e35824ef75203c918fce5cee8eb453d49c Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 21 Jul 2021 12:27:01 -0600 Subject: [PATCH] Ensure that soft wrapped lines don't cause horizontal scrolling --- zed/src/editor.rs | 2 +- zed/src/editor/element.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/zed/src/editor.rs b/zed/src/editor.rs index c04c76930761aa5f64869ab35dda9b14962d746c..0ee4b398d5f73e251f9e4f59a45d1e823aa94285 100644 --- a/zed/src/editor.rs +++ b/zed/src/editor.rs @@ -2198,7 +2198,7 @@ impl Editor { font_cache.em_width(font_id, settings.buffer_font_size) } - pub fn set_width(&self, width: f32) { + pub fn set_wrap_width(&self, width: f32) { self.display_map.set_wrap_width(Some(width)); } diff --git a/zed/src/editor/element.rs b/zed/src/editor/element.rs index 76b22e3a9f7f3e0c541e94ca33b63e046e6c23af..b1b2a81adc964099394bf54e976dee433d4c152b 100644 --- a/zed/src/editor/element.rs +++ b/zed/src/editor/element.rs @@ -200,7 +200,6 @@ impl EditorElement { fn paint_text(&mut self, bounds: RectF, layout: &LayoutState, cx: &mut PaintContext) { let view = self.view(cx.app); let line_height = view.line_height(cx.font_cache); - let descent = view.font_descent(cx.font_cache); let start_row = view.scroll_position().y() as u32; let scroll_top = view.scroll_position().y() * line_height; let end_row = ((scroll_top + bounds.height()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen @@ -226,7 +225,7 @@ impl EditorElement { ]; let mut cursors = SmallVec::<[Cursor; 32]>::new(); - let content_origin = bounds.origin() + vec2f(-descent, 0.0); + let content_origin = bounds.origin() + layout.text_offset; for selection_set_id in view.active_selection_sets(cx.app) { let (selection_color, cursor_color) = colors[selection_set_id.replica_id as usize % colors.len()]; @@ -361,7 +360,13 @@ impl Element for EditorElement { let gutter_size = vec2f(gutter_width, size.y()); let text_size = size - vec2f(gutter_width, 0.0); - view.set_width(text_size.x()); + let text_offset = vec2f(-view.font_descent(cx.font_cache), 0.); + let em_width = view.em_width(cx.font_cache); + let overscroll = vec2f(em_width, 0.); + let wrap_width = text_size.x() - text_offset.x() - overscroll.x(); + // TODO: Core text doesn't seem to be keeping our lines below the specified wrap width. Find out why. + let wrap_width = wrap_width - em_width; + view.set_wrap_width(wrap_width); let autoscroll_horizontally = view.autoscroll_vertically(size.y(), line_height, app); @@ -406,6 +411,8 @@ impl Element for EditorElement { gutter_size, gutter_padding, text_size, + overscroll, + text_offset, line_layouts, line_number_layouts, max_visible_line_width, @@ -521,6 +528,8 @@ pub struct LayoutState { text_size: Vector2F, line_layouts: Vec, line_number_layouts: Vec, + overscroll: Vector2F, + text_offset: Vector2F, max_visible_line_width: f32, autoscroll_horizontally: bool, } @@ -538,7 +547,7 @@ impl LayoutState { .layout_line(row, font_cache, layout_cache, cx) .unwrap() .width(); - longest_line_width.max(self.max_visible_line_width) + view.em_width(font_cache) + longest_line_width.max(self.max_visible_line_width) + self.overscroll.x() } fn scroll_max(