From 2ae3fbd6b254c6a7ec089e1db3ba5521e218ad27 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Mon, 26 Sep 2022 19:37:55 -0700 Subject: [PATCH 1/3] Improved terminal selection ergonomics --- crates/terminal/src/terminal.rs | 15 ++++----------- crates/terminal/src/terminal_element.rs | 11 ----------- 2 files changed, 4 insertions(+), 22 deletions(-) diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs index 333be558fa..473bbd4f52 100644 --- a/crates/terminal/src/terminal.rs +++ b/crates/terminal/src/terminal.rs @@ -53,9 +53,7 @@ use thiserror::Error; use gpui::{ geometry::vector::{vec2f, Vector2F}, keymap::Keystroke, - scene::{ - ClickRegionEvent, DownRegionEvent, DragRegionEvent, ScrollWheelRegionEvent, UpRegionEvent, - }, + scene::{DownRegionEvent, DragRegionEvent, ScrollWheelRegionEvent, UpRegionEvent}, ClipboardItem, Entity, ModelContext, MouseButton, MouseMovedEvent, MutableAppContext, Task, }; @@ -969,8 +967,6 @@ impl Terminal { self.events .push_back(InternalEvent::Scroll(AlacScroll::Delta(scroll_lines))); - self.events - .push_back(InternalEvent::UpdateSelection(position)) } } } @@ -996,21 +992,18 @@ impl Terminal { self.last_content.size, self.last_content.display_offset, ); - let side = mouse_side(position, self.last_content.size); + // let side = mouse_side(position, self.last_content.size); if self.mouse_mode(e.shift) { if let Some(bytes) = mouse_button_report(point, e, true, self.last_content.mode) { self.pty_tx.notify(bytes); } } else if e.button == MouseButton::Left { - self.events.push_back(InternalEvent::SetSelection(Some(( - Selection::new(SelectionType::Simple, point, side), - point, - )))); + self.left_click(e, origin) } } - pub fn left_click(&mut self, e: &ClickRegionEvent, origin: Vector2F) { + pub fn left_click(&mut self, e: &DownRegionEvent, origin: Vector2F) { let position = e.position.sub(origin); if !self.mouse_mode(e.shift) { //Hyperlinks diff --git a/crates/terminal/src/terminal_element.rs b/crates/terminal/src/terminal_element.rs index a945c18c43..8cb193555d 100644 --- a/crates/terminal/src/terminal_element.rs +++ b/crates/terminal/src/terminal_element.rs @@ -429,17 +429,6 @@ impl TerminalElement { }, ), ) - // Handle click based selections - .on_click( - MouseButton::Left, - TerminalElement::generic_button_handler( - connection, - origin, - move |terminal, origin, e, _cx| { - terminal.left_click(&e, origin); - }, - ), - ) // Context menu .on_click(MouseButton::Right, move |e, cx| { let mouse_mode = if let Some(conn_handle) = connection.upgrade(cx.app) { From 24cc9859c7d28aaf35cf461839145314c4d9586f Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Mon, 26 Sep 2022 20:01:05 -0700 Subject: [PATCH 2/3] Added terminal::SendText command, for sending text to the terminal --- Cargo.lock | 1 + crates/terminal/Cargo.toml | 2 ++ crates/terminal/src/terminal_view.rs | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9d2c6a35fa..3c8a7ce1a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5489,6 +5489,7 @@ dependencies = [ "procinfo", "project", "rand 0.8.5", + "serde", "settings", "shellexpand", "smallvec", diff --git a/crates/terminal/Cargo.toml b/crates/terminal/Cargo.toml index da59979145..a0b5231501 100644 --- a/crates/terminal/Cargo.toml +++ b/crates/terminal/Cargo.toml @@ -30,6 +30,8 @@ libc = "0.2" anyhow = "1" thiserror = "1.0" lazy_static = "1.4.0" +serde = { version = "1.0", features = ["derive"] } + diff --git a/crates/terminal/src/terminal_view.rs b/crates/terminal/src/terminal_view.rs index 33d573a76a..856ed3af41 100644 --- a/crates/terminal/src/terminal_view.rs +++ b/crates/terminal/src/terminal_view.rs @@ -6,11 +6,12 @@ use gpui::{ actions, elements::{AnchorCorner, ChildView, ParentElement, Stack}, geometry::vector::Vector2F, - impl_internal_actions, + impl_actions, impl_internal_actions, keymap::Keystroke, AnyViewHandle, AppContext, Element, ElementBox, Entity, ModelHandle, MutableAppContext, Task, View, ViewContext, ViewHandle, }; +use serde::Deserialize; use settings::{Settings, TerminalBlink}; use smol::Timer; use workspace::pane; @@ -28,6 +29,9 @@ pub struct DeployContextMenu { pub position: Vector2F, } +#[derive(Clone, Default, Deserialize, PartialEq)] +pub struct SendText(String); + actions!( terminal, [ @@ -43,6 +47,9 @@ actions!( SearchTest ] ); + +impl_actions!(terminal, [SendText]); + impl_internal_actions!(project_panel, [DeployContextMenu]); pub fn init(cx: &mut MutableAppContext) { @@ -53,6 +60,7 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(TerminalView::escape); cx.add_action(TerminalView::enter); //Useful terminal views + cx.add_action(TerminalView::send_text); cx.add_action(TerminalView::deploy_context_menu); cx.add_action(TerminalView::copy); cx.add_action(TerminalView::paste); @@ -283,6 +291,13 @@ impl TerminalView { } } + 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()); + }); + } + ///Synthesize the keyboard event corresponding to 'up' fn up(&mut self, _: &Up, cx: &mut ViewContext) { self.clear_bel(cx); From 9a596030651ba89a3e48da2083bcf2d3c737e9f8 Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Mon, 26 Sep 2022 20:39:40 -0700 Subject: [PATCH 3/3] Added a SendKeystroke action and rewrote terminal actions to remove duplication --- assets/keymaps/default.json | 44 +++++++++++++++---- crates/terminal/src/terminal_view.rs | 64 +++++++++------------------- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index 7a25dc19d3..80ec6301c5 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -427,17 +427,45 @@ { "context": "Terminal", "bindings": { - // Overrides for global bindings, remove at your own risk: - "up": "terminal::Up", - "down": "terminal::Down", - "escape": "terminal::Escape", - "enter": "terminal::Enter", - "ctrl-c": "terminal::CtrlC", - // Useful terminal actions: "ctrl-cmd-space": "terminal::ShowCharacterPalette", "cmd-c": "terminal::Copy", "cmd-v": "terminal::Paste", - "cmd-k": "terminal::Clear" + "cmd-k": "terminal::Clear", + // Some nice conveniences + "cmd-backspace": [ + "terminal::SendText", + "\u0015" + ], + "cmd-right": [ + "terminal::SendText", + "\u0005" + ], + "cmd-left": [ + "terminal::SendText", + "\u0001" + ], + // There are conflicting bindings for these keys in the global context. + // these bindings override them, remove at your own risk: + "up": [ + "terminal::SendKeystroke", + "up" + ], + "down": [ + "terminal::SendKeystroke", + "down" + ], + "escape": [ + "terminal::SendKeystroke", + "escape" + ], + "enter": [ + "terminal::SendKeystroke", + "enter" + ], + "ctrl-c": [ + "terminal::SendKeystroke", + "ctrl-c" + ] } } ] \ No newline at end of file diff --git a/crates/terminal/src/terminal_view.rs b/crates/terminal/src/terminal_view.rs index 856ed3af41..2742076045 100644 --- a/crates/terminal/src/terminal_view.rs +++ b/crates/terminal/src/terminal_view.rs @@ -14,6 +14,7 @@ use gpui::{ use serde::Deserialize; use settings::{Settings, TerminalBlink}; use smol::Timer; +use util::ResultExt; use workspace::pane; use crate::{terminal_element::TerminalElement, Event, Terminal}; @@ -32,6 +33,9 @@ pub struct DeployContextMenu { #[derive(Clone, Default, Deserialize, PartialEq)] pub struct SendText(String); +#[derive(Clone, Default, Deserialize, PartialEq)] +pub struct SendKeystroke(String); + actions!( terminal, [ @@ -48,19 +52,14 @@ actions!( ] ); -impl_actions!(terminal, [SendText]); +impl_actions!(terminal, [SendText, SendKeystroke]); impl_internal_actions!(project_panel, [DeployContextMenu]); pub fn init(cx: &mut MutableAppContext) { - //Global binding overrrides - cx.add_action(TerminalView::ctrl_c); - cx.add_action(TerminalView::up); - cx.add_action(TerminalView::down); - cx.add_action(TerminalView::escape); - cx.add_action(TerminalView::enter); //Useful terminal views cx.add_action(TerminalView::send_text); + cx.add_action(TerminalView::send_keystroke); cx.add_action(TerminalView::deploy_context_menu); cx.add_action(TerminalView::copy); cx.add_action(TerminalView::paste); @@ -298,44 +297,19 @@ impl TerminalView { }); } - ///Synthesize the keyboard event corresponding to 'up' - fn up(&mut self, _: &Up, cx: &mut ViewContext) { - self.clear_bel(cx); - self.terminal.update(cx, |term, _| { - term.try_keystroke(&Keystroke::parse("up").unwrap(), false) - }); - } - - ///Synthesize the keyboard event corresponding to 'down' - fn down(&mut self, _: &Down, cx: &mut ViewContext) { - self.clear_bel(cx); - self.terminal.update(cx, |term, _| { - term.try_keystroke(&Keystroke::parse("down").unwrap(), false) - }); - } - - ///Synthesize the keyboard event corresponding to 'ctrl-c' - fn ctrl_c(&mut self, _: &CtrlC, cx: &mut ViewContext) { - self.clear_bel(cx); - self.terminal.update(cx, |term, _| { - term.try_keystroke(&Keystroke::parse("ctrl-c").unwrap(), false) - }); - } - - ///Synthesize the keyboard event corresponding to 'escape' - fn escape(&mut self, _: &Escape, cx: &mut ViewContext) { - self.clear_bel(cx); - self.terminal.update(cx, |term, _| { - term.try_keystroke(&Keystroke::parse("escape").unwrap(), false) - }); - } - - ///Synthesize the keyboard event corresponding to 'enter' - fn enter(&mut self, _: &Enter, cx: &mut ViewContext) { - self.clear_bel(cx); - self.terminal.update(cx, |term, _| { - term.try_keystroke(&Keystroke::parse("enter").unwrap(), false) - }); + 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, + cx.global::() + .terminal_overrides + .option_as_meta + .unwrap_or(false), + ); + }); + } } }