Files
loki/widget/sheet.ts
T
CatoandClaude Opus 5.5 da75cbd356 fix(phone): one row of widget header, room to type, the tab not the pill, one menu (#1297)
* fix(phone): one row of widget header, room to type, the tab not the pill, one menu

The owner on a phone, on Loki's own Control (2026-10-18): "this looks
disgusting… why is it so crowded?"

Widget panel (owner):
- The status line IS the header: the brand row and the page name above it
  are gone for the owner (a visitor keeps them). Close sits at its end.
- The line carries one number of one named thing: "11 changes need you",
  "2 being built", "noticed 2 things" — not "4 to look at · 11 needs you".
  It wraps to a second line on a narrow phone instead of cutting the noun.
- Typing is one state however the keyboard is drawn. Loki's pages shrink
  for the keyboard (interactive-widget=resizes-content), so the inset check
  never fired, the sheet stayed at 68% of a halved viewport and the thread
  was a one-pixel sliver. A focused box now fills what is visible and steps
  the extras aside; the sheet resets on close.

Launcher: the phone bottom bar asks for the edge tab (data-fc-place="tab"),
so the "Watching" pill no longer floats over Control's cards. An open dialog
still wins over a later region's tab (resolvePlaceDirective).

One menu on a phone: the Profile/Chat/Control/Terminal strip no longer
renders below md (the bottom bar is the menu there), nor on /control, where
it named one remembered project above a page about all of them.

Control: "Default agent and model" moved into the Settings sheet the foot
already opens, and the foot is the page's last line (shown with no projects
too). The sort and Select row of the project list is desk-only.

Tests: new widget-owner-phone-browser (fails on the old header and on the
old sheet); nav-surfaces and phone-chrome guards updated to the new rules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* fix(widget): keep buildShadowCSS within the function-length limit

The owner-header rules took buildShadowCSS to 516 lines (limit 510); the
same rules, compacted onto two lines in the file's own one-line style.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* test(widget): wait for each state, not a clock, in the owner phone test

CI runs it beside every other unit test on two cores, where the owner's
changes arrive late; fixed half-second sleeps failed there (its own output
showed only a closing brace). It now waits for the status line, the typing
state and the edge tab, and prints a one-line reason last if it fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* fix(widget): the keyboard, not the focus, decides the typing layout

CI's fonts left the thread 44px at 320px with the keyboard up: the composer's
"This page / Point to area" row cost the room. It steps aside while the
keyboard is up, with the starters. And the typing layout now follows the
keyboard (the visible area well below its tallest), not focus alone:
Android's back key drops the keyboard but keeps focus in the box, and the
extras then come back. The phone test checks both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

* test(widget): the thread must show what it holds, not be 60px tall

CI's thread held 44px of text and showed all 44px — content-sized, not
squeezed. The check is now what the bug was: a thread showing less than
min(60px, what it holds). The original sliver (1px of 64px) still fails it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-18 08:48:33 +02:00

168 lines
6.3 KiB
TypeScript

/**
* The panel as a phone sheet you can size with your thumb: drag its top edge
* up for the whole screen, down for a peek, further down to put it away.
* Desktop keeps the corner card — there the page is beside it, not under it.
*
* Pure decision (sheetSnap) is tested in scripts/test/widget-sheet.ts.
*/
import { h } from "./dom";
export type SheetSize = "peek" | "auto" | "full";
/** Where a drag of `dy` px (down is positive) from `from` should settle. */
export function sheetSnap(from: SheetSize, dy: number): SheetSize | "close" {
const order: SheetSize[] = ["peek", "auto", "full"];
if (dy > 160 && from !== "full") return "close";
if (dy > 160) return "peek";
if (Math.abs(dy) < 40) return from;
const i = order.indexOf(from) + (dy < 0 ? 1 : -1);
if (i < 0) return "close";
return order[Math.min(i, order.length - 1)];
}
/**
* How much of the bottom of the screen the on-screen keyboard covers, in px.
*
* Android Chrome (by default, and on every host page that does not opt into
* `interactive-widget=resizes-content`) lays the keyboard OVER the page: the
* layout viewport keeps its height, so a sheet pinned to `bottom: 0` with a
* `vh` height has its lower part — the composer's Send buttons — under the
* keys. The visual viewport is what is actually visible. Under 80px is a
* browser toolbar settling, not a keyboard.
*/
export function keyboardInset(innerHeight: number, vvHeight: number, vvOffsetTop: number): number {
const covered = Math.round(innerHeight - vvHeight - vvOffsetTop);
return covered >= 80 ? covered : 0;
}
const HEIGHT: Record<SheetSize, string> = {
peek: "42vh",
auto: "",
full: "calc(100vh - 16px)",
};
export function attachSheet(opts: {
panel: HTMLElement;
/** The header — dragging it moves the sheet; its buttons still work. */
handle: HTMLElement;
onClose: () => void;
}): { reset: () => void } {
const { panel, handle } = opts;
const grip = h("div", "grip");
grip.setAttribute("aria-hidden", "true");
panel.prepend(grip);
let size: SheetSize = "auto";
const phone = () => window.matchMedia("(max-width: 480px)").matches;
/** Keyboard up: the sheet stands on it and fits what is still visible. */
let kb = 0;
/**
* Typing in the panel. A page with `interactive-widget=resizes-content`
* (Loki's own) SHRINKS for the keyboard instead of laying it over, so
* keyboardInset reads 0 and the sheet stayed at 68% of a viewport already
* halved: the thread became a one-pixel sliver under three rows of extras
* (owner's phone, 2026-10-18). A focused text box on a phone is the same
* moment whichever way the browser draws the keyboard.
*/
let typing = false;
/** The tallest the visible area has been: a page that shrinks for the
* keyboard shows it only as being much shorter than that. */
let tallest = 0;
const visible = () => window.visualViewport?.height ?? window.innerHeight;
/** Typing AND the keyboard is up. Android's back key drops the keyboard
* but keeps focus in the box, and then the extras come back. */
const squeezed = () => {
tallest = Math.max(tallest, visible());
return typing && visible() < tallest * 0.8;
};
const apply = () => {
if (!kb && squeezed() && phone()) {
panel.style.bottom = "";
panel.style.height = "";
panel.style.maxHeight = `${Math.max(160, (window.visualViewport?.height ?? window.innerHeight) - 8)}px`;
} else if (kb && phone()) {
const room = `${Math.max(160, (window.visualViewport?.height ?? window.innerHeight) - 8)}px`;
panel.style.bottom = `${kb}px`;
panel.style.height = size === "full" ? room : "";
panel.style.maxHeight = room;
} else {
panel.style.bottom = "";
panel.style.height = HEIGHT[size];
panel.style.maxHeight = size === "full" ? "none" : "";
}
// While typing, the extras step aside for the conversation (theme.ts .kb).
panel.classList.toggle("kb", (kb > 0 || squeezed()) && phone());
};
const isTextBox = (el: EventTarget | null) =>
el instanceof HTMLTextAreaElement ||
(el instanceof HTMLInputElement && !["button", "checkbox", "radio", "file"].includes(el.type));
panel.addEventListener("focusin", (e) => {
if (!isTextBox(e.target) || typing) return;
typing = true;
apply();
});
panel.addEventListener("focusout", (e) => {
// Moving from the box to a button in the panel (Send, Talk) keeps the
// layout still: a sheet that resized between a press and its click would
// move Send out from under the thumb. Only leaving the panel ends it.
const to = e.relatedTarget;
if (!isTextBox(e.target) || (to instanceof Node && panel.contains(to))) return;
typing = false;
apply();
});
const vv = window.visualViewport;
const onViewport = () => {
if (!vv) return;
if (!typing) tallest = Math.max(tallest, vv.height);
const next = keyboardInset(window.innerHeight, vv.height, vv.offsetTop);
// While typing on a page that shrinks for the keyboard, the room to fill
// changes with every resize even though no inset ever appears.
if (next === kb && !typing) return;
kb = next;
apply();
};
vv?.addEventListener("resize", onViewport);
vv?.addEventListener("scroll", onViewport);
let startY: number | null = null;
let dy = 0;
const down = (e: PointerEvent) => {
if (!phone() || (e.target as Element).closest("button,a,input,textarea")) return;
startY = e.clientY;
dy = 0;
panel.style.transition = "none";
};
const move = (e: PointerEvent) => {
if (startY === null) return;
dy = e.clientY - startY;
// Follow the thumb downward; upward is a resize, shown on release.
panel.style.transform = dy > 0 ? `translateY(${dy}px)` : "";
};
const up = () => {
if (startY === null) return;
startY = null;
panel.style.transition = "";
panel.style.transform = "";
const next = sheetSnap(size, dy);
if (next === "close") {
size = "auto";
apply();
opts.onClose();
return;
}
size = next;
apply();
};
for (const el of [grip, handle]) el.addEventListener("pointerdown", down);
window.addEventListener("pointermove", move);
window.addEventListener("pointerup", up);
window.addEventListener("pointercancel", up);
return {
reset() {
size = "auto";
typing = false;
apply();
},
};
}