diff --git a/crabport-core/src/keybind.rs b/crabport-core/src/keybind.rs index c89f260..47938d3 100644 --- a/crabport-core/src/keybind.rs +++ b/crabport-core/src/keybind.rs @@ -47,11 +47,6 @@ pub fn default_bindings() -> Vec { ..Modifiers::default() }; - let shift = Modifiers { - shift: true, - ..Modifiers::default() - }; - let ctrl_shift = Modifiers { control: true, shift: true, @@ -63,11 +58,6 @@ pub fn default_bindings() -> Vec { ..Modifiers::default() }; - let alt = Modifiers { - alt: true, - ..Modifiers::default() - }; - vec![ // ---- Platform shortcuts (GUI, not sent to PTY) ---- Binding { @@ -128,6 +118,7 @@ pub fn default_bindings() -> Vec { binding_ctrl("y", 0x19), binding_ctrl("z", 0x1a), // Ctrl+other keys + // ---- Ctrl+other keys ---- Binding { modifiers: ctrl, key: "space", @@ -169,179 +160,11 @@ pub fn default_bindings() -> Vec { key: "escape", action: KeyAction::Bytes(vec![0x1b]), }, - // ---- Cursor keys ---- - Binding { - modifiers: Modifiers::default(), - key: "up", - action: KeyAction::Bytes(b"\x1b[A".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "down", - action: KeyAction::Bytes(b"\x1b[B".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "right", - action: KeyAction::Bytes(b"\x1b[C".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "left", - action: KeyAction::Bytes(b"\x1b[D".to_vec()), - }, - Binding { - modifiers: shift, - key: "up", - action: KeyAction::Bytes(b"\x1b[1;2A".to_vec()), - }, - Binding { - modifiers: shift, - key: "down", - action: KeyAction::Bytes(b"\x1b[1;2B".to_vec()), - }, - Binding { - modifiers: shift, - key: "right", - action: KeyAction::Bytes(b"\x1b[1;2C".to_vec()), - }, - Binding { - modifiers: shift, - key: "left", - action: KeyAction::Bytes(b"\x1b[1;2D".to_vec()), - }, - Binding { - modifiers: alt, - key: "up", - action: KeyAction::Bytes(b"\x1b[1;3A".to_vec()), - }, - Binding { - modifiers: alt, - key: "down", - action: KeyAction::Bytes(b"\x1b[1;3B".to_vec()), - }, - Binding { - modifiers: alt, - key: "right", - action: KeyAction::Bytes(b"\x1b[1;3C".to_vec()), - }, - Binding { - modifiers: alt, - key: "left", - action: KeyAction::Bytes(b"\x1b[1;3D".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "up", - action: KeyAction::Bytes(b"\x1b[1;5A".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "down", - action: KeyAction::Bytes(b"\x1b[1;5B".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "right", - action: KeyAction::Bytes(b"\x1b[1;5C".to_vec()), - }, - Binding { - modifiers: ctrl, - key: "left", - action: KeyAction::Bytes(b"\x1b[1;5D".to_vec()), - }, - // ---- Home / End / PageUp / PageDown / Insert / Delete ---- - Binding { - modifiers: Modifiers::default(), - key: "home", - action: KeyAction::Bytes(b"\x1b[H".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "end", - action: KeyAction::Bytes(b"\x1b[F".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "pageup", - action: KeyAction::Bytes(b"\x1b[5~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "pagedown", - action: KeyAction::Bytes(b"\x1b[6~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "insert", - action: KeyAction::Bytes(b"\x1b[2~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "delete", - action: KeyAction::Bytes(b"\x1b[3~".to_vec()), - }, - // ---- Function keys ---- - Binding { - modifiers: Modifiers::default(), - key: "f1", - action: KeyAction::Bytes(b"\x1bOP".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f2", - action: KeyAction::Bytes(b"\x1bOQ".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f3", - action: KeyAction::Bytes(b"\x1bOR".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f4", - action: KeyAction::Bytes(b"\x1bOS".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f5", - action: KeyAction::Bytes(b"\x1b[15~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f6", - action: KeyAction::Bytes(b"\x1b[17~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f7", - action: KeyAction::Bytes(b"\x1b[18~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f8", - action: KeyAction::Bytes(b"\x1b[19~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f9", - action: KeyAction::Bytes(b"\x1b[20~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f10", - action: KeyAction::Bytes(b"\x1b[21~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f11", - action: KeyAction::Bytes(b"\x1b[23~".to_vec()), - }, - Binding { - modifiers: Modifiers::default(), - key: "f12", - action: KeyAction::Bytes(b"\x1b[24~".to_vec()), - }, + // Cursor keys, Home/End, PageUp/PageDown, Insert/Delete and F1–F12 + // are encoded by [`encode_special_key`] at keypress time, so they can + // follow the program's DECCKM mode and the xterm modifier encoding — + // a static table can only ever send the unmodified CSI forms, which + // is what made the cursor keys wrong inside vim/less. ] } @@ -389,6 +212,92 @@ pub fn resolve<'a>(keystroke: &Keystroke, bindings: &'a [Binding]) -> Option<&'a .map(|b| &b.action) } +/// The xterm modifier parameter for a key event: `1 + shift + 2*alt + 4*ctrl`. +/// `1` is the protocol's "no modifier" value. +fn modifier_parameter(m: &Modifiers) -> u8 { + 1 + (m.shift as u8) + 2 * (m.alt as u8) + 4 * (m.control as u8) +} + +/// Encode a special (non-text) key the way xterm does, honouring the +/// program's DECCKM mode and the xterm modifier encoding: +/// +/// - Arrow, Home and End use the CSI forms (`ESC [ A`, `ESC [ H`) in normal +/// mode and the SS3 forms (`ESC O A`, `ESC O H`) once the program enables +/// DECCKM (`ESC [ ? 1 h`) — what vim/less do at startup. +/// - With a modifier held, xterm switches to the parameterised CSI form +/// (`ESC [ 1;5C` for Ctrl+Right), so DECCKM no longer applies. F1–F4 use +/// SS3 unmodified (`ESC O P`) and CSI with modifiers (`ESC [ 1;2P`). +/// - PageUp/PageDown, Insert/Delete and F5–F12 are `~`-form sequences and are +/// unaffected by DECCKM. +/// +/// Returns `None` for every other key, and when ⌘ is held (⌘ shortcuts stay +/// with the application, as the kitty-protocol path already does). +pub fn encode_special_key(key: &str, mods: &Modifiers, app_cursor: bool) -> Option> { + if mods.platform || mods.function { + return None; + } + let m = modifier_parameter(mods); + + let seq = match key { + "up" => cursor('A', m, app_cursor), + "down" => cursor('B', m, app_cursor), + "right" => cursor('C', m, app_cursor), + "left" => cursor('D', m, app_cursor), + "home" => cursor('H', m, app_cursor), + "end" => cursor('F', m, app_cursor), + "pageup" => tilde(5, m), + "pagedown" => tilde(6, m), + "insert" => tilde(2, m), + "delete" => tilde(3, m), + "f1" => function('P', m), + "f2" => function('Q', m), + "f3" => function('R', m), + "f4" => function('S', m), + "f5" => tilde(15, m), + "f6" => tilde(17, m), + "f7" => tilde(18, m), + "f8" => tilde(19, m), + "f9" => tilde(20, m), + "f10" => tilde(21, m), + "f11" => tilde(23, m), + "f12" => tilde(24, m), + _ => return None, + }; + Some(seq.into_bytes()) +} + +/// One of the cursor-family keys: CSI `X` (or SS3 `X` in application mode) +/// unmodified, parameterised CSI with a modifier held. +fn cursor(final_byte: char, m: u8, app_cursor: bool) -> String { + if m == 1 { + if app_cursor { + format!("\x1bO{final_byte}") + } else { + format!("\x1b[{final_byte}") + } + } else { + format!("\x1b[1;{m}{final_byte}") + } +} + +/// One of the `~`-form keys (PageUp/PageDown, Insert/Delete, F5–F12). +fn tilde(number: u8, m: u8) -> String { + if m == 1 { + format!("\x1b[{number}~") + } else { + format!("\x1b[{number};{m}~") + } +} + +/// F1–F4: SS3 unmodified, parameterised CSI with a modifier held. +fn function(final_byte: char, m: u8) -> String { + if m == 1 { + format!("\x1bO{final_byte}") + } else { + format!("\x1b[1;{m}{final_byte}") + } +} + #[cfg(test)] mod tests { use super::*; @@ -465,6 +374,160 @@ mod tests { assert_eq!(control_byte(&key("home", ctrl())), None); assert_eq!(control_byte(&key("ab", ctrl())), None); } + + fn shift() -> Modifiers { + Modifiers { + shift: true, + ..Modifiers::default() + } + } + + fn alt() -> Modifiers { + Modifiers { + alt: true, + ..Modifiers::default() + } + } + + // ----------------------------------------------------------------- + // encode_special_key + // ----------------------------------------------------------------- + + #[test] + fn arrows_use_csi_outside_application_mode() { + assert_eq!( + encode_special_key("up", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[A".as_slice()) + ); + assert_eq!( + encode_special_key("down", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[B".as_slice()) + ); + assert_eq!( + encode_special_key("right", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[C".as_slice()) + ); + assert_eq!( + encode_special_key("left", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[D".as_slice()) + ); + } + + /// In vim/less (DECCKM on) the unmodified cursor keys must use the SS3 + /// forms, and Home/End follow the same rule. + #[test] + fn application_mode_switches_to_ss3() { + assert_eq!( + encode_special_key("up", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOA".as_slice()) + ); + assert_eq!( + encode_special_key("left", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOD".as_slice()) + ); + assert_eq!( + encode_special_key("home", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOH".as_slice()) + ); + assert_eq!( + encode_special_key("end", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOF".as_slice()) + ); + } + + #[test] + fn modifiers_use_the_parameterised_csi_form_even_in_application_mode() { + // 1 + shift = 2. + assert_eq!( + encode_special_key("up", &shift(), false).as_deref(), + Some(b"\x1b[1;2A".as_slice()) + ); + // 1 + alt = 3. + assert_eq!( + encode_special_key("left", &alt(), true).as_deref(), + Some(b"\x1b[1;3D".as_slice()) + ); + // 1 + ctrl = 5. + assert_eq!( + encode_special_key("right", &ctrl(), false).as_deref(), + Some(b"\x1b[1;5C".as_slice()) + ); + // 1 + shift + ctrl = 6. + let mut m = ctrl(); + m.shift = true; + assert_eq!( + encode_special_key("home", &m, true).as_deref(), + Some(b"\x1b[1;6H".as_slice()) + ); + } + + #[test] + fn home_and_end_use_csi_outside_application_mode() { + assert_eq!( + encode_special_key("home", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[H".as_slice()) + ); + assert_eq!( + encode_special_key("end", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[F".as_slice()) + ); + } + + #[test] + fn tilde_keys_ignore_application_mode() { + assert_eq!( + encode_special_key("pageup", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[5~".as_slice()) + ); + assert_eq!( + encode_special_key("pagedown", &Modifiers::default(), true).as_deref(), + Some(b"\x1b[6~".as_slice()) + ); + assert_eq!( + encode_special_key("insert", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[2~".as_slice()) + ); + assert_eq!( + encode_special_key("delete", &shift(), false).as_deref(), + Some(b"\x1b[3;2~".as_slice()) + ); + } + + #[test] + fn function_keys_use_ss3_then_csi() { + assert_eq!( + encode_special_key("f1", &Modifiers::default(), false).as_deref(), + Some(b"\x1bOP".as_slice()) + ); + assert_eq!( + encode_special_key("f4", &Modifiers::default(), true).as_deref(), + Some(b"\x1bOS".as_slice()) + ); + assert_eq!( + encode_special_key("f1", &shift(), false).as_deref(), + Some(b"\x1b[1;2P".as_slice()) + ); + assert_eq!( + encode_special_key("f5", &Modifiers::default(), false).as_deref(), + Some(b"\x1b[15~".as_slice()) + ); + assert_eq!( + encode_special_key("f12", &ctrl(), false).as_deref(), + Some(b"\x1b[24;5~".as_slice()) + ); + } + + #[test] + fn cmd_shortcuts_and_unknown_keys_are_left_to_the_app() { + let mut m = Modifiers::default(); + m.platform = true; + assert_eq!(encode_special_key("up", &m, false), None); + assert_eq!( + encode_special_key("enter", &Modifiers::default(), false), + None + ); + assert_eq!(encode_special_key("a", &Modifiers::default(), false), None); + } } /// The control byte xterm would send for a key with `Ctrl` held and no ⌘, diff --git a/crabport-terminal/src/terminal.rs b/crabport-terminal/src/terminal.rs index 5117e3c..fd6c5d9 100644 --- a/crabport-terminal/src/terminal.rs +++ b/crabport-terminal/src/terminal.rs @@ -1164,6 +1164,19 @@ impl TerminalSession { self.kitty_keyboard.load(Ordering::SeqCst) != 0 } + /// Whether the running program has enabled DECCKM (`\x1b[?1h`), the + /// application-cursor-keys mode that vim/less/nano switch into. Arrow and + /// Home/End keys must then use the SS3 forms (`ESC O A`, `ESC O H`) + /// instead of the CSI forms. + /// + /// Reads through a try-lock: a keypress that lands while the PTY thread + /// holds the term lock encodes the normal CSI form rather than stalling + /// input. + pub fn app_cursor_mode(&self) -> bool { + self.try_with_term_mut(|term| term.mode().contains(TermMode::APP_CURSOR)) + .unwrap_or(false) + } + /// Scan a chunk of *program output* for kitty-keyboard negotiation /// requests and react accordingly. /// diff --git a/crabport-ui/i18n/en.toml b/crabport-ui/i18n/en.toml index 8e2d0da..1df849c 100644 --- a/crabport-ui/i18n/en.toml +++ b/crabport-ui/i18n/en.toml @@ -673,6 +673,7 @@ action_terminal_reset_font = "Terminal: Reset Font Size" action_terminal_search = "Terminal: Search" action_split_vertical = "Terminal: Split Pane Vertically" action_split_horizontal = "Terminal: Split Pane Horizontally" +action_close_pane = "Terminal: Close Pane" [menu] app_name = "CrabPort" diff --git a/crabport-ui/i18n/zh-CN.toml b/crabport-ui/i18n/zh-CN.toml index 2888486..6f41792 100644 --- a/crabport-ui/i18n/zh-CN.toml +++ b/crabport-ui/i18n/zh-CN.toml @@ -674,6 +674,7 @@ action_terminal_reset_font = "终端:重置字体" action_terminal_search = "终端:搜索" action_split_vertical = "终端:垂直分割面板" action_split_horizontal = "终端:水平分割面板" +action_close_pane = "终端:关闭窗格" [menu] app_name = "CrabPort" diff --git a/crabport-ui/src/app.rs b/crabport-ui/src/app.rs index 1edda8a..0f49c57 100644 --- a/crabport-ui/src/app.rs +++ b/crabport-ui/src/app.rs @@ -78,6 +78,7 @@ actions!( TerminalSearch, SplitVertical, SplitHorizontal, + ClosePane, ] ); diff --git a/crabport-ui/src/app/tabs.rs b/crabport-ui/src/app/tabs.rs index 9dd9544..4fde688 100644 --- a/crabport-ui/src/app/tabs.rs +++ b/crabport-ui/src/app/tabs.rs @@ -932,6 +932,27 @@ impl CrabportApp { self.focus_pane_next_frame(new_pane_id, cx); } + /// The tab whose split tree contains `pane_id`, if any. + fn tab_id_for_pane(&self, pane_id: u64) -> Option { + self.split_trees + .iter() + .find(|(_, tree)| tree.root.find_pane(pane_id)) + .map(|(tab_id, _)| *tab_id) + } + + /// Close the pane that owns the keyboard focus in the active terminal + /// tab (⌘W, #89). [`Self::close_pane`] collapses the split, or closes + /// the whole tab when the pane was the last one. A no-op on tabs with + /// no split tree (Home, SFTP). + pub fn close_focused_pane(&mut self, cx: &mut Context) { + let tab_id = self.active_tab_id; + let Some(tree) = self.split_trees.get(&tab_id) else { + return; + }; + let pane_id = tree.active_pane; + self.close_pane(tab_id, pane_id, cx); + } + /// Close a single pane by pane id. If it was the last pane in the tab, /// the whole tab is closed (mirrors `close_tab`). Otherwise the split /// tree collapses and the active pane is updated. @@ -1163,6 +1184,28 @@ impl CrabportApp { }, )); + // Close Pane — collapses the split the right-clicked pane belongs + // to (#89). Only offered when the tab actually has more than one + // pane, so the item never appears where there is nothing to close: + // with a single pane, closing means closing the tab, and the tab + // bar already has a close button for that. + if let Some(pane_id) = real_pane_id + && let Some(close_tab_id) = self.tab_id_for_pane(pane_id) + && let Some(tree) = self.split_trees.get(&close_tab_id) + && tree.root.find_pane(pane_id) + && tree.root.pane_ids().len() > 1 + { + let app_for_close = cx.entity().clone(); + items.push(ContextMenuItem::new( + t!("terminal.close_pane").to_string(), + move |_w, cx| { + app_for_close.update(cx, |app, cx| { + app.close_pane(close_tab_id, pane_id, cx); + }); + }, + )); + } + // Clear Screen — erases the visible terminal display but keeps // scrollback. Equivalent to the `clear` shell command. items.push(ContextMenuItem::new( @@ -1210,6 +1253,7 @@ impl CrabportApp { let app_handle = cx.entity().downgrade(); let app_handle2 = cx.entity().downgrade(); let app_handle3 = cx.entity().downgrade(); + let app_handle4 = cx.entity().downgrade(); // Capture the view entity so the `on_focused` callback can resolve // the real pane_id (the key in `pane_views`) by entity equality. // The `pane_id` passed to the callback is `TerminalView::count`, @@ -1253,6 +1297,17 @@ impl CrabportApp { }); }); }); + v.set_on_close_request(move |cx| { + // Defer like the split callback: close_pane touches + // pane_views / split_trees and other entities, which would + // re-enter the borrow held by the on_action handler. + let h = app_handle4.clone(); + cx.defer(move |cx| { + let _ = h.update(cx, |app, cx| { + app.close_focused_pane(cx); + }); + }); + }); // Right-click inside the terminal pane surfaces a // Copy/Paste/Select-All context menu. Resolve the real pane_id // (the key in `pane_views`) by entity equality so the menu diff --git a/crabport-ui/src/keybinds.rs b/crabport-ui/src/keybinds.rs index 247953e..80edff3 100644 --- a/crabport-ui/src/keybinds.rs +++ b/crabport-ui/src/keybinds.rs @@ -108,6 +108,10 @@ pub fn catalog() -> Vec { "terminal_search" => crate::app::TerminalSearch, TERM, plat("cmd-f", "ctrl-f"), true; "split_vertical" => crate::app::SplitVertical, TERM, plat("cmd-d", "ctrl-d"), true; "split_horizontal" => crate::app::SplitHorizontal, TERM, plat("cmd-shift-d", "ctrl-shift-d"), true; + // ⌘W closes the focused pane, or the tab when it is the last one. + // Empty on other platforms: Ctrl+W is 0x17 (delete word) and must + // reach the shell. + "close_pane" => crate::app::ClosePane, TERM, plat("cmd-w", ""), true; ) } diff --git a/crabport-ui/src/layouts/content.rs b/crabport-ui/src/layouts/content.rs index 66aadaa..246ff5a 100644 --- a/crabport-ui/src/layouts/content.rs +++ b/crabport-ui/src/layouts/content.rs @@ -265,8 +265,16 @@ pub fn render_content( // If this tab has a split tree, render the panes recursively; // otherwise fall back to the single terminal view. let inner: AnyElement = if let Some(tree) = split_trees.get(&tab_id) { - render_split_node(&tree.root, tree.active_pane, tab_id, pane_views, handle) - .into_any_element() + let multi_pane = tree.root.pane_ids().len() > 1; + render_split_node( + &tree.root, + tree.active_pane, + multi_pane, + tab_id, + pane_views, + handle, + ) + .into_any_element() } else if let Some(terminal_entity) = terminal_views.get(&tab_id) { div() .size_full() @@ -1039,15 +1047,22 @@ pub fn render_content( fn render_split_node( node: &SplitNode, active_pane: u64, + multi_pane: bool, tab_id: u64, pane_views: &HashMap>, handle: &Entity, ) -> AnyElement { use gpui_component::resizable::{h_resizable, resizable_panel, v_resizable}; match node { - SplitNode::Pane(pane_id) => { - render_pane(*pane_id, active_pane, tab_id, pane_views, handle).into_any_element() - } + SplitNode::Pane(pane_id) => render_pane( + *pane_id, + active_pane, + multi_pane, + tab_id, + pane_views, + handle, + ) + .into_any_element(), SplitNode::Split { dir, a, b, .. } => { let dir = *dir; let group_id = @@ -1058,8 +1073,10 @@ fn render_split_node( }; // Render children. For a nested split child, wrap it in a // `resizable_panel` so the inner group fills its allocated space. - let child_a = render_split_child(a, active_pane, tab_id, pane_views, handle); - let child_b = render_split_child(b, active_pane, tab_id, pane_views, handle); + let child_a = + render_split_child(a, active_pane, multi_pane, tab_id, pane_views, handle); + let child_b = + render_split_child(b, active_pane, multi_pane, tab_id, pane_views, handle); let group = if axis == gpui::Axis::Horizontal { h_resizable(group_id) @@ -1079,25 +1096,36 @@ fn render_split_node( fn render_split_child( node: &SplitNode, active_pane: u64, + multi_pane: bool, tab_id: u64, pane_views: &HashMap>, handle: &Entity, ) -> AnyElement { match node { - SplitNode::Pane(pane_id) => { - render_pane(*pane_id, active_pane, tab_id, pane_views, handle).into_any_element() - } + SplitNode::Pane(pane_id) => render_pane( + *pane_id, + active_pane, + multi_pane, + tab_id, + pane_views, + handle, + ) + .into_any_element(), SplitNode::Split { .. } => { // Nested split: render as a group, which itself becomes the child. - render_split_node(node, active_pane, tab_id, pane_views, handle) + render_split_node(node, active_pane, multi_pane, tab_id, pane_views, handle) } } } -/// Render a single terminal pane with click-to-focus + active highlight. +/// Render a single terminal pane with click-to-focus, and — when the tab +/// holds more than one pane — a focus ring on the pane that owns the +/// keyboard (#89): with identical-looking panes, the ring is what tells you +/// at a glance which terminal you're typing into. fn render_pane( pane_id: u64, active_pane: u64, + multi_pane: bool, tab_id: u64, pane_views: &HashMap>, handle: &Entity, @@ -1105,6 +1133,7 @@ fn render_pane( let is_active = pane_id == active_pane; let view = pane_views.get(&pane_id).cloned(); let view_for_focus = view.clone(); + let glow_id = ElementId::Name(format!("pane-glow-{}-{}", tab_id, pane_id).into()); let mut el = div() .id(ElementId::Name( format!("pane-{}-{}", tab_id, pane_id).into(), @@ -1116,7 +1145,6 @@ fn render_pane( // single click to fire `on_mouse_down` on *every* pane, re-focusing // the wrong one. .occlude() - .when(is_active, |el| el.bg(rgba((surface_hover() << 8) | 0x18))) .on_mouse_down(MouseButton::Left, { let handle = handle.clone(); move |_e, w, cx| { @@ -1133,6 +1161,82 @@ fn render_pane( if let Some(view) = view { el = el.child(view); } + // The glow paints *after* the view, because TerminalView fills itself + // with an opaque `term_bg` — the old active-pane tint sat on this + // container and was painted over, which is why the focused pane was + // impossible to pick out. The overlay has no listeners, so every click + // and drag passes through to the terminal beneath; the glow fades in on + // the pane that takes focus and out of the one that loses it. + if multi_pane { + let glow = input_border_focused(); + // Same hue at both stops so the fade never drifts gray in Hsla. + let lit = rgba((glow << 8) | 0x38); + let clear = rgba((glow << 8) | 0x00); + // A tight rim: the wash reaches only ~12pt in from each edge, so + // the terminal keeps its plain background across the middle. + let glow_px = px(12.0); + // gpui 0.2 has two-stop linear gradients only, so the radial + // edge-to-centre wash is composed from one strip per side; the two + // neighbours overlap at each corner, pooling the light there. + let strip = |angle: f32| { + linear_gradient( + angle, + linear_color_stop(lit, 0.0), + linear_color_stop(clear, 1.0), + ) + }; + el = el.child( + div() + .id(glow_id.clone()) + .absolute() + .inset_0() + // Pointer-events: none — the glow is purely visual. + .with_transition(glow_id) + .transition_when_else( + is_active, + duration_fast(), + EASE_STANDARD, + |el| el.opacity(1.0), + |el| el.opacity(0.0), + ) + .child( + div() + .absolute() + .top_0() + .left_0() + .w_full() + .h(glow_px) + .bg(strip(180.0)), + ) + .child( + div() + .absolute() + .bottom_0() + .left_0() + .w_full() + .h(glow_px) + .bg(strip(0.0)), + ) + .child( + div() + .absolute() + .top_0() + .left_0() + .h_full() + .w(glow_px) + .bg(strip(90.0)), + ) + .child( + div() + .absolute() + .top_0() + .right_0() + .h_full() + .w(glow_px) + .bg(strip(270.0)), + ), + ); + } el } diff --git a/crabport-ui/src/views/terminal.rs b/crabport-ui/src/views/terminal.rs index c641050..7366e23 100644 --- a/crabport-ui/src/views/terminal.rs +++ b/crabport-ui/src/views/terminal.rs @@ -193,6 +193,10 @@ pub struct TerminalView { /// Invoked when the user triggers a split via keyboard shortcut. /// Receives the split direction. The app calls `split_active_pane`. on_split_request: Option>, + /// Invoked when the user asks to close this pane (⌘W). The app calls + /// `close_pane`, which collapses the split — or closes the tab when this + /// was the last pane (#89). + on_close_request: Option>, /// Invoked when the user right-clicks inside the terminal pane. /// Receives the click position (window-relative pixels) and the pane /// id, so the app can show a context menu with Copy/Paste/etc. actions @@ -643,6 +647,7 @@ impl TerminalView { on_connection_result: None, on_focused: None, on_split_request: None, + on_close_request: None, on_context_menu: None, sftp_progress: None, on_sftp_progress_changed: None, @@ -1507,6 +1512,13 @@ impl TerminalView { self.on_split_request.clone() } + /// Set the callback invoked when the user asks to close this pane + /// (⌘W). The app collapses the split the pane belongs to, or closes + /// the whole tab when this was the last pane. + pub fn set_on_close_request(&mut self, f: impl Fn(&mut App) + 'static) { + self.on_close_request = Some(Rc::new(f)); + } + /// Set the callback invoked when the user right-clicks inside this /// terminal pane. The app uses it to show a context menu with /// Copy/Paste/Select-All actions scoped to this pane. The callback @@ -2284,6 +2296,11 @@ impl Render for TerminalView { } }), ) + .on_action(cx.listener(|this, _: &crate::app::ClosePane, _window, cx| { + if let Some(cb) = &this.on_close_request { + cb(cx); + } + })) .on_action( cx.listener(|this, _: &crate::app::TerminalSearch, window, cx| { this.toggle_search(window, cx); @@ -2392,6 +2409,22 @@ impl Render for TerminalView { None => { // Keys with Ctrl held that no binding covers fall // through to the xterm control-byte table (#88): + // Keys no binding covers fall through to the xterm + // encodings: + // + // - `Ctrl+` → its control byte (#88). gpui + // leaves `key_char` unset whenever Ctrl is held, and + // reports the shifted character as the key name with + // `shift` cleared, so `Ctrl+Shift+-` arrives as + // `key = "_"` — a name no binding matched, and the + // IME path never sees control keydowns either. + // - Special keys (arrows, Home/End, PageUp/PageDown, + // Insert/Delete, F1–F12) → the xterm forms that + // follow the program's DECCKM mode and carry + // modifiers, which is what vim/less need. + // + // Skipped while an IME composition is active so + // editing keys still reach the input context. // `Ctrl+Shift+-` arrives from gpui as `key = "_"` with // `shift` cleared and `key_char` unset, so nothing // above can match it, and the IME path never sees @@ -2404,6 +2437,25 @@ impl Render for TerminalView { .as_ref() .map(|s| !s.is_empty()) .unwrap_or(false); + if !composing { + let bytes = keybind::control_byte(&event.keystroke) + .map(|byte| vec![byte]) + .or_else(|| { + keybind::encode_special_key( + &event.keystroke.key, + &event.keystroke.modifiers, + this.session.app_cursor_mode(), + ) + }); + if let Some(bytes) = bytes { + this.session.write(&bytes); + this.session.scroll_to_bottom(); + cx.notify(); + // Stop propagation so the platform's IME input + // context doesn't also receive this keydown: + // these are control sequences, not text. + cx.stop_propagation(); + } if !composing && let Some(byte) = keybind::control_byte(&event.keystroke) { this.session.write(&[byte]); this.session.scroll_to_bottom();