Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

thinkterm-web: new tab, split, zoom, close and fit from the strip

RoversX99b6f5593056
diff --git a/thinkterm-web/src/app.rs b/thinkterm-web/src/app.rs
index 9c750865..2224ee92 100644
--- a/thinkterm-web/src/app.rs
+++ b/thinkterm-web/src/app.rs
@@ -65,6 +65,8 @@ const RECONNECT_DOUBT_AFTER: u32 = 6;
/// Shorter than the shortest useful outage and longer than the time a
/// server that is restarting in a loop stays up.
const RECONNECT_STABLE_MS: f64 = 5_000.0;
+/// How long the close button waits for its second press.
+const CLOSE_CONFIRM_MS: f64 = 3_000.0;
/// The desktop's `inactive_pane_hsb` default: panes without the focus are
/// a little darker and a little greyer.
const INACTIVE_PANE_HSB: wezterm_color_types::HsbTransform = wezterm_color_types::HsbTransform {
@@ -202,6 +204,9 @@ pub struct Inner {
/// A switch is under way; a second request waits for the next push
/// or click rather than racing it.
switching: bool,
+ /// The close button was pressed once; the second press, within a
+ /// few seconds, is the one that closes.
+ closing_since: Option<f64>,
}
pub struct App {
@@ -345,6 +350,7 @@ impl App {
strip: setup.strip,
layout_refresh_pending: false,
switching: false,
+ closing_since: None,
};
let app = Rc::new(Self {
inner: RefCell::new(inner),
@@ -753,6 +759,23 @@ impl App {
return true;
}
KeyCode::Char('v') | KeyCode::Char('V') if cmd || ctrl_shift => return false,
+ // Ctrl+Shift+Enter splits to the right, Ctrl+Shift+\ below,
+ // Ctrl+Shift+Z zooms; the same as the strip's buttons.
+ KeyCode::Enter if ctrl_shift => {
+ drop(inner);
+ self.split(thinkterm_proto::SplitDirection::Horizontal);
+ return true;
+ }
+ KeyCode::Char('\\') | KeyCode::Char('|') if ctrl_shift => {
+ drop(inner);
+ self.split(thinkterm_proto::SplitDirection::Vertical);
+ return true;
+ }
+ KeyCode::Char('z') | KeyCode::Char('Z') if ctrl_shift => {
+ drop(inner);
+ self.toggle_zoom();
+ return true;
+ }
// Ctrl+Shift+F fits the tab to this window; Ctrl+Shift+T
// takes the terminal over from another device (Handoff mode).
KeyCode::Char('f') | KeyCode::Char('F') if ctrl_shift => {
@@ -1241,7 +1264,15 @@ impl App {
return;
};
let tabs = crate::chrome::model(layout, inner.focused_pane, inner.title());
- strip.render(&tabs, inner.following);
+ let controls = crate::chrome::Controls {
+ following: inner.following,
+ zoomed: inner.tab_layout.as_ref().is_some_and(|l| l.zoomed.is_some()),
+ fit: inner.link.lease().fit,
+ closing: inner
+ .closing_since
+ .is_some_and(|since| monotonic_ms() - since < CLOSE_CONFIRM_MS),
+ };
+ strip.render(&tabs, controls);
}
/// List the server's panes again, redraw the strip, and bring the tab
@@ -1472,17 +1503,174 @@ impl App {
/// A click on the strip.
pub fn on_chrome_click(self: &Rc<Self>, click: crate::chrome::Click) {
+ use crate::chrome::Click;
+ // Any other click withdraws a pending close.
+ if click != Click::Close {
+ self.inner.borrow_mut().closing_since = None;
+ }
match click {
- crate::chrome::Click::Pane(pane_id) => self.switch_to_pane(pane_id, true),
- crate::chrome::Click::Follow => {
+ Click::Pane(pane_id) => self.switch_to_pane(pane_id, true),
+ Click::Follow => {
let mut inner = self.inner.borrow_mut();
inner.following = !inner.following;
Self::render_strip(&inner);
let _ = inner.textarea.focus();
}
+ Click::NewTab => self.new_tab(),
+ Click::SplitRight => self.split(thinkterm_proto::SplitDirection::Horizontal),
+ Click::SplitBelow => self.split(thinkterm_proto::SplitDirection::Vertical),
+ Click::Zoom => self.toggle_zoom(),
+ Click::Fit => {
+ let on = !self.inner.borrow().link.lease().fit;
+ self.fit(on);
+ }
+ Click::Close => {
+ let confirmed = {
+ let mut inner = self.inner.borrow_mut();
+ let now = monotonic_ms();
+ match inner.closing_since {
+ Some(since) if now - since < CLOSE_CONFIRM_MS => {
+ inner.closing_since = None;
+ true
+ }
+ _ => {
+ inner.closing_since = Some(now);
+ Self::render_strip(&inner);
+ let _ = inner.textarea.focus();
+ false
+ }
+ }
+ };
+ if confirmed {
+ self.close_pane();
+ } else {
+ // Back to a plain × when the moment passes.
+ let app = Rc::clone(self);
+ let closure = Closure::once_into_js(move || {
+ let inner = app.inner.borrow();
+ Self::render_strip(&inner);
+ });
+ if let Some(window) = web_sys::window() {
+ let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(
+ closure.as_ref().unchecked_ref(),
+ CLOSE_CONFIRM_MS as i32 + 50,
+ );
+ }
+ }
+ }
}
}
+ /// One request to the server, its refusal shown rather than logged,
+ /// and the layout listed again afterwards; the server's own pushes
+ /// list it too, which is harmless.
+ fn act<F>(self: &Rc<Self>, what: &'static str, pdu: Pdu, done: F)
+ where
+ F: FnOnce(&Rc<Self>, Pdu) + 'static,
+ {
+ let link = self.inner.borrow().link.clone();
+ let app = Rc::clone(self);
+ wasm_bindgen_futures::spawn_local(async move {
+ match thinkterm_session::host::request(&link, pdu, Ok).await {
+ Ok(answer) => {
+ done(&app, answer);
+ app.refresh_layout();
+ }
+ Err(err) => {
+ log::warn!("{what}: {err:#}");
+ let inner = app.inner.borrow();
+ Self::set_status(&inner, &format!("could not {what}: {err:#}"));
+ }
+ }
+ let _ = app.inner.borrow().textarea.focus();
+ });
+ }
+
+ /// A new tab in the window this tab is in, at the tab's own size.
+ pub fn new_tab(self: &Rc<Self>) {
+ let (window_id, workspace, size) = {
+ let inner = self.inner.borrow();
+ let size = inner
+ .link
+ .lease()
+ .canonical_size
+ .or_else(|| inner.tab_layout.as_ref().map(|l| l.size))
+ .unwrap_or_default();
+ (inner.window_id, inner.workspace.clone(), size)
+ };
+ self.act(
+ "open a new tab",
+ Pdu::SpawnV2(codec::SpawnV2 {
+ domain: thinkterm_proto::SpawnTabDomain::CurrentPaneDomain,
+ window_id: Some(window_id),
+ command: None,
+ command_dir: None,
+ size,
+ workspace,
+ }),
+ |app, answer| {
+ if let Pdu::SpawnResponse(spawned) = answer {
+ // The page moves with it; the desktop already has.
+ app.switch_to_pane(spawned.pane_id, false);
+ }
+ },
+ );
+ }
+
+ /// Split the focused pane; the new pane takes the focus.
+ pub fn split(self: &Rc<Self>, direction: thinkterm_proto::SplitDirection) {
+ let pane_id = self.inner.borrow().focused_pane;
+ self.act(
+ "split the pane",
+ Pdu::SplitPane(codec::SplitPane {
+ pane_id,
+ split_request: thinkterm_proto::SplitRequest {
+ direction,
+ target_is_second: true,
+ top_level: false,
+ size: thinkterm_proto::SplitSize::Percent(50),
+ },
+ command: None,
+ command_dir: None,
+ domain: thinkterm_proto::SpawnTabDomain::CurrentPaneDomain,
+ move_pane_id: None,
+ }),
+ |app, answer| {
+ if let Pdu::SpawnResponse(spawned) = answer {
+ app.switch_to_pane(spawned.pane_id, false);
+ }
+ },
+ );
+ }
+
+ /// Zoom the focused pane to the whole tab, or back.
+ pub fn toggle_zoom(self: &Rc<Self>) {
+ let (tab_id, pane_id, zoomed) = {
+ let inner = self.inner.borrow();
+ let zoomed = inner.tab_layout.as_ref().is_some_and(|l| l.zoomed.is_some());
+ (inner.tab_id, inner.focused_pane, zoomed)
+ };
+ self.act(
+ "zoom the pane",
+ Pdu::SetPaneZoomed(codec::SetPaneZoomed {
+ containing_tab_id: tab_id,
+ pane_id,
+ zoomed: !zoomed,
+ }),
+ |_, _| {},
+ );
+ }
+
+ /// Close the focused pane, ending its program.
+ pub fn close_pane(self: &Rc<Self>) {
+ let pane_id = self.inner.borrow().focused_pane;
+ self.act(
+ "close the pane",
+ Pdu::KillPane(codec::KillPane { pane_id }),
+ |_, _| {},
+ );
+ }
+
/// The pane at `pane_id`, and the page's focus with it. `chosen` is a
/// person's click, which also stops the page following the desktop;
/// a focus push is not.
diff --git a/thinkterm-web/src/chrome.rs b/thinkterm-web/src/chrome.rs
index 6aa89d89..065a79ef 100644
--- a/thinkterm-web/src/chrome.rs
+++ b/thinkterm-web/src/chrome.rs
@@ -135,6 +135,23 @@ mod dom {
pub enum Click {
Pane(PaneId),
Follow,
+ NewTab,
+ SplitRight,
+ SplitBelow,
+ Zoom,
+ Close,
+ Fit,
+ }
+
+ /// The state the strip's buttons show.
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+ pub struct Controls {
+ pub following: bool,
+ pub zoomed: bool,
+ pub fit: bool,
+ /// The close button was pressed once and waits for the second
+ /// press that means it.
+ pub closing: bool,
}
fn escape(text: &str) -> String {
@@ -174,7 +191,8 @@ mod dom {
/// Redraw the whole strip. It is a few dozen elements; rebuilding
/// them is cheaper to get right than diffing them.
- pub fn render(&self, tabs: &[TabView], following: bool) {
+ pub fn render(&self, tabs: &[TabView], controls: Controls) {
+ let following = controls.following;
let mut html = String::new();
for tab in tabs {
let class = if tab.current { "tab current" } else { "tab" };
@@ -212,6 +230,33 @@ mod dom {
"This page stays on this pane. Click to follow the desktop's focus again.",
)
};
+ html.push_str(
+ "<span class=\"act\" data-action=\"new-tab\" title=\"New tab, next to this one on the desktop\">+</span>",
+ );
+ html.push_str("<span class=\"actions\">");
+ html.push_str(
+ "<span class=\"act\" data-action=\"split-right\" title=\"Split the focused pane: new pane to the right\">◧</span>",
+ );
+ html.push_str(
+ "<span class=\"act\" data-action=\"split-below\" title=\"Split the focused pane: new pane below\">⬒</span>",
+ );
+ let zoom_class = if controls.zoomed { "act on" } else { "act" };
+ html.push_str(&format!(
+ "<span class=\"{zoom_class}\" data-action=\"zoom\" title=\"Zoom the focused pane to the whole tab (again to unzoom)\">⤢</span>"
+ ));
+ let fit_class = if controls.fit { "act on" } else { "act" };
+ html.push_str(&format!(
+ "<span class=\"{fit_class}\" data-action=\"fit\" title=\"Fit the tab to this window; the desktop letterboxes. Again to give the size back. (Ctrl+Shift+F)\">⤡</span>"
+ ));
+ let (close_class, close_text) = if controls.closing {
+ ("act danger", "close pane?")
+ } else {
+ ("act", "×")
+ };
+ html.push_str(&format!(
+ "<span class=\"{close_class}\" data-action=\"close\" title=\"Close the focused pane and end its program. Asks twice.\">{close_text}</span>"
+ ));
+ html.push_str("</span>");
html.push_str(&format!(
"<span class=\"{class}\" data-follow=\"\" title=\"{hint}\">{text}</span>"
));
@@ -221,17 +266,28 @@ mod dom {
/// What a click landed on, if anything the strip owns.
pub fn click_target(ev: &web_sys::MouseEvent) -> Option<Click> {
let target: web_sys::Element = ev.target()?.dyn_into().ok()?;
- let hit = target.closest("[data-pane],[data-follow]").ok()??;
+ let hit = target.closest("[data-pane],[data-follow],[data-action]").ok()??;
if hit.has_attribute("data-follow") {
return Some(Click::Follow);
}
+ if let Some(action) = hit.get_attribute("data-action") {
+ return match action.as_str() {
+ "new-tab" => Some(Click::NewTab),
+ "split-right" => Some(Click::SplitRight),
+ "split-below" => Some(Click::SplitBelow),
+ "zoom" => Some(Click::Zoom),
+ "close" => Some(Click::Close),
+ "fit" => Some(Click::Fit),
+ _ => None,
+ };
+ }
hit.get_attribute("data-pane")?.parse().ok().map(Click::Pane)
}
}
}
#[cfg(target_arch = "wasm32")]
-pub use dom::{Click, TabStrip};
+pub use dom::{Click, Controls, TabStrip};
#[cfg(test)]
mod tests {
diff --git a/thinkterm-web/www/index.html b/thinkterm-web/www/index.html
index 6cad8fe5..26f2eda6 100644
--- a/thinkterm-web/www/index.html
+++ b/thinkterm-web/www/index.html
@@ -14,7 +14,12 @@
#tabs .pane { padding: 0 7px; height: 16px; line-height: 16px; border-radius: 8px; background: #1a1a1a; color: #9a9a9a; font-size: 11px; cursor: pointer; }
#tabs .pane:hover { color: #ddd; }
#tabs .pane.current { background: #3a3a3a; color: #fff; }
- #tabs .follow { margin-left: auto; padding: 0 10px; height: 20px; line-height: 20px; border-radius: 10px; color: #777; cursor: pointer; font-size: 11px; }
+ #tabs .act { display: inline-flex; align-items: center; justify-content: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; color: #8a8a8a; cursor: pointer; font-size: 13px; }
+ #tabs .act:hover { background: #1c1c1c; color: #eee; }
+ #tabs .act.on { color: #6cb6ff; }
+ #tabs .act.danger { background: #5a1e1e; color: #ffb4b4; font-size: 11px; }
+ #tabs .actions { display: inline-flex; gap: 2px; margin-left: auto; }
+ #tabs .follow { padding: 0 10px; height: 20px; line-height: 20px; border-radius: 10px; color: #777; cursor: pointer; font-size: 11px; }
#tabs .follow.on { color: #6cb6ff; }
#tabs .follow:hover { background: #1c1c1c; }
#term { position: absolute; inset: 28px 0 22px 0; width: 100%; height: calc(100% - 50px); display: block; outline: none; }