From d89c51135a7e2110e766265fb4ba301f594dc722 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 14 Nov 2023 11:03:24 +0200 Subject: [PATCH 1/5] Start porting terminal_view to gpui2 Co-Authored-By: Mikayla Maki --- Cargo.lock | 33 + Cargo.toml | 1 + crates/terminal_view2/Cargo.toml | 46 + crates/terminal_view2/README.md | 23 + .../terminal_view2/scripts/print256color.sh | 96 ++ crates/terminal_view2/scripts/truecolor.sh | 19 + crates/terminal_view2/src/persistence.rs | 72 + crates/terminal_view2/src/terminal_element.rs | 937 +++++++++++++ crates/terminal_view2/src/terminal_panel.rs | 460 +++++++ crates/terminal_view2/src/terminal_view.rs | 1165 +++++++++++++++++ 10 files changed, 2852 insertions(+) create mode 100644 crates/terminal_view2/Cargo.toml create mode 100644 crates/terminal_view2/README.md create mode 100755 crates/terminal_view2/scripts/print256color.sh create mode 100755 crates/terminal_view2/scripts/truecolor.sh create mode 100644 crates/terminal_view2/src/persistence.rs create mode 100644 crates/terminal_view2/src/terminal_element.rs create mode 100644 crates/terminal_view2/src/terminal_panel.rs create mode 100644 crates/terminal_view2/src/terminal_view.rs diff --git a/Cargo.lock b/Cargo.lock index e2cf75f34a79d10806ea30e22c439bde1c5b65ec..0e155e9e995fa9cbdfa9b4ae87cd8dda5a7c947b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9175,6 +9175,39 @@ dependencies = [ "workspace", ] +[[package]] +name = "terminal_view2" +version = "0.1.0" +dependencies = [ + "anyhow", + "client2", + "db2", + "dirs 4.0.0", + "editor2", + "futures 0.3.28", + "gpui2", + "itertools 0.10.5", + "language2", + "lazy_static", + "libc", + "mio-extras", + "ordered-float 2.10.0", + "procinfo", + "project2", + "rand 0.8.5", + "serde", + "serde_derive", + "settings2", + "shellexpand", + "smallvec", + "smol", + "terminal2", + "theme2", + "thiserror", + "util", + "workspace2", +] + [[package]] name = "text" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 7685bf6a316f0e3c17245cd2e1b98d6028e7910f..f8d0af77fa85220f348c866edfe5242d0cccbeec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,6 +98,7 @@ members = [ "crates/sum_tree", "crates/terminal", "crates/terminal2", + "crates/terminal_view2", "crates/text", "crates/theme", "crates/theme2", diff --git a/crates/terminal_view2/Cargo.toml b/crates/terminal_view2/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..f0d2e6ccf02f2d5aa9673136b2cba6f4cf283322 --- /dev/null +++ b/crates/terminal_view2/Cargo.toml @@ -0,0 +1,46 @@ +[package] +name = "terminal_view2" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +path = "src/terminal_view.rs" +doctest = false + +[dependencies] +# context_menu = { package = "context_menu2", path = "../context_menu2" } +editor = { package = "editor2", path = "../editor2" } +language = { package = "language2", path = "../language2" } +gpui = { package = "gpui2", path = "../gpui2" } +project = { package = "project2", path = "../project2" } +# search = { package = "search2", path = "../search2" } +settings = { package = "settings2", path = "../settings2" } +theme = { package = "theme2", path = "../theme2" } +util = { path = "../util" } +workspace = { package = "workspace2", path = "../workspace2" } +db = { package = "db2", path = "../db2" } +procinfo = { git = "https://github.com/zed-industries/wezterm", rev = "5cd757e5f2eb039ed0c6bb6512223e69d5efc64d", default-features = false } +terminal = { package = "terminal2", path = "../terminal2" } +smallvec.workspace = true +smol.workspace = true +mio-extras = "2.0.6" +futures.workspace = true +ordered-float.workspace = true +itertools = "0.10" +dirs = "4.0.0" +shellexpand = "2.1.0" +libc = "0.2" +anyhow.workspace = true +thiserror.workspace = true +lazy_static.workspace = true +serde.workspace = true +serde_derive.workspace = true + +[dev-dependencies] +editor = { package = "editor2", path = "../editor2", features = ["test-support"] } +gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] } +client = { package = "client2", path = "../client2", features = ["test-support"]} +project = { package = "project2", path = "../project2", features = ["test-support"]} +workspace = { package = "workspace2", path = "../workspace2", features = ["test-support"] } +rand.workspace = true diff --git a/crates/terminal_view2/README.md b/crates/terminal_view2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ca48f545427993caf97f6f7670a23e3e309dc214 --- /dev/null +++ b/crates/terminal_view2/README.md @@ -0,0 +1,23 @@ +Design notes: + +This crate is split into two conceptual halves: +- The terminal.rs file and the src/mappings/ folder, these contain the code for interacting with Alacritty and maintaining the pty event loop. Some behavior in this file is constrained by terminal protocols and standards. The Zed init function is also placed here. +- Everything else. These other files integrate the `Terminal` struct created in terminal.rs into the rest of GPUI. The main entry point for GPUI is the terminal_view.rs file and the modal.rs file. + +ttys are created externally, and so can fail in unexpected ways. However, GPUI currently does not have an API for models than can fail to instantiate. `TerminalBuilder` solves this by using Rust's type system to split tty instantiation into a 2 step process: first attempt to create the file handles with `TerminalBuilder::new()`, check the result, then call `TerminalBuilder::subscribe(cx)` from within a model context. + +The TerminalView struct abstracts over failed and successful terminals, passing focus through to the associated view and allowing clients to build a terminal without worrying about errors. + +#Input + +There are currently many distinct paths for getting keystrokes to the terminal: + +1. Terminal specific characters and bindings. Things like ctrl-a mapping to ASCII control character 1, ANSI escape codes associated with the function keys, etc. These are caught with a raw key-down handler in the element and are processed immediately. This is done with the `try_keystroke()` method on Terminal + +2. GPU Action handlers. GPUI clobbers a few vital keys by adding bindings to them in the global context. These keys are synthesized and then dispatched through the same `try_keystroke()` API as the above mappings + +3. IME text. When the special character mappings fail, we pass the keystroke back to GPUI to hand it to the IME system. This comes back to us in the `View::replace_text_in_range()` method, and we then send that to the terminal directly, bypassing `try_keystroke()`. + +4. Pasted text has a separate pathway. + +Generally, there's a distinction between 'keystrokes that need to be mapped' and 'strings which need to be written'. I've attempted to unify these under the '.try_keystroke()' API and the `.input()` API (which try_keystroke uses) so we have consistent input handling across the terminal diff --git a/crates/terminal_view2/scripts/print256color.sh b/crates/terminal_view2/scripts/print256color.sh new file mode 100755 index 0000000000000000000000000000000000000000..8a53f3bc025842d900b5b4797eefc6d8a0946120 --- /dev/null +++ b/crates/terminal_view2/scripts/print256color.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Tom Hale, 2016. MIT Licence. +# Print out 256 colours, with each number printed in its corresponding colour +# See http://askubuntu.com/questions/821157/print-a-256-color-test-pattern-in-the-terminal/821163#821163 + +set -eu # Fail on errors or undeclared variables + +printable_colours=256 + +# Return a colour that contrasts with the given colour +# Bash only does integer division, so keep it integral +function contrast_colour { + local r g b luminance + colour="$1" + + if (( colour < 16 )); then # Initial 16 ANSI colours + (( colour == 0 )) && printf "15" || printf "0" + return + fi + + # Greyscale # rgb_R = rgb_G = rgb_B = (number - 232) * 10 + 8 + if (( colour > 231 )); then # Greyscale ramp + (( colour < 244 )) && printf "15" || printf "0" + return + fi + + # All other colours: + # 6x6x6 colour cube = 16 + 36*R + 6*G + B # Where RGB are [0..5] + # See http://stackoverflow.com/a/27165165/5353461 + + # r=$(( (colour-16) / 36 )) + g=$(( ((colour-16) % 36) / 6 )) + # b=$(( (colour-16) % 6 )) + + # If luminance is bright, print number in black, white otherwise. + # Green contributes 587/1000 to human perceived luminance - ITU R-REC-BT.601 + (( g > 2)) && printf "0" || printf "15" + return + + # Uncomment the below for more precise luminance calculations + + # # Calculate perceived brightness + # # See https://www.w3.org/TR/AERT#color-contrast + # # and http://www.itu.int/rec/R-REC-BT.601 + # # Luminance is in range 0..5000 as each value is 0..5 + # luminance=$(( (r * 299) + (g * 587) + (b * 114) )) + # (( $luminance > 2500 )) && printf "0" || printf "15" +} + +# Print a coloured block with the number of that colour +function print_colour { + local colour="$1" contrast + contrast=$(contrast_colour "$1") + printf "\e[48;5;%sm" "$colour" # Start block of colour + printf "\e[38;5;%sm%3d" "$contrast" "$colour" # In contrast, print number + printf "\e[0m " # Reset colour +} + +# Starting at $1, print a run of $2 colours +function print_run { + local i + for (( i = "$1"; i < "$1" + "$2" && i < printable_colours; i++ )) do + print_colour "$i" + done + printf " " +} + +# Print blocks of colours +function print_blocks { + local start="$1" i + local end="$2" # inclusive + local block_cols="$3" + local block_rows="$4" + local blocks_per_line="$5" + local block_length=$((block_cols * block_rows)) + + # Print sets of blocks + for (( i = start; i <= end; i += (blocks_per_line-1) * block_length )) do + printf "\n" # Space before each set of blocks + # For each block row + for (( row = 0; row < block_rows; row++ )) do + # Print block columns for all blocks on the line + for (( block = 0; block < blocks_per_line; block++ )) do + print_run $(( i + (block * block_length) )) "$block_cols" + done + (( i += block_cols )) # Prepare to print the next row + printf "\n" + done + done +} + +print_run 0 16 # The first 16 colours are spread over the whole spectrum +printf "\n" +print_blocks 16 231 6 6 3 # 6x6x6 colour cube between 16 and 231 inclusive +print_blocks 232 255 12 2 1 # Not 50, but 24 Shades of Grey diff --git a/crates/terminal_view2/scripts/truecolor.sh b/crates/terminal_view2/scripts/truecolor.sh new file mode 100755 index 0000000000000000000000000000000000000000..14e5d813085d69262920685a556d404c02981735 --- /dev/null +++ b/crates/terminal_view2/scripts/truecolor.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# Copied from: https://unix.stackexchange.com/a/696756 +# Based on: https://gist.github.com/XVilka/8346728 and https://unix.stackexchange.com/a/404415/395213 + +awk -v term_cols="${width:-$(tput cols || echo 80)}" -v term_lines="${height:-1}" 'BEGIN{ + s="/\\"; + total_cols=term_cols*term_lines; + for (colnum = 0; colnum255) g = 510-g; + printf "\033[48;2;%d;%d;%dm", r,g,b; + printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b; + printf "%s\033[0m", substr(s,colnum%2+1,1); + if (colnum%term_cols==term_cols) printf "\n"; + } + printf "\n"; +}' \ No newline at end of file diff --git a/crates/terminal_view2/src/persistence.rs b/crates/terminal_view2/src/persistence.rs new file mode 100644 index 0000000000000000000000000000000000000000..38dad88a8e29cd660ccdb40c995ce9b746184dc9 --- /dev/null +++ b/crates/terminal_view2/src/persistence.rs @@ -0,0 +1,72 @@ +use std::path::PathBuf; + +use db::{define_connection, query, sqlez_macros::sql}; +use gpui::EntityId; +use workspace::{ItemId, WorkspaceDb, WorkspaceId}; + +define_connection! { + pub static ref TERMINAL_DB: TerminalDb = + &[sql!( + CREATE TABLE terminals ( + workspace_id INTEGER, + item_id INTEGER UNIQUE, + working_directory BLOB, + PRIMARY KEY(workspace_id, item_id), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) + ON DELETE CASCADE + ) STRICT; + ), + // Remove the unique constraint on the item_id table + // SQLite doesn't have a way of doing this automatically, so + // we have to do this silly copying. + sql!( + CREATE TABLE terminals2 ( + workspace_id INTEGER, + item_id INTEGER, + working_directory BLOB, + PRIMARY KEY(workspace_id, item_id), + FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id) + ON DELETE CASCADE + ) STRICT; + + INSERT INTO terminals2 (workspace_id, item_id, working_directory) + SELECT workspace_id, item_id, working_directory FROM terminals; + + DROP TABLE terminals; + + ALTER TABLE terminals2 RENAME TO terminals; + )]; +} + +impl TerminalDb { + query! { + pub async fn update_workspace_id( + new_id: WorkspaceId, + old_id: WorkspaceId, + item_id: ItemId + ) -> Result<()> { + UPDATE terminals + SET workspace_id = ? + WHERE workspace_id = ? AND item_id = ? + } + } + + query! { + pub async fn save_working_directory( + item_id: ItemId, + workspace_id: WorkspaceId, + working_directory: PathBuf + ) -> Result<()> { + INSERT OR REPLACE INTO terminals(item_id, workspace_id, working_directory) + VALUES (?, ?, ?) + } + } + + query! { + pub fn get_working_directory(item_id: ItemId, workspace_id: WorkspaceId) -> Result> { + SELECT working_directory + FROM terminals + WHERE item_id = ? AND workspace_id = ? + } + } +} diff --git a/crates/terminal_view2/src/terminal_element.rs b/crates/terminal_view2/src/terminal_element.rs new file mode 100644 index 0000000000000000000000000000000000000000..30dbccf4559f2175901b6fbfc74e090020b89f34 --- /dev/null +++ b/crates/terminal_view2/src/terminal_element.rs @@ -0,0 +1,937 @@ +use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; +use gpui::{ + color::Color, + elements::{Empty, Overlay}, + fonts::{HighlightStyle, Properties, Style::Italic, TextStyle, Underline, Weight}, + geometry::{ + rect::RectF, + vector::{vec2f, Vector2F}, + }, + platform::{CursorStyle, MouseButton}, + serde_json::json, + text_layout::{Line, RunStyle}, + AnyElement, Element, EventContext, FontCache, ModelContext, MouseRegion, Quad, SizeConstraint, + TextLayoutCache, ViewContext, WeakModelHandle, WindowContext, +}; +use itertools::Itertools; +use language::CursorShape; +use ordered_float::OrderedFloat; +use terminal::{ + alacritty_terminal::{ + ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor}, + grid::Dimensions, + index::Point, + term::{cell::Flags, TermMode}, + }, + mappings::colors::convert_color, + terminal_settings::TerminalSettings, + IndexedCell, Terminal, TerminalContent, TerminalSize, +}; +use theme::{TerminalStyle, ThemeSettings}; +use util::ResultExt; + +use std::{fmt::Debug, ops::RangeInclusive}; +use std::{mem, ops::Range}; + +use crate::TerminalView; + +///The information generated during layout that is necessary for painting +pub struct LayoutState { + cells: Vec, + rects: Vec, + relative_highlighted_ranges: Vec<(RangeInclusive, Color)>, + cursor: Option, + background_color: Color, + size: TerminalSize, + mode: TermMode, + display_offset: usize, + hyperlink_tooltip: Option>, + gutter: f32, +} + +///Helper struct for converting data between alacritty's cursor points, and displayed cursor points +struct DisplayCursor { + line: i32, + col: usize, +} + +impl DisplayCursor { + fn from(cursor_point: Point, display_offset: usize) -> Self { + Self { + line: cursor_point.line.0 + display_offset as i32, + col: cursor_point.column.0, + } + } + + pub fn line(&self) -> i32 { + self.line + } + + pub fn col(&self) -> usize { + self.col + } +} + +#[derive(Clone, Debug, Default)] +struct LayoutCell { + point: Point, + text: Line, +} + +impl LayoutCell { + fn new(point: Point, text: Line) -> LayoutCell { + LayoutCell { point, text } + } + + fn paint( + &self, + origin: Vector2F, + layout: &LayoutState, + visible_bounds: RectF, + _view: &mut TerminalView, + cx: &mut WindowContext, + ) { + let pos = { + let point = self.point; + vec2f( + (origin.x() + point.column as f32 * layout.size.cell_width).floor(), + origin.y() + point.line as f32 * layout.size.line_height, + ) + }; + + self.text + .paint(pos, visible_bounds, layout.size.line_height, cx); + } +} + +#[derive(Clone, Debug, Default)] +struct LayoutRect { + point: Point, + num_of_cells: usize, + color: Color, +} + +impl LayoutRect { + fn new(point: Point, num_of_cells: usize, color: Color) -> LayoutRect { + LayoutRect { + point, + num_of_cells, + color, + } + } + + fn extend(&self) -> Self { + LayoutRect { + point: self.point, + num_of_cells: self.num_of_cells + 1, + color: self.color, + } + } + + fn paint( + &self, + origin: Vector2F, + layout: &LayoutState, + _view: &mut TerminalView, + cx: &mut ViewContext, + ) { + let position = { + let point = self.point; + vec2f( + (origin.x() + point.column as f32 * layout.size.cell_width).floor(), + origin.y() + point.line as f32 * layout.size.line_height, + ) + }; + let size = vec2f( + (layout.size.cell_width * self.num_of_cells as f32).ceil(), + layout.size.line_height, + ); + + cx.scene().push_quad(Quad { + bounds: RectF::new(position, size), + background: Some(self.color), + border: Default::default(), + corner_radii: Default::default(), + }) + } +} + +///The GPUI element that paints the terminal. +///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection? +pub struct TerminalElement { + terminal: WeakModelHandle, + focused: bool, + cursor_visible: bool, + can_navigate_to_selected_word: bool, +} + +impl TerminalElement { + pub fn new( + terminal: WeakModelHandle, + focused: bool, + cursor_visible: bool, + can_navigate_to_selected_word: bool, + ) -> TerminalElement { + TerminalElement { + terminal, + focused, + cursor_visible, + can_navigate_to_selected_word, + } + } + + //Vec> -> Clip out the parts of the ranges + + fn layout_grid( + grid: &Vec, + text_style: &TextStyle, + terminal_theme: &TerminalStyle, + text_layout_cache: &TextLayoutCache, + font_cache: &FontCache, + hyperlink: Option<(HighlightStyle, &RangeInclusive)>, + ) -> (Vec, Vec) { + let mut cells = vec![]; + let mut rects = vec![]; + + let mut cur_rect: Option = None; + let mut cur_alac_color = None; + + let linegroups = grid.into_iter().group_by(|i| i.point.line); + for (line_index, (_, line)) in linegroups.into_iter().enumerate() { + for cell in line { + let mut fg = cell.fg; + let mut bg = cell.bg; + if cell.flags.contains(Flags::INVERSE) { + mem::swap(&mut fg, &mut bg); + } + + //Expand background rect range + { + if matches!(bg, Named(NamedColor::Background)) { + //Continue to next cell, resetting variables if necessary + cur_alac_color = None; + if let Some(rect) = cur_rect { + rects.push(rect); + cur_rect = None + } + } else { + match cur_alac_color { + Some(cur_color) => { + if bg == cur_color { + cur_rect = cur_rect.take().map(|rect| rect.extend()); + } else { + cur_alac_color = Some(bg); + if cur_rect.is_some() { + rects.push(cur_rect.take().unwrap()); + } + cur_rect = Some(LayoutRect::new( + Point::new(line_index as i32, cell.point.column.0 as i32), + 1, + convert_color(&bg, &terminal_theme), + )); + } + } + None => { + cur_alac_color = Some(bg); + cur_rect = Some(LayoutRect::new( + Point::new(line_index as i32, cell.point.column.0 as i32), + 1, + convert_color(&bg, &terminal_theme), + )); + } + } + } + } + + //Layout current cell text + { + let cell_text = &cell.c.to_string(); + if !is_blank(&cell) { + let cell_style = TerminalElement::cell_style( + &cell, + fg, + terminal_theme, + text_style, + font_cache, + hyperlink, + ); + + let layout_cell = text_layout_cache.layout_str( + cell_text, + text_style.font_size, + &[(cell_text.len(), cell_style)], + ); + + cells.push(LayoutCell::new( + Point::new(line_index as i32, cell.point.column.0 as i32), + layout_cell, + )) + }; + } + } + + if cur_rect.is_some() { + rects.push(cur_rect.take().unwrap()); + } + } + (cells, rects) + } + + // 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 + fn shape_cursor( + cursor_point: DisplayCursor, + size: TerminalSize, + text_fragment: &Line, + ) -> Option<(Vector2F, f32)> { + if cursor_point.line() < size.total_lines() as i32 { + let cursor_width = if text_fragment.width() == 0. { + size.cell_width() + } else { + text_fragment.width() + }; + + //Cursor should always surround as much of the text as possible, + //hence when on pixel boundaries round the origin down and the width up + Some(( + vec2f( + (cursor_point.col() as f32 * size.cell_width()).floor(), + (cursor_point.line() as f32 * size.line_height()).floor(), + ), + cursor_width.ceil(), + )) + } else { + None + } + } + + ///Convert the Alacritty cell styles to GPUI text styles and background color + fn cell_style( + indexed: &IndexedCell, + fg: terminal::alacritty_terminal::ansi::Color, + style: &TerminalStyle, + text_style: &TextStyle, + font_cache: &FontCache, + hyperlink: Option<(HighlightStyle, &RangeInclusive)>, + ) -> RunStyle { + let flags = indexed.cell.flags; + let fg = convert_color(&fg, &style); + + let mut underline = flags + .intersects(Flags::ALL_UNDERLINES) + .then(|| Underline { + color: Some(fg), + squiggly: flags.contains(Flags::UNDERCURL), + thickness: OrderedFloat(1.), + }) + .unwrap_or_default(); + + if indexed.cell.hyperlink().is_some() { + if underline.thickness == OrderedFloat(0.) { + underline.thickness = OrderedFloat(1.); + } + } + + let mut properties = Properties::new(); + if indexed.flags.intersects(Flags::BOLD | Flags::DIM_BOLD) { + properties = *properties.weight(Weight::BOLD); + } + if indexed.flags.intersects(Flags::ITALIC) { + properties = *properties.style(Italic); + } + + let font_id = font_cache + .select_font(text_style.font_family_id, &properties) + .unwrap_or(text_style.font_id); + + let mut result = RunStyle { + color: fg, + font_id, + underline, + }; + + if let Some((style, range)) = hyperlink { + if range.contains(&indexed.point) { + if let Some(underline) = style.underline { + result.underline = underline; + } + + if let Some(color) = style.color { + result.color = color; + } + } + } + + result + } + + fn generic_button_handler( + connection: WeakModelHandle, + origin: Vector2F, + f: impl Fn(&mut Terminal, Vector2F, E, &mut ModelContext), + ) -> impl Fn(E, &mut TerminalView, &mut EventContext) { + move |event, _: &mut TerminalView, cx| { + cx.focus_parent(); + if let Some(conn_handle) = connection.upgrade(cx) { + conn_handle.update(cx, |terminal, cx| { + f(terminal, origin, event, cx); + + cx.notify(); + }) + } + } + } + + fn attach_mouse_handlers( + &self, + origin: Vector2F, + visible_bounds: RectF, + mode: TermMode, + cx: &mut ViewContext, + ) { + let connection = self.terminal; + + let mut region = MouseRegion::new::(cx.view_id(), 0, visible_bounds); + + // Terminal Emulator controlled behavior: + region = region + // Start selections + .on_down(MouseButton::Left, move |event, v: &mut TerminalView, cx| { + let terminal_view = cx.handle(); + cx.focus(&terminal_view); + v.context_menu.update(cx, |menu, _cx| menu.delay_cancel()); + if let Some(conn_handle) = connection.upgrade(cx) { + conn_handle.update(cx, |terminal, cx| { + terminal.mouse_down(&event, origin); + + cx.notify(); + }) + } + }) + // Update drag selections + .on_drag(MouseButton::Left, move |event, _: &mut TerminalView, cx| { + if event.end { + return; + } + + if cx.is_self_focused() { + if let Some(conn_handle) = connection.upgrade(cx) { + conn_handle.update(cx, |terminal, cx| { + terminal.mouse_drag(event, origin); + cx.notify(); + }) + } + } + }) + // Copy on up behavior + .on_up( + MouseButton::Left, + TerminalElement::generic_button_handler( + connection, + origin, + move |terminal, origin, e, cx| { + terminal.mouse_up(&e, origin, cx); + }, + ), + ) + // Context menu + .on_click( + MouseButton::Right, + move |event, view: &mut TerminalView, cx| { + let mouse_mode = if let Some(conn_handle) = connection.upgrade(cx) { + conn_handle.update(cx, |terminal, _cx| terminal.mouse_mode(event.shift)) + } else { + // If we can't get the model handle, probably can't deploy the context menu + true + }; + if !mouse_mode { + view.deploy_context_menu(event.position, cx); + } + }, + ) + .on_move(move |event, _: &mut TerminalView, cx| { + if cx.is_self_focused() { + if let Some(conn_handle) = connection.upgrade(cx) { + conn_handle.update(cx, |terminal, cx| { + terminal.mouse_move(&event, origin); + cx.notify(); + }) + } + } + }) + .on_scroll(move |event, _: &mut TerminalView, cx| { + if let Some(conn_handle) = connection.upgrade(cx) { + conn_handle.update(cx, |terminal, cx| { + terminal.scroll_wheel(event, origin); + cx.notify(); + }) + } + }); + + // Mouse mode handlers: + // All mouse modes need the extra click handlers + if mode.intersects(TermMode::MOUSE_MODE) { + region = region + .on_down( + MouseButton::Right, + TerminalElement::generic_button_handler( + connection, + origin, + move |terminal, origin, e, _cx| { + terminal.mouse_down(&e, origin); + }, + ), + ) + .on_down( + MouseButton::Middle, + TerminalElement::generic_button_handler( + connection, + origin, + move |terminal, origin, e, _cx| { + terminal.mouse_down(&e, origin); + }, + ), + ) + .on_up( + MouseButton::Right, + TerminalElement::generic_button_handler( + connection, + origin, + move |terminal, origin, e, cx| { + terminal.mouse_up(&e, origin, cx); + }, + ), + ) + .on_up( + MouseButton::Middle, + TerminalElement::generic_button_handler( + connection, + origin, + move |terminal, origin, e, cx| { + terminal.mouse_up(&e, origin, cx); + }, + ), + ) + } + + cx.scene().push_mouse_region(region); + } +} + +impl Element for TerminalElement { + type LayoutState = LayoutState; + type PaintState = (); + + fn layout( + &mut self, + constraint: gpui::SizeConstraint, + view: &mut TerminalView, + cx: &mut ViewContext, + ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { + let settings = settings::get::(cx); + let terminal_settings = settings::get::(cx); + + //Setup layout information + let terminal_theme = settings.theme.terminal.clone(); //TODO: Try to minimize this clone. + let link_style = settings.theme.editor.link_definition; + let tooltip_style = settings.theme.tooltip.clone(); + + let font_cache = cx.font_cache(); + let font_size = terminal_settings + .font_size(cx) + .unwrap_or(settings.buffer_font_size(cx)); + let font_family_name = terminal_settings + .font_family + .as_ref() + .unwrap_or(&settings.buffer_font_family_name); + let font_features = terminal_settings + .font_features + .as_ref() + .unwrap_or(&settings.buffer_font_features); + let family_id = font_cache + .load_family(&[font_family_name], &font_features) + .log_err() + .unwrap_or(settings.buffer_font_family); + let font_id = font_cache + .select_font(family_id, &Default::default()) + .unwrap(); + + let text_style = TextStyle { + color: settings.theme.editor.text_color, + font_family_id: family_id, + font_family_name: font_cache.family_name(family_id).unwrap(), + font_id, + font_size, + font_properties: Default::default(), + underline: Default::default(), + soft_wrap: false, + }; + let selection_color = settings.theme.editor.selection.selection; + let match_color = settings.theme.search.match_background; + let gutter; + let dimensions = { + let line_height = text_style.font_size * terminal_settings.line_height.value(); + let cell_width = font_cache.em_advance(text_style.font_id, text_style.font_size); + gutter = cell_width; + + let size = constraint.max - vec2f(gutter, 0.); + TerminalSize::new(line_height, cell_width, size) + }; + + let search_matches = if let Some(terminal_model) = self.terminal.upgrade(cx) { + terminal_model.read(cx).matches.clone() + } else { + Default::default() + }; + + let background_color = terminal_theme.background; + let terminal_handle = self.terminal.upgrade(cx).unwrap(); + + let last_hovered_word = terminal_handle.update(cx, |terminal, cx| { + terminal.set_size(dimensions); + terminal.try_sync(cx); + if self.can_navigate_to_selected_word && terminal.can_navigate_to_selected_word() { + terminal.last_content.last_hovered_word.clone() + } else { + None + } + }); + + let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| { + let mut tooltip = Overlay::new( + Empty::new() + .contained() + .constrained() + .with_width(dimensions.width()) + .with_height(dimensions.height()) + .with_tooltip::( + hovered_word.id, + hovered_word.word, + None, + tooltip_style, + cx, + ), + ) + .with_position_mode(gpui::elements::OverlayPositionMode::Local) + .into_any(); + + tooltip.layout( + SizeConstraint::new(Vector2F::zero(), cx.window_size()), + view, + cx, + ); + tooltip + }); + + let TerminalContent { + cells, + mode, + display_offset, + cursor_char, + selection, + cursor, + .. + } = { &terminal_handle.read(cx).last_content }; + + // searches, highlights to a single range representations + let mut relative_highlighted_ranges = Vec::new(); + for search_match in search_matches { + relative_highlighted_ranges.push((search_match, match_color)) + } + if let Some(selection) = selection { + relative_highlighted_ranges.push((selection.start..=selection.end, selection_color)); + } + + // then have that representation be converted to the appropriate highlight data structure + + let (cells, rects) = TerminalElement::layout_grid( + cells, + &text_style, + &terminal_theme, + cx.text_layout_cache(), + cx.font_cache(), + last_hovered_word + .as_ref() + .map(|last_hovered_word| (link_style, &last_hovered_word.word_match)), + ); + + //Layout cursor. Rectangle is used for IME, so we should lay it out even + //if we don't end up showing it. + let cursor = if let AlacCursorShape::Hidden = cursor.shape { + None + } else { + let cursor_point = DisplayCursor::from(cursor.point, *display_offset); + let cursor_text = { + let str_trxt = cursor_char.to_string(); + + let color = if self.focused { + terminal_theme.background + } else { + terminal_theme.foreground + }; + + cx.text_layout_cache().layout_str( + &str_trxt, + text_style.font_size, + &[( + str_trxt.len(), + RunStyle { + font_id: text_style.font_id, + color, + underline: Default::default(), + }, + )], + ) + }; + + let focused = self.focused; + TerminalElement::shape_cursor(cursor_point, dimensions, &cursor_text).map( + move |(cursor_position, block_width)| { + let (shape, text) = match cursor.shape { + AlacCursorShape::Block if !focused => (CursorShape::Hollow, None), + AlacCursorShape::Block => (CursorShape::Block, Some(cursor_text)), + AlacCursorShape::Underline => (CursorShape::Underscore, None), + AlacCursorShape::Beam => (CursorShape::Bar, None), + AlacCursorShape::HollowBlock => (CursorShape::Hollow, None), + //This case is handled in the if wrapping the whole cursor layout + AlacCursorShape::Hidden => unreachable!(), + }; + + Cursor::new( + cursor_position, + block_width, + dimensions.line_height, + terminal_theme.cursor, + shape, + text, + ) + }, + ) + }; + + //Done! + ( + constraint.max, + LayoutState { + cells, + cursor, + background_color, + size: dimensions, + rects, + relative_highlighted_ranges, + mode: *mode, + display_offset: *display_offset, + hyperlink_tooltip, + gutter, + }, + ) + } + + fn paint( + &mut self, + bounds: RectF, + visible_bounds: RectF, + layout: &mut Self::LayoutState, + view: &mut TerminalView, + cx: &mut ViewContext, + ) -> Self::PaintState { + let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); + + //Setup element stuff + let clip_bounds = Some(visible_bounds); + + cx.paint_layer(clip_bounds, |cx| { + let origin = bounds.origin() + vec2f(layout.gutter, 0.); + + // Elements are ephemeral, only at paint time do we know what could be clicked by a mouse + self.attach_mouse_handlers(origin, visible_bounds, layout.mode, cx); + + cx.scene().push_cursor_region(gpui::CursorRegion { + bounds, + style: if layout.hyperlink_tooltip.is_some() { + CursorStyle::PointingHand + } else { + CursorStyle::IBeam + }, + }); + + cx.paint_layer(clip_bounds, |cx| { + //Start with a background color + cx.scene().push_quad(Quad { + bounds: RectF::new(bounds.origin(), bounds.size()), + background: Some(layout.background_color), + border: Default::default(), + corner_radii: Default::default(), + }); + + for rect in &layout.rects { + rect.paint(origin, layout, view, cx); + } + }); + + //Draw Highlighted Backgrounds + cx.paint_layer(clip_bounds, |cx| { + for (relative_highlighted_range, color) in layout.relative_highlighted_ranges.iter() + { + if let Some((start_y, highlighted_range_lines)) = + to_highlighted_range_lines(relative_highlighted_range, layout, origin) + { + let hr = HighlightedRange { + start_y, //Need to change this + line_height: layout.size.line_height, + lines: highlighted_range_lines, + color: color.clone(), + //Copied from editor. TODO: move to theme or something + corner_radius: 0.15 * layout.size.line_height, + }; + hr.paint(bounds, cx); + } + } + }); + + //Draw the text cells + cx.paint_layer(clip_bounds, |cx| { + for cell in &layout.cells { + cell.paint(origin, layout, visible_bounds, view, cx); + } + }); + + //Draw cursor + if self.cursor_visible { + if let Some(cursor) = &layout.cursor { + cx.paint_layer(clip_bounds, |cx| { + cursor.paint(origin, cx); + }) + } + } + + if let Some(element) = &mut layout.hyperlink_tooltip { + element.paint(origin, visible_bounds, view, cx) + } + }); + } + + fn metadata(&self) -> Option<&dyn std::any::Any> { + None + } + + fn debug( + &self, + _: RectF, + _: &Self::LayoutState, + _: &Self::PaintState, + _: &TerminalView, + _: &gpui::ViewContext, + ) -> gpui::serde_json::Value { + json!({ + "type": "TerminalElement", + }) + } + + fn rect_for_text_range( + &self, + _: Range, + bounds: RectF, + _: RectF, + layout: &Self::LayoutState, + _: &Self::PaintState, + _: &TerminalView, + _: &gpui::ViewContext, + ) -> Option { + // Use the same origin that's passed to `Cursor::paint` in the paint + // method bove. + let mut origin = bounds.origin() + vec2f(layout.size.cell_width, 0.); + + // TODO - Why is it necessary to move downward one line to get correct + // positioning? I would think that we'd want the same rect that is + // painted for the cursor. + origin += vec2f(0., layout.size.line_height); + + Some(layout.cursor.as_ref()?.bounding_rect(origin)) + } +} + +fn is_blank(cell: &IndexedCell) -> bool { + if cell.c != ' ' { + return false; + } + + if cell.bg != AnsiColor::Named(NamedColor::Background) { + return false; + } + + if cell.hyperlink().is_some() { + return false; + } + + if cell + .flags + .intersects(Flags::ALL_UNDERLINES | Flags::INVERSE | Flags::STRIKEOUT) + { + return false; + } + + return true; +} + +fn to_highlighted_range_lines( + range: &RangeInclusive, + layout: &LayoutState, + origin: Vector2F, +) -> Option<(f32, Vec)> { + // Step 1. Normalize the points to be viewport relative. + // When display_offset = 1, here's how the grid is arranged: + //-2,0 -2,1... + //--- Viewport top + //-1,0 -1,1... + //--------- Terminal Top + // 0,0 0,1... + // 1,0 1,1... + //--- Viewport Bottom + // 2,0 2,1... + //--------- Terminal Bottom + + // Normalize to viewport relative, from terminal relative. + // lines are i32s, which are negative above the top left corner of the terminal + // If the user has scrolled, we use the display_offset to tell us which offset + // of the grid data we should be looking at. But for the rendering step, we don't + // want negatives. We want things relative to the 'viewport' (the area of the grid + // which is currently shown according to the display offset) + let unclamped_start = Point::new( + range.start().line + layout.display_offset, + range.start().column, + ); + let unclamped_end = Point::new(range.end().line + layout.display_offset, range.end().column); + + // Step 2. Clamp range to viewport, and return None if it doesn't overlap + if unclamped_end.line.0 < 0 || unclamped_start.line.0 > layout.size.num_lines() as i32 { + return None; + } + + let clamped_start_line = unclamped_start.line.0.max(0) as usize; + let clamped_end_line = unclamped_end.line.0.min(layout.size.num_lines() as i32) as usize; + //Convert the start of the range to pixels + let start_y = origin.y() + clamped_start_line as f32 * layout.size.line_height; + + // Step 3. Expand ranges that cross lines into a collection of single-line ranges. + // (also convert to pixels) + let mut highlighted_range_lines = Vec::new(); + for line in clamped_start_line..=clamped_end_line { + let mut line_start = 0; + let mut line_end = layout.size.columns(); + + if line == clamped_start_line { + line_start = unclamped_start.column.0 as usize; + } + if line == clamped_end_line { + line_end = unclamped_end.column.0 as usize + 1; //+1 for inclusive + } + + highlighted_range_lines.push(HighlightedRangeLine { + start_x: origin.x() + line_start as f32 * layout.size.cell_width, + end_x: origin.x() + line_end as f32 * layout.size.cell_width, + }); + } + + Some((start_y, highlighted_range_lines)) +} diff --git a/crates/terminal_view2/src/terminal_panel.rs b/crates/terminal_view2/src/terminal_panel.rs new file mode 100644 index 0000000000000000000000000000000000000000..0bfa84e7546968ccce18d9302af7012cf760eb4b --- /dev/null +++ b/crates/terminal_view2/src/terminal_panel.rs @@ -0,0 +1,460 @@ +use std::{path::PathBuf, sync::Arc}; + +use crate::TerminalView; +use db::kvp::KEY_VALUE_STORE; +use gpui::{ + actions, anyhow::Result, elements::*, serde_json, Action, AppContext, AsyncAppContext, Entity, + Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext, +}; +use project::Fs; +use serde::{Deserialize, Serialize}; +use settings::SettingsStore; +use terminal::terminal_settings::{TerminalDockPosition, TerminalSettings}; +use util::{ResultExt, TryFutureExt}; +use workspace::{ + dock::{DockPosition, Panel}, + item::Item, + pane, DraggedItem, Pane, Workspace, +}; + +const TERMINAL_PANEL_KEY: &'static str = "TerminalPanel"; + +actions!(terminal_panel, [ToggleFocus]); + +pub fn init(cx: &mut AppContext) { + cx.add_action(TerminalPanel::new_terminal); + cx.add_action(TerminalPanel::open_terminal); +} + +#[derive(Debug)] +pub enum Event { + Close, + DockPositionChanged, + ZoomIn, + ZoomOut, + Focus, +} + +pub struct TerminalPanel { + pane: ViewHandle, + fs: Arc, + workspace: WeakViewHandle, + width: Option, + height: Option, + pending_serialization: Task>, + _subscriptions: Vec, +} + +impl TerminalPanel { + fn new(workspace: &Workspace, cx: &mut ViewContext) -> Self { + let weak_self = cx.weak_handle(); + let pane = cx.add_view(|cx| { + let window = cx.window(); + let mut pane = Pane::new( + workspace.weak_handle(), + workspace.project().clone(), + workspace.app_state().background_actions, + Default::default(), + cx, + ); + pane.set_can_split(false, cx); + pane.set_can_navigate(false, cx); + pane.on_can_drop(move |drag_and_drop, cx| { + drag_and_drop + .currently_dragged::(window) + .map_or(false, |(_, item)| { + item.handle.act_as::(cx).is_some() + }) + }); + pane.set_render_tab_bar_buttons(cx, move |pane, cx| { + let this = weak_self.clone(); + Flex::row() + .with_child(Pane::render_tab_bar_button( + 0, + "icons/plus.svg", + false, + Some(("New Terminal", Some(Box::new(workspace::NewTerminal)))), + cx, + move |_, cx| { + let this = this.clone(); + cx.window_context().defer(move |cx| { + if let Some(this) = this.upgrade(cx) { + this.update(cx, |this, cx| { + this.add_terminal(None, cx); + }); + } + }) + }, + |_, _| {}, + None, + )) + .with_child(Pane::render_tab_bar_button( + 1, + if pane.is_zoomed() { + "icons/minimize.svg" + } else { + "icons/maximize.svg" + }, + pane.is_zoomed(), + Some(("Toggle Zoom".into(), Some(Box::new(workspace::ToggleZoom)))), + cx, + move |pane, cx| pane.toggle_zoom(&Default::default(), cx), + |_, _| {}, + None, + )) + .into_any() + }); + let buffer_search_bar = cx.add_view(search::BufferSearchBar::new); + pane.toolbar() + .update(cx, |toolbar, cx| toolbar.add_item(buffer_search_bar, cx)); + pane + }); + let subscriptions = vec![ + cx.observe(&pane, |_, _, cx| cx.notify()), + cx.subscribe(&pane, Self::handle_pane_event), + ]; + let this = Self { + pane, + fs: workspace.app_state().fs.clone(), + workspace: workspace.weak_handle(), + pending_serialization: Task::ready(None), + width: None, + height: None, + _subscriptions: subscriptions, + }; + let mut old_dock_position = this.position(cx); + cx.observe_global::(move |this, cx| { + let new_dock_position = this.position(cx); + if new_dock_position != old_dock_position { + old_dock_position = new_dock_position; + cx.emit(Event::DockPositionChanged); + } + }) + .detach(); + this + } + + pub fn load( + workspace: WeakViewHandle, + cx: AsyncAppContext, + ) -> Task>> { + cx.spawn(|mut cx| async move { + let serialized_panel = if let Some(panel) = cx + .background() + .spawn(async move { KEY_VALUE_STORE.read_kvp(TERMINAL_PANEL_KEY) }) + .await + .log_err() + .flatten() + { + Some(serde_json::from_str::(&panel)?) + } else { + None + }; + let (panel, pane, items) = workspace.update(&mut cx, |workspace, cx| { + let panel = cx.add_view(|cx| TerminalPanel::new(workspace, cx)); + let items = if let Some(serialized_panel) = serialized_panel.as_ref() { + panel.update(cx, |panel, cx| { + cx.notify(); + panel.height = serialized_panel.height; + panel.width = serialized_panel.width; + panel.pane.update(cx, |_, cx| { + serialized_panel + .items + .iter() + .map(|item_id| { + TerminalView::deserialize( + workspace.project().clone(), + workspace.weak_handle(), + workspace.database_id(), + *item_id, + cx, + ) + }) + .collect::>() + }) + }) + } else { + Default::default() + }; + let pane = panel.read(cx).pane.clone(); + (panel, pane, items) + })?; + + let pane = pane.downgrade(); + let items = futures::future::join_all(items).await; + pane.update(&mut cx, |pane, cx| { + let active_item_id = serialized_panel + .as_ref() + .and_then(|panel| panel.active_item_id); + let mut active_ix = None; + for item in items { + if let Some(item) = item.log_err() { + let item_id = item.id(); + pane.add_item(Box::new(item), false, false, None, cx); + if Some(item_id) == active_item_id { + active_ix = Some(pane.items_len() - 1); + } + } + } + + if let Some(active_ix) = active_ix { + pane.activate_item(active_ix, false, false, cx) + } + })?; + + Ok(panel) + }) + } + + fn handle_pane_event( + &mut self, + _pane: ViewHandle, + event: &pane::Event, + cx: &mut ViewContext, + ) { + match event { + pane::Event::ActivateItem { .. } => self.serialize(cx), + pane::Event::RemoveItem { .. } => self.serialize(cx), + pane::Event::Remove => cx.emit(Event::Close), + pane::Event::ZoomIn => cx.emit(Event::ZoomIn), + pane::Event::ZoomOut => cx.emit(Event::ZoomOut), + pane::Event::Focus => cx.emit(Event::Focus), + + pane::Event::AddItem { item } => { + if let Some(workspace) = self.workspace.upgrade(cx) { + let pane = self.pane.clone(); + workspace.update(cx, |workspace, cx| item.added_to_pane(workspace, pane, cx)) + } + } + + _ => {} + } + } + + pub fn open_terminal( + workspace: &mut Workspace, + action: &workspace::OpenTerminal, + cx: &mut ViewContext, + ) { + let Some(this) = workspace.focus_panel::(cx) else { + return; + }; + + this.update(cx, |this, cx| { + this.add_terminal(Some(action.working_directory.clone()), cx) + }) + } + + ///Create a new Terminal in the current working directory or the user's home directory + fn new_terminal( + workspace: &mut Workspace, + _: &workspace::NewTerminal, + cx: &mut ViewContext, + ) { + let Some(this) = workspace.focus_panel::(cx) else { + return; + }; + + this.update(cx, |this, cx| this.add_terminal(None, cx)) + } + + fn add_terminal(&mut self, working_directory: Option, cx: &mut ViewContext) { + let workspace = self.workspace.clone(); + cx.spawn(|this, mut cx| async move { + let pane = this.read_with(&cx, |this, _| this.pane.clone())?; + workspace.update(&mut cx, |workspace, cx| { + let working_directory = if let Some(working_directory) = working_directory { + Some(working_directory) + } else { + let working_directory_strategy = settings::get::(cx) + .working_directory + .clone(); + crate::get_working_directory(workspace, cx, working_directory_strategy) + }; + + let window = cx.window(); + if let Some(terminal) = workspace.project().update(cx, |project, cx| { + project + .create_terminal(working_directory, window, cx) + .log_err() + }) { + let terminal = Box::new(cx.add_view(|cx| { + TerminalView::new( + terminal, + workspace.weak_handle(), + workspace.database_id(), + cx, + ) + })); + pane.update(cx, |pane, cx| { + let focus = pane.has_focus(); + pane.add_item(terminal, true, focus, None, cx); + }); + } + })?; + this.update(&mut cx, |this, cx| this.serialize(cx))?; + anyhow::Ok(()) + }) + .detach_and_log_err(cx); + } + + fn serialize(&mut self, cx: &mut ViewContext) { + let items = self + .pane + .read(cx) + .items() + .map(|item| item.id()) + .collect::>(); + let active_item_id = self.pane.read(cx).active_item().map(|item| item.id()); + let height = self.height; + let width = self.width; + self.pending_serialization = cx.background().spawn( + async move { + KEY_VALUE_STORE + .write_kvp( + TERMINAL_PANEL_KEY.into(), + serde_json::to_string(&SerializedTerminalPanel { + items, + active_item_id, + height, + width, + })?, + ) + .await?; + anyhow::Ok(()) + } + .log_err(), + ); + } +} + +impl Entity for TerminalPanel { + type Event = Event; +} + +impl View for TerminalPanel { + fn ui_name() -> &'static str { + "TerminalPanel" + } + + fn render(&mut self, cx: &mut ViewContext) -> gpui::AnyElement { + ChildView::new(&self.pane, cx).into_any() + } + + fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext) { + if cx.is_self_focused() { + cx.focus(&self.pane); + } + } +} + +impl Panel for TerminalPanel { + fn position(&self, cx: &WindowContext) -> DockPosition { + match settings::get::(cx).dock { + TerminalDockPosition::Left => DockPosition::Left, + TerminalDockPosition::Bottom => DockPosition::Bottom, + TerminalDockPosition::Right => DockPosition::Right, + } + } + + fn position_is_valid(&self, _: DockPosition) -> bool { + true + } + + fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext) { + settings::update_settings_file::(self.fs.clone(), cx, move |settings| { + let dock = match position { + DockPosition::Left => TerminalDockPosition::Left, + DockPosition::Bottom => TerminalDockPosition::Bottom, + DockPosition::Right => TerminalDockPosition::Right, + }; + settings.dock = Some(dock); + }); + } + + fn size(&self, cx: &WindowContext) -> f32 { + let settings = settings::get::(cx); + match self.position(cx) { + DockPosition::Left | DockPosition::Right => { + self.width.unwrap_or_else(|| settings.default_width) + } + DockPosition::Bottom => self.height.unwrap_or_else(|| settings.default_height), + } + } + + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + match self.position(cx) { + DockPosition::Left | DockPosition::Right => self.width = size, + DockPosition::Bottom => self.height = size, + } + self.serialize(cx); + cx.notify(); + } + + fn should_zoom_in_on_event(event: &Event) -> bool { + matches!(event, Event::ZoomIn) + } + + fn should_zoom_out_on_event(event: &Event) -> bool { + matches!(event, Event::ZoomOut) + } + + fn is_zoomed(&self, cx: &WindowContext) -> bool { + self.pane.read(cx).is_zoomed() + } + + fn set_zoomed(&mut self, zoomed: bool, cx: &mut ViewContext) { + self.pane.update(cx, |pane, cx| pane.set_zoomed(zoomed, cx)); + } + + fn set_active(&mut self, active: bool, cx: &mut ViewContext) { + if active && self.pane.read(cx).items_len() == 0 { + self.add_terminal(None, cx) + } + } + + fn icon_path(&self, _: &WindowContext) -> Option<&'static str> { + Some("icons/terminal.svg") + } + + fn icon_tooltip(&self) -> (String, Option>) { + ("Terminal Panel".into(), Some(Box::new(ToggleFocus))) + } + + fn icon_label(&self, cx: &WindowContext) -> Option { + let count = self.pane.read(cx).items_len(); + if count == 0 { + None + } else { + Some(count.to_string()) + } + } + + fn should_change_position_on_event(event: &Self::Event) -> bool { + matches!(event, Event::DockPositionChanged) + } + + fn should_activate_on_event(_: &Self::Event) -> bool { + false + } + + fn should_close_on_event(event: &Event) -> bool { + matches!(event, Event::Close) + } + + fn has_focus(&self, cx: &WindowContext) -> bool { + self.pane.read(cx).has_focus() + } + + fn is_focus_event(event: &Self::Event) -> bool { + matches!(event, Event::Focus) + } +} + +#[derive(Serialize, Deserialize)] +struct SerializedTerminalPanel { + items: Vec, + active_item_id: Option, + width: Option, + height: Option, +} diff --git a/crates/terminal_view2/src/terminal_view.rs b/crates/terminal_view2/src/terminal_view.rs new file mode 100644 index 0000000000000000000000000000000000000000..74954ad5c8d9c0a8964f79d97af1210678c2053a --- /dev/null +++ b/crates/terminal_view2/src/terminal_view.rs @@ -0,0 +1,1165 @@ +#![allow(unused_variables)] +//todo!(remove) + +// mod persistence; +pub mod terminal_element; +pub mod terminal_panel; + +use crate::terminal_element::TerminalElement; +use anyhow::Context; +use dirs::home_dir; +use editor::{scroll::autoscroll::Autoscroll, Editor}; +use gpui::{ + actions, div, img, red, register_action, AnyElement, AppContext, Component, Div, EventEmitter, + FocusEvent, FocusHandle, Focusable, FocusableKeyDispatch, InputHandler, KeyDownEvent, + Keystroke, Model, ParentElement, Pixels, Render, StatefulInteractivity, StatelessInteractive, + Styled, Task, View, ViewContext, VisualContext, WeakView, +}; +use language::Bias; +use project::{search::SearchQuery, LocalWorktree, Project}; +use serde::Deserialize; +use settings::Settings; +use smol::Timer; +use std::{ + borrow::Cow, + ops::RangeInclusive, + path::{Path, PathBuf}, + sync::Arc, + time::Duration, +}; +use terminal::{ + alacritty_terminal::{ + index::Point, + term::{search::RegexSearch, TermMode}, + }, + terminal_settings::{TerminalBlink, TerminalSettings, WorkingDirectory}, + Event, MaybeNavigationTarget, Terminal, +}; +use util::{paths::PathLikeWithPosition, ResultExt}; +use workspace::{ + item::{BreadcrumbText, Item, ItemEvent}, + notifications::NotifyResultExt, + register_deserializable_item, + searchable::{SearchEvent, SearchOptions, SearchableItem, SearchableItemHandle}, + NewCenterTerminal, Pane, ToolbarItemLocation, Workspace, WorkspaceId, +}; + +const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500); + +///Event to transmit the scroll from the element to the view +#[derive(Clone, Debug, PartialEq)] +pub struct ScrollTerminal(pub i32); + +#[register_action] +#[derive(Clone, Default, Deserialize, PartialEq)] +pub struct SendText(String); + +#[register_action] +#[derive(Clone, Default, Deserialize, PartialEq)] +pub struct SendKeystroke(String); + +actions!(Clear, Copy, Paste, ShowCharacterPalette, SearchTest); + +pub fn init(cx: &mut AppContext) { + terminal_panel::init(cx); + terminal::init(cx); + + register_deserializable_item::(cx); + + cx.observe_new_views( + |workspace: &mut Workspace, cx: &mut ViewContext| { + workspace.register_action(TerminalView::deploy) + }, + ) + .detach(); +} + +///A terminal view, maintains the PTY's file handles and communicates with the terminal +pub struct TerminalView { + terminal: Model, + focus_handle: FocusHandle, + has_new_content: bool, + //Currently using iTerm bell, show bell emoji in tab until input is received + has_bell: bool, + // context_menu: View, + blink_state: bool, + blinking_on: bool, + blinking_paused: bool, + blink_epoch: usize, + can_navigate_to_selected_word: bool, + workspace_id: WorkspaceId, +} + +impl EventEmitter for TerminalView {} +impl EventEmitter for TerminalView {} +impl EventEmitter for TerminalView {} + +impl TerminalView { + ///Create a new Terminal in the current working directory or the user's home directory + pub fn deploy( + workspace: &mut Workspace, + _: &NewCenterTerminal, + cx: &mut ViewContext, + ) { + let strategy = TerminalSettings::get_global(cx); + let working_directory = + get_working_directory(workspace, cx, strategy.working_directory.clone()); + + let window = cx.window_handle(); + let terminal = workspace + .project() + .update(cx, |project, cx| { + project.create_terminal(working_directory, window, cx) + }) + .notify_err(workspace, cx); + + if let Some(terminal) = terminal { + let view = cx.build_view(|cx| { + TerminalView::new( + terminal, + workspace.weak_handle(), + workspace.database_id(), + cx, + ) + }); + workspace.add_item(Box::new(view), cx) + } + } + + pub fn new( + terminal: Model, + workspace: WeakView, + workspace_id: WorkspaceId, + cx: &mut ViewContext, + ) -> Self { + let view_id = cx.entity_id(); + cx.observe(&terminal, |_, _, cx| cx.notify()).detach(); + cx.subscribe(&terminal, move |this, _, event, cx| match event { + Event::Wakeup => { + if !this.focus_handle.is_focused(cx) { + this.has_new_content = true; + } + cx.notify(); + cx.emit(Event::Wakeup); + cx.emit(ItemEvent::UpdateTab); + cx.emit(SearchEvent::MatchesInvalidated); + } + + Event::Bell => { + this.has_bell = true; + cx.emit(Event::Wakeup); + } + + Event::BlinkChanged => this.blinking_on = !this.blinking_on, + + Event::TitleChanged => { + cx.emit(ItemEvent::UpdateTab); + if let Some(foreground_info) = &this.terminal().read(cx).foreground_process_info { + let cwd = foreground_info.cwd.clone(); + + let item_id = cx.entity_id(); + let workspace_id = this.workspace_id; + // todo!(persistence) + // cx.background_executor() + // .spawn(async move { + // TERMINAL_DB + // .save_working_directory(item_id, workspace_id, cwd) + // .await + // .log_err(); + // }) + // .detach(); + } + } + + Event::NewNavigationTarget(maybe_navigation_target) => { + this.can_navigate_to_selected_word = match maybe_navigation_target { + Some(MaybeNavigationTarget::Url(_)) => true, + Some(MaybeNavigationTarget::PathLike(maybe_path)) => { + !possible_open_targets(&workspace, maybe_path, cx).is_empty() + } + None => false, + } + } + + Event::Open(maybe_navigation_target) => match maybe_navigation_target { + MaybeNavigationTarget::Url(url) => cx.open_url(url), + + MaybeNavigationTarget::PathLike(maybe_path) => { + if !this.can_navigate_to_selected_word { + return; + } + let potential_abs_paths = possible_open_targets(&workspace, maybe_path, cx); + if let Some(path) = potential_abs_paths.into_iter().next() { + let is_dir = path.path_like.is_dir(); + let task_workspace = workspace.clone(); + cx.spawn(|_, mut cx| async move { + let opened_items = task_workspace + .update(&mut cx, |workspace, cx| { + workspace.open_paths(vec![path.path_like], is_dir, cx) + }) + .context("workspace update")? + .await; + anyhow::ensure!( + opened_items.len() == 1, + "For a single path open, expected single opened item" + ); + let opened_item = opened_items + .into_iter() + .next() + .unwrap() + .transpose() + .context("path open")?; + if is_dir { + task_workspace.update(&mut cx, |workspace, cx| { + workspace.project().update(cx, |_, cx| { + cx.emit(project::Event::ActivateProjectPanel); + }) + })?; + } else { + if let Some(row) = path.row { + let col = path.column.unwrap_or(0); + if let Some(active_editor) = + opened_item.and_then(|item| item.downcast::()) + { + active_editor + .downgrade() + .update(&mut cx, |editor, cx| { + let snapshot = editor.snapshot(cx).display_snapshot; + let point = snapshot.buffer_snapshot.clip_point( + language::Point::new( + row.saturating_sub(1), + col.saturating_sub(1), + ), + Bias::Left, + ); + editor.change_selections( + Some(Autoscroll::center()), + cx, + |s| s.select_ranges([point..point]), + ); + }) + .log_err(); + } + } + } + anyhow::Ok(()) + }) + .detach_and_log_err(cx); + } + } + }, + Event::BreadcrumbsChanged => cx.emit(ItemEvent::UpdateBreadcrumbs), + Event::CloseTerminal => cx.emit(ItemEvent::CloseItem), + Event::SelectionsChanged => cx.emit(SearchEvent::ActiveMatchChanged), + }) + .detach(); + + Self { + terminal, + has_new_content: true, + has_bell: false, + focus_handle: cx.focus_handle(), + // todo!() + // context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)), + blink_state: true, + blinking_on: false, + blinking_paused: false, + blink_epoch: 0, + can_navigate_to_selected_word: false, + workspace_id, + } + } + + pub fn model(&self) -> &Model { + &self.terminal + } + + pub fn has_new_content(&self) -> bool { + self.has_new_content + } + + pub fn has_bell(&self) -> bool { + self.has_bell + } + + pub fn clear_bel(&mut self, cx: &mut ViewContext) { + self.has_bell = false; + cx.emit(Event::Wakeup); + } + + pub fn deploy_context_menu(&mut self, _position: Point, _cx: &mut ViewContext) { + //todo!(context_menu) + // let menu_entries = vec![ + // ContextMenuItem::action("Clear", Clear), + // ContextMenuItem::action("Close", pane::CloseActiveItem { save_intent: None }), + // ]; + + // self.context_menu.update(cx, |menu, cx| { + // menu.show(position, AnchorCorner::TopLeft, menu_entries, cx) + // }); + + // cx.notify(); + } + + fn show_character_palette(&mut self, _: &ShowCharacterPalette, cx: &mut ViewContext) { + if !self + .terminal + .read(cx) + .last_content + .mode + .contains(TermMode::ALT_SCREEN) + { + cx.show_character_palette(); + } else { + self.terminal.update(cx, |term, cx| { + term.try_keystroke( + &Keystroke::parse("ctrl-cmd-space").unwrap(), + TerminalSettings::get_global(cx).option_as_meta, + ) + }); + } + } + + fn select_all(&mut self, _: &editor::SelectAll, cx: &mut ViewContext) { + self.terminal.update(cx, |term, _| term.select_all()); + cx.notify(); + } + + fn clear(&mut self, _: &Clear, cx: &mut ViewContext) { + self.terminal.update(cx, |term, _| term.clear()); + cx.notify(); + } + + pub fn should_show_cursor(&self, focused: bool, cx: &mut gpui::ViewContext) -> bool { + //Don't blink the cursor when not focused, blinking is disabled, or paused + if !focused + || !self.blinking_on + || self.blinking_paused + || self + .terminal + .read(cx) + .last_content + .mode + .contains(TermMode::ALT_SCREEN) + { + return true; + } + + match TerminalSettings::get_global(cx).blinking { + //If the user requested to never blink, don't blink it. + TerminalBlink::Off => true, + //If the terminal is controlling it, check terminal mode + TerminalBlink::TerminalControlled | TerminalBlink::On => self.blink_state, + } + } + + fn blink_cursors(&mut self, epoch: usize, cx: &mut ViewContext) { + if epoch == self.blink_epoch && !self.blinking_paused { + self.blink_state = !self.blink_state; + cx.notify(); + + let epoch = self.next_blink_epoch(); + cx.spawn(|this, mut cx| async move { + Timer::after(CURSOR_BLINK_INTERVAL).await; + this.update(&mut cx, |this, cx| this.blink_cursors(epoch, cx)) + .log_err(); + }) + .detach(); + } + } + + pub fn pause_cursor_blinking(&mut self, cx: &mut ViewContext) { + self.blink_state = true; + cx.notify(); + + let epoch = self.next_blink_epoch(); + cx.spawn(|this, mut cx| async move { + Timer::after(CURSOR_BLINK_INTERVAL).await; + this.update(&mut cx, |this, cx| this.resume_cursor_blinking(epoch, cx)) + .ok(); + }) + .detach(); + } + + pub fn find_matches( + &mut self, + query: Arc, + cx: &mut ViewContext, + ) -> Task>> { + let searcher = regex_search_for_query(&query); + + if let Some(searcher) = searcher { + self.terminal + .update(cx, |term, cx| term.find_matches(searcher, cx)) + } else { + cx.background_executor().spawn(async { Vec::new() }) + } + } + + pub fn terminal(&self) -> &Model { + &self.terminal + } + + fn next_blink_epoch(&mut self) -> usize { + self.blink_epoch += 1; + self.blink_epoch + } + + fn resume_cursor_blinking(&mut self, epoch: usize, cx: &mut ViewContext) { + if epoch == self.blink_epoch { + self.blinking_paused = false; + self.blink_cursors(epoch, cx); + } + } + + ///Attempt to paste the clipboard into the terminal + fn copy(&mut self, _: &Copy, cx: &mut ViewContext) { + self.terminal.update(cx, |term, _| term.copy()) + } + + ///Attempt to paste the clipboard into the terminal + fn paste(&mut self, _: &Paste, cx: &mut ViewContext) { + if let Some(item) = cx.read_from_clipboard() { + self.terminal + .update(cx, |terminal, _cx| terminal.paste(item.text())); + } + } + + fn send_text(&mut self, text: &SendText, cx: &mut ViewContext) { + self.clear_bel(cx); + self.terminal.update(cx, |term, _| { + term.input(text.0.to_string()); + }); + } + + fn send_keystroke(&mut self, text: &SendKeystroke, cx: &mut ViewContext) { + if let Some(keystroke) = Keystroke::parse(&text.0).log_err() { + self.clear_bel(cx); + self.terminal.update(cx, |term, cx| { + term.try_keystroke(&keystroke, TerminalSettings::get_global(cx).option_as_meta); + }); + } + } +} + +fn possible_open_targets( + workspace: &WeakView, + maybe_path: &String, + cx: &mut ViewContext<'_, TerminalView>, +) -> Vec> { + let path_like = PathLikeWithPosition::parse_str(maybe_path.as_str(), |path_str| { + Ok::<_, std::convert::Infallible>(Path::new(path_str).to_path_buf()) + }) + .expect("infallible"); + let maybe_path = path_like.path_like; + let potential_abs_paths = if maybe_path.is_absolute() { + vec![maybe_path] + } else if maybe_path.starts_with("~") { + if let Some(abs_path) = maybe_path + .strip_prefix("~") + .ok() + .and_then(|maybe_path| Some(dirs::home_dir()?.join(maybe_path))) + { + vec![abs_path] + } else { + Vec::new() + } + } else if let Some(workspace) = workspace.upgrade() { + workspace.update(cx, |workspace, cx| { + workspace + .worktrees(cx) + .map(|worktree| worktree.read(cx).abs_path().join(&maybe_path)) + .collect() + }) + } else { + Vec::new() + }; + + potential_abs_paths + .into_iter() + .filter(|path| path.exists()) + .map(|path| PathLikeWithPosition { + path_like: path, + row: path_like.row, + column: path_like.column, + }) + .collect() +} + +pub fn regex_search_for_query(query: &project::search::SearchQuery) -> Option { + let query = query.as_str(); + let searcher = RegexSearch::new(&query); + searcher.ok() +} + +impl TerminalView { + fn key_down(&mut self, event: &KeyDownEvent, cx: &mut ViewContext) -> bool { + self.clear_bel(cx); + self.pause_cursor_blinking(cx); + + self.terminal.update(cx, |term, cx| { + term.try_keystroke( + &event.keystroke, + TerminalSettings::get_global(cx).option_as_meta, + ) + }) + } + + fn focus_in(&mut self, event: &FocusEvent, cx: &mut ViewContext) { + self.has_new_content = false; + self.terminal.read(cx).focus_in(); + self.blink_cursors(self.blink_epoch, cx); + cx.notify(); + } + + fn focus_out(&mut self, event: &FocusEvent, cx: &mut ViewContext) { + self.terminal.update(cx, |terminal, _| { + terminal.focus_out(); + }); + cx.notify(); + } +} + +impl Render for TerminalView { + type Element = Div, FocusableKeyDispatch>; + + fn render(&mut self, cx: &mut ViewContext) -> Self::Element { + let terminal_handle = self.terminal.clone().downgrade(); + + let self_id = cx.entity_id(); + let focused = self.focus_handle.is_focused(cx); + + div() + .track_focus(&self.focus_handle) + .on_focus_in(Self::focus_out) + .on_focus_out(Self::focus_out) + .on_key_down(Self::key_down) + .on_action(TerminalView::send_text) + .on_action(TerminalView::send_keystroke) + .on_action(TerminalView::copy) + .on_action(TerminalView::paste) + .on_action(TerminalView::clear) + .on_action(TerminalView::show_character_palette) + .on_action(TerminalView::select_all) + .child(TerminalElement::new( + terminal_handle, + focused, + self.should_show_cursor(focused, cx), + self.can_navigate_to_selected_word, + )) + // todo!() + // .child(ChildView::new(&self.context_menu, cx)) + } +} + +// impl View for TerminalView { +//todo!() +// fn modifiers_changed( +// &mut self, +// event: &ModifiersChangedEvent, +// cx: &mut ViewContext, +// ) -> bool { +// let handled = self +// .terminal() +// .update(cx, |term, _| term.try_modifiers_change(&event.modifiers)); +// if handled { +// cx.notify(); +// } +// handled +// } +// } + +// todo!() +// fn update_keymap_context(&self, keymap: &mut KeymapContext, cx: &gpui::AppContext) { +// Self::reset_to_default_keymap_context(keymap); + +// let mode = self.terminal.read(cx).last_content.mode; +// keymap.add_key( +// "screen", +// if mode.contains(TermMode::ALT_SCREEN) { +// "alt" +// } else { +// "normal" +// }, +// ); + +// if mode.contains(TermMode::APP_CURSOR) { +// keymap.add_identifier("DECCKM"); +// } +// if mode.contains(TermMode::APP_KEYPAD) { +// keymap.add_identifier("DECPAM"); +// } else { +// keymap.add_identifier("DECPNM"); +// } +// if mode.contains(TermMode::SHOW_CURSOR) { +// keymap.add_identifier("DECTCEM"); +// } +// if mode.contains(TermMode::LINE_WRAP) { +// keymap.add_identifier("DECAWM"); +// } +// if mode.contains(TermMode::ORIGIN) { +// keymap.add_identifier("DECOM"); +// } +// if mode.contains(TermMode::INSERT) { +// keymap.add_identifier("IRM"); +// } +// //LNM is apparently the name for this. https://vt100.net/docs/vt510-rm/LNM.html +// if mode.contains(TermMode::LINE_FEED_NEW_LINE) { +// keymap.add_identifier("LNM"); +// } +// if mode.contains(TermMode::FOCUS_IN_OUT) { +// keymap.add_identifier("report_focus"); +// } +// if mode.contains(TermMode::ALTERNATE_SCROLL) { +// keymap.add_identifier("alternate_scroll"); +// } +// if mode.contains(TermMode::BRACKETED_PASTE) { +// keymap.add_identifier("bracketed_paste"); +// } +// if mode.intersects(TermMode::MOUSE_MODE) { +// keymap.add_identifier("any_mouse_reporting"); +// } +// { +// let mouse_reporting = if mode.contains(TermMode::MOUSE_REPORT_CLICK) { +// "click" +// } else if mode.contains(TermMode::MOUSE_DRAG) { +// "drag" +// } else if mode.contains(TermMode::MOUSE_MOTION) { +// "motion" +// } else { +// "off" +// }; +// keymap.add_key("mouse_reporting", mouse_reporting); +// } +// { +// let format = if mode.contains(TermMode::SGR_MOUSE) { +// "sgr" +// } else if mode.contains(TermMode::UTF8_MOUSE) { +// "utf8" +// } else { +// "normal" +// }; +// keymap.add_key("mouse_format", format); +// } +// } + +impl InputHandler for TerminalView { + fn text_for_range( + &mut self, + range: std::ops::Range, + cx: &mut ViewContext, + ) -> Option { + todo!() + } + + fn selected_text_range(&self, cx: &AppContext) -> Option> { + if self + .terminal + .read(cx) + .last_content + .mode + .contains(TermMode::ALT_SCREEN) + { + None + } else { + Some(0..0) + } + } + + fn marked_text_range(&self, cx: &mut ViewContext) -> Option> { + todo!() + } + + fn unmark_text(&mut self, cx: &mut ViewContext) { + todo!() + } + + fn replace_text_in_range( + &mut self, + _: Option>, + text: &str, + cx: &mut ViewContext, + ) { + self.terminal.update(cx, |terminal, _| { + terminal.input(text.into()); + }); + } + + fn replace_and_mark_text_in_range( + &mut self, + range: Option>, + new_text: &str, + new_selected_range: Option>, + cx: &mut ViewContext, + ) { + todo!() + } + + fn bounds_for_range( + &mut self, + range_utf16: std::ops::Range, + element_bounds: gpui::Bounds, + cx: &mut ViewContext, + ) -> Option> { + todo!() + } +} + +impl Item for TerminalView { + fn tab_tooltip_text(&self, cx: &AppContext) -> Option> { + Some(self.terminal().read(cx).title().into()) + } + + fn tab_content( + &self, + _detail: Option, + cx: &gpui::AppContext, + ) -> AnyElement { + let title = self.terminal().read(cx).title(); + + div() + .child(img().uri("icons/terminal.svg").bg(red())) + .child(title) + .render() + } + + fn clone_on_split( + &self, + _workspace_id: WorkspaceId, + _cx: &mut ViewContext, + ) -> Option { + //From what I can tell, there's no way to tell the current working + //Directory of the terminal from outside the shell. There might be + //solutions to this, but they are non-trivial and require more IPC + + // Some(TerminalContainer::new( + // Err(anyhow::anyhow!("failed to instantiate terminal")), + // workspace_id, + // cx, + // )) + + // TODO + None + } + + fn is_dirty(&self, _cx: &gpui::AppContext) -> bool { + self.has_bell() + } + + fn has_conflict(&self, _cx: &AppContext) -> bool { + false + } + + // todo!() + // fn as_searchable(&self, handle: &View) -> Option> { + // Some(Box::new(handle.clone())) + // } + + fn breadcrumb_location(&self) -> ToolbarItemLocation { + ToolbarItemLocation::PrimaryLeft { flex: None } + } + + fn breadcrumbs(&self, _: &theme::Theme, cx: &AppContext) -> Option> { + Some(vec![BreadcrumbText { + text: self.terminal().read(cx).breadcrumb_text.clone(), + highlights: None, + }]) + } + + fn serialized_item_kind() -> Option<&'static str> { + Some("Terminal") + } + + fn deserialize( + project: Model, + workspace: WeakView, + workspace_id: workspace::WorkspaceId, + item_id: workspace::ItemId, + cx: &mut ViewContext, + ) -> Task>> { + let window = cx.window_handle(); + cx.spawn(|pane, mut cx| async move { + let cwd = None; + // todo!() + // TERMINAL_DB + // .get_working_directory(item_id, workspace_id) + // .log_err() + // .flatten() + // .or_else(|| { + // cx.read(|cx| { + // let strategy = TerminalSettings::get_global(cx).working_directory.clone(); + // workspace + // .upgrade(cx) + // .map(|workspace| { + // get_working_directory(workspace.read(cx), cx, strategy) + // }) + // .flatten() + // }) + // }); + + let terminal = project.update(&mut cx, |project, cx| { + project.create_terminal(cwd, window, cx) + })??; + pane.update(&mut cx, |_, cx| { + cx.build_view(|cx| TerminalView::new(terminal, workspace, workspace_id, cx)) + }) + }) + } + + fn added_to_workspace(&mut self, workspace: &mut Workspace, cx: &mut ViewContext) { + // todo!() + // cx.background() + // .spawn(TERMINAL_DB.update_workspace_id( + // workspace.database_id(), + // self.workspace_id, + // cx.view_id(), + // )) + // .detach(); + self.workspace_id = workspace.database_id(); + } +} + +impl SearchableItem for TerminalView { + type Match = RangeInclusive; + + fn supported_options() -> SearchOptions { + SearchOptions { + case: false, + word: false, + regex: false, + replacement: false, + } + } + + /// Clear stored matches + fn clear_matches(&mut self, cx: &mut ViewContext) { + self.terminal().update(cx, |term, _| term.matches.clear()) + } + + /// Store matches returned from find_matches somewhere for rendering + fn update_matches(&mut self, matches: Vec, cx: &mut ViewContext) { + self.terminal().update(cx, |term, _| term.matches = matches) + } + + /// Return the selection content to pre-load into this search + fn query_suggestion(&mut self, cx: &mut ViewContext) -> String { + self.terminal() + .read(cx) + .last_content + .selection_text + .clone() + .unwrap_or_default() + } + + /// Focus match at given index into the Vec of matches + fn activate_match(&mut self, index: usize, _: Vec, cx: &mut ViewContext) { + self.terminal() + .update(cx, |term, _| term.activate_match(index)); + cx.notify(); + } + + /// Add selections for all matches given. + fn select_matches(&mut self, matches: Vec, cx: &mut ViewContext) { + self.terminal() + .update(cx, |term, _| term.select_matches(matches)); + cx.notify(); + } + + /// Get all of the matches for this query, should be done on the background + fn find_matches( + &mut self, + query: Arc, + cx: &mut ViewContext, + ) -> Task> { + if let Some(searcher) = regex_search_for_query(&query) { + self.terminal() + .update(cx, |term, cx| term.find_matches(searcher, cx)) + } else { + Task::ready(vec![]) + } + } + + /// Reports back to the search toolbar what the active match should be (the selection) + fn active_match_index( + &mut self, + matches: Vec, + cx: &mut ViewContext, + ) -> Option { + // Selection head might have a value if there's a selection that isn't + // associated with a match. Therefore, if there are no matches, we should + // report None, no matter the state of the terminal + let res = if matches.len() > 0 { + if let Some(selection_head) = self.terminal().read(cx).selection_head { + // If selection head is contained in a match. Return that match + if let Some(ix) = matches + .iter() + .enumerate() + .find(|(_, search_match)| { + search_match.contains(&selection_head) + || search_match.start() > &selection_head + }) + .map(|(ix, _)| ix) + { + Some(ix) + } else { + // If no selection after selection head, return the last match + Some(matches.len().saturating_sub(1)) + } + } else { + // Matches found but no active selection, return the first last one (closest to cursor) + Some(matches.len().saturating_sub(1)) + } + } else { + None + }; + + res + } + fn replace(&mut self, _: &Self::Match, _: &SearchQuery, _: &mut ViewContext) { + // Replacement is not supported in terminal view, so this is a no-op. + } +} + +///Get's the working directory for the given workspace, respecting the user's settings. +pub fn get_working_directory( + workspace: &Workspace, + cx: &AppContext, + strategy: WorkingDirectory, +) -> Option { + let res = match strategy { + WorkingDirectory::CurrentProjectDirectory => current_project_directory(workspace, cx) + .or_else(|| first_project_directory(workspace, cx)), + WorkingDirectory::FirstProjectDirectory => first_project_directory(workspace, cx), + WorkingDirectory::AlwaysHome => None, + WorkingDirectory::Always { directory } => { + shellexpand::full(&directory) //TODO handle this better + .ok() + .map(|dir| Path::new(&dir.to_string()).to_path_buf()) + .filter(|dir| dir.is_dir()) + } + }; + res.or_else(home_dir) +} + +///Get's the first project's home directory, or the home directory +fn first_project_directory(workspace: &Workspace, cx: &AppContext) -> Option { + workspace + .worktrees(cx) + .next() + .and_then(|worktree_handle| worktree_handle.read(cx).as_local()) + .and_then(get_path_from_wt) +} + +///Gets the intuitively correct working directory from the given workspace +///If there is an active entry for this project, returns that entry's worktree root. +///If there's no active entry but there is a worktree, returns that worktrees root. +///If either of these roots are files, or if there are any other query failures, +/// returns the user's home directory +fn current_project_directory(workspace: &Workspace, cx: &AppContext) -> Option { + let project = workspace.project().read(cx); + + project + .active_entry() + .and_then(|entry_id| project.worktree_for_entry(entry_id, cx)) + .or_else(|| workspace.worktrees(cx).next()) + .and_then(|worktree_handle| worktree_handle.read(cx).as_local()) + .and_then(get_path_from_wt) +} + +fn get_path_from_wt(wt: &LocalWorktree) -> Option { + wt.root_entry() + .filter(|re| re.is_dir()) + .map(|_| wt.abs_path().to_path_buf()) +} + +#[cfg(test)] +mod tests { + use super::*; + use gpui::TestAppContext; + use project::{Entry, Project, ProjectPath, Worktree}; + use std::path::Path; + use workspace::AppState; + + // Working directory calculation tests + + // No Worktrees in project -> home_dir() + #[gpui::test] + async fn no_worktree(cx: &mut TestAppContext) { + let (project, workspace) = init_test(cx).await; + cx.read(|cx| { + let workspace = workspace.read(cx); + let active_entry = project.read(cx).active_entry(); + + //Make sure environment is as expected + assert!(active_entry.is_none()); + assert!(workspace.worktrees(cx).next().is_none()); + + let res = current_project_directory(workspace, cx); + assert_eq!(res, None); + let res = first_project_directory(workspace, cx); + assert_eq!(res, None); + }); + } + + // No active entry, but a worktree, worktree is a file -> home_dir() + #[gpui::test] + async fn no_active_entry_worktree_is_file(cx: &mut TestAppContext) { + let (project, workspace) = init_test(cx).await; + + create_file_wt(project.clone(), "/root.txt", cx).await; + cx.read(|cx| { + let workspace = workspace.read(cx); + let active_entry = project.read(cx).active_entry(); + + //Make sure environment is as expected + assert!(active_entry.is_none()); + assert!(workspace.worktrees(cx).next().is_some()); + + let res = current_project_directory(workspace, cx); + assert_eq!(res, None); + let res = first_project_directory(workspace, cx); + assert_eq!(res, None); + }); + } + + // No active entry, but a worktree, worktree is a folder -> worktree_folder + #[gpui::test] + async fn no_active_entry_worktree_is_dir(cx: &mut TestAppContext) { + let (project, workspace) = init_test(cx).await; + + let (_wt, _entry) = create_folder_wt(project.clone(), "/root/", cx).await; + cx.update(|cx| { + let workspace = workspace.read(cx); + let active_entry = project.read(cx).active_entry(); + + assert!(active_entry.is_none()); + assert!(workspace.worktrees(cx).next().is_some()); + + let res = current_project_directory(workspace, cx); + assert_eq!(res, Some((Path::new("/root/")).to_path_buf())); + let res = first_project_directory(workspace, cx); + assert_eq!(res, Some((Path::new("/root/")).to_path_buf())); + }); + } + + // Active entry with a work tree, worktree is a file -> home_dir() + #[gpui::test] + async fn active_entry_worktree_is_file(cx: &mut TestAppContext) { + let (project, workspace) = init_test(cx).await; + + let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await; + let (wt2, entry2) = create_file_wt(project.clone(), "/root2.txt", cx).await; + insert_active_entry_for(wt2, entry2, project.clone(), cx); + + cx.update(|cx| { + let workspace = workspace.read(cx); + let active_entry = project.read(cx).active_entry(); + + assert!(active_entry.is_some()); + + let res = current_project_directory(workspace, cx); + assert_eq!(res, None); + let res = first_project_directory(workspace, cx); + assert_eq!(res, Some((Path::new("/root1/")).to_path_buf())); + }); + } + + // Active entry, with a worktree, worktree is a folder -> worktree_folder + #[gpui::test] + async fn active_entry_worktree_is_dir(cx: &mut TestAppContext) { + let (project, workspace) = init_test(cx).await; + + let (_wt, _entry) = create_folder_wt(project.clone(), "/root1/", cx).await; + let (wt2, entry2) = create_folder_wt(project.clone(), "/root2/", cx).await; + insert_active_entry_for(wt2, entry2, project.clone(), cx); + + cx.update(|cx| { + let workspace = workspace.read(cx); + let active_entry = project.read(cx).active_entry(); + + assert!(active_entry.is_some()); + + let res = current_project_directory(workspace, cx); + assert_eq!(res, Some((Path::new("/root2/")).to_path_buf())); + let res = first_project_directory(workspace, cx); + assert_eq!(res, Some((Path::new("/root1/")).to_path_buf())); + }); + } + + /// Creates a worktree with 1 file: /root.txt + pub async fn init_test(cx: &mut TestAppContext) -> (Model, View) { + let params = cx.update(AppState::test); + cx.update(|cx| { + theme::init(cx); + Project::init_settings(cx); + language::init(cx); + }); + + let project = Project::test(params.fs.clone(), [], cx).await; + let workspace = cx + .add_window(|cx| Workspace::test_new(project.clone(), cx)) + .root_view(cx); + + (project, workspace) + } + + /// Creates a worktree with 1 folder: /root{suffix}/ + async fn create_folder_wt( + project: Model, + path: impl AsRef, + cx: &mut TestAppContext, + ) -> (Model, Entry) { + create_wt(project, true, path, cx).await + } + + /// Creates a worktree with 1 file: /root{suffix}.txt + async fn create_file_wt( + project: Model, + path: impl AsRef, + cx: &mut TestAppContext, + ) -> (Model, Entry) { + create_wt(project, false, path, cx).await + } + + async fn create_wt( + project: Model, + is_dir: bool, + path: impl AsRef, + cx: &mut TestAppContext, + ) -> (Model, Entry) { + let (wt, _) = project + .update(cx, |project, cx| { + project.find_or_create_local_worktree(path, true, cx) + }) + .await + .unwrap(); + + let entry = cx + .update(|cx| { + wt.update(cx, |wt, cx| { + wt.as_local() + .unwrap() + .create_entry(Path::new(""), is_dir, cx) + }) + }) + .await + .unwrap(); + + (wt, entry) + } + + pub fn insert_active_entry_for( + wt: Model, + entry: Entry, + project: Model, + cx: &mut TestAppContext, + ) { + cx.update(|cx| { + let p = ProjectPath { + worktree_id: wt.read(cx).id(), + path: entry.path, + }; + project.update(cx, |project, cx| project.set_active_path(Some(p), cx)); + }); + } +} From 61d6cb880c9db96c0f090017d145d4b393db015d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 14 Nov 2023 11:15:57 +0200 Subject: [PATCH 2/5] Start porting terminal_element to gpui2 Co-Authored-By: Mikayla Maki --- crates/gpui2/src/geometry.rs | 4 + crates/terminal_view2/src/terminal_element.rs | 120 +++++++++--------- 2 files changed, 63 insertions(+), 61 deletions(-) diff --git a/crates/gpui2/src/geometry.rs b/crates/gpui2/src/geometry.rs index 854453101eeb1de0808565cdaf3414475f0123a5..a1898bfd6d2839f7d7336d5cf649b7dc81f15531 100644 --- a/crates/gpui2/src/geometry.rs +++ b/crates/gpui2/src/geometry.rs @@ -335,6 +335,10 @@ where }; Bounds { origin, size } } + + pub fn new(origin: Point, size: Size) -> Self { + Bounds { origin, size } + } } impl Bounds diff --git a/crates/terminal_view2/src/terminal_element.rs b/crates/terminal_view2/src/terminal_element.rs index 30dbccf4559f2175901b6fbfc74e090020b89f34..95a76f143fe5bf3ad33cf339654a9ee6aaeca219 100644 --- a/crates/terminal_view2/src/terminal_element.rs +++ b/crates/terminal_view2/src/terminal_element.rs @@ -1,17 +1,7 @@ use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; use gpui::{ - color::Color, - elements::{Empty, Overlay}, - fonts::{HighlightStyle, Properties, Style::Italic, TextStyle, Underline, Weight}, - geometry::{ - rect::RectF, - vector::{vec2f, Vector2F}, - }, - platform::{CursorStyle, MouseButton}, - serde_json::json, - text_layout::{Line, RunStyle}, - AnyElement, Element, EventContext, FontCache, ModelContext, MouseRegion, Quad, SizeConstraint, - TextLayoutCache, ViewContext, WeakModelHandle, WindowContext, + serde_json::json, AnyElement, Bounds, HighlightStyle, Hsla, Line, ModelContext, MouseButton, + Pixels, Point, TextStyle, ViewContext, WeakModel, WindowContext, }; use itertools::Itertools; use language::CursorShape; @@ -20,14 +10,17 @@ use terminal::{ alacritty_terminal::{ ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor}, grid::Dimensions, - index::Point, + index::Point as AlacPoint, term::{cell::Flags, TermMode}, }, - mappings::colors::convert_color, + // mappings::colors::convert_color, terminal_settings::TerminalSettings, - IndexedCell, Terminal, TerminalContent, TerminalSize, + IndexedCell, + Terminal, + TerminalContent, + TerminalSize, }; -use theme::{TerminalStyle, ThemeSettings}; +use theme::ThemeSettings; use util::ResultExt; use std::{fmt::Debug, ops::RangeInclusive}; @@ -39,9 +32,9 @@ use crate::TerminalView; pub struct LayoutState { cells: Vec, rects: Vec, - relative_highlighted_ranges: Vec<(RangeInclusive, Color)>, + relative_highlighted_ranges: Vec<(RangeInclusive, Hsla)>, cursor: Option, - background_color: Color, + background_color: Hsla, size: TerminalSize, mode: TermMode, display_offset: usize, @@ -56,7 +49,7 @@ struct DisplayCursor { } impl DisplayCursor { - fn from(cursor_point: Point, display_offset: usize) -> Self { + fn from(cursor_point: AlacPoint, display_offset: usize) -> Self { Self { line: cursor_point.line.0 + display_offset as i32, col: cursor_point.column.0, @@ -74,45 +67,45 @@ impl DisplayCursor { #[derive(Clone, Debug, Default)] struct LayoutCell { - point: Point, + point: AlacPoint, text: Line, } impl LayoutCell { - fn new(point: Point, text: Line) -> LayoutCell { + fn new(point: AlacPoint, text: Line) -> LayoutCell { LayoutCell { point, text } } fn paint( &self, - origin: Vector2F, + origin: Point, layout: &LayoutState, - visible_bounds: RectF, + _visible_bounds: Bounds, _view: &mut TerminalView, cx: &mut WindowContext, ) { let pos = { let point = self.point; - vec2f( - (origin.x() + point.column as f32 * layout.size.cell_width).floor(), - origin.y() + point.line as f32 * layout.size.line_height, + + Point::new( + (origin.x + point.column as f32 * layout.size.cell_width).floor(), + origin.y + point.line as f32 * layout.size.line_height, ) }; - self.text - .paint(pos, visible_bounds, layout.size.line_height, cx); + self.text.paint(pos, layout.size.line_height, cx); } } #[derive(Clone, Debug, Default)] struct LayoutRect { - point: Point, + point: AlacPoint, num_of_cells: usize, - color: Color, + color: Hsla, } impl LayoutRect { - fn new(point: Point, num_of_cells: usize, color: Color) -> LayoutRect { + fn new(point: AlacPoint, num_of_cells: usize, color: Hsla) -> LayoutRect { LayoutRect { point, num_of_cells, @@ -130,7 +123,7 @@ impl LayoutRect { fn paint( &self, - origin: Vector2F, + origin: Point, layout: &LayoutState, _view: &mut TerminalView, cx: &mut ViewContext, @@ -147,19 +140,20 @@ impl LayoutRect { layout.size.line_height, ); - cx.scene().push_quad(Quad { - bounds: RectF::new(position, size), - background: Some(self.color), - border: Default::default(), - corner_radii: Default::default(), - }) + cx.paint_quad( + Bounds::new(position, size), + Default::default(), + Some(self.color), + Default::default(), + Default::default(), + ); } } ///The GPUI element that paints the terminal. ///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection? pub struct TerminalElement { - terminal: WeakModelHandle, + terminal: WeakModel, focused: bool, cursor_visible: bool, can_navigate_to_selected_word: bool, @@ -167,7 +161,7 @@ pub struct TerminalElement { impl TerminalElement { pub fn new( - terminal: WeakModelHandle, + terminal: WeakModel, focused: bool, cursor_visible: bool, can_navigate_to_selected_word: bool, @@ -180,7 +174,7 @@ impl TerminalElement { } } - //Vec> -> Clip out the parts of the ranges + //Vec> -> Clip out the parts of the ranges fn layout_grid( grid: &Vec, @@ -188,7 +182,7 @@ impl TerminalElement { terminal_theme: &TerminalStyle, text_layout_cache: &TextLayoutCache, font_cache: &FontCache, - hyperlink: Option<(HighlightStyle, &RangeInclusive)>, + hyperlink: Option<(HighlightStyle, &RangeInclusive)>, ) -> (Vec, Vec) { let mut cells = vec![]; let mut rects = vec![]; @@ -225,7 +219,10 @@ impl TerminalElement { rects.push(cur_rect.take().unwrap()); } cur_rect = Some(LayoutRect::new( - Point::new(line_index as i32, cell.point.column.0 as i32), + AlacPoint::new( + line_index as i32, + cell.point.column.0 as i32, + ), 1, convert_color(&bg, &terminal_theme), )); @@ -234,7 +231,7 @@ impl TerminalElement { None => { cur_alac_color = Some(bg); cur_rect = Some(LayoutRect::new( - Point::new(line_index as i32, cell.point.column.0 as i32), + AlacPoint::new(line_index as i32, cell.point.column.0 as i32), 1, convert_color(&bg, &terminal_theme), )); @@ -263,7 +260,7 @@ impl TerminalElement { ); cells.push(LayoutCell::new( - Point::new(line_index as i32, cell.point.column.0 as i32), + AlacPoint::new(line_index as i32, cell.point.column.0 as i32), layout_cell, )) }; @@ -312,7 +309,7 @@ impl TerminalElement { style: &TerminalStyle, text_style: &TextStyle, font_cache: &FontCache, - hyperlink: Option<(HighlightStyle, &RangeInclusive)>, + hyperlink: Option<(HighlightStyle, &RangeInclusive)>, ) -> RunStyle { let flags = indexed.cell.flags; let fg = convert_color(&fg, &style); @@ -366,8 +363,8 @@ impl TerminalElement { } fn generic_button_handler( - connection: WeakModelHandle, - origin: Vector2F, + connection: WeakModel, + origin: Point, f: impl Fn(&mut Terminal, Vector2F, E, &mut ModelContext), ) -> impl Fn(E, &mut TerminalView, &mut EventContext) { move |event, _: &mut TerminalView, cx| { @@ -384,8 +381,8 @@ impl TerminalElement { fn attach_mouse_handlers( &self, - origin: Vector2F, - visible_bounds: RectF, + origin: Point, + visible_bounds: Bounds, mode: TermMode, cx: &mut ViewContext, ) { @@ -729,8 +726,8 @@ impl Element for TerminalElement { fn paint( &mut self, - bounds: RectF, - visible_bounds: RectF, + bounds: Bounds, + visible_bounds: Bounds, layout: &mut Self::LayoutState, view: &mut TerminalView, cx: &mut ViewContext, @@ -749,7 +746,7 @@ impl Element for TerminalElement { cx.scene().push_cursor_region(gpui::CursorRegion { bounds, style: if layout.hyperlink_tooltip.is_some() { - CursorStyle::PointingHand + CursorStyle::AlacPointingHand } else { CursorStyle::IBeam }, @@ -817,7 +814,7 @@ impl Element for TerminalElement { fn debug( &self, - _: RectF, + _: Bounds, _: &Self::LayoutState, _: &Self::PaintState, _: &TerminalView, @@ -831,8 +828,8 @@ impl Element for TerminalElement { fn rect_for_text_range( &self, _: Range, - bounds: RectF, - _: RectF, + bounds: Bounds, + _: Bounds, layout: &Self::LayoutState, _: &Self::PaintState, _: &TerminalView, @@ -875,9 +872,9 @@ fn is_blank(cell: &IndexedCell) -> bool { } fn to_highlighted_range_lines( - range: &RangeInclusive, + range: &RangeInclusive, layout: &LayoutState, - origin: Vector2F, + origin: Point, ) -> Option<(f32, Vec)> { // Step 1. Normalize the points to be viewport relative. // When display_offset = 1, here's how the grid is arranged: @@ -897,11 +894,12 @@ fn to_highlighted_range_lines( // of the grid data we should be looking at. But for the rendering step, we don't // want negatives. We want things relative to the 'viewport' (the area of the grid // which is currently shown according to the display offset) - let unclamped_start = Point::new( + let unclamped_start = AlacPoint::new( range.start().line + layout.display_offset, range.start().column, ); - let unclamped_end = Point::new(range.end().line + layout.display_offset, range.end().column); + let unclamped_end = + AlacPoint::new(range.end().line + layout.display_offset, range.end().column); // Step 2. Clamp range to viewport, and return None if it doesn't overlap if unclamped_end.line.0 < 0 || unclamped_start.line.0 > layout.size.num_lines() as i32 { @@ -911,7 +909,7 @@ fn to_highlighted_range_lines( let clamped_start_line = unclamped_start.line.0.max(0) as usize; let clamped_end_line = unclamped_end.line.0.min(layout.size.num_lines() as i32) as usize; //Convert the start of the range to pixels - let start_y = origin.y() + clamped_start_line as f32 * layout.size.line_height; + let start_y = origin.y + clamped_start_line as f32 * layout.size.line_height; // Step 3. Expand ranges that cross lines into a collection of single-line ranges. // (also convert to pixels) From a238368296b4e9c7907a3ce58458db81bf199ed2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Tue, 14 Nov 2023 17:40:29 +0200 Subject: [PATCH 3/5] More compilation fixes --- crates/terminal2/src/terminal_settings.rs | 12 +- crates/terminal_view2/src/terminal_element.rs | 196 ++++++++++-------- crates/terminal_view2/src/terminal_panel.rs | 153 +++++++------- crates/terminal_view2/src/terminal_view.rs | 45 ++-- crates/workspace2/src/pane.rs | 24 +-- crates/workspace2/src/workspace2.rs | 19 +- 6 files changed, 244 insertions(+), 205 deletions(-) diff --git a/crates/terminal2/src/terminal_settings.rs b/crates/terminal2/src/terminal_settings.rs index 1d1e1cea2a30695571724afbf0fa18f63529b01d..16ec2869227098ad26d7b5db22b6456b9c4f0edb 100644 --- a/crates/terminal2/src/terminal_settings.rs +++ b/crates/terminal2/src/terminal_settings.rs @@ -1,4 +1,4 @@ -use gpui::{AppContext, FontFeatures}; +use gpui::{AppContext, FontFeatures, Pixels}; use schemars::JsonSchema; use serde_derive::{Deserialize, Serialize}; use std::{collections::HashMap, path::PathBuf}; @@ -15,7 +15,7 @@ pub enum TerminalDockPosition { pub struct TerminalSettings { pub shell: Shell, pub working_directory: WorkingDirectory, - font_size: Option, + pub font_size: Option, pub font_family: Option, pub line_height: TerminalLineHeight, pub font_features: Option, @@ -90,14 +90,6 @@ pub struct TerminalSettingsContent { pub detect_venv: Option, } -impl TerminalSettings { - // todo!("move to terminal element") - // pub fn font_size(&self, cx: &AppContext) -> Option { - // self.font_size - // .map(|size| theme2::adjusted_font_size(size, cx)) - // } -} - impl settings::Settings for TerminalSettings { const KEY: Option<&'static str> = Some("terminal"); diff --git a/crates/terminal_view2/src/terminal_element.rs b/crates/terminal_view2/src/terminal_element.rs index 95a76f143fe5bf3ad33cf339654a9ee6aaeca219..50ab14144b027176615b5eae475950eba7f0af44 100644 --- a/crates/terminal_view2/src/terminal_element.rs +++ b/crates/terminal_view2/src/terminal_element.rs @@ -1,11 +1,13 @@ use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; use gpui::{ - serde_json::json, AnyElement, Bounds, HighlightStyle, Hsla, Line, ModelContext, MouseButton, - Pixels, Point, TextStyle, ViewContext, WeakModel, WindowContext, + AnyElement, AppContext, Bounds, Component, Element, HighlightStyle, Hsla, LayoutId, Line, + ModelContext, MouseButton, Pixels, Point, TextStyle, Underline, ViewContext, WeakModel, + WindowContext, }; use itertools::Itertools; use language::CursorShape; use ordered_float::OrderedFloat; +use settings::Settings; use terminal::{ alacritty_terminal::{ ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor}, @@ -21,10 +23,9 @@ use terminal::{ TerminalSize, }; use theme::ThemeSettings; -use util::ResultExt; +use std::mem; use std::{fmt::Debug, ops::RangeInclusive}; -use std::{mem, ops::Range}; use crate::TerminalView; @@ -143,7 +144,7 @@ impl LayoutRect { cx.paint_quad( Bounds::new(position, size), Default::default(), - Some(self.color), + self.color, Default::default(), Default::default(), ); @@ -282,10 +283,10 @@ impl TerminalElement { text_fragment: &Line, ) -> Option<(Vector2F, f32)> { if cursor_point.line() < size.total_lines() as i32 { - let cursor_width = if text_fragment.width() == 0. { + let cursor_width = if text_fragment.width == 0. { size.cell_width() } else { - text_fragment.width() + text_fragment.width }; //Cursor should always surround as much of the text as possible, @@ -339,7 +340,7 @@ impl TerminalElement { let font_id = font_cache .select_font(text_style.font_family_id, &properties) - .unwrap_or(text_style.font_id); + .unwrap_or(8text_style.font_id); let mut result = RunStyle { color: fg, @@ -369,7 +370,7 @@ impl TerminalElement { ) -> impl Fn(E, &mut TerminalView, &mut EventContext) { move |event, _: &mut TerminalView, cx| { cx.focus_parent(); - if let Some(conn_handle) = connection.upgrade(cx) { + if let Some(conn_handle) = connection.upgrade() { conn_handle.update(cx, |terminal, cx| { f(terminal, origin, event, cx); @@ -382,7 +383,7 @@ impl TerminalElement { fn attach_mouse_handlers( &self, origin: Point, - visible_bounds: Bounds, + visible_bounds: Bounds, mode: TermMode, cx: &mut ViewContext, ) { @@ -397,7 +398,7 @@ impl TerminalElement { let terminal_view = cx.handle(); cx.focus(&terminal_view); v.context_menu.update(cx, |menu, _cx| menu.delay_cancel()); - if let Some(conn_handle) = connection.upgrade(cx) { + if let Some(conn_handle) = connection.upgrade() { conn_handle.update(cx, |terminal, cx| { terminal.mouse_down(&event, origin); @@ -412,7 +413,7 @@ impl TerminalElement { } if cx.is_self_focused() { - if let Some(conn_handle) = connection.upgrade(cx) { + if let Some(conn_handle) = connection.upgrade() { conn_handle.update(cx, |terminal, cx| { terminal.mouse_drag(event, origin); cx.notify(); @@ -435,7 +436,7 @@ impl TerminalElement { .on_click( MouseButton::Right, move |event, view: &mut TerminalView, cx| { - let mouse_mode = if let Some(conn_handle) = connection.upgrade(cx) { + let mouse_mode = if let Some(conn_handle) = connection.upgrade() { conn_handle.update(cx, |terminal, _cx| terminal.mouse_mode(event.shift)) } else { // If we can't get the model handle, probably can't deploy the context menu @@ -448,7 +449,7 @@ impl TerminalElement { ) .on_move(move |event, _: &mut TerminalView, cx| { if cx.is_self_focused() { - if let Some(conn_handle) = connection.upgrade(cx) { + if let Some(conn_handle) = connection.upgrade() { conn_handle.update(cx, |terminal, cx| { terminal.mouse_move(&event, origin); cx.notify(); @@ -457,7 +458,7 @@ impl TerminalElement { } }) .on_scroll(move |event, _: &mut TerminalView, cx| { - if let Some(conn_handle) = connection.upgrade(cx) { + if let Some(conn_handle) = connection.upgrade() { conn_handle.update(cx, |terminal, cx| { terminal.scroll_wheel(event, origin); cx.notify(); @@ -516,17 +517,16 @@ impl TerminalElement { } impl Element for TerminalElement { - type LayoutState = LayoutState; - type PaintState = (); + type ElementState = LayoutState; fn layout( &mut self, - constraint: gpui::SizeConstraint, - view: &mut TerminalView, + view_state: &mut TerminalView, + element_state: &mut Self::ElementState, cx: &mut ViewContext, - ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { - let settings = settings::get::(cx); - let terminal_settings = settings::get::(cx); + ) -> LayoutId { + let settings = ThemeSettings::get_global(cx); + let terminal_settings = TerminalSettings::get_global(cx); //Setup layout information let terminal_theme = settings.theme.terminal.clone(); //TODO: Try to minimize this clone. @@ -534,9 +534,7 @@ impl Element for TerminalElement { let tooltip_style = settings.theme.tooltip.clone(); let font_cache = cx.font_cache(); - let font_size = terminal_settings - .font_size(cx) - .unwrap_or(settings.buffer_font_size(cx)); + let font_size = font_size(&terminal_settings, cx).unwrap_or(settings.buffer_font_size(cx)); let font_family_name = terminal_settings .font_family .as_ref() @@ -575,14 +573,14 @@ impl Element for TerminalElement { TerminalSize::new(line_height, cell_width, size) }; - let search_matches = if let Some(terminal_model) = self.terminal.upgrade(cx) { + let search_matches = if let Some(terminal_model) = self.terminal.upgrade() { terminal_model.read(cx).matches.clone() } else { Default::default() }; let background_color = terminal_theme.background; - let terminal_handle = self.terminal.upgrade(cx).unwrap(); + let terminal_handle = self.terminal.upgrade().unwrap(); let last_hovered_word = terminal_handle.update(cx, |terminal, cx| { terminal.set_size(dimensions); @@ -614,7 +612,7 @@ impl Element for TerminalElement { tooltip.layout( SizeConstraint::new(Vector2F::zero(), cx.window_size()), - view, + view_state, cx, ); tooltip @@ -709,7 +707,7 @@ impl Element for TerminalElement { //Done! ( constraint.max, - LayoutState { + Self::ElementState { cells, cursor, background_color, @@ -726,26 +724,25 @@ impl Element for TerminalElement { fn paint( &mut self, - bounds: Bounds, - visible_bounds: Bounds, - layout: &mut Self::LayoutState, - view: &mut TerminalView, + bounds: Bounds, + view_state: &mut TerminalView, + element_state: &mut Self::ElementState, cx: &mut ViewContext, - ) -> Self::PaintState { + ) { let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); //Setup element stuff let clip_bounds = Some(visible_bounds); cx.paint_layer(clip_bounds, |cx| { - let origin = bounds.origin() + vec2f(layout.gutter, 0.); + let origin = bounds.origin() + vec2f(element_state.gutter, 0.); // Elements are ephemeral, only at paint time do we know what could be clicked by a mouse - self.attach_mouse_handlers(origin, visible_bounds, layout.mode, cx); + self.attach_mouse_handlers(origin, visible_bounds, element_state.mode, cx); cx.scene().push_cursor_region(gpui::CursorRegion { bounds, - style: if layout.hyperlink_tooltip.is_some() { + style: if element_state.hyperlink_tooltip.is_some() { CursorStyle::AlacPointingHand } else { CursorStyle::IBeam @@ -755,31 +752,34 @@ impl Element for TerminalElement { cx.paint_layer(clip_bounds, |cx| { //Start with a background color cx.scene().push_quad(Quad { - bounds: RectF::new(bounds.origin(), bounds.size()), - background: Some(layout.background_color), + bounds, + background: Some(element_state.background_color), border: Default::default(), corner_radii: Default::default(), }); - for rect in &layout.rects { - rect.paint(origin, layout, view, cx); + for rect in &element_state.rects { + rect.paint(origin, element_state, view_state, cx); } }); //Draw Highlighted Backgrounds cx.paint_layer(clip_bounds, |cx| { - for (relative_highlighted_range, color) in layout.relative_highlighted_ranges.iter() + for (relative_highlighted_range, color) in + element_state.relative_highlighted_ranges.iter() { - if let Some((start_y, highlighted_range_lines)) = - to_highlighted_range_lines(relative_highlighted_range, layout, origin) - { + if let Some((start_y, highlighted_range_lines)) = to_highlighted_range_lines( + relative_highlighted_range, + element_state, + origin, + ) { let hr = HighlightedRange { start_y, //Need to change this - line_height: layout.size.line_height, + line_height: element_state.size.line_height, lines: highlighted_range_lines, color: color.clone(), //Copied from editor. TODO: move to theme or something - corner_radius: 0.15 * layout.size.line_height, + corner_radius: 0.15 * element_state.size.line_height, }; hr.paint(bounds, cx); } @@ -788,63 +788,83 @@ impl Element for TerminalElement { //Draw the text cells cx.paint_layer(clip_bounds, |cx| { - for cell in &layout.cells { - cell.paint(origin, layout, visible_bounds, view, cx); + for cell in &element_state.cells { + cell.paint(origin, element_state, visible_bounds, view_state, cx); } }); //Draw cursor if self.cursor_visible { - if let Some(cursor) = &layout.cursor { + if let Some(cursor) = &element_state.cursor { cx.paint_layer(clip_bounds, |cx| { cursor.paint(origin, cx); }) } } - if let Some(element) = &mut layout.hyperlink_tooltip { - element.paint(origin, visible_bounds, view, cx) + if let Some(element) = &mut element_state.hyperlink_tooltip { + element.paint(origin, visible_bounds, view_state, cx) } }); } - fn metadata(&self) -> Option<&dyn std::any::Any> { - None + fn id(&self) -> Option { + todo!() } - fn debug( - &self, - _: Bounds, - _: &Self::LayoutState, - _: &Self::PaintState, - _: &TerminalView, - _: &gpui::ViewContext, - ) -> gpui::serde_json::Value { - json!({ - "type": "TerminalElement", - }) + fn initialize( + &mut self, + view_state: &mut TerminalView, + element_state: Option, + cx: &mut ViewContext, + ) -> Self::ElementState { + todo!() } - fn rect_for_text_range( - &self, - _: Range, - bounds: Bounds, - _: Bounds, - layout: &Self::LayoutState, - _: &Self::PaintState, - _: &TerminalView, - _: &gpui::ViewContext, - ) -> Option { - // Use the same origin that's passed to `Cursor::paint` in the paint - // method bove. - let mut origin = bounds.origin() + vec2f(layout.size.cell_width, 0.); - - // TODO - Why is it necessary to move downward one line to get correct - // positioning? I would think that we'd want the same rect that is - // painted for the cursor. - origin += vec2f(0., layout.size.line_height); - - Some(layout.cursor.as_ref()?.bounding_rect(origin)) + // todo!() remove? + // fn metadata(&self) -> Option<&dyn std::any::Any> { + // None + // } + + // fn debug( + // &self, + // _: Bounds, + // _: &Self::ElementState, + // _: &Self::PaintState, + // _: &TerminalView, + // _: &gpui::ViewContext, + // ) -> gpui::serde_json::Value { + // json!({ + // "type": "TerminalElement", + // }) + // } + + // fn rect_for_text_range( + // &self, + // _: Range, + // bounds: Bounds, + // _: Bounds, + // layout: &Self::ElementState, + // _: &Self::PaintState, + // _: &TerminalView, + // _: &gpui::ViewContext, + // ) -> Option> { + // // Use the same origin that's passed to `Cursor::paint` in the paint + // // method bove. + // let mut origin = bounds.origin() + vec2f(layout.size.cell_width, 0.); + + // // TODO - Why is it necessary to move downward one line to get correct + // // positioning? I would think that we'd want the same rect that is + // // painted for the cursor. + // origin += vec2f(0., layout.size.line_height); + + // Some(layout.cursor.as_ref()?.bounding_rect(origin)) + // } +} + +impl Component for TerminalElement { + fn render(self) -> AnyElement { + todo!() } } @@ -933,3 +953,9 @@ fn to_highlighted_range_lines( Some((start_y, highlighted_range_lines)) } + +fn font_size(terminal_settings: &TerminalSettings, cx: &mut AppContext) -> Option { + terminal_settings + .font_size + .map(|size| theme::adjusted_font_size(size, cx)) +} diff --git a/crates/terminal_view2/src/terminal_panel.rs b/crates/terminal_view2/src/terminal_panel.rs index 0bfa84e7546968ccce18d9302af7012cf760eb4b..94140450bcd29c3350087fdffea4752aa77b1336 100644 --- a/crates/terminal_view2/src/terminal_panel.rs +++ b/crates/terminal_view2/src/terminal_panel.rs @@ -3,27 +3,34 @@ use std::{path::PathBuf, sync::Arc}; use crate::TerminalView; use db::kvp::KEY_VALUE_STORE; use gpui::{ - actions, anyhow::Result, elements::*, serde_json, Action, AppContext, AsyncAppContext, Entity, - Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowContext, + actions, serde_json, Action, AppContext, AsyncAppContext, Entity, EventEmitter, Render, + Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowContext, }; use project::Fs; use serde::{Deserialize, Serialize}; -use settings::SettingsStore; +use settings::{Settings, SettingsStore}; use terminal::terminal_settings::{TerminalDockPosition, TerminalSettings}; use util::{ResultExt, TryFutureExt}; use workspace::{ - dock::{DockPosition, Panel}, + dock::{DockPosition, Panel, PanelEvent}, item::Item, - pane, DraggedItem, Pane, Workspace, + pane, Pane, Workspace, }; +use anyhow::Result; + const TERMINAL_PANEL_KEY: &'static str = "TerminalPanel"; -actions!(terminal_panel, [ToggleFocus]); +actions!(ToggleFocus); pub fn init(cx: &mut AppContext) { - cx.add_action(TerminalPanel::new_terminal); - cx.add_action(TerminalPanel::open_terminal); + cx.observe_new_views( + |workspace: &mut Workspace, _: &mut ViewContext| { + workspace.register_action(TerminalPanel::new_terminal); + workspace.register_action(TerminalPanel::open_terminal); + }, + ) + .detach(); } #[derive(Debug)] @@ -36,9 +43,9 @@ pub enum Event { } pub struct TerminalPanel { - pane: ViewHandle, + pane: View, fs: Arc, - workspace: WeakViewHandle, + workspace: WeakView, width: Option, height: Option, pending_serialization: Task>, @@ -48,12 +55,11 @@ pub struct TerminalPanel { impl TerminalPanel { fn new(workspace: &Workspace, cx: &mut ViewContext) -> Self { let weak_self = cx.weak_handle(); - let pane = cx.add_view(|cx| { - let window = cx.window(); + let pane = cx.build_view(|cx| { + let window = cx.window_handle(); let mut pane = Pane::new( workspace.weak_handle(), workspace.project().clone(), - workspace.app_state().background_actions, Default::default(), cx, ); @@ -78,7 +84,7 @@ impl TerminalPanel { move |_, cx| { let this = this.clone(); cx.window_context().defer(move |cx| { - if let Some(this) = this.upgrade(cx) { + if let Some(this) = this.upgrade() { this.update(cx, |this, cx| { this.add_terminal(None, cx); }); @@ -104,7 +110,7 @@ impl TerminalPanel { )) .into_any() }); - let buffer_search_bar = cx.add_view(search::BufferSearchBar::new); + let buffer_search_bar = cx.build_view(search::BufferSearchBar::new); pane.toolbar() .update(cx, |toolbar, cx| toolbar.add_item(buffer_search_bar, cx)); pane @@ -123,7 +129,7 @@ impl TerminalPanel { _subscriptions: subscriptions, }; let mut old_dock_position = this.position(cx); - cx.observe_global::(move |this, cx| { + cx.observe_global::(move |this, cx| { let new_dock_position = this.position(cx); if new_dock_position != old_dock_position { old_dock_position = new_dock_position; @@ -134,13 +140,10 @@ impl TerminalPanel { this } - pub fn load( - workspace: WeakViewHandle, - cx: AsyncAppContext, - ) -> Task>> { + pub fn load(workspace: WeakView, cx: AsyncAppContext) -> Task>> { cx.spawn(|mut cx| async move { let serialized_panel = if let Some(panel) = cx - .background() + .background_executor() .spawn(async move { KEY_VALUE_STORE.read_kvp(TERMINAL_PANEL_KEY) }) .await .log_err() @@ -151,7 +154,7 @@ impl TerminalPanel { None }; let (panel, pane, items) = workspace.update(&mut cx, |workspace, cx| { - let panel = cx.add_view(|cx| TerminalPanel::new(workspace, cx)); + let panel = cx.build_view(|cx| TerminalPanel::new(workspace, cx)); let items = if let Some(serialized_panel) = serialized_panel.as_ref() { panel.update(cx, |panel, cx| { cx.notify(); @@ -189,7 +192,7 @@ impl TerminalPanel { let mut active_ix = None; for item in items { if let Some(item) = item.log_err() { - let item_id = item.id(); + let item_id = item.entity_id().as_u64(); pane.add_item(Box::new(item), false, false, None, cx); if Some(item_id) == active_item_id { active_ix = Some(pane.items_len() - 1); @@ -208,7 +211,7 @@ impl TerminalPanel { fn handle_pane_event( &mut self, - _pane: ViewHandle, + _pane: View, event: &pane::Event, cx: &mut ViewContext, ) { @@ -221,7 +224,7 @@ impl TerminalPanel { pane::Event::Focus => cx.emit(Event::Focus), pane::Event::AddItem { item } => { - if let Some(workspace) = self.workspace.upgrade(cx) { + if let Some(workspace) = self.workspace.upgrade() { let pane = self.pane.clone(); workspace.update(cx, |workspace, cx| item.added_to_pane(workspace, pane, cx)) } @@ -261,24 +264,23 @@ impl TerminalPanel { fn add_terminal(&mut self, working_directory: Option, cx: &mut ViewContext) { let workspace = self.workspace.clone(); cx.spawn(|this, mut cx| async move { - let pane = this.read_with(&cx, |this, _| this.pane.clone())?; + let pane = this.update(&mut cx, |this, _| this.pane.clone())?; workspace.update(&mut cx, |workspace, cx| { let working_directory = if let Some(working_directory) = working_directory { Some(working_directory) } else { - let working_directory_strategy = settings::get::(cx) - .working_directory - .clone(); + let working_directory_strategy = + TerminalSettings::get_global(cx).working_directory.clone(); crate::get_working_directory(workspace, cx, working_directory_strategy) }; - let window = cx.window(); + let window = cx.window_handle(); if let Some(terminal) = workspace.project().update(cx, |project, cx| { project .create_terminal(working_directory, window, cx) .log_err() }) { - let terminal = Box::new(cx.add_view(|cx| { + let terminal = Box::new(cx.build_view(|cx| { TerminalView::new( terminal, workspace.weak_handle(), @@ -287,7 +289,7 @@ impl TerminalPanel { ) })); pane.update(cx, |pane, cx| { - let focus = pane.has_focus(); + let focus = pane.has_focus(cx); pane.add_item(terminal, true, focus, None, cx); }); } @@ -303,12 +305,16 @@ impl TerminalPanel { .pane .read(cx) .items() - .map(|item| item.id()) + .map(|item| item.id().as_u64()) .collect::>(); - let active_item_id = self.pane.read(cx).active_item().map(|item| item.id()); + let active_item_id = self + .pane + .read(cx) + .active_item() + .map(|item| item.id().as_u64()); let height = self.height; let width = self.width; - self.pending_serialization = cx.background().spawn( + self.pending_serialization = cx.background_executor().spawn( async move { KEY_VALUE_STORE .write_kvp( @@ -328,29 +334,25 @@ impl TerminalPanel { } } -impl Entity for TerminalPanel { - type Event = Event; -} - -impl View for TerminalPanel { - fn ui_name() -> &'static str { - "TerminalPanel" - } +impl EventEmitter for TerminalPanel {} +impl EventEmitter for TerminalPanel {} +impl Render for TerminalPanel { fn render(&mut self, cx: &mut ViewContext) -> gpui::AnyElement { ChildView::new(&self.pane, cx).into_any() } - fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext) { - if cx.is_self_focused() { - cx.focus(&self.pane); - } - } + // todo!() + // fn focus_in(&mut self, _: gpui::AnyView, cx: &mut ViewContext) { + // if cx.is_self_focused() { + // cx.focus(&self.pane); + // } + // } } impl Panel for TerminalPanel { fn position(&self, cx: &WindowContext) -> DockPosition { - match settings::get::(cx).dock { + match TerminalSettings::get_global(cx).dock { TerminalDockPosition::Left => DockPosition::Left, TerminalDockPosition::Bottom => DockPosition::Bottom, TerminalDockPosition::Right => DockPosition::Right, @@ -373,7 +375,7 @@ impl Panel for TerminalPanel { } fn size(&self, cx: &WindowContext) -> f32 { - let settings = settings::get::(cx); + let settings = TerminalSettings::get_global(cx); match self.position(cx) { DockPosition::Left | DockPosition::Right => { self.width.unwrap_or_else(|| settings.default_width) @@ -391,14 +393,6 @@ impl Panel for TerminalPanel { cx.notify(); } - fn should_zoom_in_on_event(event: &Event) -> bool { - matches!(event, Event::ZoomIn) - } - - fn should_zoom_out_on_event(event: &Event) -> bool { - matches!(event, Event::ZoomOut) - } - fn is_zoomed(&self, cx: &WindowContext) -> bool { self.pane.read(cx).is_zoomed() } @@ -430,31 +424,44 @@ impl Panel for TerminalPanel { } } - fn should_change_position_on_event(event: &Self::Event) -> bool { - matches!(event, Event::DockPositionChanged) + fn has_focus(&self, cx: &WindowContext) -> bool { + self.pane.read(cx).has_focus(cx) } - fn should_activate_on_event(_: &Self::Event) -> bool { - false + fn persistent_name(&self) -> &'static str { + todo!() } - fn should_close_on_event(event: &Event) -> bool { - matches!(event, Event::Close) - } + // todo!() is it needed? + // fn should_change_position_on_event(event: &Self::Event) -> bool { + // matches!(event, Event::DockPositionChanged) + // } - fn has_focus(&self, cx: &WindowContext) -> bool { - self.pane.read(cx).has_focus() - } + // fn should_activate_on_event(_: &Self::Event) -> bool { + // false + // } - fn is_focus_event(event: &Self::Event) -> bool { - matches!(event, Event::Focus) - } + // fn should_close_on_event(event: &Event) -> bool { + // matches!(event, Event::Close) + // } + + // fn is_focus_event(event: &Self::Event) -> bool { + // matches!(event, Event::Focus) + // } + + // fn should_zoom_in_on_event(event: &Event) -> bool { + // matches!(event, Event::ZoomIn) + // } + + // fn should_zoom_out_on_event(event: &Event) -> bool { + // matches!(event, Event::ZoomOut) + // } } #[derive(Serialize, Deserialize)] struct SerializedTerminalPanel { - items: Vec, - active_item_id: Option, + items: Vec, + active_item_id: Option, width: Option, height: Option, } diff --git a/crates/terminal_view2/src/terminal_view.rs b/crates/terminal_view2/src/terminal_view.rs index 74954ad5c8d9c0a8964f79d97af1210678c2053a..254951716531e460f09e42d9d88644b70d47c20b 100644 --- a/crates/terminal_view2/src/terminal_view.rs +++ b/crates/terminal_view2/src/terminal_view.rs @@ -10,10 +10,11 @@ use anyhow::Context; use dirs::home_dir; use editor::{scroll::autoscroll::Autoscroll, Editor}; use gpui::{ - actions, div, img, red, register_action, AnyElement, AppContext, Component, Div, EventEmitter, - FocusEvent, FocusHandle, Focusable, FocusableKeyDispatch, InputHandler, KeyDownEvent, - Keystroke, Model, ParentElement, Pixels, Render, StatefulInteractivity, StatelessInteractive, - Styled, Task, View, ViewContext, VisualContext, WeakView, + actions, div, img, red, register_action, AnyElement, AppContext, Component, DispatchPhase, Div, + EventEmitter, FocusEvent, FocusHandle, Focusable, FocusableKeyDispatch, InputHandler, + KeyDownEvent, Keystroke, Model, ParentElement, Pixels, Render, SharedString, + StatefulInteractivity, StatelessInteractive, Styled, Task, View, ViewContext, VisualContext, + WeakView, }; use language::Bias; use project::{search::SearchQuery, LocalWorktree, Project}; @@ -21,7 +22,6 @@ use serde::Deserialize; use settings::Settings; use smol::Timer; use std::{ - borrow::Cow, ops::RangeInclusive, path::{Path, PathBuf}, sync::Arc, @@ -40,7 +40,7 @@ use workspace::{ item::{BreadcrumbText, Item, ItemEvent}, notifications::NotifyResultExt, register_deserializable_item, - searchable::{SearchEvent, SearchOptions, SearchableItem, SearchableItemHandle}, + searchable::{SearchEvent, SearchOptions, SearchableItem}, NewCenterTerminal, Pane, ToolbarItemLocation, Workspace, WorkspaceId, }; @@ -51,11 +51,11 @@ const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500); pub struct ScrollTerminal(pub i32); #[register_action] -#[derive(Clone, Default, Deserialize, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SendText(String); #[register_action] -#[derive(Clone, Default, Deserialize, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct SendKeystroke(String); actions!(Clear, Copy, Paste, ShowCharacterPalette, SearchTest); @@ -260,7 +260,7 @@ impl TerminalView { has_bell: false, focus_handle: cx.focus_handle(), // todo!() - // context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)), + // context_menu: cx.build_view(|cx| ContextMenu::new(view_id, cx)), blink_state: true, blinking_on: false, blinking_paused: false, @@ -493,7 +493,12 @@ pub fn regex_search_for_query(query: &project::search::SearchQuery) -> Option) -> bool { + fn key_down( + &mut self, + event: &KeyDownEvent, + _dispatch_phase: DispatchPhase, + cx: &mut ViewContext, + ) { self.clear_bel(cx); self.pause_cursor_blinking(cx); @@ -502,7 +507,7 @@ impl TerminalView { &event.keystroke, TerminalSettings::get_global(cx).option_as_meta, ) - }) + }); } fn focus_in(&mut self, event: &FocusEvent, cx: &mut ViewContext) { @@ -652,7 +657,10 @@ impl InputHandler for TerminalView { todo!() } - fn selected_text_range(&self, cx: &AppContext) -> Option> { + fn selected_text_range( + &mut self, + cx: &mut ViewContext, + ) -> Option> { if self .terminal .read(cx) @@ -706,7 +714,7 @@ impl InputHandler for TerminalView { } impl Item for TerminalView { - fn tab_tooltip_text(&self, cx: &AppContext) -> Option> { + fn tab_tooltip_text(&self, cx: &AppContext) -> Option { Some(self.terminal().read(cx).title().into()) } @@ -727,7 +735,7 @@ impl Item for TerminalView { &self, _workspace_id: WorkspaceId, _cx: &mut ViewContext, - ) -> Option { + ) -> Option> { //From what I can tell, there's no way to tell the current working //Directory of the terminal from outside the shell. There might be //solutions to this, but they are non-trivial and require more IPC @@ -789,7 +797,7 @@ impl Item for TerminalView { // cx.read(|cx| { // let strategy = TerminalSettings::get_global(cx).working_directory.clone(); // workspace - // .upgrade(cx) + // .upgrade() // .map(|workspace| { // get_working_directory(workspace.read(cx), cx, strategy) // }) @@ -817,6 +825,10 @@ impl Item for TerminalView { // .detach(); self.workspace_id = workspace.database_id(); } + + fn focus_handle(&self) -> FocusHandle { + self.focus_handle.clone() + } } impl SearchableItem for TerminalView { @@ -1098,7 +1110,8 @@ mod tests { let project = Project::test(params.fs.clone(), [], cx).await; let workspace = cx .add_window(|cx| Workspace::test_new(project.clone(), cx)) - .root_view(cx); + .root_view(cx) + .unwrap(); (project, workspace) } diff --git a/crates/workspace2/src/pane.rs b/crates/workspace2/src/pane.rs index c40b1a9dd32c480a683744b900228ec5d1ef2ee1..dcc8a4a14f182e118a2620a22578044f5c268925 100644 --- a/crates/workspace2/src/pane.rs +++ b/crates/workspace2/src/pane.rs @@ -179,7 +179,7 @@ pub struct Pane { workspace: WeakView, project: Model, // can_drop: Rc, &WindowContext) -> bool>, - // can_split: bool, + can_split: bool, // render_tab_bar_buttons: Rc) -> AnyElement>, } @@ -347,7 +347,7 @@ impl Pane { workspace, project, // can_drop: Rc::new(|_, _| true), - // can_split: true, + can_split: true, // render_tab_bar_buttons: Rc::new(move |pane, cx| { // Flex::row() // // New menu @@ -427,17 +427,17 @@ impl Pane { // self.can_drop = Rc::new(can_drop); // } - // pub fn set_can_split(&mut self, can_split: bool, cx: &mut ViewContext) { - // self.can_split = can_split; - // cx.notify(); - // } + pub fn set_can_split(&mut self, can_split: bool, cx: &mut ViewContext) { + self.can_split = can_split; + cx.notify(); + } - // pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext) { - // self.toolbar.update(cx, |toolbar, cx| { - // toolbar.set_can_navigate(can_navigate, cx); - // }); - // cx.notify(); - // } + pub fn set_can_navigate(&mut self, can_navigate: bool, cx: &mut ViewContext) { + self.toolbar.update(cx, |toolbar, cx| { + toolbar.set_can_navigate(can_navigate, cx); + }); + cx.notify(); + } // pub fn set_render_tab_bar_buttons(&mut self, cx: &mut ViewContext, render: F) // where diff --git a/crates/workspace2/src/workspace2.rs b/crates/workspace2/src/workspace2.rs index db13d268a74026ad94c89ba173b9bd82bc6d3845..1b8244833a1f0c045d8d26dd15b4288f45278183 100644 --- a/crates/workspace2/src/workspace2.rs +++ b/crates/workspace2/src/workspace2.rs @@ -36,11 +36,12 @@ use futures::{ Future, FutureExt, StreamExt, }; use gpui::{ - actions, div, point, prelude::*, size, Action, AnyModel, AnyView, AnyWeakView, AppContext, + actions, div, point, register_action, size, Action, AnyModel, AnyView, AnyWeakView, AppContext, AsyncAppContext, AsyncWindowContext, Bounds, Div, Entity, EntityId, EventEmitter, FocusHandle, - FocusableView, GlobalPixels, KeyContext, Model, ModelContext, ParentComponent, Point, Render, - Size, Styled, Subscription, Task, View, ViewContext, WeakView, WindowBounds, WindowContext, - WindowHandle, WindowOptions, + FocusableView, GlobalPixels, KeyContext, Model, ModelContext, ParentElement, Point, Render, + Size, StatefulInteractive, StatelessInteractive, StatelessInteractivity, Styled, Subscription, + Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle, + WindowOptions, }; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem}; use itertools::Itertools; @@ -193,10 +194,11 @@ impl Clone for Toast { } } -// #[derive(Clone, Deserialize, PartialEq)] -// pub struct OpenTerminal { -// pub working_directory: PathBuf, -// } +#[register_action] +#[derive(Debug, Default, Clone, Deserialize, PartialEq)] +pub struct OpenTerminal { + pub working_directory: PathBuf, +} // impl_actions!( // workspace, @@ -206,7 +208,6 @@ impl Clone for Toast { // SwapPaneInDirection, // NewFileInDirection, // Toast, -// OpenTerminal, // SaveAll, // Save, // CloseAllItemsAndPanes, From e3465fbcf99b352e5d777b2476a6c182d903e65a Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 16 Nov 2023 09:54:55 +0200 Subject: [PATCH 4/5] Comment out the terminal view --- crates/terminal_view2/src/terminal_view.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/terminal_view2/src/terminal_view.rs b/crates/terminal_view2/src/terminal_view.rs index 254951716531e460f09e42d9d88644b70d47c20b..1f0836422f6452d282e4ef80842f9f8aba18fdfc 100644 --- a/crates/terminal_view2/src/terminal_view.rs +++ b/crates/terminal_view2/src/terminal_view.rs @@ -546,12 +546,15 @@ impl Render for TerminalView { .on_action(TerminalView::clear) .on_action(TerminalView::show_character_palette) .on_action(TerminalView::select_all) - .child(TerminalElement::new( - terminal_handle, - focused, - self.should_show_cursor(focused, cx), - self.can_navigate_to_selected_word, - )) + // todo!() + .child( + "TERMINAL HERE", // TerminalElement::new( + // terminal_handle, + // focused, + // self.should_show_cursor(focused, cx), + // self.can_navigate_to_selected_word, + // ) + ) // todo!() // .child(ChildView::new(&self.context_menu, cx)) } From fd61683c46fedba888acd70edfab1bce57c393f2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Thu, 16 Nov 2023 10:03:54 +0200 Subject: [PATCH 5/5] WIP --- Cargo.lock | 1 + crates/editor2/src/scroll.rs | 2 +- crates/gpui2/src/window.rs | 6 +- crates/sqlez/src/bindable.rs | 17 + crates/terminal_view2/src/persistence.rs | 1 - crates/terminal_view2/src/terminal_element.rs | 1913 ++++++++--------- crates/terminal_view2/src/terminal_panel.rs | 315 ++- crates/terminal_view2/src/terminal_view.rs | 46 +- crates/workspace2/src/persistence/model.rs | 2 +- crates/workspace2/src/workspace2.rs | 40 +- crates/zed2/Cargo.toml | 2 +- crates/zed2/src/main.rs | 2 +- crates/zed2/src/zed2.rs | 9 +- 13 files changed, 1172 insertions(+), 1184 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0e155e9e995fa9cbdfa9b4ae87cd8dda5a7c947b..4bbada23d044759885088153dfbc3746130de4b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11567,6 +11567,7 @@ dependencies = [ "smol", "sum_tree", "tempdir", + "terminal_view2", "text2", "theme2", "thiserror", diff --git a/crates/editor2/src/scroll.rs b/crates/editor2/src/scroll.rs index 52e7498f970c8c325ae6133529da0fe98443d4d2..360c1e3c36814d02dda6b20807796a3fa53385d9 100644 --- a/crates/editor2/src/scroll.rs +++ b/crates/editor2/src/scroll.rs @@ -426,7 +426,7 @@ impl Editor { pub fn read_scroll_position_from_db( &mut self, - item_id: usize, + item_id: u64, workspace_id: WorkspaceId, cx: &mut ViewContext, ) { diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index 19280d5beba8fd418dd95245b6e37d8168c25318..b0d9d07df2db7b3b84db7d9950d3f6364b3b8c9d 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -1830,8 +1830,8 @@ impl<'a, V: 'static> ViewContext<'a, V> { self.view } - pub fn model(&self) -> Model { - self.view.model.clone() + pub fn model(&self) -> &Model { + &self.view.model } /// Access the underlying window context. @@ -2163,7 +2163,7 @@ impl<'a, V: 'static> ViewContext<'a, V> { pub fn observe_global( &mut self, - f: impl Fn(&mut V, &mut ViewContext<'_, V>) + 'static, + mut f: impl FnMut(&mut V, &mut ViewContext<'_, V>) + 'static, ) -> Subscription { let window_handle = self.window.handle; let view = self.view().downgrade(); diff --git a/crates/sqlez/src/bindable.rs b/crates/sqlez/src/bindable.rs index 4c874c4585ebb6cc852704581485cff17d52c047..ebfddbe1da845e777b417eb85408573382f0f0f5 100644 --- a/crates/sqlez/src/bindable.rs +++ b/crates/sqlez/src/bindable.rs @@ -164,6 +164,23 @@ impl Column for i64 { } } +impl StaticColumnCount for u64 {} +impl Bind for u64 { + fn bind(&self, statement: &Statement, start_index: i32) -> Result { + statement + .bind_int64(start_index, (*self) as i64) + .with_context(|| format!("Failed to bind i64 at index {start_index}"))?; + Ok(start_index + 1) + } +} + +impl Column for u64 { + fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> { + let result = statement.column_int64(start_index)? as u64; + Ok((result, start_index + 1)) + } +} + impl StaticColumnCount for u32 {} impl Bind for u32 { fn bind(&self, statement: &Statement, start_index: i32) -> Result { diff --git a/crates/terminal_view2/src/persistence.rs b/crates/terminal_view2/src/persistence.rs index 38dad88a8e29cd660ccdb40c995ce9b746184dc9..0da9ed47299d5b71d73d9173d80c526e66a0e507 100644 --- a/crates/terminal_view2/src/persistence.rs +++ b/crates/terminal_view2/src/persistence.rs @@ -1,7 +1,6 @@ use std::path::PathBuf; use db::{define_connection, query, sqlez_macros::sql}; -use gpui::EntityId; use workspace::{ItemId, WorkspaceDb, WorkspaceId}; define_connection! { diff --git a/crates/terminal_view2/src/terminal_element.rs b/crates/terminal_view2/src/terminal_element.rs index 50ab14144b027176615b5eae475950eba7f0af44..00f1dca17d686766310fb1fe67c2704b9c736eaf 100644 --- a/crates/terminal_view2/src/terminal_element.rs +++ b/crates/terminal_view2/src/terminal_element.rs @@ -1,961 +1,952 @@ -use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; -use gpui::{ - AnyElement, AppContext, Bounds, Component, Element, HighlightStyle, Hsla, LayoutId, Line, - ModelContext, MouseButton, Pixels, Point, TextStyle, Underline, ViewContext, WeakModel, - WindowContext, -}; -use itertools::Itertools; -use language::CursorShape; -use ordered_float::OrderedFloat; -use settings::Settings; -use terminal::{ - alacritty_terminal::{ - ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor}, - grid::Dimensions, - index::Point as AlacPoint, - term::{cell::Flags, TermMode}, - }, - // mappings::colors::convert_color, - terminal_settings::TerminalSettings, - IndexedCell, - Terminal, - TerminalContent, - TerminalSize, -}; -use theme::ThemeSettings; - -use std::mem; -use std::{fmt::Debug, ops::RangeInclusive}; - -use crate::TerminalView; - -///The information generated during layout that is necessary for painting -pub struct LayoutState { - cells: Vec, - rects: Vec, - relative_highlighted_ranges: Vec<(RangeInclusive, Hsla)>, - cursor: Option, - background_color: Hsla, - size: TerminalSize, - mode: TermMode, - display_offset: usize, - hyperlink_tooltip: Option>, - gutter: f32, -} - -///Helper struct for converting data between alacritty's cursor points, and displayed cursor points -struct DisplayCursor { - line: i32, - col: usize, -} - -impl DisplayCursor { - fn from(cursor_point: AlacPoint, display_offset: usize) -> Self { - Self { - line: cursor_point.line.0 + display_offset as i32, - col: cursor_point.column.0, - } - } - - pub fn line(&self) -> i32 { - self.line - } - - pub fn col(&self) -> usize { - self.col - } -} - -#[derive(Clone, Debug, Default)] -struct LayoutCell { - point: AlacPoint, - text: Line, -} - -impl LayoutCell { - fn new(point: AlacPoint, text: Line) -> LayoutCell { - LayoutCell { point, text } - } - - fn paint( - &self, - origin: Point, - layout: &LayoutState, - _visible_bounds: Bounds, - _view: &mut TerminalView, - cx: &mut WindowContext, - ) { - let pos = { - let point = self.point; - - Point::new( - (origin.x + point.column as f32 * layout.size.cell_width).floor(), - origin.y + point.line as f32 * layout.size.line_height, - ) - }; - - self.text.paint(pos, layout.size.line_height, cx); - } -} - -#[derive(Clone, Debug, Default)] -struct LayoutRect { - point: AlacPoint, - num_of_cells: usize, - color: Hsla, -} - -impl LayoutRect { - fn new(point: AlacPoint, num_of_cells: usize, color: Hsla) -> LayoutRect { - LayoutRect { - point, - num_of_cells, - color, - } - } - - fn extend(&self) -> Self { - LayoutRect { - point: self.point, - num_of_cells: self.num_of_cells + 1, - color: self.color, - } - } - - fn paint( - &self, - origin: Point, - layout: &LayoutState, - _view: &mut TerminalView, - cx: &mut ViewContext, - ) { - let position = { - let point = self.point; - vec2f( - (origin.x() + point.column as f32 * layout.size.cell_width).floor(), - origin.y() + point.line as f32 * layout.size.line_height, - ) - }; - let size = vec2f( - (layout.size.cell_width * self.num_of_cells as f32).ceil(), - layout.size.line_height, - ); - - cx.paint_quad( - Bounds::new(position, size), - Default::default(), - self.color, - Default::default(), - Default::default(), - ); - } -} - -///The GPUI element that paints the terminal. -///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection? -pub struct TerminalElement { - terminal: WeakModel, - focused: bool, - cursor_visible: bool, - can_navigate_to_selected_word: bool, -} - -impl TerminalElement { - pub fn new( - terminal: WeakModel, - focused: bool, - cursor_visible: bool, - can_navigate_to_selected_word: bool, - ) -> TerminalElement { - TerminalElement { - terminal, - focused, - cursor_visible, - can_navigate_to_selected_word, - } - } - - //Vec> -> Clip out the parts of the ranges - - fn layout_grid( - grid: &Vec, - text_style: &TextStyle, - terminal_theme: &TerminalStyle, - text_layout_cache: &TextLayoutCache, - font_cache: &FontCache, - hyperlink: Option<(HighlightStyle, &RangeInclusive)>, - ) -> (Vec, Vec) { - let mut cells = vec![]; - let mut rects = vec![]; - - let mut cur_rect: Option = None; - let mut cur_alac_color = None; - - let linegroups = grid.into_iter().group_by(|i| i.point.line); - for (line_index, (_, line)) in linegroups.into_iter().enumerate() { - for cell in line { - let mut fg = cell.fg; - let mut bg = cell.bg; - if cell.flags.contains(Flags::INVERSE) { - mem::swap(&mut fg, &mut bg); - } - - //Expand background rect range - { - if matches!(bg, Named(NamedColor::Background)) { - //Continue to next cell, resetting variables if necessary - cur_alac_color = None; - if let Some(rect) = cur_rect { - rects.push(rect); - cur_rect = None - } - } else { - match cur_alac_color { - Some(cur_color) => { - if bg == cur_color { - cur_rect = cur_rect.take().map(|rect| rect.extend()); - } else { - cur_alac_color = Some(bg); - if cur_rect.is_some() { - rects.push(cur_rect.take().unwrap()); - } - cur_rect = Some(LayoutRect::new( - AlacPoint::new( - line_index as i32, - cell.point.column.0 as i32, - ), - 1, - convert_color(&bg, &terminal_theme), - )); - } - } - None => { - cur_alac_color = Some(bg); - cur_rect = Some(LayoutRect::new( - AlacPoint::new(line_index as i32, cell.point.column.0 as i32), - 1, - convert_color(&bg, &terminal_theme), - )); - } - } - } - } - - //Layout current cell text - { - let cell_text = &cell.c.to_string(); - if !is_blank(&cell) { - let cell_style = TerminalElement::cell_style( - &cell, - fg, - terminal_theme, - text_style, - font_cache, - hyperlink, - ); - - let layout_cell = text_layout_cache.layout_str( - cell_text, - text_style.font_size, - &[(cell_text.len(), cell_style)], - ); - - cells.push(LayoutCell::new( - AlacPoint::new(line_index as i32, cell.point.column.0 as i32), - layout_cell, - )) - }; - } - } - - if cur_rect.is_some() { - rects.push(cur_rect.take().unwrap()); - } - } - (cells, rects) - } - - // 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 - fn shape_cursor( - cursor_point: DisplayCursor, - size: TerminalSize, - text_fragment: &Line, - ) -> Option<(Vector2F, f32)> { - if cursor_point.line() < size.total_lines() as i32 { - let cursor_width = if text_fragment.width == 0. { - size.cell_width() - } else { - text_fragment.width - }; - - //Cursor should always surround as much of the text as possible, - //hence when on pixel boundaries round the origin down and the width up - Some(( - vec2f( - (cursor_point.col() as f32 * size.cell_width()).floor(), - (cursor_point.line() as f32 * size.line_height()).floor(), - ), - cursor_width.ceil(), - )) - } else { - None - } - } - - ///Convert the Alacritty cell styles to GPUI text styles and background color - fn cell_style( - indexed: &IndexedCell, - fg: terminal::alacritty_terminal::ansi::Color, - style: &TerminalStyle, - text_style: &TextStyle, - font_cache: &FontCache, - hyperlink: Option<(HighlightStyle, &RangeInclusive)>, - ) -> RunStyle { - let flags = indexed.cell.flags; - let fg = convert_color(&fg, &style); - - let mut underline = flags - .intersects(Flags::ALL_UNDERLINES) - .then(|| Underline { - color: Some(fg), - squiggly: flags.contains(Flags::UNDERCURL), - thickness: OrderedFloat(1.), - }) - .unwrap_or_default(); - - if indexed.cell.hyperlink().is_some() { - if underline.thickness == OrderedFloat(0.) { - underline.thickness = OrderedFloat(1.); - } - } - - let mut properties = Properties::new(); - if indexed.flags.intersects(Flags::BOLD | Flags::DIM_BOLD) { - properties = *properties.weight(Weight::BOLD); - } - if indexed.flags.intersects(Flags::ITALIC) { - properties = *properties.style(Italic); - } - - let font_id = font_cache - .select_font(text_style.font_family_id, &properties) - .unwrap_or(8text_style.font_id); - - let mut result = RunStyle { - color: fg, - font_id, - underline, - }; - - if let Some((style, range)) = hyperlink { - if range.contains(&indexed.point) { - if let Some(underline) = style.underline { - result.underline = underline; - } - - if let Some(color) = style.color { - result.color = color; - } - } - } - - result - } - - fn generic_button_handler( - connection: WeakModel, - origin: Point, - f: impl Fn(&mut Terminal, Vector2F, E, &mut ModelContext), - ) -> impl Fn(E, &mut TerminalView, &mut EventContext) { - move |event, _: &mut TerminalView, cx| { - cx.focus_parent(); - if let Some(conn_handle) = connection.upgrade() { - conn_handle.update(cx, |terminal, cx| { - f(terminal, origin, event, cx); - - cx.notify(); - }) - } - } - } - - fn attach_mouse_handlers( - &self, - origin: Point, - visible_bounds: Bounds, - mode: TermMode, - cx: &mut ViewContext, - ) { - let connection = self.terminal; - - let mut region = MouseRegion::new::(cx.view_id(), 0, visible_bounds); - - // Terminal Emulator controlled behavior: - region = region - // Start selections - .on_down(MouseButton::Left, move |event, v: &mut TerminalView, cx| { - let terminal_view = cx.handle(); - cx.focus(&terminal_view); - v.context_menu.update(cx, |menu, _cx| menu.delay_cancel()); - if let Some(conn_handle) = connection.upgrade() { - conn_handle.update(cx, |terminal, cx| { - terminal.mouse_down(&event, origin); - - cx.notify(); - }) - } - }) - // Update drag selections - .on_drag(MouseButton::Left, move |event, _: &mut TerminalView, cx| { - if event.end { - return; - } - - if cx.is_self_focused() { - if let Some(conn_handle) = connection.upgrade() { - conn_handle.update(cx, |terminal, cx| { - terminal.mouse_drag(event, origin); - cx.notify(); - }) - } - } - }) - // Copy on up behavior - .on_up( - MouseButton::Left, - TerminalElement::generic_button_handler( - connection, - origin, - move |terminal, origin, e, cx| { - terminal.mouse_up(&e, origin, cx); - }, - ), - ) - // Context menu - .on_click( - MouseButton::Right, - move |event, view: &mut TerminalView, cx| { - let mouse_mode = if let Some(conn_handle) = connection.upgrade() { - conn_handle.update(cx, |terminal, _cx| terminal.mouse_mode(event.shift)) - } else { - // If we can't get the model handle, probably can't deploy the context menu - true - }; - if !mouse_mode { - view.deploy_context_menu(event.position, cx); - } - }, - ) - .on_move(move |event, _: &mut TerminalView, cx| { - if cx.is_self_focused() { - if let Some(conn_handle) = connection.upgrade() { - conn_handle.update(cx, |terminal, cx| { - terminal.mouse_move(&event, origin); - cx.notify(); - }) - } - } - }) - .on_scroll(move |event, _: &mut TerminalView, cx| { - if let Some(conn_handle) = connection.upgrade() { - conn_handle.update(cx, |terminal, cx| { - terminal.scroll_wheel(event, origin); - cx.notify(); - }) - } - }); - - // Mouse mode handlers: - // All mouse modes need the extra click handlers - if mode.intersects(TermMode::MOUSE_MODE) { - region = region - .on_down( - MouseButton::Right, - TerminalElement::generic_button_handler( - connection, - origin, - move |terminal, origin, e, _cx| { - terminal.mouse_down(&e, origin); - }, - ), - ) - .on_down( - MouseButton::Middle, - TerminalElement::generic_button_handler( - connection, - origin, - move |terminal, origin, e, _cx| { - terminal.mouse_down(&e, origin); - }, - ), - ) - .on_up( - MouseButton::Right, - TerminalElement::generic_button_handler( - connection, - origin, - move |terminal, origin, e, cx| { - terminal.mouse_up(&e, origin, cx); - }, - ), - ) - .on_up( - MouseButton::Middle, - TerminalElement::generic_button_handler( - connection, - origin, - move |terminal, origin, e, cx| { - terminal.mouse_up(&e, origin, cx); - }, - ), - ) - } - - cx.scene().push_mouse_region(region); - } -} - -impl Element for TerminalElement { - type ElementState = LayoutState; - - fn layout( - &mut self, - view_state: &mut TerminalView, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) -> LayoutId { - let settings = ThemeSettings::get_global(cx); - let terminal_settings = TerminalSettings::get_global(cx); - - //Setup layout information - let terminal_theme = settings.theme.terminal.clone(); //TODO: Try to minimize this clone. - let link_style = settings.theme.editor.link_definition; - let tooltip_style = settings.theme.tooltip.clone(); - - let font_cache = cx.font_cache(); - let font_size = font_size(&terminal_settings, cx).unwrap_or(settings.buffer_font_size(cx)); - let font_family_name = terminal_settings - .font_family - .as_ref() - .unwrap_or(&settings.buffer_font_family_name); - let font_features = terminal_settings - .font_features - .as_ref() - .unwrap_or(&settings.buffer_font_features); - let family_id = font_cache - .load_family(&[font_family_name], &font_features) - .log_err() - .unwrap_or(settings.buffer_font_family); - let font_id = font_cache - .select_font(family_id, &Default::default()) - .unwrap(); - - let text_style = TextStyle { - color: settings.theme.editor.text_color, - font_family_id: family_id, - font_family_name: font_cache.family_name(family_id).unwrap(), - font_id, - font_size, - font_properties: Default::default(), - underline: Default::default(), - soft_wrap: false, - }; - let selection_color = settings.theme.editor.selection.selection; - let match_color = settings.theme.search.match_background; - let gutter; - let dimensions = { - let line_height = text_style.font_size * terminal_settings.line_height.value(); - let cell_width = font_cache.em_advance(text_style.font_id, text_style.font_size); - gutter = cell_width; - - let size = constraint.max - vec2f(gutter, 0.); - TerminalSize::new(line_height, cell_width, size) - }; - - let search_matches = if let Some(terminal_model) = self.terminal.upgrade() { - terminal_model.read(cx).matches.clone() - } else { - Default::default() - }; - - let background_color = terminal_theme.background; - let terminal_handle = self.terminal.upgrade().unwrap(); - - let last_hovered_word = terminal_handle.update(cx, |terminal, cx| { - terminal.set_size(dimensions); - terminal.try_sync(cx); - if self.can_navigate_to_selected_word && terminal.can_navigate_to_selected_word() { - terminal.last_content.last_hovered_word.clone() - } else { - None - } - }); - - let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| { - let mut tooltip = Overlay::new( - Empty::new() - .contained() - .constrained() - .with_width(dimensions.width()) - .with_height(dimensions.height()) - .with_tooltip::( - hovered_word.id, - hovered_word.word, - None, - tooltip_style, - cx, - ), - ) - .with_position_mode(gpui::elements::OverlayPositionMode::Local) - .into_any(); - - tooltip.layout( - SizeConstraint::new(Vector2F::zero(), cx.window_size()), - view_state, - cx, - ); - tooltip - }); - - let TerminalContent { - cells, - mode, - display_offset, - cursor_char, - selection, - cursor, - .. - } = { &terminal_handle.read(cx).last_content }; - - // searches, highlights to a single range representations - let mut relative_highlighted_ranges = Vec::new(); - for search_match in search_matches { - relative_highlighted_ranges.push((search_match, match_color)) - } - if let Some(selection) = selection { - relative_highlighted_ranges.push((selection.start..=selection.end, selection_color)); - } - - // then have that representation be converted to the appropriate highlight data structure - - let (cells, rects) = TerminalElement::layout_grid( - cells, - &text_style, - &terminal_theme, - cx.text_layout_cache(), - cx.font_cache(), - last_hovered_word - .as_ref() - .map(|last_hovered_word| (link_style, &last_hovered_word.word_match)), - ); - - //Layout cursor. Rectangle is used for IME, so we should lay it out even - //if we don't end up showing it. - let cursor = if let AlacCursorShape::Hidden = cursor.shape { - None - } else { - let cursor_point = DisplayCursor::from(cursor.point, *display_offset); - let cursor_text = { - let str_trxt = cursor_char.to_string(); - - let color = if self.focused { - terminal_theme.background - } else { - terminal_theme.foreground - }; - - cx.text_layout_cache().layout_str( - &str_trxt, - text_style.font_size, - &[( - str_trxt.len(), - RunStyle { - font_id: text_style.font_id, - color, - underline: Default::default(), - }, - )], - ) - }; - - let focused = self.focused; - TerminalElement::shape_cursor(cursor_point, dimensions, &cursor_text).map( - move |(cursor_position, block_width)| { - let (shape, text) = match cursor.shape { - AlacCursorShape::Block if !focused => (CursorShape::Hollow, None), - AlacCursorShape::Block => (CursorShape::Block, Some(cursor_text)), - AlacCursorShape::Underline => (CursorShape::Underscore, None), - AlacCursorShape::Beam => (CursorShape::Bar, None), - AlacCursorShape::HollowBlock => (CursorShape::Hollow, None), - //This case is handled in the if wrapping the whole cursor layout - AlacCursorShape::Hidden => unreachable!(), - }; - - Cursor::new( - cursor_position, - block_width, - dimensions.line_height, - terminal_theme.cursor, - shape, - text, - ) - }, - ) - }; - - //Done! - ( - constraint.max, - Self::ElementState { - cells, - cursor, - background_color, - size: dimensions, - rects, - relative_highlighted_ranges, - mode: *mode, - display_offset: *display_offset, - hyperlink_tooltip, - gutter, - }, - ) - } - - fn paint( - &mut self, - bounds: Bounds, - view_state: &mut TerminalView, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); - - //Setup element stuff - let clip_bounds = Some(visible_bounds); - - cx.paint_layer(clip_bounds, |cx| { - let origin = bounds.origin() + vec2f(element_state.gutter, 0.); - - // Elements are ephemeral, only at paint time do we know what could be clicked by a mouse - self.attach_mouse_handlers(origin, visible_bounds, element_state.mode, cx); - - cx.scene().push_cursor_region(gpui::CursorRegion { - bounds, - style: if element_state.hyperlink_tooltip.is_some() { - CursorStyle::AlacPointingHand - } else { - CursorStyle::IBeam - }, - }); - - cx.paint_layer(clip_bounds, |cx| { - //Start with a background color - cx.scene().push_quad(Quad { - bounds, - background: Some(element_state.background_color), - border: Default::default(), - corner_radii: Default::default(), - }); - - for rect in &element_state.rects { - rect.paint(origin, element_state, view_state, cx); - } - }); - - //Draw Highlighted Backgrounds - cx.paint_layer(clip_bounds, |cx| { - for (relative_highlighted_range, color) in - element_state.relative_highlighted_ranges.iter() - { - if let Some((start_y, highlighted_range_lines)) = to_highlighted_range_lines( - relative_highlighted_range, - element_state, - origin, - ) { - let hr = HighlightedRange { - start_y, //Need to change this - line_height: element_state.size.line_height, - lines: highlighted_range_lines, - color: color.clone(), - //Copied from editor. TODO: move to theme or something - corner_radius: 0.15 * element_state.size.line_height, - }; - hr.paint(bounds, cx); - } - } - }); - - //Draw the text cells - cx.paint_layer(clip_bounds, |cx| { - for cell in &element_state.cells { - cell.paint(origin, element_state, visible_bounds, view_state, cx); - } - }); - - //Draw cursor - if self.cursor_visible { - if let Some(cursor) = &element_state.cursor { - cx.paint_layer(clip_bounds, |cx| { - cursor.paint(origin, cx); - }) - } - } - - if let Some(element) = &mut element_state.hyperlink_tooltip { - element.paint(origin, visible_bounds, view_state, cx) - } - }); - } - - fn id(&self) -> Option { - todo!() - } - - fn initialize( - &mut self, - view_state: &mut TerminalView, - element_state: Option, - cx: &mut ViewContext, - ) -> Self::ElementState { - todo!() - } - - // todo!() remove? - // fn metadata(&self) -> Option<&dyn std::any::Any> { - // None - // } - - // fn debug( - // &self, - // _: Bounds, - // _: &Self::ElementState, - // _: &Self::PaintState, - // _: &TerminalView, - // _: &gpui::ViewContext, - // ) -> gpui::serde_json::Value { - // json!({ - // "type": "TerminalElement", - // }) - // } - - // fn rect_for_text_range( - // &self, - // _: Range, - // bounds: Bounds, - // _: Bounds, - // layout: &Self::ElementState, - // _: &Self::PaintState, - // _: &TerminalView, - // _: &gpui::ViewContext, - // ) -> Option> { - // // Use the same origin that's passed to `Cursor::paint` in the paint - // // method bove. - // let mut origin = bounds.origin() + vec2f(layout.size.cell_width, 0.); - - // // TODO - Why is it necessary to move downward one line to get correct - // // positioning? I would think that we'd want the same rect that is - // // painted for the cursor. - // origin += vec2f(0., layout.size.line_height); - - // Some(layout.cursor.as_ref()?.bounding_rect(origin)) - // } -} - -impl Component for TerminalElement { - fn render(self) -> AnyElement { - todo!() - } -} - -fn is_blank(cell: &IndexedCell) -> bool { - if cell.c != ' ' { - return false; - } - - if cell.bg != AnsiColor::Named(NamedColor::Background) { - return false; - } - - if cell.hyperlink().is_some() { - return false; - } - - if cell - .flags - .intersects(Flags::ALL_UNDERLINES | Flags::INVERSE | Flags::STRIKEOUT) - { - return false; - } - - return true; -} - -fn to_highlighted_range_lines( - range: &RangeInclusive, - layout: &LayoutState, - origin: Point, -) -> Option<(f32, Vec)> { - // Step 1. Normalize the points to be viewport relative. - // When display_offset = 1, here's how the grid is arranged: - //-2,0 -2,1... - //--- Viewport top - //-1,0 -1,1... - //--------- Terminal Top - // 0,0 0,1... - // 1,0 1,1... - //--- Viewport Bottom - // 2,0 2,1... - //--------- Terminal Bottom - - // Normalize to viewport relative, from terminal relative. - // lines are i32s, which are negative above the top left corner of the terminal - // If the user has scrolled, we use the display_offset to tell us which offset - // of the grid data we should be looking at. But for the rendering step, we don't - // want negatives. We want things relative to the 'viewport' (the area of the grid - // which is currently shown according to the display offset) - let unclamped_start = AlacPoint::new( - range.start().line + layout.display_offset, - range.start().column, - ); - let unclamped_end = - AlacPoint::new(range.end().line + layout.display_offset, range.end().column); - - // Step 2. Clamp range to viewport, and return None if it doesn't overlap - if unclamped_end.line.0 < 0 || unclamped_start.line.0 > layout.size.num_lines() as i32 { - return None; - } - - let clamped_start_line = unclamped_start.line.0.max(0) as usize; - let clamped_end_line = unclamped_end.line.0.min(layout.size.num_lines() as i32) as usize; - //Convert the start of the range to pixels - let start_y = origin.y + clamped_start_line as f32 * layout.size.line_height; - - // Step 3. Expand ranges that cross lines into a collection of single-line ranges. - // (also convert to pixels) - let mut highlighted_range_lines = Vec::new(); - for line in clamped_start_line..=clamped_end_line { - let mut line_start = 0; - let mut line_end = layout.size.columns(); - - if line == clamped_start_line { - line_start = unclamped_start.column.0 as usize; - } - if line == clamped_end_line { - line_end = unclamped_end.column.0 as usize + 1; //+1 for inclusive - } - - highlighted_range_lines.push(HighlightedRangeLine { - start_x: origin.x() + line_start as f32 * layout.size.cell_width, - end_x: origin.x() + line_end as f32 * layout.size.cell_width, - }); - } - - Some((start_y, highlighted_range_lines)) -} - -fn font_size(terminal_settings: &TerminalSettings, cx: &mut AppContext) -> Option { - terminal_settings - .font_size - .map(|size| theme::adjusted_font_size(size, cx)) -} +// use editor::{Cursor, HighlightedRange, HighlightedRangeLine}; +// use gpui::{ +// AnyElement, AppContext, Bounds, Component, Element, HighlightStyle, Hsla, LayoutId, Line, +// ModelContext, MouseButton, Pixels, Point, TextStyle, Underline, ViewContext, WeakModel, +// WindowContext, +// }; +// use itertools::Itertools; +// use language::CursorShape; +// use ordered_float::OrderedFloat; +// use settings::Settings; +// use terminal::{ +// alacritty_terminal::{ +// ansi::{Color as AnsiColor, Color::Named, CursorShape as AlacCursorShape, NamedColor}, +// grid::Dimensions, +// index::Point as AlacPoint, +// term::{cell::Flags, TermMode}, +// }, +// // mappings::colors::convert_color, +// terminal_settings::TerminalSettings, +// IndexedCell, +// Terminal, +// TerminalContent, +// TerminalSize, +// }; +// use theme::ThemeSettings; + +// use std::mem; +// use std::{fmt::Debug, ops::RangeInclusive}; + +// use crate::TerminalView; + +// ///The information generated during layout that is necessary for painting +// pub struct LayoutState { +// cells: Vec, +// rects: Vec, +// relative_highlighted_ranges: Vec<(RangeInclusive, Hsla)>, +// cursor: Option, +// background_color: Hsla, +// size: TerminalSize, +// mode: TermMode, +// display_offset: usize, +// hyperlink_tooltip: Option>, +// gutter: f32, +// } + +// ///Helper struct for converting data between alacritty's cursor points, and displayed cursor points +// struct DisplayCursor { +// line: i32, +// col: usize, +// } + +// impl DisplayCursor { +// fn from(cursor_point: AlacPoint, display_offset: usize) -> Self { +// Self { +// line: cursor_point.line.0 + display_offset as i32, +// col: cursor_point.column.0, +// } +// } + +// pub fn line(&self) -> i32 { +// self.line +// } + +// pub fn col(&self) -> usize { +// self.col +// } +// } + +// #[derive(Clone, Debug, Default)] +// struct LayoutCell { +// point: AlacPoint, +// text: Line, +// } + +// impl LayoutCell { +// fn new(point: AlacPoint, text: Line) -> LayoutCell { +// LayoutCell { point, text } +// } + +// fn paint( +// &self, +// origin: Point, +// layout: &LayoutState, +// _visible_bounds: Bounds, +// _view: &mut TerminalView, +// cx: &mut WindowContext, +// ) { +// let pos = { +// let point = self.point; + +// Point::new( +// (origin.x + point.column as f32 * layout.size.cell_width).floor(), +// origin.y + point.line as f32 * layout.size.line_height, +// ) +// }; + +// self.text.paint(pos, layout.size.line_height, cx); +// } +// } + +// #[derive(Clone, Debug, Default)] +// struct LayoutRect { +// point: AlacPoint, +// num_of_cells: usize, +// color: Hsla, +// } + +// impl LayoutRect { +// fn new(point: AlacPoint, num_of_cells: usize, color: Hsla) -> LayoutRect { +// LayoutRect { +// point, +// num_of_cells, +// color, +// } +// } + +// fn extend(&self) -> Self { +// LayoutRect { +// point: self.point, +// num_of_cells: self.num_of_cells + 1, +// color: self.color, +// } +// } + +// fn paint( +// &self, +// origin: Point, +// layout: &LayoutState, +// _view: &mut TerminalView, +// cx: &mut ViewContext, +// ) { +// let position = { +// let point = self.point; +// vec2f( +// (origin.x() + point.column as f32 * layout.size.cell_width).floor(), +// origin.y() + point.line as f32 * layout.size.line_height, +// ) +// }; +// let size = vec2f( +// (layout.size.cell_width * self.num_of_cells as f32).ceil(), +// layout.size.line_height, +// ); + +// cx.paint_quad( +// Bounds::new(position, size), +// Default::default(), +// self.color, +// Default::default(), +// Default::default(), +// ); +// } +// } + +// ///The GPUI element that paints the terminal. +// ///We need to keep a reference to the view for mouse events, do we need it for any other terminal stuff, or can we move that to connection? +// pub struct TerminalElement { +// terminal: WeakModel, +// focused: bool, +// cursor_visible: bool, +// can_navigate_to_selected_word: bool, +// } + +// impl TerminalElement { +// pub fn new( +// terminal: WeakModel, +// focused: bool, +// cursor_visible: bool, +// can_navigate_to_selected_word: bool, +// ) -> TerminalElement { +// TerminalElement { +// terminal, +// focused, +// cursor_visible, +// can_navigate_to_selected_word, +// } +// } + +// //Vec> -> Clip out the parts of the ranges + +// fn layout_grid( +// grid: &Vec, +// text_style: &TextStyle, +// terminal_theme: &TerminalStyle, +// text_layout_cache: &TextLayoutCache, +// font_cache: &FontCache, +// hyperlink: Option<(HighlightStyle, &RangeInclusive)>, +// ) -> (Vec, Vec) { +// let mut cells = vec![]; +// let mut rects = vec![]; + +// let mut cur_rect: Option = None; +// let mut cur_alac_color = None; + +// let linegroups = grid.into_iter().group_by(|i| i.point.line); +// for (line_index, (_, line)) in linegroups.into_iter().enumerate() { +// for cell in line { +// let mut fg = cell.fg; +// let mut bg = cell.bg; +// if cell.flags.contains(Flags::INVERSE) { +// mem::swap(&mut fg, &mut bg); +// } + +// //Expand background rect range +// { +// if matches!(bg, Named(NamedColor::Background)) { +// //Continue to next cell, resetting variables if necessary +// cur_alac_color = None; +// if let Some(rect) = cur_rect { +// rects.push(rect); +// cur_rect = None +// } +// } else { +// match cur_alac_color { +// Some(cur_color) => { +// if bg == cur_color { +// cur_rect = cur_rect.take().map(|rect| rect.extend()); +// } else { +// cur_alac_color = Some(bg); +// if cur_rect.is_some() { +// rects.push(cur_rect.take().unwrap()); +// } +// cur_rect = Some(LayoutRect::new( +// AlacPoint::new( +// line_index as i32, +// cell.point.column.0 as i32, +// ), +// 1, +// convert_color(&bg, &terminal_theme), +// )); +// } +// } +// None => { +// cur_alac_color = Some(bg); +// cur_rect = Some(LayoutRect::new( +// AlacPoint::new(line_index as i32, cell.point.column.0 as i32), +// 1, +// convert_color(&bg, &terminal_theme), +// )); +// } +// } +// } +// } + +// //Layout current cell text +// { +// let cell_text = &cell.c.to_string(); +// if !is_blank(&cell) { +// let cell_style = TerminalElement::cell_style( +// &cell, +// fg, +// terminal_theme, +// text_style, +// font_cache, +// hyperlink, +// ); + +// let layout_cell = text_layout_cache.layout_str( +// cell_text, +// text_style.font_size, +// &[(cell_text.len(), cell_style)], +// ); + +// cells.push(LayoutCell::new( +// AlacPoint::new(line_index as i32, cell.point.column.0 as i32), +// layout_cell, +// )) +// }; +// } +// } + +// if cur_rect.is_some() { +// rects.push(cur_rect.take().unwrap()); +// } +// } +// (cells, rects) +// } + +// // 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 +// fn shape_cursor( +// cursor_point: DisplayCursor, +// size: TerminalSize, +// text_fragment: &Line, +// ) -> Option<(Vector2F, f32)> { +// if cursor_point.line() < size.total_lines() as i32 { +// let cursor_width = if text_fragment.width == 0. { +// size.cell_width() +// } else { +// text_fragment.width +// }; + +// //Cursor should always surround as much of the text as possible, +// //hence when on pixel boundaries round the origin down and the width up +// Some(( +// vec2f( +// (cursor_point.col() as f32 * size.cell_width()).floor(), +// (cursor_point.line() as f32 * size.line_height()).floor(), +// ), +// cursor_width.ceil(), +// )) +// } else { +// None +// } +// } + +// ///Convert the Alacritty cell styles to GPUI text styles and background color +// fn cell_style( +// indexed: &IndexedCell, +// fg: terminal::alacritty_terminal::ansi::Color, +// style: &TerminalStyle, +// text_style: &TextStyle, +// font_cache: &FontCache, +// hyperlink: Option<(HighlightStyle, &RangeInclusive)>, +// ) -> RunStyle { +// let flags = indexed.cell.flags; +// let fg = convert_color(&fg, &style); + +// let mut underline = flags +// .intersects(Flags::ALL_UNDERLINES) +// .then(|| Underline { +// color: Some(fg), +// squiggly: flags.contains(Flags::UNDERCURL), +// thickness: OrderedFloat(1.), +// }) +// .unwrap_or_default(); + +// if indexed.cell.hyperlink().is_some() { +// if underline.thickness == OrderedFloat(0.) { +// underline.thickness = OrderedFloat(1.); +// } +// } + +// let mut properties = Properties::new(); +// if indexed.flags.intersects(Flags::BOLD | Flags::DIM_BOLD) { +// properties = *properties.weight(Weight::BOLD); +// } +// if indexed.flags.intersects(Flags::ITALIC) { +// properties = *properties.style(Italic); +// } + +// let font_id = font_cache +// .select_font(text_style.font_family_id, &properties) +// .unwrap_or(8text_style.font_id); + +// let mut result = RunStyle { +// color: fg, +// font_id, +// underline, +// }; + +// if let Some((style, range)) = hyperlink { +// if range.contains(&indexed.point) { +// if let Some(underline) = style.underline { +// result.underline = underline; +// } + +// if let Some(color) = style.color { +// result.color = color; +// } +// } +// } + +// result +// } + +// fn generic_button_handler( +// connection: WeakModel, +// origin: Point, +// f: impl Fn(&mut Terminal, Vector2F, E, &mut ModelContext), +// ) -> impl Fn(E, &mut TerminalView, &mut EventContext) { +// move |event, _: &mut TerminalView, cx| { +// cx.focus_parent(); +// if let Some(conn_handle) = connection.upgrade() { +// conn_handle.update(cx, |terminal, cx| { +// f(terminal, origin, event, cx); + +// cx.notify(); +// }) +// } +// } +// } + +// fn attach_mouse_handlers( +// &self, +// origin: Point, +// visible_bounds: Bounds, +// mode: TermMode, +// cx: &mut ViewContext, +// ) { +// let connection = self.terminal; + +// let mut region = MouseRegion::new::(cx.view_id(), 0, visible_bounds); + +// // Terminal Emulator controlled behavior: +// region = region +// // Start selections +// .on_down(MouseButton::Left, move |event, v: &mut TerminalView, cx| { +// let terminal_view = cx.handle(); +// cx.focus(&terminal_view); +// v.context_menu.update(cx, |menu, _cx| menu.delay_cancel()); +// if let Some(conn_handle) = connection.upgrade() { +// conn_handle.update(cx, |terminal, cx| { +// terminal.mouse_down(&event, origin); + +// cx.notify(); +// }) +// } +// }) +// // Update drag selections +// .on_drag(MouseButton::Left, move |event, _: &mut TerminalView, cx| { +// if event.end { +// return; +// } + +// if cx.is_self_focused() { +// if let Some(conn_handle) = connection.upgrade() { +// conn_handle.update(cx, |terminal, cx| { +// terminal.mouse_drag(event, origin); +// cx.notify(); +// }) +// } +// } +// }) +// // Copy on up behavior +// .on_up( +// MouseButton::Left, +// TerminalElement::generic_button_handler( +// connection, +// origin, +// move |terminal, origin, e, cx| { +// terminal.mouse_up(&e, origin, cx); +// }, +// ), +// ) +// // Context menu +// .on_click( +// MouseButton::Right, +// move |event, view: &mut TerminalView, cx| { +// let mouse_mode = if let Some(conn_handle) = connection.upgrade() { +// conn_handle.update(cx, |terminal, _cx| terminal.mouse_mode(event.shift)) +// } else { +// // If we can't get the model handle, probably can't deploy the context menu +// true +// }; +// if !mouse_mode { +// view.deploy_context_menu(event.position, cx); +// } +// }, +// ) +// .on_move(move |event, _: &mut TerminalView, cx| { +// if cx.is_self_focused() { +// if let Some(conn_handle) = connection.upgrade() { +// conn_handle.update(cx, |terminal, cx| { +// terminal.mouse_move(&event, origin); +// cx.notify(); +// }) +// } +// } +// }) +// .on_scroll(move |event, _: &mut TerminalView, cx| { +// if let Some(conn_handle) = connection.upgrade() { +// conn_handle.update(cx, |terminal, cx| { +// terminal.scroll_wheel(event, origin); +// cx.notify(); +// }) +// } +// }); + +// // Mouse mode handlers: +// // All mouse modes need the extra click handlers +// if mode.intersects(TermMode::MOUSE_MODE) { +// region = region +// .on_down( +// MouseButton::Right, +// TerminalElement::generic_button_handler( +// connection, +// origin, +// move |terminal, origin, e, _cx| { +// terminal.mouse_down(&e, origin); +// }, +// ), +// ) +// .on_down( +// MouseButton::Middle, +// TerminalElement::generic_button_handler( +// connection, +// origin, +// move |terminal, origin, e, _cx| { +// terminal.mouse_down(&e, origin); +// }, +// ), +// ) +// .on_up( +// MouseButton::Right, +// TerminalElement::generic_button_handler( +// connection, +// origin, +// move |terminal, origin, e, cx| { +// terminal.mouse_up(&e, origin, cx); +// }, +// ), +// ) +// .on_up( +// MouseButton::Middle, +// TerminalElement::generic_button_handler( +// connection, +// origin, +// move |terminal, origin, e, cx| { +// terminal.mouse_up(&e, origin, cx); +// }, +// ), +// ) +// } + +// cx.scene().push_mouse_region(region); +// } +// } + +// impl Element for TerminalElement { +// type ElementState = LayoutState; + +// fn layout( +// &mut self, +// view_state: &mut TerminalView, +// element_state: &mut Self::ElementState, +// cx: &mut ViewContext, +// ) -> LayoutId { +// let settings = ThemeSettings::get_global(cx); +// let terminal_settings = TerminalSettings::get_global(cx); + +// //Setup layout information +// let terminal_theme = settings.theme.terminal.clone(); //TODO: Try to minimize this clone. +// let link_style = settings.theme.editor.link_definition; +// let tooltip_style = settings.theme.tooltip.clone(); + +// let font_cache = cx.font_cache(); +// let font_size = font_size(&terminal_settings, cx).unwrap_or(settings.buffer_font_size(cx)); +// let font_family_name = terminal_settings +// .font_family +// .as_ref() +// .unwrap_or(&settings.buffer_font_family_name); +// let font_features = terminal_settings +// .font_features +// .as_ref() +// .unwrap_or(&settings.buffer_font_features); +// let family_id = font_cache +// .load_family(&[font_family_name], &font_features) +// .log_err() +// .unwrap_or(settings.buffer_font_family); +// let font_id = font_cache +// .select_font(family_id, &Default::default()) +// .unwrap(); + +// let text_style = TextStyle { +// color: settings.theme.editor.text_color, +// font_family_id: family_id, +// font_family_name: font_cache.family_name(family_id).unwrap(), +// font_id, +// font_size, +// font_properties: Default::default(), +// underline: Default::default(), +// soft_wrap: false, +// }; +// let selection_color = settings.theme.editor.selection.selection; +// let match_color = settings.theme.search.match_background; +// let gutter; +// let dimensions = { +// let line_height = text_style.font_size * terminal_settings.line_height.value(); +// let cell_width = font_cache.em_advance(text_style.font_id, text_style.font_size); +// gutter = cell_width; + +// let size = constraint.max - vec2f(gutter, 0.); +// TerminalSize::new(line_height, cell_width, size) +// }; + +// let search_matches = if let Some(terminal_model) = self.terminal.upgrade() { +// terminal_model.read(cx).matches.clone() +// } else { +// Default::default() +// }; + +// let background_color = terminal_theme.background; +// let terminal_handle = self.terminal.upgrade().unwrap(); + +// let last_hovered_word = terminal_handle.update(cx, |terminal, cx| { +// terminal.set_size(dimensions); +// terminal.try_sync(cx); +// if self.can_navigate_to_selected_word && terminal.can_navigate_to_selected_word() { +// terminal.last_content.last_hovered_word.clone() +// } else { +// None +// } +// }); + +// let hyperlink_tooltip = last_hovered_word.clone().map(|hovered_word| { +// let mut tooltip = Overlay::new( +// Empty::new() +// .contained() +// .constrained() +// .with_width(dimensions.width()) +// .with_height(dimensions.height()) +// .with_tooltip::( +// hovered_word.id, +// hovered_word.word, +// None, +// tooltip_style, +// cx, +// ), +// ) +// .with_position_mode(gpui::elements::OverlayPositionMode::Local) +// .into_any(); + +// tooltip.layout( +// SizeConstraint::new(Vector2F::zero(), cx.window_size()), +// view_state, +// cx, +// ); +// tooltip +// }); + +// let TerminalContent { +// cells, +// mode, +// display_offset, +// cursor_char, +// selection, +// cursor, +// .. +// } = { &terminal_handle.read(cx).last_content }; + +// // searches, highlights to a single range representations +// let mut relative_highlighted_ranges = Vec::new(); +// for search_match in search_matches { +// relative_highlighted_ranges.push((search_match, match_color)) +// } +// if let Some(selection) = selection { +// relative_highlighted_ranges.push((selection.start..=selection.end, selection_color)); +// } + +// // then have that representation be converted to the appropriate highlight data structure + +// let (cells, rects) = TerminalElement::layout_grid( +// cells, +// &text_style, +// &terminal_theme, +// cx.text_layout_cache(), +// cx.font_cache(), +// last_hovered_word +// .as_ref() +// .map(|last_hovered_word| (link_style, &last_hovered_word.word_match)), +// ); + +// //Layout cursor. Rectangle is used for IME, so we should lay it out even +// //if we don't end up showing it. +// let cursor = if let AlacCursorShape::Hidden = cursor.shape { +// None +// } else { +// let cursor_point = DisplayCursor::from(cursor.point, *display_offset); +// let cursor_text = { +// let str_trxt = cursor_char.to_string(); + +// let color = if self.focused { +// terminal_theme.background +// } else { +// terminal_theme.foreground +// }; + +// cx.text_layout_cache().layout_str( +// &str_trxt, +// text_style.font_size, +// &[( +// str_trxt.len(), +// RunStyle { +// font_id: text_style.font_id, +// color, +// underline: Default::default(), +// }, +// )], +// ) +// }; + +// let focused = self.focused; +// TerminalElement::shape_cursor(cursor_point, dimensions, &cursor_text).map( +// move |(cursor_position, block_width)| { +// let (shape, text) = match cursor.shape { +// AlacCursorShape::Block if !focused => (CursorShape::Hollow, None), +// AlacCursorShape::Block => (CursorShape::Block, Some(cursor_text)), +// AlacCursorShape::Underline => (CursorShape::Underscore, None), +// AlacCursorShape::Beam => (CursorShape::Bar, None), +// AlacCursorShape::HollowBlock => (CursorShape::Hollow, None), +// //This case is handled in the if wrapping the whole cursor layout +// AlacCursorShape::Hidden => unreachable!(), +// }; + +// Cursor::new( +// cursor_position, +// block_width, +// dimensions.line_height, +// terminal_theme.cursor, +// shape, +// text, +// ) +// }, +// ) +// }; + +// //Done! +// ( +// constraint.max, +// Self::ElementState { +// cells, +// cursor, +// background_color, +// size: dimensions, +// rects, +// relative_highlighted_ranges, +// mode: *mode, +// display_offset: *display_offset, +// hyperlink_tooltip, +// gutter, +// }, +// ) +// } + +// fn paint( +// &mut self, +// bounds: Bounds, +// view_state: &mut TerminalView, +// element_state: &mut Self::ElementState, +// cx: &mut ViewContext, +// ) { +// let visible_bounds = bounds.intersection(visible_bounds).unwrap_or_default(); + +// //Setup element stuff +// let clip_bounds = Some(visible_bounds); + +// cx.paint_layer(clip_bounds, |cx| { +// let origin = bounds.origin() + vec2f(element_state.gutter, 0.); + +// // Elements are ephemeral, only at paint time do we know what could be clicked by a mouse +// self.attach_mouse_handlers(origin, visible_bounds, element_state.mode, cx); + +// cx.scene().push_cursor_region(gpui::CursorRegion { +// bounds, +// style: if element_state.hyperlink_tooltip.is_some() { +// CursorStyle::AlacPointingHand +// } else { +// CursorStyle::IBeam +// }, +// }); + +// cx.paint_layer(clip_bounds, |cx| { +// //Start with a background color +// cx.scene().push_quad(Quad { +// bounds, +// background: Some(element_state.background_color), +// border: Default::default(), +// corner_radii: Default::default(), +// }); + +// for rect in &element_state.rects { +// rect.paint(origin, element_state, view_state, cx); +// } +// }); + +// //Draw Highlighted Backgrounds +// cx.paint_layer(clip_bounds, |cx| { +// for (relative_highlighted_range, color) in +// element_state.relative_highlighted_ranges.iter() +// { +// if let Some((start_y, highlighted_range_lines)) = to_highlighted_range_lines( +// relative_highlighted_range, +// element_state, +// origin, +// ) { +// let hr = HighlightedRange { +// start_y, //Need to change this +// line_height: element_state.size.line_height, +// lines: highlighted_range_lines, +// color: color.clone(), +// //Copied from editor. TODO: move to theme or something +// corner_radius: 0.15 * element_state.size.line_height, +// }; +// hr.paint(bounds, cx); +// } +// } +// }); + +// //Draw the text cells +// cx.paint_layer(clip_bounds, |cx| { +// for cell in &element_state.cells { +// cell.paint(origin, element_state, visible_bounds, view_state, cx); +// } +// }); + +// //Draw cursor +// if self.cursor_visible { +// if let Some(cursor) = &element_state.cursor { +// cx.paint_layer(clip_bounds, |cx| { +// cursor.paint(origin, cx); +// }) +// } +// } + +// if let Some(element) = &mut element_state.hyperlink_tooltip { +// element.paint(origin, visible_bounds, view_state, cx) +// } +// }); +// } + +// fn id(&self) -> Option { +// todo!() +// } + +// // todo!() remove? +// // fn metadata(&self) -> Option<&dyn std::any::Any> { +// // None +// // } + +// // fn debug( +// // &self, +// // _: Bounds, +// // _: &Self::ElementState, +// // _: &Self::PaintState, +// // _: &TerminalView, +// // _: &gpui::ViewContext, +// // ) -> gpui::serde_json::Value { +// // json!({ +// // "type": "TerminalElement", +// // }) +// // } + +// // fn rect_for_text_range( +// // &self, +// // _: Range, +// // bounds: Bounds, +// // _: Bounds, +// // layout: &Self::ElementState, +// // _: &Self::PaintState, +// // _: &TerminalView, +// // _: &gpui::ViewContext, +// // ) -> Option> { +// // // Use the same origin that's passed to `Cursor::paint` in the paint +// // // method bove. +// // let mut origin = bounds.origin() + vec2f(layout.size.cell_width, 0.); + +// // // TODO - Why is it necessary to move downward one line to get correct +// // // positioning? I would think that we'd want the same rect that is +// // // painted for the cursor. +// // origin += vec2f(0., layout.size.line_height); + +// // Some(layout.cursor.as_ref()?.bounding_rect(origin)) +// // } +// } + +// impl Component for TerminalElement { +// fn render(self) -> AnyElement { +// todo!() +// } +// } + +// fn is_blank(cell: &IndexedCell) -> bool { +// if cell.c != ' ' { +// return false; +// } + +// if cell.bg != AnsiColor::Named(NamedColor::Background) { +// return false; +// } + +// if cell.hyperlink().is_some() { +// return false; +// } + +// if cell +// .flags +// .intersects(Flags::ALL_UNDERLINES | Flags::INVERSE | Flags::STRIKEOUT) +// { +// return false; +// } + +// return true; +// } + +// fn to_highlighted_range_lines( +// range: &RangeInclusive, +// layout: &LayoutState, +// origin: Point, +// ) -> Option<(f32, Vec)> { +// // Step 1. Normalize the points to be viewport relative. +// // When display_offset = 1, here's how the grid is arranged: +// //-2,0 -2,1... +// //--- Viewport top +// //-1,0 -1,1... +// //--------- Terminal Top +// // 0,0 0,1... +// // 1,0 1,1... +// //--- Viewport Bottom +// // 2,0 2,1... +// //--------- Terminal Bottom + +// // Normalize to viewport relative, from terminal relative. +// // lines are i32s, which are negative above the top left corner of the terminal +// // If the user has scrolled, we use the display_offset to tell us which offset +// // of the grid data we should be looking at. But for the rendering step, we don't +// // want negatives. We want things relative to the 'viewport' (the area of the grid +// // which is currently shown according to the display offset) +// let unclamped_start = AlacPoint::new( +// range.start().line + layout.display_offset, +// range.start().column, +// ); +// let unclamped_end = +// AlacPoint::new(range.end().line + layout.display_offset, range.end().column); + +// // Step 2. Clamp range to viewport, and return None if it doesn't overlap +// if unclamped_end.line.0 < 0 || unclamped_start.line.0 > layout.size.num_lines() as i32 { +// return None; +// } + +// let clamped_start_line = unclamped_start.line.0.max(0) as usize; +// let clamped_end_line = unclamped_end.line.0.min(layout.size.num_lines() as i32) as usize; +// //Convert the start of the range to pixels +// let start_y = origin.y + clamped_start_line as f32 * layout.size.line_height; + +// // Step 3. Expand ranges that cross lines into a collection of single-line ranges. +// // (also convert to pixels) +// let mut highlighted_range_lines = Vec::new(); +// for line in clamped_start_line..=clamped_end_line { +// let mut line_start = 0; +// let mut line_end = layout.size.columns(); + +// if line == clamped_start_line { +// line_start = unclamped_start.column.0 as usize; +// } +// if line == clamped_end_line { +// line_end = unclamped_end.column.0 as usize + 1; //+1 for inclusive +// } + +// highlighted_range_lines.push(HighlightedRangeLine { +// start_x: origin.x() + line_start as f32 * layout.size.cell_width, +// end_x: origin.x() + line_end as f32 * layout.size.cell_width, +// }); +// } + +// Some((start_y, highlighted_range_lines)) +// } + +// fn font_size(terminal_settings: &TerminalSettings, cx: &mut AppContext) -> Option { +// terminal_settings +// .font_size +// .map(|size| theme::adjusted_font_size(size, cx)) +// } diff --git a/crates/terminal_view2/src/terminal_panel.rs b/crates/terminal_view2/src/terminal_panel.rs index 94140450bcd29c3350087fdffea4752aa77b1336..fbb1bd535268ec3c06968f8afe5c6a55c9ea8cb4 100644 --- a/crates/terminal_view2/src/terminal_panel.rs +++ b/crates/terminal_view2/src/terminal_panel.rs @@ -3,8 +3,9 @@ use std::{path::PathBuf, sync::Arc}; use crate::TerminalView; use db::kvp::KEY_VALUE_STORE; use gpui::{ - actions, serde_json, Action, AppContext, AsyncAppContext, Entity, EventEmitter, Render, - Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowContext, + actions, div, serde_json, AppContext, AsyncWindowContext, Div, Entity, EventEmitter, + FocusHandle, FocusableView, ParentComponent, Render, Subscription, Task, View, ViewContext, + VisualContext, WeakView, WindowContext, }; use project::Fs; use serde::{Deserialize, Serialize}; @@ -14,7 +15,9 @@ use util::{ResultExt, TryFutureExt}; use workspace::{ dock::{DockPosition, Panel, PanelEvent}, item::Item, - pane, Pane, Workspace, + pane, + ui::Icon, + Pane, Workspace, }; use anyhow::Result; @@ -28,20 +31,14 @@ pub fn init(cx: &mut AppContext) { |workspace: &mut Workspace, _: &mut ViewContext| { workspace.register_action(TerminalPanel::new_terminal); workspace.register_action(TerminalPanel::open_terminal); + workspace.register_action(|workspace, _: &ToggleFocus, cx| { + workspace.toggle_panel_focus::(cx); + }); }, ) .detach(); } -#[derive(Debug)] -pub enum Event { - Close, - DockPositionChanged, - ZoomIn, - ZoomOut, - Focus, -} - pub struct TerminalPanel { pane: View, fs: Arc, @@ -54,9 +51,9 @@ pub struct TerminalPanel { impl TerminalPanel { fn new(workspace: &Workspace, cx: &mut ViewContext) -> Self { - let weak_self = cx.weak_handle(); + let _weak_self = cx.view().downgrade(); let pane = cx.build_view(|cx| { - let window = cx.window_handle(); + let _window = cx.window_handle(); let mut pane = Pane::new( workspace.weak_handle(), workspace.project().clone(), @@ -65,54 +62,55 @@ impl TerminalPanel { ); pane.set_can_split(false, cx); pane.set_can_navigate(false, cx); - pane.on_can_drop(move |drag_and_drop, cx| { - drag_and_drop - .currently_dragged::(window) - .map_or(false, |(_, item)| { - item.handle.act_as::(cx).is_some() - }) - }); - pane.set_render_tab_bar_buttons(cx, move |pane, cx| { - let this = weak_self.clone(); - Flex::row() - .with_child(Pane::render_tab_bar_button( - 0, - "icons/plus.svg", - false, - Some(("New Terminal", Some(Box::new(workspace::NewTerminal)))), - cx, - move |_, cx| { - let this = this.clone(); - cx.window_context().defer(move |cx| { - if let Some(this) = this.upgrade() { - this.update(cx, |this, cx| { - this.add_terminal(None, cx); - }); - } - }) - }, - |_, _| {}, - None, - )) - .with_child(Pane::render_tab_bar_button( - 1, - if pane.is_zoomed() { - "icons/minimize.svg" - } else { - "icons/maximize.svg" - }, - pane.is_zoomed(), - Some(("Toggle Zoom".into(), Some(Box::new(workspace::ToggleZoom)))), - cx, - move |pane, cx| pane.toggle_zoom(&Default::default(), cx), - |_, _| {}, - None, - )) - .into_any() - }); - let buffer_search_bar = cx.build_view(search::BufferSearchBar::new); - pane.toolbar() - .update(cx, |toolbar, cx| toolbar.add_item(buffer_search_bar, cx)); + // todo!() + // pane.on_can_drop(move |drag_and_drop, cx| { + // drag_and_drop + // .currently_dragged::(window) + // .map_or(false, |(_, item)| { + // item.handle.act_as::(cx).is_some() + // }) + // }); + // pane.set_render_tab_bar_buttons(cx, move |pane, cx| { + // let this = weak_self.clone(); + // Flex::row() + // .with_child(Pane::render_tab_bar_button( + // 0, + // "icons/plus.svg", + // false, + // Some(("New Terminal", Some(Box::new(workspace::NewTerminal)))), + // cx, + // move |_, cx| { + // let this = this.clone(); + // cx.window_context().defer(move |cx| { + // if let Some(this) = this.upgrade() { + // this.update(cx, |this, cx| { + // this.add_terminal(None, cx); + // }); + // } + // }) + // }, + // |_, _| {}, + // None, + // )) + // .with_child(Pane::render_tab_bar_button( + // 1, + // if pane.is_zoomed() { + // "icons/minimize.svg" + // } else { + // "icons/maximize.svg" + // }, + // pane.is_zoomed(), + // Some(("Toggle Zoom".into(), Some(Box::new(workspace::ToggleZoom)))), + // cx, + // move |pane, cx| pane.toggle_zoom(&Default::default(), cx), + // |_, _| {}, + // None, + // )) + // .into_any() + // }); + // let buffer_search_bar = cx.build_view(search::BufferSearchBar::new); + // pane.toolbar() + // .update(cx, |toolbar, cx| toolbar.add_item(buffer_search_bar, cx)); pane }); let subscriptions = vec![ @@ -133,80 +131,81 @@ impl TerminalPanel { let new_dock_position = this.position(cx); if new_dock_position != old_dock_position { old_dock_position = new_dock_position; - cx.emit(Event::DockPositionChanged); + cx.emit(PanelEvent::ChangePosition); } }) .detach(); this } - pub fn load(workspace: WeakView, cx: AsyncAppContext) -> Task>> { - cx.spawn(|mut cx| async move { - let serialized_panel = if let Some(panel) = cx - .background_executor() - .spawn(async move { KEY_VALUE_STORE.read_kvp(TERMINAL_PANEL_KEY) }) - .await - .log_err() - .flatten() - { - Some(serde_json::from_str::(&panel)?) + pub async fn load( + workspace: WeakView, + mut cx: AsyncWindowContext, + ) -> Result> { + let serialized_panel = cx + .background_executor() + .spawn(async move { KEY_VALUE_STORE.read_kvp(TERMINAL_PANEL_KEY) }) + .await + .log_err() + .flatten() + .map(|panel| serde_json::from_str::(&panel)) + .transpose() + .log_err() + .flatten(); + + let (panel, pane, items) = workspace.update(&mut cx, |workspace, cx| { + let panel = cx.build_view(|cx| TerminalPanel::new(workspace, cx)); + let items = if let Some(serialized_panel) = serialized_panel.as_ref() { + panel.update(cx, |panel, cx| { + cx.notify(); + panel.height = serialized_panel.height; + panel.width = serialized_panel.width; + panel.pane.update(cx, |_, cx| { + serialized_panel + .items + .iter() + .map(|item_id| { + TerminalView::deserialize( + workspace.project().clone(), + workspace.weak_handle(), + workspace.database_id(), + *item_id, + cx, + ) + }) + .collect::>() + }) + }) } else { - None + Default::default() }; - let (panel, pane, items) = workspace.update(&mut cx, |workspace, cx| { - let panel = cx.build_view(|cx| TerminalPanel::new(workspace, cx)); - let items = if let Some(serialized_panel) = serialized_panel.as_ref() { - panel.update(cx, |panel, cx| { - cx.notify(); - panel.height = serialized_panel.height; - panel.width = serialized_panel.width; - panel.pane.update(cx, |_, cx| { - serialized_panel - .items - .iter() - .map(|item_id| { - TerminalView::deserialize( - workspace.project().clone(), - workspace.weak_handle(), - workspace.database_id(), - *item_id, - cx, - ) - }) - .collect::>() - }) - }) - } else { - Default::default() - }; - let pane = panel.read(cx).pane.clone(); - (panel, pane, items) - })?; - - let pane = pane.downgrade(); - let items = futures::future::join_all(items).await; - pane.update(&mut cx, |pane, cx| { - let active_item_id = serialized_panel - .as_ref() - .and_then(|panel| panel.active_item_id); - let mut active_ix = None; - for item in items { - if let Some(item) = item.log_err() { - let item_id = item.entity_id().as_u64(); - pane.add_item(Box::new(item), false, false, None, cx); - if Some(item_id) == active_item_id { - active_ix = Some(pane.items_len() - 1); - } + let pane = panel.read(cx).pane.clone(); + (panel, pane, items) + })?; + + let pane = pane.downgrade(); + let items = futures::future::join_all(items).await; + pane.update(&mut cx, |pane, cx| { + let active_item_id = serialized_panel + .as_ref() + .and_then(|panel| panel.active_item_id); + let mut active_ix = None; + for item in items { + if let Some(item) = item.log_err() { + let item_id = item.entity_id().as_u64(); + pane.add_item(Box::new(item), false, false, None, cx); + if Some(item_id) == active_item_id { + active_ix = Some(pane.items_len() - 1); } } + } - if let Some(active_ix) = active_ix { - pane.activate_item(active_ix, false, false, cx) - } - })?; + if let Some(active_ix) = active_ix { + pane.activate_item(active_ix, false, false, cx) + } + })?; - Ok(panel) - }) + Ok(panel) } fn handle_pane_event( @@ -218,10 +217,10 @@ impl TerminalPanel { match event { pane::Event::ActivateItem { .. } => self.serialize(cx), pane::Event::RemoveItem { .. } => self.serialize(cx), - pane::Event::Remove => cx.emit(Event::Close), - pane::Event::ZoomIn => cx.emit(Event::ZoomIn), - pane::Event::ZoomOut => cx.emit(Event::ZoomOut), - pane::Event::Focus => cx.emit(Event::Focus), + pane::Event::Remove => cx.emit(PanelEvent::Close), + pane::Event::ZoomIn => cx.emit(PanelEvent::ZoomIn), + pane::Event::ZoomOut => cx.emit(PanelEvent::ZoomOut), + pane::Event::Focus => cx.emit(PanelEvent::Focus), pane::Event::AddItem { item } => { if let Some(workspace) = self.workspace.upgrade() { @@ -334,20 +333,20 @@ impl TerminalPanel { } } -impl EventEmitter for TerminalPanel {} impl EventEmitter for TerminalPanel {} impl Render for TerminalPanel { - fn render(&mut self, cx: &mut ViewContext) -> gpui::AnyElement { - ChildView::new(&self.pane, cx).into_any() + type Element = Div; + + fn render(&mut self, _cx: &mut ViewContext) -> Self::Element { + div().child(self.pane.clone()) } +} - // todo!() - // fn focus_in(&mut self, _: gpui::AnyView, cx: &mut ViewContext) { - // if cx.is_self_focused() { - // cx.focus(&self.pane); - // } - // } +impl FocusableView for TerminalPanel { + fn focus_handle(&self, cx: &AppContext) -> FocusHandle { + self.pane.focus_handle(cx) + } } impl Panel for TerminalPanel { @@ -407,14 +406,6 @@ impl Panel for TerminalPanel { } } - fn icon_path(&self, _: &WindowContext) -> Option<&'static str> { - Some("icons/terminal.svg") - } - - fn icon_tooltip(&self) -> (String, Option>) { - ("Terminal Panel".into(), Some(Box::new(ToggleFocus))) - } - fn icon_label(&self, cx: &WindowContext) -> Option { let count = self.pane.read(cx).items_len(); if count == 0 { @@ -428,34 +419,22 @@ impl Panel for TerminalPanel { self.pane.read(cx).has_focus(cx) } - fn persistent_name(&self) -> &'static str { - todo!() + fn persistent_name() -> &'static str { + "TerminalPanel" } - // todo!() is it needed? - // fn should_change_position_on_event(event: &Self::Event) -> bool { - // matches!(event, Event::DockPositionChanged) - // } - - // fn should_activate_on_event(_: &Self::Event) -> bool { - // false - // } - - // fn should_close_on_event(event: &Event) -> bool { - // matches!(event, Event::Close) - // } - - // fn is_focus_event(event: &Self::Event) -> bool { - // matches!(event, Event::Focus) + // todo!() + // fn icon_tooltip(&self) -> (String, Option>) { + // ("Terminal Panel".into(), Some(Box::new(ToggleFocus))) // } - // fn should_zoom_in_on_event(event: &Event) -> bool { - // matches!(event, Event::ZoomIn) - // } + fn icon(&self, _cx: &WindowContext) -> Option { + Some(Icon::Terminal) + } - // fn should_zoom_out_on_event(event: &Event) -> bool { - // matches!(event, Event::ZoomOut) - // } + fn toggle_action(&self) -> Box { + Box::new(ToggleFocus) + } } #[derive(Serialize, Deserialize)] diff --git a/crates/terminal_view2/src/terminal_view.rs b/crates/terminal_view2/src/terminal_view.rs index 1f0836422f6452d282e4ef80842f9f8aba18fdfc..55b7c1af669c0c7039f664bf23b2a93c520009a4 100644 --- a/crates/terminal_view2/src/terminal_view.rs +++ b/crates/terminal_view2/src/terminal_view.rs @@ -1,22 +1,23 @@ #![allow(unused_variables)] //todo!(remove) -// mod persistence; +mod persistence; pub mod terminal_element; pub mod terminal_panel; -use crate::terminal_element::TerminalElement; +// todo!() +// use crate::terminal_element::TerminalElement; use anyhow::Context; use dirs::home_dir; use editor::{scroll::autoscroll::Autoscroll, Editor}; use gpui::{ actions, div, img, red, register_action, AnyElement, AppContext, Component, DispatchPhase, Div, - EventEmitter, FocusEvent, FocusHandle, Focusable, FocusableKeyDispatch, InputHandler, - KeyDownEvent, Keystroke, Model, ParentElement, Pixels, Render, SharedString, - StatefulInteractivity, StatelessInteractive, Styled, Task, View, ViewContext, VisualContext, - WeakView, + EventEmitter, FocusEvent, FocusHandle, Focusable, FocusableComponent, FocusableView, + InputHandler, InteractiveComponent, KeyDownEvent, Keystroke, Model, ParentComponent, Pixels, + Render, SharedString, Styled, Task, View, ViewContext, VisualContext, WeakView, }; use language::Bias; +use persistence::TERMINAL_DB; use project::{search::SearchQuery, LocalWorktree, Project}; use serde::Deserialize; use settings::Settings; @@ -68,7 +69,7 @@ pub fn init(cx: &mut AppContext) { cx.observe_new_views( |workspace: &mut Workspace, cx: &mut ViewContext| { - workspace.register_action(TerminalView::deploy) + workspace.register_action(TerminalView::deploy); }, ) .detach(); @@ -94,6 +95,12 @@ impl EventEmitter for TerminalView {} impl EventEmitter for TerminalView {} impl EventEmitter for TerminalView {} +impl FocusableView for TerminalView { + fn focus_handle(&self, _cx: &AppContext) -> FocusHandle { + self.focus_handle.clone() + } +} + impl TerminalView { ///Create a new Terminal in the current working directory or the user's home directory pub fn deploy( @@ -159,15 +166,14 @@ impl TerminalView { let item_id = cx.entity_id(); let workspace_id = this.workspace_id; - // todo!(persistence) - // cx.background_executor() - // .spawn(async move { - // TERMINAL_DB - // .save_working_directory(item_id, workspace_id, cwd) - // .await - // .log_err(); - // }) - // .detach(); + cx.background_executor() + .spawn(async move { + TERMINAL_DB + .save_working_directory(item_id.as_u64(), workspace_id, cwd) + .await + .log_err(); + }) + .detach(); } } @@ -526,7 +532,7 @@ impl TerminalView { } impl Render for TerminalView { - type Element = Div, FocusableKeyDispatch>; + type Element = Focusable>; fn render(&mut self, cx: &mut ViewContext) -> Self::Element { let terminal_handle = self.terminal.clone().downgrade(); @@ -536,7 +542,7 @@ impl Render for TerminalView { div() .track_focus(&self.focus_handle) - .on_focus_in(Self::focus_out) + .on_focus_in(Self::focus_in) .on_focus_out(Self::focus_out) .on_key_down(Self::key_down) .on_action(TerminalView::send_text) @@ -828,10 +834,6 @@ impl Item for TerminalView { // .detach(); self.workspace_id = workspace.database_id(); } - - fn focus_handle(&self) -> FocusHandle { - self.focus_handle.clone() - } } impl SearchableItem for TerminalView { diff --git a/crates/workspace2/src/persistence/model.rs b/crates/workspace2/src/persistence/model.rs index 2b8ec94bd45d468b582e912141d907f53c592c83..fde052706bf8cf003395737af75c233f0ff1c381 100644 --- a/crates/workspace2/src/persistence/model.rs +++ b/crates/workspace2/src/persistence/model.rs @@ -277,7 +277,7 @@ impl SerializedPane { pub type GroupId = i64; pub type PaneId = i64; -pub type ItemId = usize; +pub type ItemId = u64; #[derive(Debug, PartialEq, Eq, Clone)] pub struct SerializedItem { diff --git a/crates/workspace2/src/workspace2.rs b/crates/workspace2/src/workspace2.rs index 1b8244833a1f0c045d8d26dd15b4288f45278183..f28675661de250b03f9e3d174da21d9835a2d2fd 100644 --- a/crates/workspace2/src/workspace2.rs +++ b/crates/workspace2/src/workspace2.rs @@ -15,13 +15,6 @@ mod status_bar; mod toolbar; mod workspace_settings; -pub use crate::persistence::{ - model::{ - DockData, DockStructure, ItemId, SerializedItem, SerializedPane, SerializedPaneGroup, - SerializedWorkspace, - }, - WorkspaceDb, -}; use anyhow::{anyhow, Context as _, Result}; use call2::ActiveCall; use client2::{ @@ -37,11 +30,10 @@ use futures::{ }; use gpui::{ actions, div, point, register_action, size, Action, AnyModel, AnyView, AnyWeakView, AppContext, - AsyncAppContext, AsyncWindowContext, Bounds, Div, Entity, EntityId, EventEmitter, FocusHandle, - FocusableView, GlobalPixels, KeyContext, Model, ModelContext, ParentElement, Point, Render, - Size, StatefulInteractive, StatelessInteractive, StatelessInteractivity, Styled, Subscription, - Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle, - WindowOptions, + AsyncAppContext, AsyncWindowContext, Bounds, Context, Div, Entity, EntityId, EventEmitter, + FocusHandle, FocusableView, GlobalPixels, InteractiveComponent, KeyContext, Model, + ModelContext, ParentComponent, Point, Render, Size, Styled, Subscription, Task, View, + ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle, WindowOptions, }; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem}; use itertools::Itertools; @@ -52,7 +44,10 @@ use node_runtime::NodeRuntime; use notifications::{simple_message_notification::MessageNotification, NotificationHandle}; pub use pane::*; pub use pane_group::*; -use persistence::{model::WorkspaceLocation, DB}; +pub use persistence::{ + model::{ItemId, SerializedWorkspace, WorkspaceLocation}, + WorkspaceDb, DB, +}; use postage::stream::Stream; use project2::{Project, ProjectEntryId, ProjectPath, Worktree}; use serde::Deserialize; @@ -69,10 +64,15 @@ use std::{ }; use theme2::{ActiveTheme, ThemeSettings}; pub use toolbar::{ToolbarItemLocation, ToolbarItemView}; +pub use ui; use util::ResultExt; use uuid::Uuid; pub use workspace_settings::{AutosaveSetting, WorkspaceSettings}; +use crate::persistence::model::{ + DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup, +}; + lazy_static! { static ref ZED_WINDOW_SIZE: Option> = env::var("ZED_WINDOW_SIZE") .ok() @@ -1582,13 +1582,11 @@ impl Workspace { self.serialize_workspace(cx); } - // /// Transfer focus to the panel of the given type. - // pub fn focus_panel(&mut self, cx: &mut ViewContext) -> Option> { - // self.focus_or_unfocus_panel::(cx, |_, _| true)? - // .as_any() - // .clone() - // .downcast() - // } + /// Transfer focus to the panel of the given type. + pub fn focus_panel(&mut self, cx: &mut ViewContext) -> Option> { + let panel = self.focus_or_unfocus_panel::(cx, |_, _| true)?; + panel.to_any().downcast().ok() + } /// Focus the panel of the given type if it isn't already focused. If it is /// already focused, then transfer focus back to the workspace center. @@ -2981,7 +2979,7 @@ impl Workspace { .filter_map(|item_handle| { Some(SerializedItem { kind: Arc::from(item_handle.serialized_item_kind()?), - item_id: item_handle.id().as_u64() as usize, + item_id: item_handle.id().as_u64(), active: Some(item_handle.id()) == active_item_id, }) }) diff --git a/crates/zed2/Cargo.toml b/crates/zed2/Cargo.toml index a1b29f022753510a8b7740c17167a8575d6b1a5d..f471ea230656bc116aab196a0e21c4a2d54d47e0 100644 --- a/crates/zed2/Cargo.toml +++ b/crates/zed2/Cargo.toml @@ -66,7 +66,7 @@ feature_flags = { package = "feature_flags2", path = "../feature_flags2" } sum_tree = { path = "../sum_tree" } shellexpand = "2.1.0" text = { package = "text2", path = "../text2" } -# terminal_view = { path = "../terminal_view" } +terminal_view = { package = "terminal_view2", path = "../terminal_view2" } theme = { package = "theme2", path = "../theme2" } # theme_selector = { path = "../theme_selector" } util = { path = "../util" } diff --git a/crates/zed2/src/main.rs b/crates/zed2/src/main.rs index abdbe94f2b0bd54b74c67b935bf9655b1f6da5ce..ee1a067a29d1e09b4f6e1893d1268d52f634b065 100644 --- a/crates/zed2/src/main.rs +++ b/crates/zed2/src/main.rs @@ -198,7 +198,7 @@ fn main() { // search::init(cx); // semantic_index::init(fs.clone(), http.clone(), languages.clone(), cx); // vim::init(cx); - // terminal_view::init(cx); + terminal_view::init(cx); // journal2::init(app_state.clone(), cx); // language_selector::init(cx); diff --git a/crates/zed2/src/zed2.rs b/crates/zed2/src/zed2.rs index 8fe5f2447ff16eb1c43fe81c8156721f2525be75..84cacccb5adb597b4b5fcd7650e11681b5ab7216 100644 --- a/crates/zed2/src/zed2.rs +++ b/crates/zed2/src/zed2.rs @@ -20,6 +20,7 @@ use anyhow::{anyhow, Context as _}; use project_panel::ProjectPanel; use settings::{initial_local_settings_content, Settings}; use std::{borrow::Cow, ops::Deref, sync::Arc}; +use terminal_view::terminal_panel::TerminalPanel; use util::{ asset_str, channel::ReleaseChannel, @@ -174,7 +175,7 @@ pub fn initialize_workspace(app_state: Arc, cx: &mut AppContext) { cx.spawn(|workspace_handle, mut cx| async move { let project_panel = ProjectPanel::load(workspace_handle.clone(), cx.clone()); - // let terminal_panel = TerminalPanel::load(workspace_handle.clone(), cx.clone()); + let terminal_panel = TerminalPanel::load(workspace_handle.clone(), cx.clone()); // let assistant_panel = AssistantPanel::load(workspace_handle.clone(), cx.clone()); let channels_panel = collab_ui::collab_panel::CollabPanel::load(workspace_handle.clone(), cx.clone()); @@ -186,14 +187,14 @@ pub fn initialize_workspace(app_state: Arc, cx: &mut AppContext) { // ); let ( project_panel, - // terminal_panel, + terminal_panel, // assistant_panel, channels_panel, // chat_panel, // notification_panel, ) = futures::try_join!( project_panel, - // terminal_panel, + terminal_panel, // assistant_panel, channels_panel, // chat_panel, @@ -203,7 +204,7 @@ pub fn initialize_workspace(app_state: Arc, cx: &mut AppContext) { workspace_handle.update(&mut cx, |workspace, cx| { let project_panel_position = project_panel.position(cx); workspace.add_panel(project_panel, cx); - // workspace.add_panel(terminal_panel, cx); + workspace.add_panel(terminal_panel, cx); // workspace.add_panel(assistant_panel, cx); workspace.add_panel(channels_panel, cx); // workspace.add_panel(chat_panel, cx);