Updated alacritty version

This commit is contained in:
Mikayla Maki 2022-07-19 16:45:48 -07:00
parent ef1a32ee92
commit 18079ced20
7 changed files with 204 additions and 273 deletions

10
Cargo.lock generated
View file

@ -62,8 +62,7 @@ dependencies = [
[[package]] [[package]]
name = "alacritty_config_derive" name = "alacritty_config_derive"
version = "0.1.0" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/zed-industries/alacritty?rev=90647e167c036530176dc5b579222c5886286f63#90647e167c036530176dc5b579222c5886286f63"
checksum = "77044c45bdb871e501b5789ad16293ecb619e5733b60f4bb01d1cb31c463c336"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -72,14 +71,13 @@ dependencies = [
[[package]] [[package]]
name = "alacritty_terminal" name = "alacritty_terminal"
version = "0.16.1" version = "0.17.0-dev"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "git+https://github.com/zed-industries/alacritty?rev=90647e167c036530176dc5b579222c5886286f63#90647e167c036530176dc5b579222c5886286f63"
checksum = "02fb5d4af84e39f9754d039ff6de2233c8996dbae0af74910156e559e5766e2f"
dependencies = [ dependencies = [
"alacritty_config_derive", "alacritty_config_derive",
"base64 0.13.0", "base64 0.13.0",
"bitflags", "bitflags",
"dirs 3.0.2", "dirs 4.0.0",
"libc", "libc",
"log", "log",
"mio 0.6.23", "mio 0.6.23",

View file

@ -8,7 +8,7 @@ path = "src/terminal.rs"
doctest = false doctest = false
[dependencies] [dependencies]
alacritty_terminal = "0.16.1" alacritty_terminal = { git = "https://github.com/zed-industries/alacritty", rev = "90647e167c036530176dc5b579222c5886286f63"}
editor = { path = "../editor" } editor = { path = "../editor" }
util = { path = "../util" } util = { path = "../util" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }

View file

@ -3,13 +3,13 @@ mod keymappings;
use alacritty_terminal::{ use alacritty_terminal::{
ansi::{ClearMode, Handler}, ansi::{ClearMode, Handler},
config::{Config, Program, PtyConfig}, config::{Config, Program, PtyConfig},
event::{Event as AlacTermEvent, EventListener, Notify}, event::{Event as AlacTermEvent, EventListener, Notify, WindowSize},
event_loop::{EventLoop, Msg, Notifier}, event_loop::{EventLoop, Msg, Notifier},
grid::Scroll, grid::Scroll,
index::{Direction, Point}, index::{Direction, Point},
selection::{Selection, SelectionType}, selection::{Selection, SelectionType},
sync::FairMutex, sync::FairMutex,
term::{RenderableContent, SizeInfo, TermMode}, term::{test::TermSize, RenderableContent, TermMode},
tty::{self, setup_env}, tty::{self, setup_env},
Term, Term,
}; };
@ -22,7 +22,10 @@ use std::{collections::HashMap, path::PathBuf, sync::Arc};
use gpui::{keymap::Keystroke, ClipboardItem, CursorStyle, Entity, ModelContext}; use gpui::{keymap::Keystroke, ClipboardItem, CursorStyle, Entity, ModelContext};
use crate::color_translation::{get_color_at_index, to_alac_rgb}; use crate::{
color_translation::{get_color_at_index, to_alac_rgb},
terminal_element::TerminalDimensions,
};
use self::keymappings::to_esc_str; use self::keymappings::to_esc_str;
@ -63,7 +66,7 @@ impl TerminalConnection {
working_directory: Option<PathBuf>, working_directory: Option<PathBuf>,
shell: Option<Shell>, shell: Option<Shell>,
env: Option<HashMap<String, String>>, env: Option<HashMap<String, String>>,
initial_size: SizeInfo, initial_size: TerminalDimensions,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> TerminalConnection { ) -> TerminalConnection {
let pty_config = { let pty_config = {
@ -97,11 +100,11 @@ impl TerminalConnection {
let (events_tx, mut events_rx) = unbounded(); let (events_tx, mut events_rx) = unbounded();
//Set up the terminal... //Set up the terminal...
let term = Term::new(&config, initial_size, ZedListener(events_tx.clone())); let term = Term::new(&config, &initial_size, ZedListener(events_tx.clone()));
let term = Arc::new(FairMutex::new(term)); let term = Arc::new(FairMutex::new(term));
//Setup the pty... //Setup the pty...
let pty = match tty::new(&pty_config, &initial_size, None) { let pty = match tty::new(&pty_config, initial_size.into(), None) {
Ok(pty) => pty, Ok(pty) => pty,
Err(error) => { Err(error) => {
return TerminalConnection::Disconnected { return TerminalConnection::Disconnected {
@ -237,6 +240,7 @@ impl Terminal {
cx.emit(Event::Bell); cx.emit(Event::Bell);
} }
AlacTermEvent::Exit => cx.emit(Event::CloseTerminal), AlacTermEvent::Exit => cx.emit(Event::CloseTerminal),
AlacTermEvent::TextAreaSizeRequest(_) => println!("Received text area resize request"),
} }
} }
@ -252,9 +256,11 @@ impl Terminal {
} }
///Resize the terminal and the PTY. This locks the terminal. ///Resize the terminal and the PTY. This locks the terminal.
pub fn set_size(&self, new_size: SizeInfo) { pub fn set_size(&self, new_size: WindowSize) {
self.pty_tx.0.send(Msg::Resize(new_size)).ok(); self.pty_tx.0.send(Msg::Resize(new_size)).ok();
self.term.lock().resize(new_size);
let term_size = TermSize::new(new_size.num_cols as usize, new_size.num_lines as usize);
self.term.lock().resize(term_size);
} }
pub fn clear(&self) { pub fn clear(&self) {
@ -300,13 +306,13 @@ impl Terminal {
self.term.lock().selection = sel; self.term.lock().selection = sel;
} }
pub fn render_lock<F, T>(&self, new_size: Option<SizeInfo>, f: F) -> T pub fn render_lock<F, T>(&self, new_size: Option<TerminalDimensions>, f: F) -> T
where where
F: FnOnce(RenderableContent) -> T, F: FnOnce(RenderableContent) -> T,
{ {
if let Some(new_size) = new_size { if let Some(new_size) = new_size {
self.pty_tx.0.send(Msg::Resize(new_size)).ok(); //Give the PTY a chance to react to the new size self.pty_tx.0.send(Msg::Resize(new_size.into())).ok(); //Give the PTY a chance to react to the new size
//TODO: Is this bad for performance? //TODO: Is this bad for performance?
} }
let mut term = self.term.lock(); //Lock let mut term = self.term.lock(); //Lock

View file

@ -3,12 +3,11 @@ pub mod connection;
mod modal; mod modal;
pub mod terminal_element; pub mod terminal_element;
use alacritty_terminal::term::SizeInfo;
use connection::{Event, TerminalConnection}; use connection::{Event, TerminalConnection};
use dirs::home_dir; use dirs::home_dir;
use gpui::{ use gpui::{
actions, elements::*, keymap::Keystroke, AppContext, ClipboardItem, Entity, ModelHandle, actions, elements::*, geometry::vector::vec2f, keymap::Keystroke, AppContext, ClipboardItem,
MutableAppContext, View, ViewContext, Entity, ModelHandle, MutableAppContext, View, ViewContext,
}; };
use modal::deploy_modal; use modal::deploy_modal;
@ -16,6 +15,8 @@ use project::{LocalWorktree, Project, ProjectPath};
use settings::{Settings, WorkingDirectory}; use settings::{Settings, WorkingDirectory};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use terminal_element::TerminalDimensions;
use workspace::{Item, Workspace}; use workspace::{Item, Workspace};
use crate::terminal_element::TerminalEl; use crate::terminal_element::TerminalEl;
@ -80,14 +81,10 @@ impl TerminalView {
///To get the right working directory from a workspace, use: `get_wd_for_workspace()` ///To get the right working directory from a workspace, use: `get_wd_for_workspace()`
fn new(working_directory: Option<PathBuf>, modal: bool, cx: &mut ViewContext<Self>) -> Self { fn new(working_directory: Option<PathBuf>, modal: bool, cx: &mut ViewContext<Self>) -> Self {
//The details here don't matter, the terminal will be resized on the first layout //The details here don't matter, the terminal will be resized on the first layout
let size_info = SizeInfo::new( let size_info = TerminalDimensions::new(
DEBUG_TERMINAL_WIDTH,
DEBUG_TERMINAL_HEIGHT,
DEBUG_CELL_WIDTH,
DEBUG_LINE_HEIGHT, DEBUG_LINE_HEIGHT,
0., DEBUG_CELL_WIDTH,
0., vec2f(DEBUG_TERMINAL_WIDTH, DEBUG_TERMINAL_HEIGHT),
false,
); );
let (shell, envs) = { let (shell, envs) = {

View file

@ -1,14 +1,12 @@
mod terminal_layout_context; pub mod terminal_layout_context;
use alacritty_terminal::{ use alacritty_terminal::{
ansi::{Color::Named, NamedColor}, ansi::{Color::Named, NamedColor},
event::WindowSize,
grid::{Dimensions, GridIterator, Indexed, Scroll}, grid::{Dimensions, GridIterator, Indexed, Scroll},
index::{Column as GridCol, Line as GridLine, Point, Side}, index::{Column as GridCol, Line as GridLine, Point, Side},
selection::SelectionRange, selection::SelectionRange,
term::{ term::cell::{Cell, Flags},
cell::{Cell, Flags},
SizeInfo,
},
}; };
use editor::{Cursor, CursorShape, HighlightedRange, HighlightedRangeLine}; use editor::{Cursor, CursorShape, HighlightedRange, HighlightedRangeLine};
use gpui::{ use gpui::{
@ -36,7 +34,7 @@ use std::{fmt::Debug, ops::Sub};
use crate::{color_translation::convert_color, connection::TerminalConnection, TerminalView}; use crate::{color_translation::convert_color, connection::TerminalConnection, TerminalView};
use self::terminal_layout_context::TerminalLayoutTheme; use self::terminal_layout_context::TerminalLayoutData;
///Scrolling is unbearably sluggish by default. Alacritty supports a configurable ///Scrolling is unbearably sluggish by default. Alacritty supports a configurable
///Scroll multiplier that is set to 3 by default. This will be removed when I ///Scroll multiplier that is set to 3 by default. This will be removed when I
@ -51,12 +49,74 @@ pub struct TerminalEl {
modal: bool, modal: bool,
} }
///New type pattern so I don't mix these two up #[derive(Clone, Copy, Debug)]
pub struct CellWidth(f32); pub struct TerminalDimensions {
pub struct LineHeight(f32); pub cell_width: f32,
pub line_height: f32,
pub height: f32,
pub width: f32,
}
///New type pattern to ensure that we use adjusted mouse positions throughout the code base, rather than impl TerminalDimensions {
struct PaneRelativePos(Vector2F); pub fn new(line_height: f32, cell_width: f32, size: Vector2F) -> Self {
TerminalDimensions {
cell_width,
line_height,
width: size.x(),
height: size.y(),
}
}
pub fn num_lines(&self) -> usize {
(self.height / self.line_height).floor() as usize
}
pub fn num_columns(&self) -> usize {
(self.width / self.cell_width).floor() as usize
}
pub fn height(&self) -> f32 {
self.height
}
pub fn width(&self) -> f32 {
self.width
}
pub fn cell_width(&self) -> f32 {
self.cell_width
}
pub fn line_height(&self) -> f32 {
self.line_height
}
}
//TODO look at what TermSize is
impl Into<WindowSize> for TerminalDimensions {
fn into(self) -> WindowSize {
WindowSize {
num_lines: self.num_lines() as u16,
num_cols: self.num_columns() as u16,
cell_width: self.cell_width() as u16,
cell_height: self.line_height() as u16,
}
}
}
impl Dimensions for TerminalDimensions {
fn total_lines(&self) -> usize {
self.num_lines() //TODO: Check that this is fine. This is supposed to be for the back buffer...
}
fn screen_lines(&self) -> usize {
self.num_lines()
}
fn columns(&self) -> usize {
self.num_columns()
}
}
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
struct LayoutCell { struct LayoutCell {
@ -78,7 +138,7 @@ impl LayoutCell {
) { ) {
let pos = point_to_absolute(origin, self.point, layout); let pos = point_to_absolute(origin, self.point, layout);
self.text self.text
.paint(pos, visible_bounds, layout.line_height.0, cx); .paint(pos, visible_bounds, layout.size.line_height, cx);
} }
} }
@ -110,8 +170,8 @@ impl LayoutRect {
let position = point_to_absolute(origin, self.point, layout); let position = point_to_absolute(origin, self.point, layout);
let size = vec2f( let size = vec2f(
(layout.em_width.0.ceil() * self.num_of_cells as f32).ceil(), (layout.size.cell_width.ceil() * self.num_of_cells as f32).ceil(),
layout.line_height.0, layout.size.line_height,
); );
cx.scene.push_quad(Quad { cx.scene.push_quad(Quad {
@ -125,8 +185,8 @@ impl LayoutRect {
fn point_to_absolute(origin: Vector2F, point: Point<i32, i32>, layout: &LayoutState) -> Vector2F { fn point_to_absolute(origin: Vector2F, point: Point<i32, i32>, layout: &LayoutState) -> Vector2F {
vec2f( vec2f(
(origin.x() + point.column as f32 * layout.em_width.0).floor(), (origin.x() + point.column as f32 * layout.size.cell_width).floor(),
origin.y() + point.line as f32 * layout.line_height.0, origin.y() + point.line as f32 * layout.size.line_height,
) )
} }
@ -146,29 +206,23 @@ impl RelativeHighlightedRange {
origin: Vector2F, origin: Vector2F,
layout: &LayoutState, layout: &LayoutState,
) -> HighlightedRangeLine { ) -> HighlightedRangeLine {
let start_x = origin.x() + self.range.start as f32 * layout.em_width.0; let start_x = origin.x() + self.range.start as f32 * layout.size.cell_width;
let end_x = origin.x() + self.range.end as f32 * layout.em_width.0 + layout.em_width.0; let end_x =
origin.x() + self.range.end as f32 * layout.size.cell_width + layout.size.cell_width;
return HighlightedRangeLine { start_x, end_x }; return HighlightedRangeLine { start_x, end_x };
} }
} }
///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))
}
///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<LayoutCell>, cells: Vec<LayoutCell>,
rects: Vec<LayoutRect>, rects: Vec<LayoutRect>,
highlights: Vec<RelativeHighlightedRange>, highlights: Vec<RelativeHighlightedRange>,
line_height: LineHeight,
em_width: CellWidth,
cursor: Option<Cursor>, cursor: Option<Cursor>,
background_color: Color, background_color: Color,
selection_color: Color, selection_color: Color,
cur_size: SizeInfo, size: TerminalDimensions,
} }
impl TerminalEl { impl TerminalEl {
@ -189,7 +243,7 @@ impl TerminalEl {
origin: Vector2F, origin: Vector2F,
view_id: usize, view_id: usize,
visible_bounds: RectF, visible_bounds: RectF,
cur_size: SizeInfo, cur_size: TerminalDimensions,
cx: &mut PaintContext, cx: &mut PaintContext,
) { ) {
let mouse_down_connection = self.connection.clone(); let mouse_down_connection = self.connection.clone();
@ -279,11 +333,8 @@ impl Element for TerminalEl {
constraint: gpui::SizeConstraint, constraint: gpui::SizeConstraint,
cx: &mut gpui::LayoutContext, cx: &mut gpui::LayoutContext,
) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) {
let tcx = TerminalLayoutTheme::new(cx.global::<Settings>(), &cx.font_cache()); let layout =
TerminalLayoutData::new(cx.global::<Settings>(), &cx.font_cache(), constraint.max);
//This locks the terminal, so resize it first.
//Layout grid cells
let cur_size = make_new_size(constraint, &tcx.cell_width, &tcx.line_height);
let terminal = self let terminal = self
.connection .connection
@ -293,34 +344,35 @@ impl Element for TerminalEl {
.get_terminal() .get_terminal()
.unwrap(); .unwrap();
let (cursor, cells, rects, highlights) = terminal.render_lock(Some(cur_size), |content| { let (cursor, cells, rects, highlights) =
let (cells, rects, highlights) = layout_grid( terminal.render_lock(Some(layout.size.clone()), |content| {
content.display_iter, let (cells, rects, highlights) = layout_grid(
&tcx.text_style, content.display_iter,
tcx.terminal_theme, &layout.text_style,
cx.text_layout_cache, layout.terminal_theme,
self.modal, cx.text_layout_cache,
content.selection, self.modal,
); content.selection,
);
//Layout cursor //Layout cursor
let cursor = layout_cursor( let cursor = layout_cursor(
// grid, // grid,
cx.text_layout_cache, cx.text_layout_cache,
&tcx, &layout,
content.cursor.point, content.cursor.point,
content.display_offset, content.display_offset,
constraint, constraint,
); );
(cursor, cells, rects, highlights) (cursor, cells, rects, highlights)
}); });
//Select background color //Select background color
let background_color = if self.modal { let background_color = if self.modal {
tcx.terminal_theme.colors.modal_background layout.terminal_theme.colors.modal_background
} else { } else {
tcx.terminal_theme.colors.background layout.terminal_theme.colors.background
}; };
//Done! //Done!
@ -328,12 +380,10 @@ impl Element for TerminalEl {
constraint.max, constraint.max,
LayoutState { LayoutState {
cells, cells,
line_height: tcx.line_height,
em_width: tcx.cell_width,
cursor, cursor,
background_color, background_color,
selection_color: tcx.selection_color, selection_color: layout.selection_color,
cur_size, size: layout.size,
rects, rects,
highlights, highlights,
}, },
@ -358,10 +408,10 @@ impl Element for TerminalEl {
let clip_bounds = Some(visible_bounds); let clip_bounds = Some(visible_bounds);
cx.paint_layer(clip_bounds, |cx| { cx.paint_layer(clip_bounds, |cx| {
let origin = bounds.origin() + vec2f(layout.em_width.0, 0.); let origin = bounds.origin() + vec2f(layout.size.cell_width, 0.);
//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
self.attach_mouse_handlers(origin, self.view.id(), visible_bounds, layout.cur_size, cx); self.attach_mouse_handlers(origin, self.view.id(), visible_bounds, layout.size, cx);
cx.paint_layer(clip_bounds, |cx| { cx.paint_layer(clip_bounds, |cx| {
//Start with a background color //Start with a background color
@ -380,7 +430,7 @@ impl Element for TerminalEl {
//Draw Selection //Draw Selection
cx.paint_layer(clip_bounds, |cx| { cx.paint_layer(clip_bounds, |cx| {
let start_y = layout.highlights.get(0).map(|highlight| { let start_y = layout.highlights.get(0).map(|highlight| {
origin.y() + highlight.line_index as f32 * layout.line_height.0 origin.y() + highlight.line_index as f32 * layout.size.line_height
}); });
if let Some(y) = start_y { if let Some(y) = start_y {
@ -394,11 +444,11 @@ impl Element for TerminalEl {
let hr = HighlightedRange { let hr = HighlightedRange {
start_y: y, //Need to change this start_y: y, //Need to change this
line_height: layout.line_height.0, line_height: layout.size.line_height,
lines: range_lines, lines: range_lines,
color: layout.selection_color, color: layout.selection_color,
//Copied from editor. TODO: move to theme or something //Copied from editor. TODO: move to theme or something
corner_radius: 0.15 * layout.line_height.0, corner_radius: 0.15 * layout.size.line_height,
}; };
hr.paint(bounds, cx.scene); hr.paint(bounds, cx.scene);
} }
@ -436,7 +486,7 @@ impl Element for TerminalEl {
.contains_point(*position) .contains_point(*position)
.then(|| { .then(|| {
let vertical_scroll = let vertical_scroll =
(delta.y() / layout.line_height.0) * ALACRITTY_SCROLL_MULTIPLIER; (delta.y() / layout.size.line_height) * ALACRITTY_SCROLL_MULTIPLIER;
self.connection self.connection
.upgrade(cx.app) .upgrade(cx.app)
@ -487,7 +537,7 @@ impl Element for TerminalEl {
fn layout_cursor( fn layout_cursor(
// grid: &Grid<Cell>, // grid: &Grid<Cell>,
text_layout_cache: &TextLayoutCache, text_layout_cache: &TextLayoutCache,
tcx: &TerminalLayoutTheme, tcx: &TerminalLayoutData,
cursor_point: Point, cursor_point: Point,
display_offset: usize, display_offset: usize,
constraint: SizeConstraint, constraint: SizeConstraint,
@ -497,22 +547,22 @@ fn layout_cursor(
cursor_point.line.0 as usize, cursor_point.line.0 as usize,
cursor_point.column.0 as usize, cursor_point.column.0 as usize,
display_offset, display_offset,
&tcx.line_height, tcx.size.line_height,
&tcx.cell_width, tcx.size.cell_width,
(constraint.max.y() / &tcx.line_height.0) as usize, //TODO (constraint.max.y() / tcx.size.line_height) as usize, //TODO
&cursor_text, &cursor_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 {
block_width block_width
} else { } else {
tcx.cell_width.0 tcx.size.cell_width
}; };
Cursor::new( Cursor::new(
cursor_position, cursor_position,
block_width, block_width,
tcx.line_height.0, tcx.size.line_height,
tcx.terminal_theme.colors.cursor, tcx.terminal_theme.colors.cursor,
CursorShape::Block, CursorShape::Block,
Some(cursor_text.clone()), Some(cursor_text.clone()),
@ -524,7 +574,7 @@ fn layout_cursor_text(
// grid: &Grid<Cell>, // grid: &Grid<Cell>,
_cursor_point: Point, _cursor_point: Point,
text_layout_cache: &TextLayoutCache, text_layout_cache: &TextLayoutCache,
tcx: &TerminalLayoutTheme, tcx: &TerminalLayoutData,
) -> Line { ) -> Line {
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();
@ -545,30 +595,41 @@ fn layout_cursor_text(
pub fn mouse_to_cell_data( pub fn mouse_to_cell_data(
pos: Vector2F, pos: Vector2F,
origin: Vector2F, origin: Vector2F,
cur_size: SizeInfo, cur_size: TerminalDimensions,
display_offset: usize, display_offset: usize,
) -> (Point, alacritty_terminal::index::Direction) { ) -> (Point, alacritty_terminal::index::Direction) {
let relative_pos = relative_pos(pos, origin); let pos = pos.sub(origin);
let point = grid_cell(&relative_pos, cur_size, display_offset); let point = {
let side = cell_side(&relative_pos, cur_size); let col = pos.x() / cur_size.cell_width; //TODO: underflow...
(point, side) let col = min(GridCol(col as usize), cur_size.last_column());
}
///Configures a size info object from the given information. let line = pos.y() / cur_size.line_height;
fn make_new_size( let line = min(line as i32, cur_size.bottommost_line().0);
constraint: SizeConstraint,
cell_width: &CellWidth, Point::new(GridLine(line - display_offset as i32), col)
line_height: &LineHeight, };
) -> SizeInfo {
SizeInfo::new( //Copied (with modifications) from alacritty/src/input.rs > Processor::cell_side()
constraint.max.x() - cell_width.0, let side = {
constraint.max.y(), let x = pos.0.x() as usize;
cell_width.0, let cell_x = x.saturating_sub(cur_size.cell_width as usize) % cur_size.cell_width as usize;
line_height.0, let half_cell_width = (cur_size.cell_width / 2.0) as usize;
0.,
0., let additional_padding =
false, (cur_size.width() - cur_size.cell_width * 2.) % cur_size.cell_width;
) let end_of_grid = cur_size.width() - cur_size.cell_width - additional_padding;
//Width: Pixels or columns?
if cell_x > half_cell_width
// Edge case when mouse leaves the window.
|| x as f32 >= end_of_grid
{
Side::Right
} else {
Side::Left
}
};
(point, side)
} }
fn layout_grid( fn layout_grid(
@ -686,23 +747,23 @@ fn get_cursor_shape(
line: usize, line: usize,
line_index: usize, line_index: usize,
display_offset: usize, display_offset: usize,
line_height: &LineHeight, line_height: f32,
cell_width: &CellWidth, cell_width: f32,
total_lines: usize, total_lines: usize,
text_fragment: &Line, text_fragment: &Line,
) -> Option<(Vector2F, f32)> { ) -> Option<(Vector2F, f32)> {
let cursor_line = line + display_offset; let cursor_line = line + display_offset;
if cursor_line <= total_lines { if cursor_line <= total_lines {
let cursor_width = if text_fragment.width() == 0. { let cursor_width = if text_fragment.width() == 0. {
cell_width.0 cell_width
} else { } else {
text_fragment.width() text_fragment.width()
}; };
Some(( Some((
vec2f( vec2f(
line_index as f32 * cell_width.0, line_index as f32 * cell_width,
cursor_line as f32 * line_height.0, cursor_line as f32 * line_height,
), ),
cursor_width, cursor_width,
)) ))
@ -737,128 +798,6 @@ fn cell_style(
} }
} }
// fn attach_mouse_handlers(
// origin: Vector2F,
// cur_size: SizeInfo,
// view_id: usize,
// terminal_mutex: &Arc<FairMutex<Term<ZedListener>>>,
// visible_bounds: RectF,
// cx: &mut PaintContext,
// ) {
// let click_mutex = terminal_mutex.clone();
// let drag_mutex = terminal_mutex.clone();
// let mouse_down_mutex = terminal_mutex.clone();
// cx.scene.push_mouse_region(
// MouseRegion::new(view_id, None, visible_bounds)
// .on_down(
// MouseButton::Left,
// move |MouseButtonEvent { position, .. }, _| {
// let mut term = mouse_down_mutex.lock();
// let (point, side) = mouse_to_cell_data(
// position,
// origin,
// cur_size,
// term.renderable_content().display_offset,
// );
// term.selection = Some(Selection::new(SelectionType::Simple, point, side))
// },
// )
// .on_click(
// MouseButton::Left,
// move |MouseButtonEvent {
// position,
// click_count,
// ..
// },
// cx| {
// let mut term = click_mutex.lock();
// let (point, side) = mouse_to_cell_data(
// position,
// origin,
// cur_size,
// term.renderable_content().display_offset,
// );
// let selection_type = match click_count {
// 0 => return, //This is a release
// 1 => Some(SelectionType::Simple),
// 2 => Some(SelectionType::Semantic),
// 3 => Some(SelectionType::Lines),
// _ => None,
// };
// let selection = selection_type
// .map(|selection_type| Selection::new(selection_type, point, side));
// term.selection = selection;
// cx.focus_parent_view();
// cx.notify();
// },
// )
// .on_drag(
// MouseButton::Left,
// move |_, MouseMovedEvent { position, .. }, cx| {
// let mut term = drag_mutex.lock();
// let (point, side) = mouse_to_cell_data(
// position,
// origin,
// cur_size,
// term.renderable_content().display_offset,
// );
// if let Some(mut selection) = term.selection.take() {
// selection.update(point, side);
// term.selection = Some(selection);
// }
// cx.notify();
// },
// ),
// );
// }
///Copied (with modifications) from alacritty/src/input.rs > Processor::cell_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 half_cell_width = (cur_size.cell_width() / 2.0) as usize;
let additional_padding =
(cur_size.width() - cur_size.cell_width() * 2.) % cur_size.cell_width();
let end_of_grid = cur_size.width() - cur_size.cell_width() - additional_padding;
if cell_x > half_cell_width
// Edge case when mouse leaves the window.
|| x as f32 >= end_of_grid
{
Side::Right
} else {
Side::Left
}
}
///Copied (with modifications) from alacritty/src/event.rs > Mouse::point()
///Position is a pane-relative position. That means the top left corner of the mouse
///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 line = pos.y() / cur_size.cell_height();
let line = min(line as i32, cur_size.bottommost_line().0);
//when clicking, need to ADD to get to the top left cell
//e.g. total_lines - viewport_height, THEN subtract display offset
//0 -> total_lines - viewport_height - display_offset + mouse_line
Point::new(GridLine(line - display_offset as i32), col)
}
mod test { mod test {
#[test] #[test]
@ -872,14 +811,10 @@ mod test {
let origin_x = 10.; let origin_x = 10.;
let origin_y = 20.; let origin_y = 20.;
let cur_size = alacritty_terminal::term::SizeInfo::new( let cur_size = crate::terminal_element::TerminalDimensions::new(
term_width,
term_height,
cell_width, cell_width,
line_height, line_height,
0., gpui::geometry::vector::vec2f(term_width, term_height),
0.,
false,
); );
let mouse_pos = gpui::geometry::vector::vec2f(mouse_pos_x, mouse_pos_y); let mouse_pos = gpui::geometry::vector::vec2f(mouse_pos_x, mouse_pos_y);

View file

@ -1,24 +1,24 @@
use super::*; use super::*;
pub struct TerminalLayoutTheme<'a> { pub struct TerminalLayoutData<'a> {
pub line_height: LineHeight,
pub cell_width: CellWidth,
pub text_style: TextStyle, pub text_style: TextStyle,
pub selection_color: Color, pub selection_color: Color,
pub terminal_theme: &'a TerminalStyle, pub terminal_theme: &'a TerminalStyle,
pub size: TerminalDimensions,
} }
impl<'a> TerminalLayoutTheme<'a> { impl<'a> TerminalLayoutData<'a> {
pub fn new(settings: &'a Settings, font_cache: &FontCache) -> Self { pub fn new(settings: &'a Settings, font_cache: &FontCache, constraint: Vector2F) -> Self {
let text_style = Self::make_text_style(font_cache, &settings); let text_style = Self::make_text_style(font_cache, &settings);
let line_height = LineHeight(font_cache.line_height(text_style.font_size));
let cell_width = CellWidth(font_cache.em_advance(text_style.font_id, text_style.font_size));
let selection_color = settings.theme.editor.selection.selection; let selection_color = settings.theme.editor.selection.selection;
let terminal_theme = &settings.theme.terminal; let terminal_theme = &settings.theme.terminal;
TerminalLayoutTheme { let line_height = font_cache.line_height(text_style.font_size);
line_height, let cell_width = font_cache.em_advance(text_style.font_id, text_style.font_size);
cell_width, let dimensions = TerminalDimensions::new(line_height, cell_width, constraint);
TerminalLayoutData {
size: dimensions,
text_style, text_style,
selection_color, selection_color,
terminal_theme, terminal_theme,

View file

@ -1,12 +1,11 @@
use std::time::Duration; use std::time::Duration;
use alacritty_terminal::term::SizeInfo; use gpui::{geometry::vector::vec2f, AppContext, ModelHandle, ReadModelWith, TestAppContext};
use gpui::{AppContext, ModelHandle, ReadModelWith, TestAppContext};
use itertools::Itertools; use itertools::Itertools;
use crate::{ use crate::{
connection::TerminalConnection, DEBUG_CELL_WIDTH, DEBUG_LINE_HEIGHT, DEBUG_TERMINAL_HEIGHT, connection::TerminalConnection, terminal_element::TerminalDimensions, DEBUG_CELL_WIDTH,
DEBUG_TERMINAL_WIDTH, DEBUG_LINE_HEIGHT, DEBUG_TERMINAL_HEIGHT, DEBUG_TERMINAL_WIDTH,
}; };
pub struct TerminalTestContext<'a> { pub struct TerminalTestContext<'a> {
@ -18,14 +17,10 @@ impl<'a> TerminalTestContext<'a> {
pub fn new(cx: &'a mut TestAppContext) -> Self { pub fn new(cx: &'a mut TestAppContext) -> Self {
cx.set_condition_duration(Some(Duration::from_secs(5))); cx.set_condition_duration(Some(Duration::from_secs(5)));
let size_info = SizeInfo::new( let size_info = TerminalDimensions::new(
DEBUG_TERMINAL_WIDTH,
DEBUG_TERMINAL_HEIGHT,
DEBUG_CELL_WIDTH, DEBUG_CELL_WIDTH,
DEBUG_LINE_HEIGHT, DEBUG_LINE_HEIGHT,
0., vec2f(DEBUG_TERMINAL_WIDTH, DEBUG_TERMINAL_HEIGHT),
0.,
false,
); );
let connection = let connection =