From ae836e14658b278e4c14481c446a3bbb590ac027 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Thu, 30 Jun 2022 20:34:06 -0700 Subject: [PATCH] Fixed a major bug and now use the same cursor paint logic as the editor --- crates/editor/src/element.rs | 22 ++++++++++-- crates/terminal/src/terminal_element.rs | 48 +++++++++++++++---------- 2 files changed, 50 insertions(+), 20 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 19226c6472c3c0fd4dcd2a5964fe3dcbcfbd693a..348ce57ef37b957d9bef86bf114fe8956eb7cce2 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -1630,7 +1630,7 @@ impl Default for CursorShape { } } -struct Cursor { +pub struct Cursor { origin: Vector2F, block_width: f32, line_height: f32, @@ -1640,7 +1640,25 @@ struct Cursor { } impl Cursor { - fn paint(&self, cx: &mut PaintContext) { + pub fn new( + origin: Vector2F, + block_width: f32, + line_height: f32, + color: Color, + shape: CursorShape, + block_text: Option, + ) -> Cursor { + Cursor { + origin, + block_width, + line_height, + color, + shape, + block_text, + } + } + + pub fn paint(&self, cx: &mut PaintContext) { let bounds = match self.shape { CursorShape::Bar => RectF::new(self.origin, vec2f(2.0, self.line_height)), CursorShape::Block => { diff --git a/crates/terminal/src/terminal_element.rs b/crates/terminal/src/terminal_element.rs index ac964dca052d4a767441385a97072cd353a397d4..697a0abed95fb7774df8212c4b6ba0088bf15203 100644 --- a/crates/terminal/src/terminal_element.rs +++ b/crates/terminal/src/terminal_element.rs @@ -7,11 +7,15 @@ use alacritty_terminal::{ SizeInfo, }, }; +use editor::{Cursor, CursorShape}; use gpui::{ color::Color, elements::*, fonts::{HighlightStyle, TextStyle, Underline}, - geometry::{rect::RectF, vector::vec2f}, + geometry::{ + rect::RectF, + vector::{vec2f, Vector2F}, + }, json::json, text_layout::Line, Event, FontCache, MouseRegion, PaintContext, Quad, SizeConstraint, WeakViewHandle, @@ -74,7 +78,7 @@ pub struct LayoutState { lines: Vec, line_height: LineHeight, em_width: CellWidth, - cursor: Option<(RectF, Color)>, + cursor: Option<(Vector2F, Color)>, cur_size: SizeInfo, background_color: Color, background_rects: Vec<(RectF, Color)>, //Vec index == Line index for the LineSpan @@ -138,12 +142,11 @@ impl Element for TerminalEl { .collect(); let background_rects = make_background_rects(backgrounds, &shaped_lines, &line_height); - let cursor = make_cursor_rect( + let cursor = get_cursor_position( content.cursor.point, &shaped_lines, content.display_offset, &line_height, - &cell_width, ) .map(|cursor_rect| (cursor_rect, terminal_theme.cursor)); @@ -179,6 +182,16 @@ impl Element for TerminalEl { ..Default::default() }); + //TODO: Implement cursor region based styling + // cx.scene.push_cursor_region(CursorRegion { + // bounds, + // style: if !view.link_go_to_definition_state.definitions.is_empty() { + // CursorStyle::PointingHand + // } else { + // CursorStyle::IBeam + // }, + // }); + let origin = bounds.origin() + vec2f(layout.em_width.0, 0.); //Start us off with a nice simple background color @@ -212,13 +225,16 @@ impl Element for TerminalEl { //Draw cursor if let Some((c, color)) = layout.cursor { - let new_origin = origin + c.origin(); - cx.scene.push_quad(Quad { - bounds: RectF::new(new_origin, c.size()), - background: Some(color), - border: Default::default(), - corner_radius: 0., - }); + let editor_cursor = Cursor::new( + origin + c, + layout.em_width.0, + layout.line_height.0, + color, + CursorShape::Block, + None, //TODO fix this + ); + + editor_cursor.paint(cx); } #[cfg(debug_assertions)] @@ -374,20 +390,16 @@ fn make_background_rects( } ///Create the rectangle for a cursor, exactly positioned according to the text -fn make_cursor_rect( +fn get_cursor_position( cursor_point: Point, shaped_lines: &Vec, display_offset: usize, line_height: &LineHeight, - cell_width: &CellWidth, -) -> Option { +) -> Option { let cursor_line = cursor_point.line.0 as usize + display_offset; shaped_lines.get(cursor_line).map(|layout_line| { let cursor_x = layout_line.x_for_index(cursor_point.column.0); - RectF::new( - vec2f(cursor_x, cursor_line as f32 * line_height.0), - vec2f(cell_width.0, line_height.0), - ) + vec2f(cursor_x, cursor_line as f32 * line_height.0) }) }