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: a tap is a tap, and the soft keyboard comes when asked

RoversX10e4afb10237
diff --git a/thinkterm-web/src/app.rs b/thinkterm-web/src/app.rs
index 55cb4105..7d74d8a7 100644
--- a/thinkterm-web/src/app.rs
+++ b/thinkterm-web/src/app.rs
@@ -1817,12 +1817,29 @@ impl App {
.map(|p| p.pane_id)
}
+ /// Focus the field the terminal types through -- unless the page says
+ /// the soft keyboard is not wanted. On a phone (`body[data-mobile]`)
+ /// focusing it raises the keyboard over half the screen, so only the
+ /// key bar's keyboard button asks for it (`body[data-keyboard]`, kept by
+ /// mobile.svelte.ts); a press or a finished rename must not.
+ fn focus_terminal(textarea: &web_sys::HtmlElement) {
+ if let Some(body) = web_sys::window()
+ .and_then(|w| w.document())
+ .and_then(|d| d.body())
+ {
+ if body.has_attribute("data-mobile") && !body.has_attribute("data-keyboard") {
+ return;
+ }
+ }
+ let _ = textarea.focus();
+ }
+
pub fn pointer(self: &Rc<Self>, ev: &web_sys::PointerEvent, what: Pointer) {
if what == Pointer::Down {
// Focus first, outside any borrow: focus() dispatches events
// synchronously and a listener may look at the app.
let textarea = self.inner.borrow().textarea.clone();
- let _ = textarea.focus();
+ Self::focus_terminal(&textarea);
let canvas = self.inner.borrow().canvas.clone();
let _ = canvas.set_pointer_capture(ev.pointer_id());
}
@@ -2620,7 +2637,7 @@ impl App {
inner.editing = Editing::None;
inner.new_project_error = None;
Self::notify(inner);
- let _ = inner.textarea.focus();
+ Self::focus_terminal(&inner.textarea);
}
"Enter" if editing == Editing::NewProject => self.add_workspace(value.trim()),
"Enter" => {
@@ -2628,7 +2645,7 @@ impl App {
let inner = &mut *self.inner.borrow_mut();
inner.editing = Editing::None;
Self::notify(inner);
- let _ = inner.textarea.focus();
+ Self::focus_terminal(&inner.textarea);
}
let value = value.trim().to_string();
if value.is_empty() {
@@ -2686,7 +2703,7 @@ impl App {
inner.editing = crate::sidebar::Editing::None;
inner.new_project_error = None;
Self::notify(inner);
- let _ = inner.textarea.focus();
+ Self::focus_terminal(&inner.textarea);
}
if let Some(thread) = existing {
self.activate_thread(thread);
@@ -3211,7 +3228,7 @@ impl App {
let mut inner = self.inner.borrow_mut();
inner.following = !inner.following;
Self::render_strip(&inner);
- let _ = inner.textarea.focus();
+ Self::focus_terminal(&inner.textarea);
}
Click::NewTab => self.new_tab(),
Click::NewInStack(pane) => self.new_in_stack(pane),
diff --git a/thinkterm-web/ui/src/KeyBar.svelte b/thinkterm-web/ui/src/KeyBar.svelte
index 176d671a..cdb8e86c 100644
--- a/thinkterm-web/ui/src/KeyBar.svelte
+++ b/thinkterm-web/ui/src/KeyBar.svelte
@@ -6,7 +6,7 @@
// (thinkterm-web/src/bridge.rs); Ctrl and Alt are sticky, and apply to
// the next key from the bar or from the soft keyboard, whichever comes.
import { handle } from './client';
- import { KEYBAR } from './mobile.svelte';
+ import { KEYBAR, toggleKeyboard } from './mobile.svelte';
/** A key: `name` is the DOM name the wasm maps, `label` what it says. */
type Key = { name: string; label: string };
@@ -36,6 +36,10 @@
const REPEATS = new Set(['ArrowLeft', 'ArrowUp', 'ArrowDown', 'ArrowRight']);
/** How long an arrow is held before it repeats, and how often it then does. */
const HOLD = 500;
+ /** How long the visual viewport has to hold still before the canvas is
+ cut to it: longer than the gap between the keyboard's animation
+ steps, shorter than a person notices. */
+ const SETTLE = 150;
const EVERY = 60;
/** The sticky modifiers: armed by a tap, spent by the next key. */
@@ -82,13 +86,6 @@
}, HOLD);
}
- /** Ask for the soft keyboard, or send it away. */
- function toggleKeyboard() {
- const kbd = document.getElementById('kbd');
- if (!(kbd instanceof HTMLTextAreaElement)) return;
- if (document.activeElement === kbd) kbd.blur();
- else kbd.focus();
- }
// One delegated handler, as the tab row and the sidebar have. The press
// must not move focus: #kbd keeps it, so the soft keyboard stays up while
@@ -158,14 +155,35 @@
// keyboard shrinks that viewport and nothing else.
$effect(() => {
const vv = window.visualViewport;
+ let settle: ReturnType<typeof setTimeout> | null = null;
+ let applied = 0;
+ /** The height the canvas is cut to, once the viewport has stopped
+ moving. The soft keyboard slides in over a few hundred milliseconds
+ and the viewport reports every step of it; cutting the canvas at
+ each step resized the terminal that many times, and every resize
+ reflowed and redrew it -- the flicker seen when the keyboard came
+ up. The bar itself still follows every step, so it never floats
+ away from the keyboard's edge. */
+ const apply = (height: number, mine: number) => {
+ document.documentElement.style.setProperty('--keybar', `${mine}px`);
+ if (height === applied) return;
+ applied = height;
+ document.documentElement.style.setProperty('--vvh', `${height}px`);
+ };
const read = () => {
const height = vv ? vv.height : window.innerHeight;
const mine = bar?.offsetHeight || KEYBAR;
top = (vv ? vv.offsetTop : 0) + height - mine;
- document.documentElement.style.setProperty('--keybar', `${mine}px`);
- document.documentElement.style.setProperty('--vvh', `${height}px`);
+ if (settle !== null) clearTimeout(settle);
+ settle = setTimeout(() => {
+ settle = null;
+ apply(height, mine);
+ }, SETTLE);
};
document.documentElement.style.setProperty('--keybar', `${KEYBAR}px`);
+ // The first cut is immediate: nothing is moving yet, and the terminal
+ // must not sit at the window's height for SETTLE before it fits.
+ apply(vv ? vv.height : window.innerHeight, bar?.offsetHeight || KEYBAR);
read();
// The first read runs before the bar has been laid out with its safe
// area, so its real height is taken on the next frame.
@@ -174,6 +192,7 @@
vv?.addEventListener('scroll', read);
window.addEventListener('resize', read);
return () => {
+ if (settle !== null) clearTimeout(settle);
vv?.removeEventListener('resize', read);
vv?.removeEventListener('scroll', read);
window.removeEventListener('resize', read);
diff --git a/thinkterm-web/ui/src/chrome.ts b/thinkterm-web/ui/src/chrome.ts
index 97e0f7a9..58bdc319 100644
--- a/thinkterm-web/ui/src/chrome.ts
+++ b/thinkterm-web/ui/src/chrome.ts
@@ -4,7 +4,7 @@
import { handle } from './client';
import { chromeClick, views } from './client.svelte';
-import { mobile, openSide } from './mobile.svelte';
+import { focusTerminal, mobile, openSide } from './mobile.svelte';
import { openPalette } from './palette.svelte';
import { openSettings, setSetting } from './settings.svelte';
@@ -25,7 +25,7 @@ export function toggleSidebar() {
// Refit in this task, before the browser paints the moved box with the
// old bitmap stretched across it.
handle.client?.resize();
- document.getElementById('kbd')?.focus();
+ focusTerminal();
}
function id(el: Element | null, attr: string): number | null {
diff --git a/thinkterm-web/ui/src/menu.svelte.ts b/thinkterm-web/ui/src/menu.svelte.ts
index 3000c87b..fe8adb0d 100644
--- a/thinkterm-web/ui/src/menu.svelte.ts
+++ b/thinkterm-web/ui/src/menu.svelte.ts
@@ -7,6 +7,7 @@
import { handle } from './client';
import { refreshViews } from './client.svelte';
import type { MenuItem, MenuOutcome } from './model';
+import { focusTerminal } from './mobile.svelte';
export const menu = $state({
/** The root list; empty when no menu is open. */
@@ -51,7 +52,7 @@ export function closeMenu() {
menu.selected = -1;
// The menu took focus off the field the terminal types through, so
// closing it has to hand focus back or the next key goes nowhere.
- document.getElementById('kbd')?.focus();
+ focusTerminal();
}
/** Close the innermost submenu; the whole menu when none is open. */
diff --git a/thinkterm-web/ui/src/mobile.svelte.ts b/thinkterm-web/ui/src/mobile.svelte.ts
index 66d299ca..d1221dc0 100644
--- a/thinkterm-web/ui/src/mobile.svelte.ts
+++ b/thinkterm-web/ui/src/mobile.svelte.ts
@@ -18,8 +18,47 @@ export const mobile = $state({
on: false,
/** The left panel is out as a drawer. Only read in the phone shape. */
side: false,
+ /** The soft keyboard was asked for, with the key bar's keyboard button.
+ Only read in the phone shape: a desktop always wants #kbd focused,
+ since focusing it costs nothing there. On a phone it costs the lower
+ half of the screen, so nothing else -- a tap on the terminal, a menu
+ closing, a rename finishing -- may put focus there uninvited. */
+ keyboard: false,
});
+/** Whether focusing #kbd is wanted right now: always on a desktop, and on
+ a phone only once the keyboard button asked for the soft keyboard. */
+export function wantKeyboard(): boolean {
+ return !mobile.on || mobile.keyboard;
+}
+
+/** Put focus back on the field the terminal types through, where that is
+ wanted (see `wantKeyboard`). Every "hand focus back to the terminal"
+ site goes through here, so a phone never gets the soft keyboard from a
+ panel closing under it. */
+export function focusTerminal() {
+ if (!wantKeyboard()) return;
+ document.getElementById('kbd')?.focus();
+}
+
+/** The keyboard button: ask for the soft keyboard, or send it away. */
+export function toggleKeyboard() {
+ const kbd = document.getElementById('kbd');
+ if (!(kbd instanceof HTMLTextAreaElement)) return;
+ const up = document.activeElement === kbd;
+ mobile.keyboard = !up;
+ paintKeyboard();
+ if (up) kbd.blur();
+ else kbd.focus();
+}
+
+/** The wasm asks the same question through `body[data-keyboard]`, since
+ it focuses #kbd itself after a press or a finished rename. */
+function paintKeyboard() {
+ if (mobile.on && mobile.keyboard) document.body.dataset.keyboard = '';
+ else delete document.body.dataset.keyboard;
+}
+
function paint(on: boolean) {
if (mobile.on === on) return;
mobile.on = on;
@@ -31,6 +70,8 @@ function paint(on: boolean) {
// The drawer is the phone shape's; a window that grew back into the
// desktop shape shows the panel beside the canvas again.
mobile.side = false;
+ mobile.keyboard = false;
+ paintKeyboard();
}
/** Follow the pointer and the window's width from here on. */
@@ -40,6 +81,19 @@ export function watchMobile() {
coarse.addEventListener('change', read);
window.addEventListener('resize', read);
read();
+ // The phone's own "done" key dismisses the soft keyboard by blurring
+ // #kbd with nothing else taking focus; from then on the keyboard is not
+ // wanted until the button asks again. A blur that moves focus into a
+ // panel is that panel's, and the flag stays for when it closes.
+ document.getElementById('kbd')?.addEventListener('blur', () => {
+ if (!mobile.on) return;
+ setTimeout(() => {
+ if (document.activeElement === document.body || document.activeElement === null) {
+ mobile.keyboard = false;
+ paintKeyboard();
+ }
+ }, 0);
+ });
}
/** Put the left drawer out, or away. */
@@ -47,6 +101,10 @@ export function openSide(open: boolean) {
if (mobile.side === open) return;
mobile.side = open;
// The soft keyboard would cover most of the drawer, so it goes away while
- // the drawer is out; the next tap on a pane asks for it back.
- if (open) (document.getElementById('kbd') as HTMLTextAreaElement | null)?.blur();
+ // the drawer is out; the keyboard button asks for it back.
+ if (open) {
+ mobile.keyboard = false;
+ paintKeyboard();
+ (document.getElementById('kbd') as HTMLTextAreaElement | null)?.blur();
+ }
}
diff --git a/thinkterm-web/ui/src/palette.svelte.ts b/thinkterm-web/ui/src/palette.svelte.ts
index 45b28a2f..64642d8a 100644
--- a/thinkterm-web/ui/src/palette.svelte.ts
+++ b/thinkterm-web/ui/src/palette.svelte.ts
@@ -9,6 +9,7 @@ import { refreshViews, views } from './client.svelte';
import { toggleSidebar } from './chrome';
import type { Hotkey, PaletteEntry, PaletteOutcome, PaletteResults } from './model';
import { openSettings, setSetting } from './settings.svelte';
+import { focusTerminal } from './mobile.svelte';
/** Where this browser keeps the picks, most recent first. */
const RECENT = 'thinkterm.recent';
@@ -68,7 +69,7 @@ export function closePalette() {
palette.results = EMPTY;
palette.selected = 0;
// The palette took focus off the field the terminal types through.
- document.getElementById('kbd')?.focus();
+ focusTerminal();
}
/** The keyboard's row, `delta` rows on, wrapping through every section. */
diff --git a/thinkterm-web/ui/src/settings.svelte.ts b/thinkterm-web/ui/src/settings.svelte.ts
index a855cb5a..fa357365 100644
--- a/thinkterm-web/ui/src/settings.svelte.ts
+++ b/thinkterm-web/ui/src/settings.svelte.ts
@@ -7,6 +7,7 @@
import { handle } from './client';
import { refreshViews, setLocale, views } from './client.svelte';
import type { LanguageOption, Scheme, Theme } from './model';
+import { focusTerminal } from './mobile.svelte';
/** Where this browser keeps the preferences, whole, as the wasm's JSON. */
const STORE = 'thinkterm.settings';
@@ -36,7 +37,7 @@ export function closeSettings() {
if (!panel.open) return;
panel.open = false;
// The panel took focus off the field the terminal types through.
- document.getElementById('kbd')?.focus();
+ focusTerminal();
}
/** Read the wasm's views back and the language list with them: a change of
diff --git a/thinkterm-web/ui/src/touch.ts b/thinkterm-web/ui/src/touch.ts
index aad31d17..44403fa6 100644
--- a/thinkterm-web/ui/src/touch.ts
+++ b/thinkterm-web/ui/src/touch.ts
@@ -15,7 +15,7 @@
// case its Up throws the selection away instead of copying it.
import { openMenu } from './menu.svelte';
-import { mobile, openSide } from './mobile.svelte';
+import { focusTerminal, mobile, openSide } from './mobile.svelte';
import { cellHeight, paneAt } from './pane';
import { smoothScroll } from './settings.svelte';
@@ -23,8 +23,6 @@ import { smoothScroll } from './settings.svelte';
const SLOP = 8;
/** How long a press has to rest before it is the pane's menu. */
const HOLD = 500;
-/** A press shorter than this, that did not wander, is a tap. */
-const TAP = 300;
/** A cell to scroll by while the layout has not named one. */
const CELL = 17;
/** How long after a long press the browser's own menu is still refused. */
@@ -47,7 +45,6 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
let first: number | null = null;
let startX = 0;
let startY = 0;
- let startAt = 0;
let mode: Mode = 'press';
let hold: ReturnType<typeof setTimeout> | null = null;
/** Where the last wheel was measured from, and the pixels not yet worth
@@ -135,19 +132,24 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
return n === 0 ? null : { x: x / n, y: y / n };
};
- /** Tell the wasm the press it saw is over, at the point it began. */
- const cancelPress = () => {
- canvas.dispatchEvent(
- new PointerEvent('pointercancel', {
- pointerId: first ?? 1,
- pointerType: 'touch',
- isPrimary: true,
- bubbles: true,
- cancelable: true,
- clientX: startX,
- clientY: startY,
- }),
- );
+ /** Hand the wasm the tap it was kept from: a press and a release at
+ the point the finger first touched, as one event each. The finger's
+ own events never reached it (see `down`), so this is the whole of
+ what it learns about the gesture. Untrusted, which the handlers here
+ and the drawer's swipe use to tell it from a finger. */
+ const deliverTap = () => {
+ const at = {
+ pointerId: first ?? 1,
+ pointerType: 'touch',
+ isPrimary: true,
+ bubbles: true,
+ cancelable: true,
+ clientX: startX,
+ clientY: startY,
+ button: 0,
+ };
+ canvas.dispatchEvent(new PointerEvent('pointerdown', { ...at, buttons: 1 }));
+ canvas.dispatchEvent(new PointerEvent('pointerup', { ...at, buttons: 0 }));
};
/** Scroll by what the fingers moved. In smooth mode every move goes over
@@ -180,12 +182,11 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
);
};
- /** The gesture is the page's from here on: the wasm's press is undone
- and every event left in the gesture is swallowed. */
+ /** The gesture is the page's from here on: the wasm never hears of it,
+ and every event left in it is swallowed. */
const takeOver = (next: Mode) => {
stopHold();
mode = next;
- cancelPress();
const mid = midpoint();
lastY = mid ? mid.y : startY;
pending = 0;
@@ -202,7 +203,6 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
first = ev.pointerId;
startX = ev.clientX;
startY = ev.clientY;
- startAt = ev.timeStamp;
lastY = ev.clientY;
lastMoveAt = ev.timeStamp;
lastAt = { x: ev.clientX, y: ev.clientY };
@@ -217,8 +217,14 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
const pane = paneAt(canvas, startX, startY);
if (pane !== null) openMenu('pane', String(pane), startX, startY);
}, HOLD);
- // The press itself goes to the wasm: it focuses the pane and takes
- // the terminal over, which is what a tap is for.
+ // The press is kept from the wasm until the finger says what it is.
+ // Handed over at once, every scroll and every long press began as a
+ // click in whatever cell the finger landed on -- a stray press in a
+ // full-screen program, a moved cursor in a shell -- with a cancel
+ // chasing it that the program had no way to honour. A tap is
+ // delivered whole when the finger lifts (`deliverTap`).
+ ev.stopImmediatePropagation();
+ ev.preventDefault();
return;
}
// A second finger is the page's, not the wasm's: two fingers scroll,
@@ -272,19 +278,16 @@ export function installTouch(canvas: HTMLCanvasElement): () => void {
const up = (ev: PointerEvent) => {
if (!ev.isTrusted || ev.pointerType !== 'touch') return;
if (!fingers.delete(ev.pointerId)) return;
- if (mode !== 'press') {
- // The wasm was told the press was cancelled; the release must not
- // reach it as a click in whatever cell the finger ended over.
- ev.stopImmediatePropagation();
- } else if (
- ev.pointerId === first &&
- ev.timeStamp - startAt < TAP &&
- Math.abs(ev.clientX - startX) <= SLOP &&
- Math.abs(ev.clientY - startY) <= SLOP
- ) {
- // A tap goes through untouched. The soft keyboard is the page's to
- // ask for, and asking has to happen inside the press itself.
- document.getElementById('kbd')?.focus();
+ // The wasm never saw this finger go down, so it must not see it come
+ // up either; what it gets is the tap below, or nothing.
+ ev.stopImmediatePropagation();
+ if (mode === 'press' && ev.pointerId === first) {
+ // Neither scrolled nor held: a tap, however long the finger rested,
+ // and it lands where the finger first touched rather than where a
+ // slight roll left it. The soft keyboard is a separate question,
+ // answered by the key bar's button (see `focusTerminal`).
+ deliverTap();
+ focusTerminal();
}
if (ev.pointerId !== first) {
// One of two fingers lifted: the other carries on scrolling from