mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-05 10:20:51 +00:00
changed renderer
This commit is contained in:
parent
342d38a9fb
commit
03cbb94057
2 changed files with 125 additions and 195 deletions
|
@ -1616,7 +1616,7 @@ impl PaintState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
pub enum CursorShape {
|
pub enum CursorShape {
|
||||||
Bar,
|
Bar,
|
||||||
Block,
|
Block,
|
||||||
|
@ -1629,6 +1629,7 @@ impl Default for CursorShape {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Cursor {
|
pub struct Cursor {
|
||||||
origin: Vector2F,
|
origin: Vector2F,
|
||||||
block_width: f32,
|
block_width: f32,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use alacritty_terminal::{
|
use alacritty_terminal::{
|
||||||
ansi::Color as AnsiColor,
|
ansi::Color as AnsiColor,
|
||||||
grid::{GridIterator, Indexed},
|
grid::{Dimensions, GridIterator, Indexed},
|
||||||
index::Point,
|
index::Point,
|
||||||
term::{
|
term::{
|
||||||
cell::{Cell, Flags},
|
cell::{Cell, Flags},
|
||||||
|
@ -11,20 +11,20 @@ use editor::{Cursor, CursorShape};
|
||||||
use gpui::{
|
use gpui::{
|
||||||
color::Color,
|
color::Color,
|
||||||
elements::*,
|
elements::*,
|
||||||
fonts::{HighlightStyle, TextStyle, Underline},
|
fonts::{TextStyle, Underline},
|
||||||
geometry::{
|
geometry::{
|
||||||
rect::RectF,
|
rect::RectF,
|
||||||
vector::{vec2f, Vector2F},
|
vector::{vec2f, Vector2F},
|
||||||
},
|
},
|
||||||
json::json,
|
json::json,
|
||||||
scene::Glyph,
|
|
||||||
text_layout::{Line, RunStyle},
|
text_layout::{Line, RunStyle},
|
||||||
Event, FontCache, MouseRegion, PaintContext, Quad, SizeConstraint, WeakViewHandle,
|
Event, FontCache, MouseRegion, PaintContext, Quad, SizeConstraint, TextLayoutCache,
|
||||||
|
WeakViewHandle,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use settings::Settings;
|
use settings::Settings;
|
||||||
use std::{iter, rc::Rc};
|
use std::rc::Rc;
|
||||||
use theme::TerminalStyle;
|
use theme::TerminalStyle;
|
||||||
|
|
||||||
use crate::{gpui_func_tools::paint_layer, Input, ScrollTerminal, Terminal};
|
use crate::{gpui_func_tools::paint_layer, Input, ScrollTerminal, Terminal};
|
||||||
|
@ -44,46 +44,36 @@ pub struct TerminalEl {
|
||||||
view: WeakViewHandle<Terminal>,
|
view: WeakViewHandle<Terminal>,
|
||||||
}
|
}
|
||||||
|
|
||||||
///Represents a span of cells in a single line in the terminal's grid.
|
|
||||||
///This is used for drawing background rectangles
|
|
||||||
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
|
|
||||||
pub struct RectSpan {
|
|
||||||
start: i32,
|
|
||||||
end: i32,
|
|
||||||
line: usize,
|
|
||||||
color: Color,
|
|
||||||
}
|
|
||||||
|
|
||||||
///A background color span
|
|
||||||
impl RectSpan {
|
|
||||||
///Creates a new LineSpan. `start` must be <= `end`.
|
|
||||||
///If `start` == `end`, then this span is considered to be over a
|
|
||||||
/// single cell
|
|
||||||
fn new(start: i32, end: i32, line: usize, color: Color) -> RectSpan {
|
|
||||||
debug_assert!(start <= end);
|
|
||||||
RectSpan {
|
|
||||||
start,
|
|
||||||
end,
|
|
||||||
line,
|
|
||||||
color,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///Helper types so I don't mix these two up
|
///Helper types so I don't mix these two up
|
||||||
struct CellWidth(f32);
|
struct CellWidth(f32);
|
||||||
struct LineHeight(f32);
|
struct LineHeight(f32);
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
struct LayoutCell {
|
||||||
|
point: Point<i32, i32>,
|
||||||
|
text: Line,
|
||||||
|
background_color: Color,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LayoutCell {
|
||||||
|
fn new(point: Point<i32, i32>, text: Line, background_color: Color) -> LayoutCell {
|
||||||
|
LayoutCell {
|
||||||
|
point,
|
||||||
|
text,
|
||||||
|
background_color,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///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 {
|
||||||
lines: Vec<Line>,
|
cells: Vec<(Point<i32, i32>, Line)>,
|
||||||
|
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>,
|
||||||
cur_size: SizeInfo,
|
|
||||||
background_color: Color,
|
background_color: Color,
|
||||||
background_rects: Vec<(RectF, Color)>, //Vec index == Line index for the LineSpan
|
cur_size: SizeInfo,
|
||||||
ccc: Glyph,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TerminalEl {
|
impl TerminalEl {
|
||||||
|
@ -128,30 +118,32 @@ impl Element for TerminalEl {
|
||||||
|
|
||||||
let content = term.renderable_content();
|
let content = term.renderable_content();
|
||||||
|
|
||||||
//And we're off! Begin layouting
|
let layout_cells = layout_cells(
|
||||||
let BuiltChunks {
|
content.display_iter,
|
||||||
chunks,
|
|
||||||
line_count,
|
|
||||||
cursor_index,
|
|
||||||
} = build_chunks(content.display_iter, &terminal_theme, cursor_point);
|
|
||||||
|
|
||||||
let shaped_lines = layout_highlighted_chunks(
|
|
||||||
chunks
|
|
||||||
.iter()
|
|
||||||
.map(|(text, style, _)| (text.as_str(), *style)),
|
|
||||||
&text_style,
|
&text_style,
|
||||||
|
terminal_theme,
|
||||||
cx.text_layout_cache,
|
cx.text_layout_cache,
|
||||||
cx.font_cache(),
|
|
||||||
usize::MAX,
|
|
||||||
line_count,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let backgrounds = chunks
|
let cells = layout_cells
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(_, _, line_span)| line_span != &RectSpan::default())
|
.map(|c| (c.point, c.text.clone()))
|
||||||
.map(|(_, _, line_span)| *line_span)
|
.collect::<Vec<(Point<i32, i32>, Line)>>();
|
||||||
.collect();
|
let background_rects = layout_cells
|
||||||
let background_rects = make_background_rects(backgrounds, &shaped_lines, &line_height);
|
.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,
|
||||||
|
@ -166,12 +158,14 @@ impl Element for TerminalEl {
|
||||||
)],
|
)],
|
||||||
);
|
);
|
||||||
|
|
||||||
let cursor = get_cursor_position(
|
let cursor = get_cursor_shape(
|
||||||
content.cursor.point.line.0 as usize,
|
content.cursor.point.line.0 as usize,
|
||||||
cursor_index,
|
content.cursor.point.column.0 as usize,
|
||||||
&shaped_lines,
|
|
||||||
content.display_offset,
|
content.display_offset,
|
||||||
&line_height,
|
&line_height,
|
||||||
|
&cell_width,
|
||||||
|
cur_size.total_lines(),
|
||||||
|
&block_text,
|
||||||
)
|
)
|
||||||
.map(move |(cursor_position, block_width)| {
|
.map(move |(cursor_position, block_width)| {
|
||||||
let block_width = if block_width != 0.0 {
|
let block_width = if block_width != 0.0 {
|
||||||
|
@ -190,25 +184,16 @@ impl Element for TerminalEl {
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
|
||||||
let g = Glyph {
|
|
||||||
font_id: text_style.font_id,
|
|
||||||
font_size: text_style.font_size,
|
|
||||||
id: 50,
|
|
||||||
origin: vec2f(0., 0.),
|
|
||||||
color: Color::red(),
|
|
||||||
};
|
|
||||||
|
|
||||||
(
|
(
|
||||||
constraint.max,
|
constraint.max,
|
||||||
LayoutState {
|
LayoutState {
|
||||||
lines: shaped_lines,
|
cells,
|
||||||
line_height,
|
line_height,
|
||||||
em_width: cell_width,
|
em_width: cell_width,
|
||||||
cursor,
|
cursor,
|
||||||
cur_size,
|
cur_size,
|
||||||
background_rects,
|
background_rects,
|
||||||
background_color: terminal_theme.background,
|
background_color: terminal_theme.background,
|
||||||
ccc: g,
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -234,13 +219,7 @@ impl Element for TerminalEl {
|
||||||
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
|
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.);
|
||||||
|
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
paint_layer(cx, clip_bounds, |cx| {
|
||||||
layout.ccc.origin.set_x(visible_bounds.origin_x() + 20.);
|
//Start with a background color
|
||||||
layout.ccc.origin.set_y(visible_bounds.origin_y() + 20.);
|
|
||||||
cx.scene.push_glyph(layout.ccc)
|
|
||||||
});
|
|
||||||
|
|
||||||
//Start us off with a nice simple background color
|
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
|
||||||
cx.scene.push_quad(Quad {
|
cx.scene.push_quad(Quad {
|
||||||
bounds: RectF::new(bounds.origin(), bounds.size()),
|
bounds: RectF::new(bounds.origin(), bounds.size()),
|
||||||
background: Some(layout.background_color),
|
background: Some(layout.background_color),
|
||||||
|
@ -260,15 +239,14 @@ impl Element for TerminalEl {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Draw text
|
||||||
paint_layer(cx, clip_bounds, |cx| {
|
paint_layer(cx, clip_bounds, |cx| {
|
||||||
let mut line_origin = origin.clone();
|
for (point, cell) in &layout.cells {
|
||||||
for line in &layout.lines {
|
let cell_origin = vec2f(
|
||||||
let boundaries =
|
origin.x() + point.column as f32 * layout.em_width.0,
|
||||||
RectF::new(line_origin, vec2f(bounds.width(), layout.line_height.0));
|
origin.y() + point.line as f32 * layout.line_height.0,
|
||||||
if boundaries.intersects(visible_bounds) {
|
);
|
||||||
line.paint(line_origin, visible_bounds, layout.line_height.0, cx);
|
cell.paint(cell_origin, visible_bounds, layout.line_height.0, cx);
|
||||||
}
|
|
||||||
line_origin.set_y(boundaries.max_y());
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -365,140 +343,91 @@ fn make_new_size(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BuiltChunks {
|
fn layout_cells(
|
||||||
pub chunks: Vec<(String, Option<HighlightStyle>, RectSpan)>,
|
grid: GridIterator<Cell>,
|
||||||
pub line_count: usize,
|
text_style: &TextStyle,
|
||||||
pub cursor_index: usize,
|
terminal_theme: &TerminalStyle,
|
||||||
}
|
text_layout_cache: &TextLayoutCache,
|
||||||
|
) -> Vec<LayoutCell> {
|
||||||
///In a single pass, this function generates the background and foreground color info for every item in the grid.
|
let mut line_count: i32 = 0;
|
||||||
pub(crate) fn build_chunks(
|
let lines = grid.group_by(|i| i.point.line);
|
||||||
grid_iterator: GridIterator<Cell>,
|
lines
|
||||||
theme: &TerminalStyle,
|
|
||||||
cursor_point: Point,
|
|
||||||
) -> BuiltChunks {
|
|
||||||
let mut line_count: usize = 0;
|
|
||||||
let mut cursor_index: usize = 0;
|
|
||||||
//Every `group_by()` -> `into_iter()` pair needs to be seperated by a local variable so
|
|
||||||
//rust knows where to put everything.
|
|
||||||
//Start by grouping by lines
|
|
||||||
let lines = grid_iterator.group_by(|i| i.point.line.0);
|
|
||||||
let result = lines
|
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(_line_grid_index, line)| {
|
.map(|(_, line)| {
|
||||||
line_count += 1;
|
line_count += 1;
|
||||||
let mut col_index = 0;
|
line.map(|indexed_cell| {
|
||||||
//Setup a variable
|
let cell_text = &indexed_cell.c.to_string();
|
||||||
|
|
||||||
//Then group by style
|
let cell_style = cell_style(&indexed_cell, terminal_theme, text_style);
|
||||||
let chunks = line.group_by(|i| cell_style(&i, theme));
|
|
||||||
chunks
|
|
||||||
.into_iter()
|
|
||||||
.map(|(style, fragment)| {
|
|
||||||
//And assemble the styled fragment into it's background and foreground information
|
|
||||||
let mut str_fragment = String::new();
|
|
||||||
for indexed_cell in fragment {
|
|
||||||
if cursor_point.line.0 == indexed_cell.point.line.0
|
|
||||||
&& indexed_cell.point.column < cursor_point.column.0
|
|
||||||
{
|
|
||||||
cursor_index += indexed_cell.c.to_string().len();
|
|
||||||
}
|
|
||||||
str_fragment.push(indexed_cell.c);
|
|
||||||
}
|
|
||||||
|
|
||||||
let start = col_index;
|
let layout_cell = text_layout_cache.layout_str(
|
||||||
let end = start + str_fragment.len() as i32;
|
cell_text,
|
||||||
|
text_style.font_size,
|
||||||
//munge it here
|
&[(cell_text.len(), cell_style)],
|
||||||
col_index = end;
|
);
|
||||||
(
|
LayoutCell::new(
|
||||||
str_fragment,
|
Point::new(line_count - 1, indexed_cell.point.column.0 as i32),
|
||||||
Some(style.0),
|
layout_cell,
|
||||||
RectSpan::new(start, end, line_count - 1, style.1), //Line count -> Line index
|
convert_color(&indexed_cell.bg, terminal_theme),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
//Add a \n to the end, as we're using text layouting rather than grid layouts
|
.collect::<Vec<LayoutCell>>()
|
||||||
.chain(iter::once(("\n".to_string(), None, Default::default())))
|
|
||||||
.collect::<Vec<(String, Option<HighlightStyle>, RectSpan)>>()
|
|
||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
//We have a Vec<Vec<>> (Vec of lines of styled chunks), flatten to just Vec<> (the styled chunks)
|
.collect::<Vec<LayoutCell>>()
|
||||||
.collect::<Vec<(String, Option<HighlightStyle>, RectSpan)>>();
|
|
||||||
|
|
||||||
BuiltChunks {
|
|
||||||
chunks: result,
|
|
||||||
line_count,
|
|
||||||
cursor_index,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
///Convert a RectSpan in terms of character offsets, into RectFs of exact offsets
|
|
||||||
fn make_background_rects(
|
|
||||||
backgrounds: Vec<RectSpan>,
|
|
||||||
shaped_lines: &Vec<Line>,
|
|
||||||
line_height: &LineHeight,
|
|
||||||
) -> Vec<(RectF, Color)> {
|
|
||||||
backgrounds
|
|
||||||
.into_iter()
|
|
||||||
.map(|line_span| {
|
|
||||||
//This should always be safe, as the shaped lines and backgrounds where derived
|
|
||||||
//At the same time earlier
|
|
||||||
let line = shaped_lines
|
|
||||||
.get(line_span.line)
|
|
||||||
.expect("Background line_num did not correspond to a line number");
|
|
||||||
let x = line.x_for_index(line_span.start as usize);
|
|
||||||
let width = line.x_for_index(line_span.end as usize) - x;
|
|
||||||
(
|
|
||||||
RectF::new(
|
|
||||||
vec2f(x, line_span.line as f32 * line_height.0),
|
|
||||||
vec2f(width, line_height.0),
|
|
||||||
),
|
|
||||||
line_span.color,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<(RectF, Color)>>()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// the same position for sequential indexes. Use em_width instead
|
// the same position for sequential indexes. Use em_width instead
|
||||||
fn get_cursor_position(
|
//TODO: This function is messy, too many arguments and too many ifs. Simplify.
|
||||||
|
fn get_cursor_shape(
|
||||||
line: usize,
|
line: usize,
|
||||||
line_index: usize,
|
line_index: usize,
|
||||||
shaped_lines: &Vec<Line>,
|
|
||||||
display_offset: usize,
|
display_offset: usize,
|
||||||
line_height: &LineHeight,
|
line_height: &LineHeight,
|
||||||
|
cell_width: &CellWidth,
|
||||||
|
total_lines: usize,
|
||||||
|
text_fragment: &Line,
|
||||||
) -> Option<(Vector2F, f32)> {
|
) -> Option<(Vector2F, f32)> {
|
||||||
let cursor_line = line + display_offset;
|
let cursor_line = line + display_offset;
|
||||||
shaped_lines.get(cursor_line).map(|layout_line| {
|
if cursor_line <= total_lines {
|
||||||
let cursor_x = layout_line.x_for_index(line_index);
|
let cursor_width = if text_fragment.width() == 0. {
|
||||||
let next_char_x = layout_line.x_for_index(line_index + 1);
|
cell_width.0
|
||||||
(
|
} else {
|
||||||
vec2f(cursor_x, cursor_line as f32 * line_height.0),
|
text_fragment.width()
|
||||||
next_char_x - cursor_x,
|
};
|
||||||
)
|
|
||||||
})
|
Some((
|
||||||
|
vec2f(
|
||||||
|
line_index as f32 * cell_width.0,
|
||||||
|
cursor_line as f32 * line_height.0,
|
||||||
|
),
|
||||||
|
cursor_width,
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///Convert the Alacritty cell styles to GPUI text styles and background color
|
///Convert the Alacritty cell styles to GPUI text styles and background color
|
||||||
fn cell_style(indexed: &Indexed<&Cell>, style: &TerminalStyle) -> (HighlightStyle, Color) {
|
fn cell_style(indexed: &Indexed<&Cell>, style: &TerminalStyle, text_style: &TextStyle) -> RunStyle {
|
||||||
let flags = indexed.cell.flags;
|
let flags = indexed.cell.flags;
|
||||||
let fg = Some(convert_color(&indexed.cell.fg, style));
|
let fg = convert_color(&indexed.cell.fg, style);
|
||||||
let bg = convert_color(&indexed.cell.bg, style);
|
|
||||||
|
|
||||||
let underline = flags.contains(Flags::UNDERLINE).then(|| Underline {
|
let underline = flags
|
||||||
|
.contains(Flags::UNDERLINE)
|
||||||
|
.then(|| Underline {
|
||||||
|
color: Some(fg),
|
||||||
|
squiggly: false,
|
||||||
|
thickness: OrderedFloat(1.),
|
||||||
|
})
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
|
RunStyle {
|
||||||
color: fg,
|
color: fg,
|
||||||
squiggly: false,
|
font_id: text_style.font_id,
|
||||||
thickness: OrderedFloat(1.),
|
underline,
|
||||||
});
|
}
|
||||||
|
|
||||||
(
|
|
||||||
HighlightStyle {
|
|
||||||
color: fg,
|
|
||||||
underline,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
bg,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///Converts a 2, 8, or 24 bit color ANSI color to the GPUI equivalent
|
///Converts a 2, 8, or 24 bit color ANSI color to the GPUI equivalent
|
||||||
|
|
Loading…
Reference in a new issue