Fixed a major bug and now use the same cursor paint logic as the editor

This commit is contained in:
Mikayla Maki 2022-06-30 20:34:06 -07:00
parent bcf5351e64
commit 831afb5ba7
3 changed files with 51 additions and 22 deletions

View file

@ -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<Line>,
) -> 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 => {

View file

@ -262,8 +262,7 @@ impl Terminal {
.active_entry()
.and_then(|entry_id| project.worktree_for_entry(entry_id, cx))
.and_then(|worktree_handle| worktree_handle.read(cx).as_local())
.map(|wt| wt.abs_path().to_path_buf())
.or_else(|| Some("~".into()));
.map(|wt| wt.abs_path().to_path_buf());
workspace.add_item(Box::new(cx.add_view(|cx| Terminal::new(cx, abs_path))), cx);
}

View file

@ -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>,
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<Line>,
display_offset: usize,
line_height: &LineHeight,
cell_width: &CellWidth,
) -> Option<RectF> {
) -> Option<Vector2F> {
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)
})
}