mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-05 19:49:29 +08:00
A theme is a stylesheet under :root[data-desktop] and React parts in place of the top bar, the Dock, the launcher and a window's title bar (web/src/themes); it loads when first shown and switches without a reload. Without a choice the device picks it: iOS, Android, else KDE. Also: the desktop's headless core (window manager, commands, apps, pins) moves to web/src/core with its tests; terminal tabs are drawn by the theme's title bar; Settings and System get a sidebar drawer in narrow windows; the first paint is a minimal splash; the console's auto-hide is removed.
233 lines
12 KiB
TypeScript
233 lines
12 KiB
TypeScript
// SPDX-License-Identifier: MIT
|
|
// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
// A window's sidebar and content (the system apps' frame, AppShell.tsx), as
|
|
// libadwaita's AdwOverlaySplitView: from `minWide` px (measured on the
|
|
// view, so on its window, not on the viewport) the sidebar is a column on
|
|
// the left; under it the view is collapsed: a header (a menu button and the
|
|
// current page's title) over the content, and the sidebar an off-canvas
|
|
// drawer that slides in over the content, the header and the footer, inside
|
|
// the window (several windows, small or snapped, each have their own).
|
|
// - The drawer opens with the menu button, or a swipe from the content's
|
|
// left edge (touch); it closes on a choice (the caller's close(), or a
|
|
// change of `current`), Esc, a press on the scrim, or a swipe to the left
|
|
// on it or on the scrim (touch; it follows the finger).
|
|
// - While it is open it is a modal dialog (aria-modal; keys.ts leaves its
|
|
// Esc to it): the header, the content and the footer are inert, Tab and
|
|
// Shift-Tab cycle inside it, and the focus goes to its selected item
|
|
// ([aria-selected="true"], else its first control); on close it goes back
|
|
// to the menu button (when it was in the drawer). The menu button has
|
|
// aria-expanded and aria-controls (the drawer). A press in the drawer
|
|
// off its controls, or on the scrim, leaves the focus where it is. A
|
|
// closed drawer is inert and hidden, its elements still in the document.
|
|
// - The menu button carries a dot while a page other than the current one
|
|
// wants attention (`attention`: a change not applied, an error count).
|
|
// - No motion with prefers-reduced-motion; coarse pointers get touch-sized
|
|
// targets.
|
|
//
|
|
// What a theme styles (style.css draws the page's own look; a theme's
|
|
// stylesheet restyles these under its :root[data-desktop] scope; the
|
|
// sidebar's own list keeps its classes, e.g. AppShell's .tabs):
|
|
// .split-view the root; [data-collapsed] under minWide,
|
|
// [data-open] while the drawer is open,
|
|
// [data-dragging] while a swipe moves it;
|
|
// --split-shown: how much of the drawer shows
|
|
// while dragged (0..1), for the scrim
|
|
// .split-main the row (wide) or column (collapsed) of the parts below
|
|
// .split-sidebar the sidebar, or the drawer when collapsed
|
|
// (role dialog); [data-app] names the app
|
|
// .split-sidebar-head the drawer's title: the app's name (collapsed only)
|
|
// .split-header the header over the content (collapsed only)
|
|
// .split-menu its menu button ([aria-expanded]; [data-attention]
|
|
// = warn or err while something wants attention)
|
|
// .split-menu-icon the button's icon (three lines)
|
|
// .split-menu-label the app's name in the button, hidden by default
|
|
// (a theme with a back button shows "< Settings")
|
|
// .split-menu-dot the attention dot
|
|
// .split-title the current page's name
|
|
// .split-content the content's column (relative: the edge sits on it)
|
|
// .split-edge the strip at its left edge a swipe opens from
|
|
// (coarse pointers only)
|
|
// .split-footer the footer, under both
|
|
// .split-scrim the dim over the rest while the drawer is open
|
|
// Themes set --split-w (the drawer's width), --split-dur and --split-ease
|
|
// (its motion), --split-scrim (the dim) on .split-view.
|
|
import { useCallback, useLayoutEffect, useRef, useState, type CSSProperties, type KeyboardEvent, type PointerEvent, type ReactNode } from "react";
|
|
import { useSize } from "../hooks.ts";
|
|
|
|
/** What the sidebar and the content are given. */
|
|
export interface SplitState {
|
|
/** Under minWide: the sidebar is a drawer. */
|
|
collapsed: boolean;
|
|
/** The drawer is open. */
|
|
open: boolean;
|
|
/** Close the drawer (a choice was made in it); nothing when it is not open. */
|
|
close(): void;
|
|
}
|
|
|
|
/** Tab stops in the drawer, for its focus trap. */
|
|
const FOCUSABLE = "button, [href], input, select, textarea, [tabindex]";
|
|
/** How far (px) a touch moves before it is a swipe (sideways) or a scroll (the browser's). */
|
|
const SLOP = 8;
|
|
/** A swipe faster than this (px/ms) opens or closes it, however far it went. */
|
|
const FLING = 0.4;
|
|
|
|
const MenuIcon = () => (
|
|
<svg width={16} height={16} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" aria-hidden="true">
|
|
<path d="M4 6.5h16M4 12h16M4 17.5h16" />
|
|
</svg>
|
|
);
|
|
|
|
interface Drag { id: number; x: number; y: number; w: number; mode: "open" | "close"; on: boolean; lx: number; lt: number; v: number }
|
|
|
|
export function SplitView({ id, name, label, title, current, minWide, attention, sidebar, footer, children }: {
|
|
/** The base of the parts' element ids (`${id}-nav`, `${id}-menu`). */
|
|
id: string;
|
|
/** The app's name (the drawer's title). */
|
|
name: string;
|
|
/** The sidebar's accessible name (the drawer's and the menu button's). */
|
|
label: string;
|
|
/** The current page's name, in the header. */
|
|
title: ReactNode;
|
|
/** The current page: a change closes the drawer. */
|
|
current: string;
|
|
/** From this width (px) the sidebar is a column. */
|
|
minWide: number;
|
|
/** Another page wants attention: a dot on the menu button. */
|
|
attention?: "warn" | "err";
|
|
sidebar: (s: SplitState) => ReactNode;
|
|
footer?: ReactNode;
|
|
children: (s: SplitState) => ReactNode;
|
|
}) {
|
|
const [root, setRoot] = useState<HTMLDivElement | null>(null);
|
|
const side = useRef<HTMLDivElement>(null);
|
|
const menu = useRef<HTMLButtonElement>(null);
|
|
// Measured on the view; a hidden one (width 0: minimized, parked) keeps its layout. Narrow until measured.
|
|
const { w } = useSize(root);
|
|
const [collapsed, setCollapsed] = useState(true);
|
|
if (w && (w < minWide) !== collapsed) setCollapsed(w < minWide);
|
|
const [open, setOpen] = useState(false);
|
|
if (open && !collapsed) setOpen(false);
|
|
const shown = open && collapsed;
|
|
const openRef = useRef(shown);
|
|
openRef.current = shown;
|
|
/** The focus was in the drawer when it closed: the menu button gets it. */
|
|
const refocus = useRef(false);
|
|
/** The drawer's offset from its open place (px, ≤ 0) while a swipe moves it. */
|
|
const [dx, setDx] = useState<number | null>(null);
|
|
const drag = useRef<Drag | null>(null);
|
|
const swallowClick = useRef(false);
|
|
|
|
const close = useCallback(() => {
|
|
if (!openRef.current) return;
|
|
refocus.current = !!side.current?.contains(document.activeElement);
|
|
setOpen(false);
|
|
}, []);
|
|
|
|
// A new page closes the drawer.
|
|
const last = useRef(current);
|
|
useLayoutEffect(() => {
|
|
if (last.current === current) return;
|
|
last.current = current;
|
|
close();
|
|
}, [current, close]);
|
|
|
|
// Opened: the focus into the drawer (its selected item); closed: back to the menu button.
|
|
useLayoutEffect(() => {
|
|
if (shown) {
|
|
const s = side.current;
|
|
const t = s?.querySelector<HTMLElement>('[aria-selected="true"]') ?? s?.querySelector<HTMLElement>(FOCUSABLE);
|
|
t?.focus({ preventScroll: true });
|
|
t?.scrollIntoView?.({ block: "nearest" });
|
|
} else if (refocus.current) {
|
|
refocus.current = false;
|
|
menu.current?.focus({ preventScroll: true });
|
|
}
|
|
}, [shown]);
|
|
|
|
const onSideKey = (e: KeyboardEvent<HTMLDivElement>) => {
|
|
if (!openRef.current) return;
|
|
if (e.key === "Escape" && !e.ctrlKey && !e.altKey && !e.metaKey && !e.shiftKey) {
|
|
e.preventDefault();
|
|
close();
|
|
return;
|
|
}
|
|
if (e.key !== "Tab") return;
|
|
const all = [...side.current!.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((el) => el.tabIndex >= 0 && !(el as HTMLButtonElement).disabled && el.getClientRects().length);
|
|
const i = all.indexOf(document.activeElement as HTMLElement);
|
|
const to = all.length < 2 ? all[0] : e.shiftKey ? (i <= 0 ? all.at(-1) : null) : (i === all.length - 1 || i < 0 ? all[0] : null);
|
|
if (to !== null) { e.preventDefault(); to?.focus(); }
|
|
};
|
|
|
|
// ---- swipes (touch): to the left on the open drawer or the scrim closes it, to the right from the edge opens it ----
|
|
const down = (mode: Drag["mode"]) => (e: PointerEvent<HTMLElement>) => {
|
|
swallowClick.current = false;
|
|
if (e.pointerType !== "touch" || !side.current) return;
|
|
drag.current = { id: e.pointerId, x: e.clientX, y: e.clientY, w: side.current.offsetWidth || 1, mode, on: false, lx: e.clientX, lt: e.timeStamp, v: 0 };
|
|
};
|
|
const offset = (d: Drag, x: number) => (d.mode === "close" ? Math.min(0, x - d.x) : Math.min(0, -d.w + Math.max(0, x - d.x)));
|
|
const move = (e: PointerEvent<HTMLElement>) => {
|
|
const d = drag.current;
|
|
if (!d || e.pointerId !== d.id) return;
|
|
if (!d.on) {
|
|
const mx = Math.abs(e.clientX - d.x), my = Math.abs(e.clientY - d.y);
|
|
if (mx < SLOP && my < SLOP) return;
|
|
if (my >= mx) { drag.current = null; return; } // a scroll
|
|
d.on = true;
|
|
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
}
|
|
const dt = e.timeStamp - d.lt;
|
|
if (dt > 0) d.v = 0.6 * ((e.clientX - d.lx) / dt) + 0.4 * d.v;
|
|
d.lx = e.clientX; d.lt = e.timeStamp;
|
|
setDx(offset(d, e.clientX));
|
|
};
|
|
const up = (e: PointerEvent<HTMLElement>) => {
|
|
const d = drag.current;
|
|
if (!d || e.pointerId !== d.id) return;
|
|
drag.current = null;
|
|
if (!d.on) return;
|
|
swallowClick.current = true;
|
|
const off = offset(d, e.clientX), cancel = e.type === "pointercancel";
|
|
setDx(null);
|
|
if (d.mode === "close") { if (!cancel && (off < -d.w / 3 || d.v < -FLING)) close(); }
|
|
else if (!cancel && (d.w + off > d.w / 3 || d.v > FLING)) setOpen(true);
|
|
};
|
|
const swipe = (mode: Drag["mode"]) => ({ onPointerDown: down(mode), onPointerMove: move, onPointerUp: up, onPointerCancel: up });
|
|
|
|
const s: SplitState = { collapsed, open: shown, close };
|
|
const dragging = dx !== null;
|
|
const navId = `${id}-nav`;
|
|
const style = dragging ? ({ "--split-shown": String(Math.max(0, Math.min(1, 1 + dx / (side.current?.offsetWidth || 1)))) } as CSSProperties) : undefined;
|
|
return (
|
|
<div ref={setRoot} className="split-view" data-collapsed={collapsed || undefined} data-open={shown || undefined} data-dragging={dragging || undefined} style={style}>
|
|
<div className="split-main">
|
|
<div ref={side} id={navId} className="split-sidebar" role={collapsed ? "dialog" : undefined} aria-modal={shown || undefined}
|
|
aria-label={collapsed ? label : undefined} tabIndex={collapsed ? -1 : undefined} inert={collapsed && !shown && !dragging} onKeyDown={onSideKey}
|
|
style={dragging ? { transform: `translateX(${dx}px)` } : undefined} {...(collapsed && shown ? swipe("close") : {})}>
|
|
{collapsed && <div className="split-sidebar-head">{name}</div>}
|
|
{sidebar(s)}
|
|
</div>
|
|
{collapsed && (
|
|
<div className="split-header" inert={shown}>
|
|
<button ref={menu} id={`${id}-menu`} type="button" className="split-menu" aria-expanded={shown} aria-controls={navId} aria-label={label} title={label}
|
|
data-attention={attention} onClick={() => setOpen(true)}>
|
|
<span className="split-menu-icon"><MenuIcon /></span>
|
|
<span className="split-menu-label">{name}</span>
|
|
{attention && <span className="split-menu-dot" aria-hidden="true" />}
|
|
</button>
|
|
<div className="split-title">{title}</div>
|
|
</div>
|
|
)}
|
|
<div className="split-content" inert={shown}>
|
|
{children(s)}
|
|
{collapsed && !shown && <div className="split-edge" aria-hidden="true" {...swipe("open")} />}
|
|
</div>
|
|
</div>
|
|
{footer && <div className="split-footer" inert={shown}>{footer}</div>}
|
|
{collapsed && (
|
|
<div className="split-scrim" aria-hidden="true" {...(shown ? swipe("close") : {})} onMouseDown={(e) => e.preventDefault()}
|
|
onClick={() => { if (swallowClick.current) { swallowClick.current = false; return; } close(); }} />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|