From 1d9cb80cdb8bf6b0f9b05960f0ff67e093a75e60 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Tue, 20 Oct 2026 16:04:09 +0100 Subject: [PATCH] web: keep mobile composition stable across keyboard and Back navigation --- internal/server/chat_browser_test.go | 8 ++++ web/base.css | 18 +++++++++ web/chat.css | 20 ++++++++-- web/dialogs.css | 1 + web/shell.css | 22 ++++++++++ web/src/app.js | 23 ++++++++--- web/src/mobileViewport.js | 60 ++++++++++++++++++++++++++++ web/test/chat-hardening-probe.mjs | 33 ++++++++++++++- web/test/mobileViewport.test.mjs | 18 +++++++++ 9 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 web/src/mobileViewport.js create mode 100644 web/test/mobileViewport.test.mjs diff --git a/internal/server/chat_browser_test.go b/internal/server/chat_browser_test.go index 839b0cc0..95cc4bec 100644 --- a/internal/server/chat_browser_test.go +++ b/internal/server/chat_browser_test.go @@ -2,6 +2,7 @@ package server import ( "context" + "fmt" "net/http/httptest" "os" "os/exec" @@ -44,6 +45,13 @@ func TestChatBrowserLifecycleAndModelDialog(t *testing.T) { fake := scriptedChat() fake.block = true fake.sessions = []protocol.ChatSession{session} + for i := 0; i < 30; i++ { + other := protocol.ChatSession{ID: fmt.Sprintf("ses_browser_%d", i), Title: fmt.Sprintf("Scratch chat %d", i), Directory: directory} + if err := store.RecordOperatorSession(ctx, other.ID, directory, other.Title, protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + fake.sessions = append(fake.sessions, other) + } fake.models = []protocol.ChatModel{{ID: "model-a", Provider: "fixture", Name: "Model Alpha"}, {ID: "model-b", Provider: "fixture", Name: "Model Beta"}} fake.messages = []protocol.ChatMessage{ {ID: "msg_user", Role: protocol.ChatRoleUser, Parts: []protocol.ChatPart{{Type: "text", Text: "Scratch prompt"}}}, diff --git a/web/base.css b/web/base.css index 1a3447fd..245b6f7e 100644 --- a/web/base.css +++ b/web/base.css @@ -79,6 +79,24 @@ textarea { a { color: var(--color-accent); } +/* Application controls should not accidentally select their labels on a long + press; message, report and code content must remain selectable. */ +button, [role="tab"] { -webkit-user-select: none; user-select: none; touch-action: manipulation; } + +@media (any-pointer: coarse) { + button, input, select { min-height: 48px; } + button { -webkit-tap-highlight-color: transparent; } + button:active { background-color: var(--color-elevated-2); } +} + +/* At 16px Safari need not zoom the page merely to focus an editable field. */ +@media (max-width: 599px) { + input[type="text"], input[type="search"], input[type="password"], select, textarea, + .chat-composer-input, .chat-toolbar .chat-search, .chat-create-instructions-input { + font-size: 16px; + } +} + .list { min-width: 0; min-height: 0; diff --git a/web/chat.css b/web/chat.css index 389e5612..d60c055f 100644 --- a/web/chat.css +++ b/web/chat.css @@ -1012,10 +1012,11 @@ box-shadow: none; background: transparent; } -.chat-tools-modal-panel .chat-tool-list { overflow: auto; } +.chat-tools-modal-panel .chat-tool-list { overflow: auto; overscroll-behavior: contain; } .chat-reasoning-modal-body { min-height: 0; overflow: auto; + overscroll-behavior: contain; padding: 12px; white-space: pre-wrap; overflow-wrap: anywhere; @@ -1024,6 +1025,10 @@ line-height: 1.6; } +@media (max-width: 599px) { + .chat-composer-input, .chat-toolbar .chat-search, .chat-create-instructions-input { font-size: 16px; } +} + /* Code in agent replies keeps the terminal's colour grammar: comments muted, strings green, numbers amber, keywords blue, literals pink. */ .tok-comment { color: var(--color-muted); font-style: italic; } @@ -1105,7 +1110,7 @@ /* ---- Phone comfort ------------------------------------------------------ */ @media (max-width: 599px) { .chat-toolbar { padding: 10px 12px; } - .chat-toolbar .chat-search { min-height: 44px; font-size: 15px; } + .chat-toolbar .chat-search { min-height: 44px; font-size: 16px; } .chat-new { min-height: 44px; padding: 0 14px; } .chat-new-label { font-size: 14px; } .chat-group-header, @@ -1137,7 +1142,7 @@ .chat-send, .chat-stop { min-width: 44px; min-height: 44px; font-size: 18px; } .chat-attach { min-width: 44px; min-height: 44px; } - .chat-composer-input { padding: 10px 12px; font-size: 15px; } + .chat-composer-input { padding: 10px 12px; font-size: 16px; } .chat-turn { font-size: 15px; } } @@ -1182,3 +1187,12 @@ color: var(--muted); font-weight: var(--weight-regular); } + +/* Input capability, not just screen width, selects touch-sized controls. */ +@media (any-pointer: coarse) { + .chat-reasoning-toggle, .chat-tool-chip, .chat-group-toggle, + .chat-send, .chat-stop, .chat-attach, .chat-model-button, .chat-model-option, + .chat-new { min-height: 48px; } + .chat-row .chat-star, .chat-row .chat-menu-button { width: 48px; height: 48px; } + .chat-row .row-head { padding-right: 54px; } +} diff --git a/web/dialogs.css b/web/dialogs.css index 58fdde5a..a407aeac 100644 --- a/web/dialogs.css +++ b/web/dialogs.css @@ -397,6 +397,7 @@ border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5); + overscroll-behavior: contain; } .confirm-title, .action-failed-title { margin: 0; font-size: 16px; } diff --git a/web/shell.css b/web/shell.css index 52386551..336c7a06 100644 --- a/web/shell.css +++ b/web/shell.css @@ -642,6 +642,28 @@ button.back-button:hover { background: var(--color-elevated); } toolbar until it hides, leaving only the status row on screen. */ body, .app-root { height: 100svh; } + .workspace { + padding-top: env(safe-area-inset-top, 0px); + padding-left: env(safe-area-inset-left, 0px); + padding-right: env(safe-area-inset-right, 0px); + } + .keyboard-open body, + .keyboard-open .app-root { height: var(--keyboard-visible-height, 100svh); } + .keyboard-open .app-root { position: relative; top: var(--keyboard-viewport-top, 0px); } + .keyboard-open .destination-dock { display: none; } + .keyboard-open .confirm-dialog, + .keyboard-open .action-failed, + .keyboard-open .chat-tools-viewer { + top: var(--keyboard-viewport-top, 0px); + bottom: auto; + height: var(--keyboard-visible-height); + padding: 12px; + } + .keyboard-open .confirm-panel, + .keyboard-open .chat-tools-modal-panel { + max-height: calc(var(--keyboard-visible-height) - 24px); + overflow-y: auto; + } #listCollapse { display: none; } .dock-item, .dock-search { width: auto; min-width: 0; } diff --git a/web/src/app.js b/web/src/app.js index 749fabbf..30ec9e79 100644 --- a/web/src/app.js +++ b/web/src/app.js @@ -67,6 +67,7 @@ import { readEphemeralChatIds, } from "./views/chat.js"; import { createSearchView } from "./views/search.js"; +import { installMobileViewport } from "./mobileViewport.js"; function defaultServerUrl() { if (typeof location === "undefined" || !location.host) return ""; @@ -76,6 +77,7 @@ function defaultServerUrl() { const client = new PipelinesClient(); const repo = new PipelinesRepository(client); +installMobileViewport(); // Register the app's shell service worker where the browser allows it (a secure // context: https, localhost, or the desktop app). On a plain-LAN http origin the @@ -1035,7 +1037,7 @@ function restoreListFocus(restore) { // first focusable descendant rather than a non-focusable div. const target = row.matches("button, a, [tabindex]") ? row : row.querySelector("button, a, [tabindex]") ?? row; - target.focus(); + target.focus({ preventScroll: true }); return; } } @@ -1107,7 +1109,14 @@ function render() { renderAttentionSheet(); } +const listScrollPositions = new Map(); + function renderContent() { + const previousList = content.querySelector(".list-pane"); + const previousScroll = previousList?.querySelector(".list-scroll"); + if (previousScroll && previousList.getClientRects().length > 0) { + listScrollPositions.set(previousList.dataset.listView, previousScroll.scrollTop); + } // The graph owns wheel/pointer/resize listeners; drop the previous mount // before rebuilding the DOM so a re-render, tab switch or view switch never // leaves an observer or listener behind. @@ -1159,7 +1168,7 @@ function renderContent() { layout.append( h( "section", - { class: "list-pane", "aria-label": "Pipelines list" }, + { class: "list-pane", "data-list-view": view, "aria-label": "Pipelines list" }, paneHeading("Pipelines", { detailOpen }), renderPipelineList(), ), @@ -1169,7 +1178,7 @@ function renderContent() { layout.append( h( "section", - { class: "list-pane", "aria-label": "Runs list" }, + { class: "list-pane", "data-list-view": view, "aria-label": "Runs list" }, paneHeading("Runs", { detailOpen }), renderRunList(), ), @@ -1179,7 +1188,7 @@ function renderContent() { layout.append( h( "section", - { class: "list-pane", "aria-label": "Templates list" }, + { class: "list-pane", "data-list-view": view, "aria-label": "Templates list" }, paneHeading("Templates", { detailOpen }), renderTemplateGallery(), ), @@ -1189,7 +1198,7 @@ function renderContent() { layout.append( h( "section", - { class: "list-pane", "aria-label": "Chat list" }, + { class: "list-pane", "data-list-view": view, "aria-label": "Chat list" }, paneHeading("Chat", { detailOpen }), renderChatList(), ), @@ -1222,6 +1231,8 @@ function renderContent() { } } content.replaceChildren(...nodes); + const restoredList = content.querySelector(".list-pane .list-scroll"); + if (restoredList) restoredList.scrollTop = listScrollPositions.get(state.view) ?? 0; if (!reduceMotion) { for (const node of content.querySelectorAll("[data-animate-key]")) { const key = node.getAttribute("data-animate-key"); @@ -2659,4 +2670,4 @@ if (bootServerUrl) { state.url = sameOrigin; openConfiguredEndpoint({ surfaceError: false }); } -} \ No newline at end of file +} diff --git a/web/src/mobileViewport.js b/web/src/mobileViewport.js new file mode 100644 index 00000000..32ec051c --- /dev/null +++ b/web/src/mobileViewport.js @@ -0,0 +1,60 @@ +// CSS viewport units account for browser toolbars, not reliably for the soft +// keyboard. Use the visual viewport only for a substantial keyboard-sized +// reduction; do not fight pinch zoom, small toolbar changes or user scrolling. +export function mobileViewportState({ width, height, layoutHeight, baselineHeight, scale = 1, offsetTop = 0, editing = false, wasOpen = false }) { + const visible = Number(height); + const baseline = Math.max(Number(layoutHeight) || 0, Number(baselineHeight) || 0); + const keyboard = (editing || wasOpen) && width < 600 && Math.abs(scale - 1) < 0.01 && + visible > 0 && baseline - visible >= Math.max(100, baseline * 0.2); + return { keyboard, height: keyboard ? Math.round(visible) : null, top: keyboard ? Math.max(0, Math.round(offsetTop)) : 0 }; +} + +/** Update geometry only: never rebuild DOM, blur an input, or scroll the page. */ +export function installMobileViewport(win = window, doc = document) { + const viewport = win.visualViewport; + if (!viewport) return () => {}; + const root = doc.documentElement; + let baseline = win.innerHeight; + let width = win.innerWidth; + let open = false; + let frame = null; + function update() { + frame = null; + const active = doc.activeElement; + const editing = Boolean(active?.matches?.("textarea, input:not([type=checkbox]):not([type=radio]):not([type=button]):not([type=submit]), [contenteditable=true]")); + if (width !== win.innerWidth) { + width = win.innerWidth; + baseline = win.innerHeight; + } else if (!editing && !open) { + baseline = Math.max(win.innerHeight, viewport.height); + } + const state = mobileViewportState({ width, height: viewport.height, layoutHeight: win.innerHeight, baselineHeight: baseline, scale: viewport.scale, offsetTop: viewport.offsetTop, editing, wasOpen: open }); + open = state.keyboard; + root.classList.toggle("keyboard-open", open); + if (open) { + root.style.setProperty("--keyboard-visible-height", `${state.height}px`); + root.style.setProperty("--keyboard-viewport-top", `${state.top}px`); + } else { + root.style.removeProperty("--keyboard-visible-height"); + root.style.removeProperty("--keyboard-viewport-top"); + } + } + function schedule() { + if (frame == null) frame = win.requestAnimationFrame(update); + } + for (const event of ["resize", "scroll"]) viewport.addEventListener(event, schedule); + win.addEventListener("resize", schedule); + doc.addEventListener("focusin", schedule); + doc.addEventListener("focusout", schedule); + update(); + return () => { + for (const event of ["resize", "scroll"]) viewport.removeEventListener(event, schedule); + win.removeEventListener("resize", schedule); + doc.removeEventListener("focusin", schedule); + doc.removeEventListener("focusout", schedule); + if (frame != null) win.cancelAnimationFrame(frame); + root.classList.remove("keyboard-open"); + root.style.removeProperty("--keyboard-visible-height"); + root.style.removeProperty("--keyboard-viewport-top"); + }; +} diff --git a/web/test/chat-hardening-probe.mjs b/web/test/chat-hardening-probe.mjs index 6a34108d..ab1e30bc 100644 --- a/web/test/chat-hardening-probe.mjs +++ b/web/test/chat-hardening-probe.mjs @@ -33,6 +33,9 @@ try { window.addEventListener('unhandledrejection', (event) => window.__errors.push(String(event.reason))); const Native = window.WebSocket; window.WebSocket = class extends Native { constructor(...args) { super(...args); window.__socket = this; } }; + // Controlled geometry, not a claim that desktop Chrome has an iOS keyboard. + const viewport = Object.assign(new EventTarget(), {height:844,width:390,scale:1,offsetTop:0}); + Object.defineProperty(window, 'visualViewport', {value:viewport,configurable:true}); })()`); await page.goto(`http://127.0.0.1:${port}/chat/${sessionId}`); await page.waitForSelector("#chatTranscript .chat-reasoning-toggle"); @@ -61,8 +64,36 @@ try { await page.evaluate("document.querySelector('.chat-reasoning-viewer .chat-tool-close').click()"); await emit({ message_id:"msg_answer",kind:"step",step:{status:"ended"} }); await eventually(() => page.evaluate("!document.querySelector('#chatStop') && !document.querySelector('.chat-activity-spinner')")); + await page.fill("#chatInput", "Keep this draft and caret"); + await page.evaluate(`(() => { + const input = document.querySelector('#chatInput'); + input.setSelectionRange(5, 9); + window.__composer = input; + window.visualViewport.height = 430; + window.visualViewport.dispatchEvent(new Event('resize')); + })()`); + await eventually(() => page.evaluate("document.documentElement.classList.contains('keyboard-open')")); + assert.equal(await page.evaluate("document.querySelector('#chatInput') === window.__composer && document.activeElement === window.__composer"), true); + assert.equal(await page.evaluate("document.querySelector('#chatInput').selectionStart"), 5); + assert.equal(await page.evaluate("getComputedStyle(document.querySelector('#chatInput')).fontSize"), "16px"); + assert.equal(await page.evaluate("document.querySelector('#chatComposer').getBoundingClientRect().bottom <= 431"), true); + if (process.env.PIPELINES_CAPTURE_DIR) { + await page.screenshot(join(process.env.PIPELINES_CAPTURE_DIR, "keyboard-composer-visible.png")); + } + await page.evaluate("window.visualViewport.height = 844; window.visualViewport.dispatchEvent(new Event('resize'))"); + await eventually(() => page.evaluate("!document.documentElement.classList.contains('keyboard-open')")); + await page.click(".chat-back"); + await page.waitForSelector("#chatRows"); + await page.evaluate("document.querySelector('#chatRows').scrollTop = 280"); + const scroll = await page.evaluate("document.querySelector('#chatRows').scrollTop"); + assert.ok(scroll > 0); + await page.evaluate("document.querySelector('#chatRows .chat-session-button').click()"); + await page.waitForSelector(".chat-back"); + await page.click(".chat-back"); + await page.waitForSelector("#chatRows"); + assert.equal(await page.evaluate("document.querySelector('#chatRows').scrollTop"), scroll); assert.deepEqual(await page.evaluate("window.__errors"), []); - console.log("Delayed model dialog loads, filters and traps focus; intermediate step retains Stop until terminal turn."); + console.log("Model dialog focus, intermediate/terminal steps, keyboard composer identity/caret and Back scroll restoration passed."); } finally { await browser?.close(); serve.kill("SIGTERM"); diff --git a/web/test/mobileViewport.test.mjs b/web/test/mobileViewport.test.mjs new file mode 100644 index 00000000..0d3d9120 --- /dev/null +++ b/web/test/mobileViewport.test.mjs @@ -0,0 +1,18 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mobileViewportState } from "../src/mobileViewport.js"; + +const base = { width: 390, height: 844, baselineHeight: 844, layoutHeight: 844, editing: true }; +test("keyboard geometry follows the visual viewport without assuming dvh resizes", () => { + assert.deepEqual(mobileViewportState({ ...base, height: 430, offsetTop: 20 }), { keyboard: true, height: 430, top: 20 }); + // Chromium's resizes-content can shrink both; the pre-focus baseline survives. + assert.equal(mobileViewportState({ ...base, height: 430, layoutHeight: 430 }).keyboard, true); + assert.equal(mobileViewportState({ ...base, height: 430, editing: false, wasOpen: true }).keyboard, true); + assert.equal(mobileViewportState(base).keyboard, false); +}); +test("toolbar motion, desktop layouts and pinch zoom never trigger keyboard layout", () => { + assert.equal(mobileViewportState({ ...base, height: 780 }).keyboard, false); + assert.equal(mobileViewportState({ ...base, height: 430, scale: 2 }).keyboard, false); + assert.equal(mobileViewportState({ ...base, height: 430, width: 900 }).keyboard, false); + assert.equal(mobileViewportState({ ...base, height: 430, editing: false }).keyboard, false); +});