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.

web: phone gestures and layout fixes for the terminal canvas and tab row

RoversX5af627cdcdf5
diff --git a/thinkterm-web/src/app.rs b/thinkterm-web/src/app.rs
index 39219d16..02d14282 100644
--- a/thinkterm-web/src/app.rs
+++ b/thinkterm-web/src/app.rs
@@ -2063,7 +2063,19 @@ impl App {
return true;
}
if ev.ctrl_key() {
- return false;
+ // A real Ctrl+wheel (a trackpad pinch on the desktop) stays the
+ // browser's zoom. The page synthesises one for a two-finger
+ // pinch on the canvas (touch.ts), where the browser's zoom is
+ // switched off; that one changes the pane's font instead.
+ if ev.is_trusted() {
+ return false;
+ }
+ let pane_id = hit.pane_id;
+ let step = if ev.delta_y() < 0.0 { 1.0 } else { -1.0 };
+ drop(inner);
+ self.focus_pane(pane_id, false);
+ self.step_font(step);
+ return true;
}
let max = max_scroll(&session.dimensions());
cell.scroll_from_bottom = if lines < 0.0 {
diff --git a/thinkterm-web/ui/src/ContextMenu.svelte b/thinkterm-web/ui/src/ContextMenu.svelte
index cd437a8f..1eb01400 100644
--- a/thinkterm-web/ui/src/ContextMenu.svelte
+++ b/thinkterm-web/ui/src/ContextMenu.svelte
@@ -5,10 +5,24 @@
// keyboard, and closed by anything that means the page moved on.
import { closeInnermost, closeMenu, menu, runItem } from './menu.svelte';
import { chevronRight, iconByName } from './icons';
+ import { mobile } from './mobile.svelte';
import type { MenuItem } from './model';
/** How near an edge a panel may come before it flips. */
const EDGE = 6;
+
+ /** The edges a panel must stay inside. On a phone that is the visual
+ viewport less the key bar -- the soft keyboard shrinks the former and
+ the bar sits at its foot -- not the layout viewport, which is the whole
+ window whatever is drawn over its lower half. */
+ function bounds(): { right: number; bottom: number } {
+ const vv = window.visualViewport;
+ if (!mobile.on || !vv) {
+ return { right: window.innerWidth - EDGE, bottom: window.innerHeight - EDGE };
+ }
+ const keybar = parseFloat(getComputedStyle(document.documentElement).getPropertyValue('--keybar')) || 0;
+ return { right: vv.offsetLeft + vv.width - EDGE, bottom: vv.offsetTop + vv.height - keybar - EDGE };
+ }
/** How long the pointer rests on a submenu row before it opens. */
const HOVER = 150;
@@ -47,8 +61,7 @@
}
const w = el.offsetWidth;
const h = el.offsetHeight;
- const right = window.innerWidth - EDGE;
- const bottom = window.innerHeight - EDGE;
+ const { right, bottom } = bounds();
let x: number;
let y: number;
if (k === 0) {
@@ -90,14 +103,20 @@
if (!inside(ev.target)) closeMenu();
};
const away = () => closeMenu();
+ // On a phone a resize is the soft keyboard coming or going, which is
+ // not the page moving on; the menu that opened the keyboard's owner
+ // must not vanish because of it.
+ const resized = () => {
+ if (!mobile.on) closeMenu();
+ };
window.addEventListener('pointerdown', down, true);
window.addEventListener('blur', away);
- window.addEventListener('resize', away);
+ window.addEventListener('resize', resized);
window.addEventListener('wheel', away, { capture: true, passive: true });
return () => {
window.removeEventListener('pointerdown', down, true);
window.removeEventListener('blur', away);
- window.removeEventListener('resize', away);
+ window.removeEventListener('resize', resized);
window.removeEventListener('wheel', away, { capture: true });
};
});
diff --git a/thinkterm-web/ui/src/tokens.css b/thinkterm-web/ui/src/tokens.css
index d92e17d6..aaf51f74 100644
--- a/thinkterm-web/ui/src/tokens.css
+++ b/thinkterm-web/ui/src/tokens.css
@@ -408,8 +408,10 @@ body[data-mobile] input, body[data-mobile] textarea, body[data-mobile] select {
are KeyBar.svelte's, set on the root while the bar is mounted. */
body[data-mobile] #term,
body[data-mobile] #panes {
- inset: var(--tabs) 0 auto 0;
- width: 100%;
+ /* Clear of the notch and the rounded corners in landscape, as the tab
+ row and the drawers already are. */
+ inset: var(--tabs) var(--safe-r) auto var(--safe-l);
+ width: auto;
height: calc(var(--vvh, 100%) - var(--tabs) - var(--keybar));
}
/* The browser must not pan or zoom the page: every finger on the canvas is
@@ -424,11 +426,18 @@ body[data-mobile] #tabs {
}
body[data-mobile] #term, body[data-mobile] #panes { top: calc(var(--tabs) + var(--safe-t)); }
body[data-mobile] #term, body[data-mobile] #panes { height: calc(var(--vvh, 100%) - var(--tabs) - var(--safe-t) - var(--keybar)); }
-body[data-mobile] #tabs .tab { height: 36px; width: 150px; padding: 0 6px 0 10px; }
+/* A capsule takes what its title needs, up to under half the screen, so
+ two or three tabs share a 390px row instead of one 150px box pushing the
+ rest off the edge; the same rule the pane capsules got below. */
+body[data-mobile] #tabs .tab { height: 36px; flex: 0 1 auto; width: auto; min-width: 88px; max-width: min(150px, 44vw); padding: 0 6px 0 10px; }
+/* The drawer toggle and the trailing buttons stay put while the capsules
+ scroll under them, so neither end is ever out of reach. */
+body[data-mobile] #tabs [data-action=sidebar] { position: sticky; left: 0; background: var(--bg); box-shadow: 4px 0 6px -2px var(--bg); }
+body[data-mobile] #tabs .trail { position: sticky; right: 0; margin-left: auto; padding-left: 6px; background: var(--bg); box-shadow: -4px 0 6px -2px var(--bg); z-index: 1; }
body[data-mobile] .act { width: 38px; height: 38px; }
/* A close button between two capsules is easy to hit by mistake; on a
phone it is only on the tab in front, and big enough to mean it. */
-body[data-mobile] .x { width: 28px; height: 28px; margin-left: 2px; }
+body[data-mobile] .x { width: 32px; height: 32px; margin-left: 2px; }
body[data-mobile] #tabs .tab:not(.current) .x { display: none; }
/* Only the zoom button fits beside the capsules on a phone; a split and a
new tab are the tab row's and the menus'. */
@@ -438,7 +447,7 @@ body[data-mobile] .nav .act[data-action=split-below] { display: none; }
/* The pane's own capsule has the width the tab row's has not: a 160px box
on a 390px screen left two thirds of the bar empty. */
body[data-mobile] .nav .cap { width: auto; flex: 1 1 auto; min-width: 0; max-width: 100%; }
-body[data-mobile] .nav .acts .act { width: 34px; height: 34px; }
+body[data-mobile] .nav .acts .act { width: 40px; height: 40px; }
/* The two panels are drawers: over the canvas, resizing nothing, or not
there at all. */
body[data-mobile] #side,
@@ -474,7 +483,7 @@ body[data-mobile] #side .row { min-height: 44px; }
body[data-mobile] #side .hdr { min-height: 36px; }
body[data-mobile] #side .top .pill, body[data-mobile] #side .pill { height: 38px; }
body[data-mobile] #side .act.round { width: 38px; height: 38px; }
-body[data-mobile] #side .act { width: 34px; height: 34px; }
+body[data-mobile] #side .act { width: 40px; height: 40px; }
body[data-mobile] #side .foot { height: 52px; left: 12px; right: 12px; }
body[data-mobile] #side .foot .act { width: 40px; height: 40px; }
body[data-mobile] #side .list { inset: 12px 10px 52px 10px; }
@@ -528,7 +537,7 @@ body[data-mobile] #settings .ctl { justify-content: flex-start; flex-wrap: wrap;
body[data-mobile] #settings .pillsel, body[data-mobile] #settings .pillbtn,
body[data-mobile] #settings .seg, body[data-mobile] #settings .stepper { height: 38px; }
body[data-mobile] #settings .pillsel { flex: 1 1 auto; min-width: 0; }
-body[data-mobile] #settings .sc { top: calc(var(--safe-t) + 10px); right: calc(10px + var(--safe-r)); width: 34px; height: 34px; }
+body[data-mobile] #settings .sc { top: calc(var(--safe-t) + 10px); right: calc(10px + var(--safe-r)); width: 40px; height: 40px; }
/* The remark and the takeover card sit clear of the key bar. */
body[data-mobile] #status {
right: calc(10px + var(--safe-r)); left: calc(10px + var(--safe-l)); max-width: none;
diff --git a/thinkterm-web/ui/src/touch.ts b/thinkterm-web/ui/src/touch.ts
index 33a1209d..e9ac7aee 100644
--- a/thinkterm-web/ui/src/touch.ts
+++ b/thinkterm-web/ui/src/touch.ts
@@ -28,6 +28,12 @@ const TAP = 300;
const CELL = 17;
/** How long after a long press the browser's own menu is still refused. */
const SETTLED = 1000;
+/** How much of a fling's speed is left after 16ms, and the speed (px/ms)
+ below which it has stopped. */
+const FLING_DECAY = 0.94;
+const FLING_MIN = 0.04;
+/** How far two fingers have to spread or close for one font step. */
+const PINCH_STEP = 48;
type Finger = { x: number; y: number };
@@ -50,6 +56,15 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
/** When the long press last opened a menu, so the browser's own -- which
some phones fire on top of it -- can be refused. */
let opened = 0;
+ /** The finger's speed down the screen (px/ms, smoothed) and when it was
+ last measured, for the fling that carries on after it lifts. */
+ let vy = 0;
+ let lastMoveAt = 0;
+ let lastAt: Finger = { x: 0, y: 0 };
+ let fling: number | null = null;
+ /** The distance between two fingers the last time a font step was
+ taken, so a pinch steps once per PINCH_STEP of travel. */
+ let pinchBase = 0;
const stopHold = () => {
if (hold === null) return;
@@ -57,6 +72,55 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
hold = null;
};
+ const stopFling = () => {
+ if (fling === null) return;
+ cancelAnimationFrame(fling);
+ fling = null;
+ };
+
+ /** Keep scrolling after the finger lifts, slowing down until it stops.
+ Each frame hands the same synthetic wheels over as the finger did. */
+ const startFling = () => {
+ if (Math.abs(vy) < FLING_MIN) return;
+ let last = performance.now();
+ const tick = (now: number) => {
+ const dt = Math.min(now - last, 64);
+ last = now;
+ scrollBy(vy * dt, lastAt);
+ vy *= Math.pow(FLING_DECAY, dt / 16);
+ if (Math.abs(vy) < FLING_MIN) {
+ fling = null;
+ return;
+ }
+ fling = requestAnimationFrame(tick);
+ };
+ fling = requestAnimationFrame(tick);
+ };
+
+ /** How far apart the two fingers are, or 0 with fewer than two. */
+ const spread = (): number => {
+ const pts = [...fingers.values()];
+ if (pts.length < 2) return 0;
+ return Math.hypot(pts[0].x - pts[1].x, pts[0].y - pts[1].y);
+ };
+
+ /** One font step for the pane under the fingers: a Ctrl+wheel the wasm
+ knows is the page's (untrusted), since the browser's own pinch-zoom is
+ off on the canvas. Spreading is zooming in, as Ctrl+wheel-up is. */
+ const zoomStep = (dir: 1 | -1, at: Finger) => {
+ canvas.dispatchEvent(
+ new WheelEvent('wheel', {
+ deltaY: -dir,
+ deltaMode: 0,
+ ctrlKey: true,
+ bubbles: true,
+ cancelable: true,
+ clientX: at.x,
+ clientY: at.y,
+ }),
+ );
+ };
+
/** The midpoint of the fingers down, which is what two of them scroll by. */
const midpoint = (): Finger | null => {
let x = 0;
@@ -116,10 +180,14 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
const mid = midpoint();
lastY = mid ? mid.y : startY;
pending = 0;
+ vy = 0;
+ pinchBase = spread();
};
const down = (ev: PointerEvent) => {
if (!ev.isTrusted || ev.pointerType !== 'touch') return;
+ // A finger on the glass stops whatever a fling was still scrolling.
+ stopFling();
fingers.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
if (first === null) {
first = ev.pointerId;
@@ -127,7 +195,10 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
startY = ev.clientY;
startAt = ev.timeStamp;
lastY = ev.clientY;
+ lastMoveAt = ev.timeStamp;
+ lastAt = { x: ev.clientX, y: ev.clientY };
pending = 0;
+ vy = 0;
mode = 'press';
hold = setTimeout(() => {
hold = null;
@@ -141,10 +212,12 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
// the terminal over, which is what a tap is for.
return;
}
- // A second finger is the page's, not the wasm's: two fingers scroll.
+ // A second finger is the page's, not the wasm's: two fingers scroll,
+ // and pinch to change the font.
ev.stopImmediatePropagation();
ev.preventDefault();
if (mode === 'press') takeOver('scroll');
+ else pinchBase = spread();
};
const move = (ev: PointerEvent) => {
@@ -163,8 +236,27 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
ev.stopImmediatePropagation();
const mid = midpoint();
if (!mid) return;
+ lastAt = mid;
+ // Two fingers: the change in their distance is a pinch, one font step
+ // per PINCH_STEP of it; what is left of their motion scrolls.
+ if (fingers.size >= 2) {
+ const now = spread();
+ if (pinchBase === 0) pinchBase = now;
+ const grown = now - pinchBase;
+ if (Math.abs(grown) >= PINCH_STEP) {
+ zoomStep(grown > 0 ? 1 : -1, mid);
+ pinchBase = now;
+ }
+ }
const dy = mid.y - lastY;
lastY = mid.y;
+ const dt = ev.timeStamp - lastMoveAt;
+ if (dt > 0) {
+ // Smoothed, so one slow last sample does not cancel a quick flick and
+ // one jittery sample does not launch one.
+ vy = vy * 0.6 + (dy / dt) * 0.4;
+ lastMoveAt = ev.timeStamp;
+ }
if (dy !== 0) scrollBy(dy, mid);
};
@@ -185,8 +277,18 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
// ask for, and asking has to happen inside the press itself.
document.getElementById('kbd')?.focus();
}
- if (ev.pointerId !== first) return;
+ if (ev.pointerId !== first) {
+ // One of two fingers lifted: the other carries on scrolling from
+ // where it is, not from where the pair's midpoint was.
+ const mid = midpoint();
+ if (mid) lastY = mid.y;
+ pinchBase = 0;
+ return;
+ }
stopHold();
+ // A flick keeps the scrollback moving; a finger that stopped before
+ // lifting (more than a frame ago) does not.
+ if (mode === 'scroll' && ev.timeStamp - lastMoveAt < 80) startFling();
first = null;
mode = 'press';
fingers.clear();
@@ -207,6 +309,7 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
canvas.addEventListener('contextmenu', contextmenu, true);
return () => {
stopHold();
+ stopFling();
canvas.removeEventListener('pointerdown', down, true);
canvas.removeEventListener('pointermove', move, true);
canvas.removeEventListener('pointerup', up, true);