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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
fix(web): keep the top filled when a phone's keyboard goes away
RoversXc04cdfe7055a
diff --git a/thinkterm-session/src/host.rs b/thinkterm-session/src/host.rsindex cf4ad61d..ac52930e 100644--- a/thinkterm-session/src/host.rs+++ b/thinkterm-session/src/host.rs@@ -119,6 +119,13 @@ pub trait HostConfig {/// How far past a painted range rows are fetched ahead, on each side,/// in viewports. Zero fetches only what is painted.fn scrollback_lookahead_screens(&self) -> usize;+ /// Rows fetched ahead on each side at the least, however short the+ /// viewport is now: a pane a phone's soft keyboard cut down then has+ /// the rows the keyboard gives back on hand when it goes away. Zero+ /// leaves it to [`scrollback_lookahead_screens`](Self::scrollback_lookahead_screens).+ fn scrollback_lookahead_rows(&self) -> usize {+ 0+ }/// Whether a pane's whole scrollback is fetched once it is first shown,/// so scrolling never paints a row that is still on its way.fn warm_scrollback(&self) -> bool;diff --git a/thinkterm-session/src/pane.rs b/thinkterm-session/src/pane.rsindex f2ef4d7c..2477b06b 100644--- a/thinkterm-session/src/pane.rs+++ b/thinkterm-session/src/pane.rs@@ -628,8 +628,8 @@ impl<H: SessionHost> PaneSession<H> {}};- let band =- (dims.viewport_rows.max(1) * config.scrollback_lookahead_screens()) as StableRowIndex;+ let band = (dims.viewport_rows.max(1) * config.scrollback_lookahead_screens())+ .max(config.scrollback_lookahead_rows()) as StableRowIndex;if band > 0 && budget > 0 {let mut ahead = uncached(st,@@ -1484,6 +1484,7 @@ mod tests {struct TestConfig {lookahead: Cell<usize>,+ lookahead_rows: Cell<usize>,warm: Cell<bool>,rate: Cell<u32>,}@@ -1491,6 +1492,7 @@ mod tests {fn default() -> Self {Self {lookahead: Cell::new(0),+ lookahead_rows: Cell::new(0),warm: Cell::new(false),rate: Cell::new(100),}@@ -1506,6 +1508,9 @@ mod tests {fn scrollback_lookahead_screens(&self) -> usize {self.lookahead.get()}+ fn scrollback_lookahead_rows(&self) -> usize {+ self.lookahead_rows.get()+ }fn warm_scrollback(&self) -> bool {self.warm.get()}@@ -2332,6 +2337,38 @@ mod tests {assert_eq!(rows_requested(&host), 0, "everything around is cached now");}+ /// A viewport a soft keyboard cut short still fetches the host's floor+ /// of rows ahead, not a screen of its own: the rows the keyboard gives+ /// back are cached by the time it goes.+ #[test]+ fn a_short_viewport_fetches_the_hosts_floor_of_rows_ahead() {+ let rows: Vec<_> = (0..48).map(|r| (r, "row")).collect();+ for (floor, reaches) in [(0, false), (30, true)] {+ let (host, session) = session(&rows);+ host.config.lookahead.set(1);+ host.config.lookahead_rows.set(floor);+ let mut short = delta(1, false, false);+ short.dimensions.viewport_rows = 8;+ short.dimensions.scrollback_rows = 48;+ short.dimensions.physical_top = 40;+ session.queue_render_delta(short);+ host.spawner.run_all();++ let _ = session.get_lines(40..48);+ host.spawner.run_all();+ let st = session.state();+ assert!(+ matches!(st.lines.peek(&33), Some(LineEntry::Line(_))),+ "a screen ahead either way"+ );+ assert_eq!(+ matches!(st.lines.peek(&12), Some(LineEntry::Line(_))),+ reaches,+ "with a floor of {floor} rows"+ );+ }+ }+/// The whole scrollback is fetched once per cache epoch when the host/// asks for it, and again after the epoch moves.#[test]diff --git a/thinkterm-web/src/app.rs b/thinkterm-web/src/app.rsindex 107a06f2..5f3e49b8 100644--- a/thinkterm-web/src/app.rs+++ b/thinkterm-web/src/app.rs@@ -335,6 +335,14 @@ pub struct Inner<P: Platform, L: Link> {/// The layout as the divider drag began: every step is measured/// from it, not from the last step's result.drag_layout: Option<crate::layout::TabLayout>,+ /// The tab as this page last claimed it for its own grid, drawn until+ /// the server's layout answers: a phone's soft keyboard going away+ /// grows the grid at once, its top filled from the rows fetched+ /// ahead, where the old layout sat flush with the bottom and left the+ /// rest of the canvas blank for a round trip.+ claimed_layout: Option<crate::layout::TabLayout>,+ /// When `claimed_layout` was claimed, on the platform's clock.+ claimed_at: f64,/// A divider claim on the wire, and the step that arrived meanwhile/// to send when it answers: one round trip at a time, the newest/// position wins.@@ -660,6 +668,8 @@ impl<P: Platform, L: Link> App<P, L> {drag_pane: None,drag_divider: None,drag_layout: None,+ claimed_layout: None,+ claimed_at: 0.0,divider_claim_busy: false,divider_claim_next: None,preview_epoch: 0,@@ -2385,7 +2395,7 @@ impl<P: Platform, L: Link> App<P, L> {inner.glyphs.metrics.cell_size.height as f64,);let (col, row) = cell_at(px, py, cw, ch);- let place = match &inner.tab_layout {+ let place = match Self::drawn_layout(inner) {Some(layout) => crate::layout::hit(layout, col, row)?.clone(),None => Self::focused_placement(inner)?,};@@ -2913,7 +2923,7 @@ impl<P: Platform, L: Link> App<P, L> {// Where the focused pane is in its scrollback: rows above the// bottom, and rows there are to scroll through.if let Some(cell) = inner.panes.get(&inner.focused_pane) {- let rows = match &inner.tab_layout {+ let rows = match Self::drawn_layout(inner) {Some(layout) => layout.panes.iter().find(|place| place.pane_id == inner.focused_pane).map(|place| Self::shown_in(inner, place).1),None => Self::focused_placement(inner).as_ref().map(|place| Self::shown_in(inner, place).1),@@ -2931,7 +2941,7 @@ impl<P: Platform, L: Link> App<P, L> {let above = cell.scroll_from_bottom as f32 + cell.scroll_px / cell_h;json.push_str(&format!(",\"scroll\":[{above:.3},{max}]"));}- match &inner.tab_layout {+ match Self::drawn_layout(inner) {Some(layout) => {json.push_str(&format!(",\"cols\":{},\"rows\":{},\"zoomed\":", layout.cols, layout.rows));match layout.zoomed {@@ -3024,7 +3034,7 @@ impl<P: Platform, L: Link> App<P, L> {// the old one drawn from the top jumped the prompt away from// the keyboard's edge and back.let height = inner.gpu.size().1 as f32;- let rows = match &inner.tab_layout {+ let rows = match Self::drawn_layout(inner) {Some(layout) if layout.rows > 0 => layout.rows as f32,_ => (height / ch.max(1.0)).floor(),};@@ -3943,8 +3953,20 @@ impl<P: Platform, L: Link> App<P, L> {.map(|p| p.pane_id).unwrap_or(want)};+ // A layout sent before the claim -- the server's word on an earlier+ // size -- leaves the claimed one drawn; the answer to it, another+ // tab, or a claim nobody met in time puts the server's back.+ let answered = inner.claimed_layout.as_ref().is_none_or(|claimed| {+ claimed.tab_id != layout.tab_id+ || (claimed.cols, claimed.rows) == (layout.cols, layout.rows)+ || inner.platform.monotonic_ms() - inner.claimed_at > Self::PREVIEW_PATIENCE_MS+ });inner.tab_layout = Some(layout);- Self::settle_previews(inner);+ // Nor do the panes' previews give way to it.+ if answered {+ inner.claimed_layout = None;+ Self::settle_previews(inner);+ }fresh}@@ -6353,6 +6375,7 @@ impl<P: Platform, L: Link> App<P, L> {let changed = cols != inner.cols || rows != inner.rows;inner.cols = cols;inner.rows = rows;+ inner.host.config.saw_rows(rows);// The bars are placed from the cell size, which may just have moved.Self::notify(&inner);if changed {@@ -6379,7 +6402,7 @@ impl<P: Platform, L: Link> App<P, L> {// Pane by pane, at frames scaled to the new grid: one// claim that reserves every bar's rows, rather than a bare// grid the listing then corrects (two reflows on screen).- if let Some(scaled) = Self::scaled_layout(&inner, cols, rows) {+ if let Some(mut scaled) = Self::scaled_layout(&inner, cols, rows) {let panes = Self::native_panes(&inner, &scaled);let sizes: Vec<(PaneId, TerminalSize)> = panes.iter().map(|p| (p.pane_id, p.size)).collect();let mut lease = inner.link.lease_mut();@@ -6387,12 +6410,25 @@ impl<P: Platform, L: Link> App<P, L> {lease.native_root = Some(size);drop(lease);Self::preview_panes(&mut inner, &sizes);+ // Drawn at the claimed grid until the server answers.+ for place in &mut scaled.panes {+ if let Some((_, claimed)) = sizes.iter().find(|(pane, _)| *pane == place.pane_id) {+ place.content = (claimed.cols, claimed.rows);+ place.size = *claimed;+ }+ }+ inner.claimed_layout = Some(scaled);+ inner.claimed_at = inner.platform.monotonic_ms();}let link = inner.link.clone();let tab_id = inner.tab_id;+ let app = Rc::clone(self);self.spawn(async move {if let Err(err) = link.claim(tab_id).await {log::warn!("fitting the tab to this window: {err:#}");+ // Nothing will answer it: back to the server's layout.+ app.inner.borrow_mut().claimed_layout = None;+ app.request_frame();}});}@@ -6653,7 +6689,7 @@ impl<P: Platform, L: Link> App<P, L> {/// The panes to draw and where. Before the first listing there is/// one, at the canvas's origin and the page's own grid.fn placements(inner: &Inner<P, L>) -> Vec<crate::layout::PanePlacement> {- if let Some(layout) = &inner.tab_layout {+ if let Some(layout) = Self::drawn_layout(inner) {return layout.panes.clone();}let cell = inner.focused();@@ -6675,6 +6711,12 @@ impl<P: Platform, L: Link> App<P, L> {}]}+ /// The tab as it is drawn and pointed at: the grid this page claimed,+ /// while the server has yet to answer the claim, else the server's.+ fn drawn_layout(inner: &Inner<P, L>) -> Option<&crate::layout::TabLayout> {+ inner.claimed_layout.as_ref().or(inner.tab_layout.as_ref())+ }+/// A pane's dimensions as it is drawn and pointed at: a preview of/// another height placed where the server will put its rows/// (`viewport::previewed`).@@ -6925,7 +6967,7 @@ impl<P: Platform, L: Link> App<P, L> {}let (cell_w, cell_h) = (root_w, root_h);// Dividers, centred in the gap cell like the desktop's.- if let Some(layout) = &inner.tab_layout {+ if let Some(layout) = inner.claimed_layout.as_ref().or(inner.tab_layout.as_ref()) {let t = (inner.glyphs.metrics.underline_height.max(1)) as f32;let colour = focused_palette.split.to_linear();// Centred in the gap cell, and as long as the desktop's: half adiff --git a/thinkterm-web/src/host.rs b/thinkterm-web/src/host.rsindex 49351f3c..0e09efeb 100644--- a/thinkterm-web/src/host.rs+++ b/thinkterm-web/src/host.rs@@ -3,7 +3,7 @@//! client; only `P` and `L` differ.use crate::platform::{Link, Platform};-use std::cell::RefCell;+use std::cell::{Cell, RefCell};use std::rc::Rc;use std::sync::Arc;use thinkterm_session::clock::{Clock, Timestamp};@@ -85,16 +85,30 @@ impl SessionEvents for Events {pub struct Config {rules: Arc<Vec<termwiz::hyperlink::Rule>>,+ /// The most rows the page has given its grid. A phone's soft keyboard+ /// cuts the grid to what shows above it, and the rows fetched ahead+ /// are kept to this many: the ones the keyboard gives back as it goes+ /// are then on hand, where a screen of the short grid fell well short+ /// of them and the top of the pane showed blank for a round trip.+ tallest_rows: Cell<usize>,}impl Default for Config {fn default() -> Self {Self {rules: Arc::new(Vec::new()),+ tallest_rows: Cell::new(0),}}}+impl Config {+ /// The page laid its grid out `rows` tall.+ pub fn saw_rows(&self, rows: usize) {+ self.tallest_rows.set(self.tallest_rows.get().max(rows));+ }+}+impl HostConfig for Config {fn hyperlink_rules(&self) -> Arc<Vec<termwiz::hyperlink::Rule>> {Arc::clone(&self.rules)@@ -105,6 +119,9 @@ impl HostConfig for Config {fn scrollback_lookahead_screens(&self) -> usize {1}+ fn scrollback_lookahead_rows(&self) -> usize {+ self.tallest_rows.get()+ }fn warm_scrollback(&self) -> bool {false}diff --git a/thinkterm-web/ui/src/KeyBar.svelte b/thinkterm-web/ui/src/KeyBar.svelteindex af55d33e..466358cd 100644--- a/thinkterm-web/ui/src/KeyBar.svelte+++ b/thinkterm-web/ui/src/KeyBar.svelte@@ -169,6 +169,15 @@let applied = 0;/** The bar's height when the canvas was last cut to fit above it. */let appliedMine = 0;+ /** The height the canvas had before the soft keyboard cut it down;+ zero while it has it. */+ let rest = 0;+ /** Grown back to `rest` as the keyboard began to go: the canvas stays+ where it is and the keyboard slides off its last rows, as a phone's+ own apps do. Riding down with the bar instead showed blank above it+ until the cut, a beat after the keyboard had gone. */+ let ahead = false;+ let lastHeight = 0;const root = document.documentElement;/** Until the canvas is cut to the viewport's new height, it rides withthe bar (--term-shift in tokens.css): its last row -- the prompt --@@ -177,7 +186,7 @@put them. Left where it was, the prompt fell behind the keyboard asit came up, and the terminal jumped down once it went. */const ride = (barTop: number) => {- const shift = applied > 0 ? barTop - (applied - appliedMine) : 0;+ const shift = applied > 0 && !ahead ? barTop - (applied - appliedMine) : 0;root.style.setProperty('--term-shift', `${Math.round(shift)}px`);};/** The height the canvas is cut to, once the viewport has stopped@@ -193,6 +202,8 @@// Cut to fit, the canvas sits where the bar is: nothing to ride.root.style.setProperty('--term-shift', '0px');if (height === applied) return;+ if (height < applied && rest === 0) rest = applied;+ if (height >= rest) rest = 0;applied = height;root.style.setProperty('--vvh', `${height}px`);};@@ -210,10 +221,18 @@// -- every time the bar moved.const barTop = (vv ? vv.offsetTop : 0) + height - mine;top = barTop;+ // The keyboard going: back to the whole height at once, one cut.+ if (height < lastHeight) ahead = false;+ if (!ahead && rest > 0 && height > applied) {+ ahead = true;+ apply(rest, mine);+ }+ lastHeight = height;ride(barTop);if (settle !== null) clearTimeout(settle);settle = setTimeout(() => {settle = null;+ ahead = false;apply(height, mine);}, SETTLE);};