Start on painting the editor

This commit is contained in:
Antonio Scandurra 2021-03-25 17:36:29 +01:00
parent 466f6e0479
commit 596fc47a68
2 changed files with 134 additions and 151 deletions

View file

@ -1,13 +1,15 @@
use super::{BufferView, DisplayPoint, SelectAction}; use super::{BufferView, DisplayPoint, SelectAction};
use gpui::{ use gpui::{
color::ColorU,
geometry::{ geometry::{
rect::RectF, rect::RectF,
vector::{vec2f, Vector2F}, vector::{vec2f, Vector2F},
}, },
text_layout::{self, TextLayoutCache}, text_layout::{self, TextLayoutCache},
AfterLayoutContext, AppContext, Element, Event, EventContext, FontCache, LayoutContext, AfterLayoutContext, AppContext, Border, Element, Event, EventContext, FontCache, LayoutContext,
MutableAppContext, PaintContext, Scene, SizeConstraint, ViewHandle, MutableAppContext, PaintContext, Quad, Scene, SizeConstraint, ViewHandle,
}; };
use smallvec::SmallVec;
use std::{ use std::{
cmp::{self}, cmp::{self},
sync::Arc, sync::Arc,
@ -161,159 +163,140 @@ impl BufferElement {
true true
} }
fn paint_gutter(&mut self, rect: RectF, ctx: &mut PaintContext) { fn paint_gutter(&mut self, rect: RectF, layout: &LayoutState, ctx: &mut PaintContext) {
// if let Some(layout) = self.layout.as_ref() { let view = self.view.as_ref(ctx.app);
// let view = self.view.as_ref(app); let line_height = view.line_height(ctx.font_cache);
// let scene = &mut ctx.scene; let scroll_top = view.scroll_position().y() * line_height;
// let font_cache = &ctx.font_cache;
// let line_height = view.line_height(font_cache);
// let scroll_top = view.scroll_position().y() * line_height;
// scene.save(); ctx.scene.push_layer();
// scene.translate(rect.origin()); ctx.scene.push_quad(Quad {
// scene.set_fill_style(FillStyle::Color(ColorU::white())); bounds: rect,
background: Some(ColorU::white()),
border: Border::new(0., ColorU::transparent_black()),
corner_radius: 0.,
});
// let rect = RectF::new(Vector2F::zero(), rect.size()); for (ix, line) in layout.line_number_layouts.iter().enumerate() {
// let mut rect_path = Path2D::new(); let line_origin = rect.origin()
// rect_path.rect(rect); + vec2f(
// scene.clip_path(rect_path, FillRule::EvenOdd); rect.width() - line.width - layout.gutter_padding,
// scene.fill_rect(rect); ix as f32 * line_height - (scroll_top % line_height),
);
line.paint(
RectF::new(line_origin, vec2f(line_height, line.width)),
&[(0..line.len, ColorU::black())],
ctx,
);
}
// for (ix, line) in layout.line_number_layouts.iter().enumerate() { ctx.scene.pop_layer();
// let line_origin = vec2f(
// rect.width() - line.width - layout.gutter_padding,
// ix as f32 * line_height - (scroll_top % line_height),
// );
// line.paint(
// line_origin,
// rect,
// &[(0..line.len, ColorU::black())],
// scene,
// font_cache,
// );
// }
// scene.restore();
// }
} }
fn paint_text(&mut self, rect: RectF, ctx: &mut PaintContext) { fn paint_text(&mut self, bounds: RectF, layout: &LayoutState, ctx: &mut PaintContext) {
// if let Some(layout) = self.layout.as_ref() { let view = self.view.as_ref(ctx.app);
// let scene = &mut ctx.scene; let line_height = view.line_height(ctx.font_cache);
// let font_cache = &ctx.font_cache; let descent = view.font_descent(ctx.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
let max_glyph_width = view.em_width(ctx.font_cache);
let scroll_left = view.scroll_position().x() * max_glyph_width;
// scene.save(); ctx.scene.push_layer();
// scene.translate(rect.origin()); ctx.scene.push_quad(Quad {
// scene.set_fill_style(FillStyle::Color(ColorU::white())); bounds,
// let rect = RectF::new(Vector2F::zero(), rect.size()); background: Some(ColorU::white()),
// let mut rect_path = Path2D::new(); border: Border::new(0., ColorU::transparent_black()),
// rect_path.rect(rect); corner_radius: 0.,
// scene.clip_path(rect_path, FillRule::EvenOdd); });
// scene.fill_rect(rect);
// let view = self.view.as_ref(app); // Draw selections
// let line_height = view.line_height(font_cache); // let corner_radius = 2.5;
// let descent = view.font_descent(font_cache); let mut cursors = SmallVec::<[Cursor; 32]>::new();
// let start_row = view.scroll_position().y() as u32;
// let scroll_top = view.scroll_position().y() * line_height;
// let end_row = ((scroll_top + rect.height()) / line_height).ceil() as u32 + 1; // Add 1 to ensure selections bleed off screen
// let max_glyph_width = view.em_width(font_cache);
// let scroll_left = view.scroll_position().x() * max_glyph_width;
// // Draw selections for selection in view.selections_in_range(
// scene.save(); DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0),
// let corner_radius = 2.5; ctx.app,
// let mut cursors = SmallVec::<[Cursor; 32]>::new(); ) {
// if selection.start != selection.end {
// let range_start = cmp::min(selection.start, selection.end);
// let range_end = cmp::max(selection.start, selection.end);
// let row_range = if range_end.column() == 0 {
// cmp::max(range_start.row(), start_row)..cmp::min(range_end.row(), end_row)
// } else {
// cmp::max(range_start.row(), start_row)..cmp::min(range_end.row() + 1, end_row)
// };
// for selection in view.selections_in_range( // let selection = Selection {
// DisplayPoint::new(start_row, 0)..DisplayPoint::new(end_row, 0), // line_height,
// app, // start_y: row_range.start as f32 * line_height - scroll_top,
// ) { // lines: row_range
// if selection.start != selection.end { // .into_iter()
// let range_start = cmp::min(selection.start, selection.end); // .map(|row| {
// let range_end = cmp::max(selection.start, selection.end); // let line_layout = &layout.line_layouts[(row - start_row) as usize];
// let row_range = if range_end.column() == 0 { // SelectionLine {
// cmp::max(range_start.row(), start_row)..cmp::min(range_end.row(), end_row) // start_x: if row == range_start.row() {
// } else { // line_layout.x_for_index(range_start.column() as usize)
// cmp::max(range_start.row(), start_row) // - scroll_left
// ..cmp::min(range_end.row() + 1, end_row) // - descent
// }; // } else {
// -scroll_left
// },
// end_x: if row == range_end.row() {
// line_layout.x_for_index(range_end.column() as usize)
// - scroll_left
// - descent
// } else {
// line_layout.width + corner_radius * 2.0 - scroll_left - descent
// },
// }
// })
// .collect(),
// };
// let selection = Selection { // selection.paint(scene);
// line_height, // }
// start_y: row_range.start as f32 * line_height - scroll_top,
// lines: row_range
// .into_iter()
// .map(|row| {
// let line_layout = &layout.line_layouts[(row - start_row) as usize];
// SelectionLine {
// start_x: if row == range_start.row() {
// line_layout.x_for_index(range_start.column() as usize)
// - scroll_left
// - descent
// } else {
// -scroll_left
// },
// end_x: if row == range_end.row() {
// line_layout.x_for_index(range_end.column() as usize)
// - scroll_left
// - descent
// } else {
// line_layout.width + corner_radius * 2.0
// - scroll_left
// - descent
// },
// }
// })
// .collect(),
// };
// selection.paint(scene); if view.cursors_visible() {
// } let cursor_position = selection.end;
if (start_row..end_row).contains(&cursor_position.row()) {
let cursor_row_layout =
&layout.line_layouts[(selection.end.row() - start_row) as usize];
let x = cursor_row_layout.x_for_index(selection.end.column() as usize)
- scroll_left
- descent;
let y = selection.end.row() as f32 * line_height - scroll_top;
cursors.push(Cursor {
origin: bounds.origin() + vec2f(x, y),
line_height,
});
}
}
}
// if view.cursors_visible() { // Draw glyphs
// let cursor_position = selection.end; for (ix, line) in layout.line_layouts.iter().enumerate() {
// if (start_row..end_row).contains(&cursor_position.row()) { let row = start_row + ix as u32;
// let cursor_row_layout = let line_origin = bounds.origin()
// &layout.line_layouts[(selection.end.row() - start_row) as usize]; + vec2f(
// cursors.push(Cursor { -scroll_left - descent,
// x: cursor_row_layout.x_for_index(selection.end.column() as usize) row as f32 * line_height - scroll_top,
// - scroll_left );
// - descent,
// y: selection.end.row() as f32 * line_height - scroll_top,
// line_height,
// });
// }
// }
// }
// scene.restore();
// // Draw glyphs line.paint(
RectF::new(line_origin, vec2f(line.width, line_height)),
&[(0..line.len, ColorU::black())],
ctx,
);
}
// scene.set_fill_style(FillStyle::Color(ColorU::black())); ctx.scene.push_layer();
for cursor in cursors {
cursor.paint(ctx);
}
ctx.scene.pop_layer();
// for (ix, line) in layout.line_layouts.iter().enumerate() { ctx.scene.pop_layer();
// let row = start_row + ix as u32;
// let line_origin = vec2f(
// -scroll_left - descent,
// row as f32 * line_height - scroll_top,
// );
// line.paint(
// line_origin,
// rect,
// &[(0..line.len, ColorU::black())],
// scene,
// font_cache,
// );
// }
// for cursor in cursors {
// cursor.paint(scene);
// }
// scene.restore()
// }
} }
} }
@ -454,9 +437,9 @@ impl Element for BufferElement {
); );
if self.view.as_ref(ctx.app).is_gutter_visible() { if self.view.as_ref(ctx.app).is_gutter_visible() {
self.paint_gutter(gutter_bounds, ctx); self.paint_gutter(gutter_bounds, layout, ctx);
} }
self.paint_text(text_bounds, ctx); self.paint_text(text_bounds, layout, ctx);
Some(PaintState { Some(PaintState {
bounds, bounds,
@ -575,18 +558,18 @@ impl PaintState {
} }
struct Cursor { struct Cursor {
x: f32, origin: Vector2F,
y: f32,
line_height: f32, line_height: f32,
} }
impl Cursor { impl Cursor {
fn paint(&self, scene: &mut Scene) { fn paint(&self, ctx: &mut PaintContext) {
// scene.set_fill_style(FillStyle::Color(ColorU::black())); ctx.scene.push_quad(Quad {
// scene.fill_rect(RectF::new( bounds: RectF::new(self.origin, vec2f(2.0, self.line_height)),
// vec2f(self.x, self.y), background: Some(ColorU::black()),
// vec2f(2.0, self.line_height), border: Border::new(0., ColorU::black()),
// )); corner_radius: 0.,
});
} }
} }

View file

@ -15,7 +15,7 @@ impl Settings {
pub fn new(font_cache: &FontCache) -> Result<Self> { pub fn new(font_cache: &FontCache) -> Result<Self> {
Ok(Self { Ok(Self {
buffer_font_family: font_cache.load_family(&["Fira Code", "Monaco"])?, buffer_font_family: font_cache.load_family(&["Fira Code", "Monaco"])?,
buffer_font_size: 16.0, buffer_font_size: 14.0,
tab_size: 4, tab_size: 4,
ui_font_family: font_cache.load_family(&["SF Pro Display"])?, ui_font_family: font_cache.load_family(&["SF Pro Display"])?,
ui_font_size: 12.0, ui_font_size: 12.0,