Make Line::paint interface consistent with Line::paint_wrapped

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra 2021-09-02 14:33:52 +02:00
parent bdc35f03f2
commit 6a071e865f
4 changed files with 46 additions and 35 deletions

View file

@ -49,7 +49,7 @@ impl gpui::Element for TextElement {
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, _: &mut Self::LayoutState,
cx: &mut gpui::PaintContext, cx: &mut gpui::PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
@ -84,11 +84,11 @@ impl gpui::Element for TextElement {
); );
cx.scene.push_quad(Quad { cx.scene.push_quad(Quad {
bounds: bounds, bounds,
background: Some(Color::white()), background: Some(Color::white()),
..Default::default() ..Default::default()
}); });
line.paint(bounds.origin(), bounds, cx); line.paint(bounds.origin(), visible_bounds, bounds.height(), cx);
} }
fn dispatch_event( fn dispatch_event(

View file

@ -132,11 +132,7 @@ impl Element for Label {
line: &mut Self::LayoutState, line: &mut Self::LayoutState,
cx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
line.paint( line.paint(bounds.origin(), visible_bounds, bounds.size().y(), cx)
bounds.origin(),
RectF::new(vec2f(0., 0.), bounds.size()),
cx,
)
} }
fn dispatch_event( fn dispatch_event(

View file

@ -205,8 +205,14 @@ impl Line {
} }
} }
pub fn paint(&self, origin: Vector2F, visible_bounds: RectF, cx: &mut PaintContext) { pub fn paint(
let padding_top = (visible_bounds.height() - self.layout.ascent - self.layout.descent) / 2.; &self,
origin: Vector2F,
visible_bounds: RectF,
line_height: f32,
cx: &mut PaintContext,
) {
let padding_top = (line_height - self.layout.ascent - self.layout.descent) / 2.;
let baseline_origin = vec2f(0., padding_top + self.layout.ascent); let baseline_origin = vec2f(0., padding_top + self.layout.ascent);
let mut color_runs = self.color_runs.iter(); let mut color_runs = self.color_runs.iter();
@ -220,7 +226,7 @@ impl Line {
.x(); .x();
for glyph in &run.glyphs { for glyph in &run.glyphs {
let glyph_origin = baseline_origin + glyph.position; let glyph_origin = origin + baseline_origin + glyph.position;
if glyph_origin.x() + max_glyph_width < visible_bounds.origin().x() { if glyph_origin.x() + max_glyph_width < visible_bounds.origin().x() {
continue; continue;
@ -243,7 +249,7 @@ impl Line {
font_id: run.font_id, font_id: run.font_id,
font_size: self.layout.font_size, font_size: self.layout.font_size,
id: glyph.id, id: glyph.id,
origin: origin + glyph_origin, origin: glyph_origin,
color, color,
}); });
} }

View file

@ -234,25 +234,33 @@ impl EditorElement {
} }
} }
fn paint_gutter(&mut self, rect: RectF, layout: &LayoutState, cx: &mut PaintContext) { fn paint_gutter(
&mut self,
bounds: RectF,
visible_bounds: RectF,
layout: &LayoutState,
cx: &mut PaintContext,
) {
let scroll_top = layout.snapshot.scroll_position().y() * layout.line_height; let scroll_top = layout.snapshot.scroll_position().y() * layout.line_height;
for (ix, line) in layout.line_number_layouts.iter().enumerate() { for (ix, line) in layout.line_number_layouts.iter().enumerate() {
if let Some(line) = line { if let Some(line) = line {
let line_origin = rect.origin() let line_origin = bounds.origin()
+ vec2f( + vec2f(
rect.width() - line.width() - layout.gutter_padding, bounds.width() - line.width() - layout.gutter_padding,
ix as f32 * layout.line_height - (scroll_top % layout.line_height), ix as f32 * layout.line_height - (scroll_top % layout.line_height),
); );
line.paint( line.paint(line_origin, visible_bounds, layout.line_height, cx);
line_origin,
RectF::new(vec2f(0., 0.), vec2f(line.width(), layout.line_height)),
cx,
);
} }
} }
} }
fn paint_text(&mut self, bounds: RectF, layout: &LayoutState, cx: &mut PaintContext) { fn paint_text(
&mut self,
bounds: RectF,
visible_bounds: RectF,
layout: &LayoutState,
cx: &mut PaintContext,
) {
let view = self.view(cx.app); let view = self.view(cx.app);
let settings = self.view(cx.app).settings.borrow(); let settings = self.view(cx.app).settings.borrow();
let theme = &settings.theme.editor; let theme = &settings.theme.editor;
@ -338,17 +346,18 @@ impl EditorElement {
} }
} }
// Draw glyphs if let Some(visible_text_bounds) = bounds.intersection(visible_bounds) {
for (ix, line) in layout.line_layouts.iter().enumerate() { // Draw glyphs
let row = start_row + ix as u32; for (ix, line) in layout.line_layouts.iter().enumerate() {
line.paint( let row = start_row + ix as u32;
content_origin + vec2f(-scroll_left, row as f32 * layout.line_height - scroll_top), line.paint(
RectF::new( content_origin
vec2f(scroll_left, 0.), + vec2f(-scroll_left, row as f32 * layout.line_height - scroll_top),
vec2f(bounds.width(), layout.line_height), visible_text_bounds,
), layout.line_height,
cx, cx,
); );
}
} }
cx.scene.push_layer(Some(bounds)); cx.scene.push_layer(Some(bounds));
@ -559,7 +568,7 @@ impl Element for EditorElement {
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
_: RectF, visible_bounds: RectF,
layout: &mut Self::LayoutState, layout: &mut Self::LayoutState,
cx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
@ -574,9 +583,9 @@ impl Element for EditorElement {
self.paint_background(gutter_bounds, text_bounds, layout, cx); self.paint_background(gutter_bounds, text_bounds, layout, cx);
if layout.gutter_size.x() > 0. { if layout.gutter_size.x() > 0. {
self.paint_gutter(gutter_bounds, layout, cx); self.paint_gutter(gutter_bounds, visible_bounds, layout, cx);
} }
self.paint_text(text_bounds, layout, cx); self.paint_text(text_bounds, visible_bounds, layout, cx);
cx.scene.pop_layer(); cx.scene.pop_layer();