Added basic selections

This commit is contained in:
Mikayla Maki 2022-07-06 17:37:12 -07:00
parent cba5b4ac11
commit 778cfd94d8
2 changed files with 216 additions and 113 deletions

View file

@ -1685,22 +1685,22 @@ impl Cursor {
} }
#[derive(Debug)] #[derive(Debug)]
struct HighlightedRange { pub struct HighlightedRange {
start_y: f32, pub start_y: f32,
line_height: f32, pub line_height: f32,
lines: Vec<HighlightedRangeLine>, pub lines: Vec<HighlightedRangeLine>,
color: Color, pub color: Color,
corner_radius: f32, pub corner_radius: f32,
} }
#[derive(Debug)] #[derive(Debug)]
struct HighlightedRangeLine { pub struct HighlightedRangeLine {
start_x: f32, pub start_x: f32,
end_x: f32, pub end_x: f32,
} }
impl HighlightedRange { impl HighlightedRange {
fn paint(&self, bounds: RectF, scene: &mut Scene) { pub fn paint(&self, bounds: RectF, scene: &mut Scene) {
if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x { if self.lines.len() >= 2 && self.lines[0].start_x > self.lines[1].end_x {
self.paint_lines(self.start_y, &self.lines[0..1], bounds, scene); self.paint_lines(self.start_y, &self.lines[0..1], bounds, scene);
self.paint_lines( self.paint_lines(

View file

@ -1,12 +1,15 @@
use alacritty_terminal::{ use alacritty_terminal::{
grid::{Dimensions, GridIterator, Indexed}, grid::{Dimensions, GridIterator, Indexed},
index::{Column as GridCol, Line as GridLine, Point, Side}, index::{Column as GridCol, Line as GridLine, Point, Side},
selection::{Selection, SelectionRange, SelectionType},
sync::FairMutex,
term::{ term::{
cell::{Cell, Flags}, cell::{Cell, Flags},
SizeInfo, SizeInfo,
}, },
Term,
}; };
use editor::{Cursor, CursorShape}; use editor::{Cursor, CursorShape, HighlightedRange, HighlightedRangeLine};
use gpui::{ use gpui::{
color::Color, color::Color,
elements::*, elements::*,
@ -23,11 +26,13 @@ use gpui::{
use itertools::Itertools; use itertools::Itertools;
use ordered_float::OrderedFloat; use ordered_float::OrderedFloat;
use settings::Settings; use settings::Settings;
use std::{cmp::min, rc::Rc}; use std::{cmp::min, ops::Range, rc::Rc, sync::Arc};
use std::{fmt::Debug, ops::Sub};
use theme::TerminalStyle; use theme::TerminalStyle;
use crate::{ use crate::{
color_translation::convert_color, gpui_func_tools::paint_layer, Input, ScrollTerminal, Terminal, color_translation::convert_color, gpui_func_tools::paint_layer, Input, ScrollTerminal,
Terminal, ZedListener,
}; };
///Scrolling is unbearably sluggish by default. Alacritty supports a configurable ///Scrolling is unbearably sluggish by default. Alacritty supports a configurable
@ -45,14 +50,27 @@ pub struct TerminalEl {
view: WeakViewHandle<Terminal>, view: WeakViewHandle<Terminal>,
} }
///Helper types so I don't mix these two up ///New type pattern so I don't mix these two up
struct CellWidth(f32); struct CellWidth(f32);
struct LineHeight(f32); struct LineHeight(f32);
struct LayoutLine {
cells: Vec<LayoutCell>,
highlighted_range: Option<Range<usize>>,
}
///New type pattern to ensure that we use adjusted mouse positions throughout the code base, rather than
struct PaneRelativePos(Vector2F);
///Functionally the constructor for the PaneRelativePos type, mutates the mouse_position
fn relative_pos(mouse_position: Vector2F, origin: Vector2F) -> PaneRelativePos {
PaneRelativePos(mouse_position.sub(origin)) //Avoid the extra allocation by mutating
}
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
struct LayoutCell { struct LayoutCell {
point: Point<i32, i32>, point: Point<i32, i32>,
text: Line, text: Line, //NOTE TO SELF THIS IS BAD PERFORMANCE RN!
background_color: Color, background_color: Color,
} }
@ -68,14 +86,15 @@ impl LayoutCell {
///The information generated during layout that is nescessary for painting ///The information generated during layout that is nescessary for painting
pub struct LayoutState { pub struct LayoutState {
cells: Vec<(Point<i32, i32>, Line)>, layout_lines: Vec<LayoutLine>,
background_rects: Vec<(RectF, Color)>, //Vec index == Line index for the LineSpan
line_height: LineHeight, line_height: LineHeight,
em_width: CellWidth, em_width: CellWidth,
cursor: Option<Cursor>, cursor: Option<Cursor>,
background_color: Color, background_color: Color,
cur_size: SizeInfo, cur_size: SizeInfo,
display_offset: usize, display_offset: usize,
terminal: Arc<FairMutex<Term<ZedListener>>>,
selection_color: Color,
} }
impl TerminalEl { impl TerminalEl {
@ -111,42 +130,31 @@ impl Element for TerminalEl {
view_handle.update(cx.app, |view, _cx| view.set_size(cur_size)); view_handle.update(cx.app, |view, _cx| view.set_size(cur_size));
//Now that we're done with the mutable portion, grab the immutable settings and view again //Now that we're done with the mutable portion, grab the immutable settings and view again
let terminal_theme = &(cx.global::<Settings>()).theme.terminal; let (selection_color, terminal_theme) = {
let term = view_handle.read(cx).term.lock(); let theme = &(cx.global::<Settings>()).theme;
(theme.editor.selection.selection, &theme.terminal)
};
let terminal_mutex = view_handle.read(cx).term.clone();
let term = terminal_mutex.lock();
let grid = term.grid(); let grid = term.grid();
let cursor_point = grid.cursor.point; let cursor_point = grid.cursor.point;
let cursor_text = grid[cursor_point.line][cursor_point.column].c.to_string(); let cursor_text = grid[cursor_point.line][cursor_point.column].c.to_string();
let content = term.renderable_content(); let content = term.renderable_content();
let layout_cells = layout_cells( //We have a 'SelectionRange' struct to work with,
//Allows us to query start, end, and contains
//content.selection.unwrap()
let layout_lines = layout_lines(
content.display_iter, content.display_iter,
&text_style, &text_style,
terminal_theme, terminal_theme,
cx.text_layout_cache, cx.text_layout_cache,
content.selection,
); );
let cells = layout_cells
.iter()
.map(|c| (c.point, c.text.clone()))
.collect::<Vec<(Point<i32, i32>, Line)>>();
let background_rects = layout_cells
.iter()
.map(|cell| {
(
RectF::new(
vec2f(
cell.point.column as f32 * cell_width.0,
cell.point.line as f32 * line_height.0,
),
vec2f(cell_width.0, line_height.0),
),
cell.background_color,
)
})
.collect::<Vec<(RectF, Color)>>();
let block_text = cx.text_layout_cache.layout_str( let block_text = cx.text_layout_cache.layout_str(
&cursor_text, &cursor_text,
text_style.font_size, text_style.font_size,
@ -185,18 +193,21 @@ impl Element for TerminalEl {
Some(block_text.clone()), Some(block_text.clone()),
) )
}); });
let display_offset = content.display_offset;
drop(term);
( (
constraint.max, constraint.max,
LayoutState { LayoutState {
cells, layout_lines,
line_height, line_height,
em_width: cell_width, em_width: cell_width,
cursor, cursor,
cur_size, cur_size,
background_rects,
background_color: terminal_theme.background, background_color: terminal_theme.background,
display_offset: content.display_offset, display_offset,
terminal: terminal_mutex,
selection_color,
}, },
) )
} }
@ -210,6 +221,7 @@ impl Element for TerminalEl {
) -> Self::PaintState { ) -> Self::PaintState {
//Setup element stuff //Setup element stuff
let clip_bounds = Some(visible_bounds); let clip_bounds = Some(visible_bounds);
paint_layer(cx, clip_bounds, |cx| { paint_layer(cx, clip_bounds, |cx| {
//Elements are ephemeral, only at paint time do we know what could be clicked by a mouse //Elements are ephemeral, only at paint time do we know what could be clicked by a mouse
@ -230,37 +242,45 @@ impl Element for TerminalEl {
*/ */
let cur_size = layout.cur_size.clone(); let cur_size = layout.cur_size.clone();
let display_offset = layout.display_offset.clone(); let display_offset = layout.display_offset.clone();
let terminal_mutex = layout.terminal.clone();
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
//TODO: Better way of doing this?
let mutex1 = terminal_mutex.clone();
let _mutex2 = terminal_mutex.clone();
cx.scene.push_mouse_region(MouseRegion { cx.scene.push_mouse_region(MouseRegion {
view_id: self.view.id(), view_id: self.view.id(),
mouse_down: Some(Rc::new(move |pos, cx| { click: Some(Rc::new(move |pos, click_count, cx| {
let point = grid_cell(pos, cur_size, display_offset); let (point, side) = mouse_to_cell_data(pos, origin, cur_size, display_offset);
let side = cell_side(cur_size, pos.x() as usize);
//One problem is we need a terminal let selection_type = match click_count {
//Second problem is that we need # of clicks 1 => Some(SelectionType::Simple),
//Third problem is that dragging reports deltas, and we need locations. 2 => Some(SelectionType::Semantic),
//Fourth (minor) is need to render the selection 3 => Some(SelectionType::Lines),
_ => None,
};
// if single_click { let selection = selection_type
// terminal.selection = Some(Selection::new(SelectionType::Simple, point, side)) .map(|selection_type| Selection::new(selection_type, point, side));
// } else if double_click {
// terminal.selection = Some(Selection::new(SelectionType::Semantic, point, side))
// } else if triple_click {
// terminal.selection = Some(Selection::new(SelectionType::Lines, point, side))
// }
let mut term = mutex1.lock();
term.selection = selection;
cx.focus_parent_view() cx.focus_parent_view()
})), })),
bounds: visible_bounds, bounds: visible_bounds,
drag: Some(Rc::new(|delta, cx| { drag: Some(Rc::new(move |_delta, _cx| {
//Calculate new point from delta // let (point, side) = mouse_to_cell_data(pos, origin, cur_size, display_offset);
//terminal.selection.update(point, side)
// let mut term = mutex2.lock();
// if let Some(mut selection) = term.selection.take() {
// selection.update(point, side);
// term.selection = Some(selection);
// }
})), })),
..Default::default() ..Default::default()
}); });
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
paint_layer(cx, clip_bounds, |cx| { paint_layer(cx, clip_bounds, |cx| {
//Start with a background color //Start with a background color
cx.scene.push_quad(Quad { cx.scene.push_quad(Quad {
@ -271,25 +291,84 @@ impl Element for TerminalEl {
}); });
//Draw cell backgrounds //Draw cell backgrounds
for background_rect in &layout.background_rects { for layout_line in &layout.layout_lines {
let new_origin = origin + background_rect.0.origin(); for layout_cell in &layout_line.cells {
cx.scene.push_quad(Quad { let position = vec2f(
bounds: RectF::new(new_origin, background_rect.0.size()), origin.x() + layout_cell.point.column as f32 * layout.em_width.0,
background: Some(background_rect.1), origin.y() + layout_cell.point.line as f32 * layout.line_height.0,
border: Default::default(), );
corner_radius: 0., let size = vec2f(layout.em_width.0, layout.line_height.0);
cx.scene.push_quad(Quad {
bounds: RectF::new(position, size),
background: Some(layout_cell.background_color),
border: Default::default(),
corner_radius: 0.,
})
}
}
});
//Draw Selection
paint_layer(cx, clip_bounds, |cx| {
let mut highlight_y = None;
let highlight_lines = layout
.layout_lines
.iter()
.filter_map(|line| {
if let Some(range) = &line.highlighted_range {
if let None = highlight_y {
highlight_y = Some(
origin.y()
+ line.cells[0].point.line as f32 * layout.line_height.0,
);
}
let start_x = origin.x()
+ line.cells[range.start].point.column as f32 * layout.em_width.0;
let end_x = origin.x()
//TODO: Why -1? I know switch from count to index... but where...
+ line.cells[range.end - 1].point.column as f32 * layout.em_width.0
+ layout.em_width.0;
return Some(HighlightedRangeLine { start_x, end_x });
} else {
return None;
}
}) })
.collect::<Vec<HighlightedRangeLine>>();
if let Some(y) = highlight_y {
let hr = HighlightedRange {
start_y: y, //Need to change this
line_height: layout.line_height.0,
lines: highlight_lines,
color: layout.selection_color,
//Copied from editor. TODO: move to theme or something
corner_radius: 0.15 * layout.line_height.0,
};
hr.paint(bounds, cx.scene);
} }
}); });
//Draw text //Draw text
paint_layer(cx, clip_bounds, |cx| { paint_layer(cx, clip_bounds, |cx| {
for (point, cell) in &layout.cells { for layout_line in &layout.layout_lines {
let cell_origin = vec2f( for layout_cell in &layout_line.cells {
origin.x() + point.column as f32 * layout.em_width.0, let point = layout_cell.point;
origin.y() + point.line as f32 * layout.line_height.0,
); //Don't actually know the start_x for a line, until here:
cell.paint(cell_origin, visible_bounds, layout.line_height.0, cx); let cell_origin = vec2f(
origin.x() + point.column as f32 * layout.em_width.0,
origin.y() + point.line as f32 * layout.line_height.0,
);
layout_cell.text.paint(
cell_origin,
visible_bounds,
layout.line_height.0,
cx,
);
}
} }
}); });
@ -354,18 +433,17 @@ impl Element for TerminalEl {
} }
} }
/* fn mouse_to_cell_data(
Mouse moved -> WindowEvent::CursorMoved pos: Vector2F,
mouse press -> WindowEvent::MouseInput origin: Vector2F,
update_selection_scrolling cur_size: SizeInfo,
display_offset: usize,
) -> (Point, alacritty_terminal::index::Direction) {
copy_selection let relative_pos = relative_pos(pos, origin);
start_selection let point = grid_cell(&relative_pos, cur_size, display_offset);
toggle_selection let side = cell_side(&relative_pos, cur_size);
update_selection (point, side)
clear_selection }
*/
///Configures a text style from the current settings. ///Configures a text style from the current settings.
fn make_text_style(font_cache: &FontCache, settings: &Settings) -> TextStyle { fn make_text_style(font_cache: &FontCache, settings: &Settings) -> TextStyle {
@ -399,38 +477,59 @@ fn make_new_size(
) )
} }
fn layout_cells( //Let's say that calculating the display is correct, that means that either calculating the highlight ranges is incorrect
//OR calculating the click ranges is incorrect
fn layout_lines(
grid: GridIterator<Cell>, grid: GridIterator<Cell>,
text_style: &TextStyle, text_style: &TextStyle,
terminal_theme: &TerminalStyle, terminal_theme: &TerminalStyle,
text_layout_cache: &TextLayoutCache, text_layout_cache: &TextLayoutCache,
) -> Vec<LayoutCell> { selection_range: Option<SelectionRange>,
let mut line_count: i32 = 0; ) -> Vec<LayoutLine> {
let lines = grid.group_by(|i| i.point.line); let lines = grid.group_by(|i| i.point.line);
lines lines
.into_iter() .into_iter()
.map(|(_, line)| { .enumerate()
line_count += 1; .map(|(line_index, (_, line))| {
line.map(|indexed_cell| { let mut highlighted_range = None;
let cell_text = &indexed_cell.c.to_string(); let cells = line
.enumerate()
.map(|(x_index, indexed_cell)| {
if selection_range
.map(|range| range.contains(indexed_cell.point))
.unwrap_or(false)
{
let mut range = highlighted_range.take().unwrap_or(x_index..x_index + 1);
range.end = range.end.max(x_index + 1);
highlighted_range = Some(range);
}
let cell_style = cell_style(&indexed_cell, terminal_theme, text_style); let cell_text = &indexed_cell.c.to_string();
let layout_cell = text_layout_cache.layout_str( let cell_style = cell_style(&indexed_cell, terminal_theme, text_style);
cell_text,
text_style.font_size, //This is where we might be able to get better performance
&[(cell_text.len(), cell_style)], let layout_cell = text_layout_cache.layout_str(
); cell_text,
LayoutCell::new( text_style.font_size,
Point::new(line_count - 1, indexed_cell.point.column.0 as i32), &[(cell_text.len(), cell_style)],
layout_cell, );
convert_color(&indexed_cell.bg, terminal_theme),
) LayoutCell::new(
}) Point::new(line_index as i32, indexed_cell.point.column.0 as i32),
.collect::<Vec<LayoutCell>>() layout_cell,
convert_color(&indexed_cell.bg, terminal_theme),
)
})
.collect::<Vec<LayoutCell>>();
LayoutLine {
cells,
highlighted_range,
}
}) })
.flatten() .collect::<Vec<LayoutLine>>()
.collect::<Vec<LayoutCell>>()
} }
// Compute the cursor position and expected block width, may return a zero width if x_for_index returns // Compute the cursor position and expected block width, may return a zero width if x_for_index returns
@ -487,7 +586,8 @@ fn cell_style(indexed: &Indexed<&Cell>, style: &TerminalStyle, text_style: &Text
} }
///Copied (with modifications) from alacritty/src/input.rs > Processor::cell_side() ///Copied (with modifications) from alacritty/src/input.rs > Processor::cell_side()
fn cell_side(cur_size: SizeInfo, x: usize) -> Side { fn cell_side(pos: &PaneRelativePos, cur_size: SizeInfo) -> Side {
let x = pos.0.x() as usize;
let cell_x = x.saturating_sub(cur_size.cell_width() as usize) % cur_size.cell_width() as usize; let cell_x = x.saturating_sub(cur_size.cell_width() as usize) % cur_size.cell_width() as usize;
let half_cell_width = (cur_size.cell_width() / 2.0) as usize; let half_cell_width = (cur_size.cell_width() / 2.0) as usize;
@ -506,11 +606,14 @@ fn cell_side(cur_size: SizeInfo, x: usize) -> Side {
} }
///Copied (with modifications) from alacritty/src/event.rs > Mouse::point() ///Copied (with modifications) from alacritty/src/event.rs > Mouse::point()
fn grid_cell(pos: Vector2F, cur_size: SizeInfo, display_offset: usize) -> Point { ///Position is a pane-relative position. That means the top left corner of the mouse
let col = pos.x() - cur_size.cell_width() / cur_size.cell_width(); //TODO: underflow... ///Region should be (0,0)
fn grid_cell(pos: &PaneRelativePos, cur_size: SizeInfo, display_offset: usize) -> Point {
let pos = pos.0;
let col = pos.x() / cur_size.cell_width(); //TODO: underflow...
let col = min(GridCol(col as usize), cur_size.last_column()); let col = min(GridCol(col as usize), cur_size.last_column());
let line = pos.y() - cur_size.padding_y() / cur_size.cell_height(); let line = pos.y() / cur_size.cell_height();
let line = min(line as usize, cur_size.bottommost_line().0 as usize); let line = min(line as usize, cur_size.bottommost_line().0 as usize);
Point::new(GridLine((line - display_offset) as i32), col) Point::new(GridLine((line - display_offset) as i32), col)