Site-agnostic parts of the X app, behind a small site adapter: - crates/waybetter-host: webview-label security policy and tab rect clamp, account profiles with native-owned accounts (AccountRules), plugin store with bundled-plugin upgrades (PluginHost), signed data-only hotfix channel, update scheduling and crash handshake, tab payloads/link routing/tab-chrome validation, and (feature "tauri") webview helpers and the Windows system menu. - packages/runtime: DOM hub, request hub, plugin API core, OKLab theme color model, split-view rail hiding, link routing and account reporting, each configured by a SiteAdapter. - packages/shell: address bar, pane, link-drop, profile, palette and update-chip models, and window controls with the Tauri API injected. Tests moved/adapted from the app: 45 Rust (headless), 40 TypeScript.
313 lines
13 KiB
TypeScript
313 lines
13 KiB
TypeScript
// Split view: site panes of one account side by side each draw the site's whole navigation rail.
|
|
// Native code tells every site pane whether another pane already shows it (`redundantNav`, the
|
|
// leftmost pane of that account wins). With the feature on, such a pane lifts the rail out of the
|
|
// layout (the content re-centers in the freed width), parks it off-screen, and slides it back in
|
|
// over the page while the pointer rests on the pane's left edge. Which element is the rail is the
|
|
// site adapter's `RailSite`.
|
|
|
|
import { watchDom, type DomWatchHandle } from './domHub.ts';
|
|
|
|
export type PaneNavHandle = { set(state: unknown): void; setEnabled(enabled: boolean): void; dispose(): void };
|
|
export type RailPaint = { width: number; background: string; line: string; accent: string; rtl: boolean };
|
|
|
|
export type RailSite = {
|
|
/** Prefix of the ids/attributes this module owns (`<prefix>-pane-nav`, `data-<prefix>-pane-nav`, ...). */
|
|
prefix: string;
|
|
/** The site's classic rail element. */
|
|
rail: string;
|
|
/** A link inside the rail, used to find it when the classic rail element is absent. */
|
|
anchor: string;
|
|
/** The content column that re-centers once the rail is parked. */
|
|
main: string;
|
|
/** An element whose background is the site accent (for the edge handle), if any. */
|
|
accentSource?: string;
|
|
/** Open while a modal sheet is up: the rail never slides in over it. */
|
|
modalOpen: string;
|
|
/** One of the site's menus (portalled out of the rail) is open. */
|
|
menuOpen: string;
|
|
/** Paint before the page has been read. */
|
|
paint: RailPaint;
|
|
};
|
|
|
|
/** Resting on the edge this long slides the rail in; crossing it on the way to the divider does not. */
|
|
const PEEK_DELAY = 140;
|
|
/** The rail stays this long after the pointer leaves it (and while one of its menus is open). */
|
|
const LEAVE_DELAY = 280;
|
|
|
|
const names = (site: RailSite) => ({
|
|
style: `${site.prefix}-pane-nav`,
|
|
edge: `${site.prefix}-rail-edge`,
|
|
/** On <html>: "hidden" (rail parked off-screen) or "peek" (rail slid in over the page). */
|
|
state: `data-${site.prefix}-pane-nav`,
|
|
/** Marks the rail when the site draws it as something other than its classic rail element. */
|
|
rail: `data-${site.prefix}-rail`,
|
|
});
|
|
|
|
/** Pure: the stylesheet for a pane whose rail is parked. RTL pages keep the rail on the right. */
|
|
export function buildPaneNavCss(site: RailSite, paint: RailPaint = site.paint): string {
|
|
const { edge, state, rail } = names(site);
|
|
const RAIL = `:is(${site.rail}, [${rail}])`;
|
|
const ease = 'cubic-bezier(.2, .8, .2, 1)';
|
|
const [side, other, sign] = paint.rtl ? ['right', 'left', 1] : ['left', 'right', -1];
|
|
return `
|
|
html[${state}] ${RAIL} {
|
|
position: fixed !important; top: 0 !important; bottom: 0 !important; ${side}: 0 !important; ${other}: auto !important;
|
|
width: ${paint.width}px !important; min-width: 0 !important; max-width: none !important; height: 100vh !important;
|
|
flex: none !important; margin: 0 !important; overflow: visible !important; z-index: 40 !important;
|
|
background-color: ${paint.background} !important;
|
|
transform: translateX(${sign * 100}%); visibility: hidden;
|
|
transition: transform .2s ${ease}, box-shadow .2s ease, visibility 0s linear .2s;
|
|
}
|
|
html[${state}="peek"] ${RAIL} {
|
|
transform: translateX(0); visibility: visible;
|
|
box-shadow: ${-sign}px 0 0 ${paint.line}, ${-sign * 16}px 0 40px rgba(0, 0, 0, .3);
|
|
transition: transform .2s ${ease}, box-shadow .2s ease;
|
|
}
|
|
html[${state}] ${RAIL} ~ ${site.main} { align-items: center !important; }
|
|
#${edge} { position: fixed; ${side}: 0; top: 0; bottom: 0; width: 10px; z-index: 41; }
|
|
#${edge}::after {
|
|
content: ""; position: absolute; ${side}: 0; top: 50%; width: 3px; height: 56px; margin-top: -28px;
|
|
border-radius: ${paint.rtl ? '3px 0 0 3px' : '0 3px 3px 0'}; background: ${paint.accent}; opacity: 0; transform: scaleY(.5);
|
|
transition: opacity .15s ease, transform .15s ease;
|
|
}
|
|
#${edge}:hover::after { opacity: .9; transform: scaleY(1); }
|
|
html[${state}="peek"] #${edge} { pointer-events: none; }
|
|
@media (prefers-reduced-motion: reduce) {
|
|
html[${state}] ${RAIL}, #${edge}::after { transition: none !important; }
|
|
}`.trim();
|
|
}
|
|
|
|
const TRANSPARENT = /^(transparent|rgba\([^)]*,\s*0\))$/;
|
|
const isTransparent = (color: string | null | undefined) => !color || TRANSPARENT.test(color.replace(/\s+/g, ' ').trim());
|
|
|
|
/** `rgb(r, g, b)` / `rgba(...)` at a new alpha; anything else falls back to a neutral line. */
|
|
export function withAlpha(color: string, alpha: number): string {
|
|
const parts = color.match(/\d+(?:\.\d+)?/g);
|
|
return parts && parts.length >= 3 ? `rgba(${parts[0]}, ${parts[1]}, ${parts[2]}, ${alpha})` : `rgba(128, 128, 128, ${alpha})`;
|
|
}
|
|
|
|
export function installPaneNav(win: Window, site: RailSite): PaneNavHandle {
|
|
const doc = win.document;
|
|
const ids = names(site);
|
|
// Installed at document start, before the parser has created <html>: never cache it.
|
|
const root = (): HTMLElement | null => doc.documentElement;
|
|
let redundant = false;
|
|
let enabled = true;
|
|
let active = false;
|
|
let disposed = false;
|
|
let rail: HTMLElement | null = null;
|
|
let edge: HTMLElement | null = null;
|
|
let watch: DomWatchHandle | null = null;
|
|
let paint: RailPaint = { ...site.paint };
|
|
let peekTimer = 0;
|
|
let leaveTimer = 0;
|
|
let resizeFrame = 0;
|
|
/** A press inside the rail may have opened one of its menus (portalled elsewhere). */
|
|
let railMenu = false;
|
|
const isRtl = () => root()?.getAttribute('dir') === 'rtl';
|
|
|
|
const ensureStyle = () => {
|
|
let style = doc.getElementById(ids.style) as HTMLStyleElement | null;
|
|
if (!style) {
|
|
const parent = doc.head || root();
|
|
if (!parent) return;
|
|
style = doc.createElement('style');
|
|
style.id = ids.style;
|
|
parent.appendChild(style);
|
|
}
|
|
const css = buildPaneNavCss(site, paint);
|
|
if (style.textContent !== css) style.textContent = css;
|
|
};
|
|
|
|
/** The site's rail: its classic element, else the sibling of <main> holding the anchor link. */
|
|
const findRail = (): HTMLElement | null => {
|
|
const classic = doc.querySelector<HTMLElement>(site.rail);
|
|
if (classic) return classic;
|
|
if (rail?.isConnected) return rail;
|
|
const main = doc.querySelector('main');
|
|
const anchor = doc.querySelector(site.anchor);
|
|
if (!main || !anchor) return null;
|
|
for (let el = anchor.parentElement; el && el !== doc.body; el = el.parentElement) {
|
|
const parent = el.parentElement;
|
|
if (!parent?.contains(main) || el.contains(main)) continue;
|
|
// Only a tall column at the start edge: never a bottom tab bar or a page-wide wrapper.
|
|
const box = el.getBoundingClientRect();
|
|
const tall = box.height >= win.innerHeight * 0.5;
|
|
const atStart = isRtl() ? box.right >= win.innerWidth - 8 : box.left <= 8;
|
|
return tall && atStart && box.width <= win.innerWidth * 0.45 ? el : null;
|
|
}
|
|
return null;
|
|
};
|
|
|
|
const railWidth = (el: HTMLElement): number => {
|
|
const column = el.firstElementChild?.getBoundingClientRect().width ?? 0;
|
|
const nav = el.querySelector('nav')?.getBoundingClientRect().width ?? 0;
|
|
const width = Math.round(Math.max(column, nav));
|
|
return width >= 48 && width <= 400 ? width : paint.width;
|
|
};
|
|
|
|
/** Reads the page colors for the slid-in rail (layout reads: only on state changes, never per mutation). */
|
|
const repaint = () => {
|
|
try {
|
|
const body = doc.body ? win.getComputedStyle(doc.body) : null;
|
|
const html = root() ? win.getComputedStyle(root()!) : null;
|
|
let background = body?.backgroundColor ?? '';
|
|
if (isTransparent(background)) background = html?.backgroundColor ?? '';
|
|
if (isTransparent(background)) background = /dark/.test(html?.colorScheme ?? '') ? 'rgb(0, 0, 0)' : 'rgb(255, 255, 255)';
|
|
const button = site.accentSource ? doc.querySelector(site.accentSource) : null;
|
|
const fill = button ? win.getComputedStyle(button).backgroundColor : '';
|
|
paint = {
|
|
width: rail ? railWidth(rail) : paint.width,
|
|
background,
|
|
line: withAlpha(body?.color ?? '', 0.16),
|
|
accent: isTransparent(fill) ? site.paint.accent : fill,
|
|
rtl: isRtl(),
|
|
};
|
|
} catch { /* keep the last paint */ }
|
|
ensureStyle();
|
|
};
|
|
|
|
const state = () => root()?.getAttribute(ids.state) ?? null;
|
|
const setState = (value: 'hidden' | 'peek') => { if (state() !== value) root()?.setAttribute(ids.state, value); };
|
|
const menuOpen = () => !!doc.querySelector(site.menuOpen);
|
|
|
|
const unpeek = () => {
|
|
win.clearTimeout(peekTimer);
|
|
win.clearTimeout(leaveTimer);
|
|
railMenu = false;
|
|
if (active) setState('hidden');
|
|
};
|
|
const tryLeave = () => {
|
|
if (state() !== 'peek') return;
|
|
// Only the rail's own menus hold it out; sites portal them like every other menu, so "open" is
|
|
// judged only after a press inside the rail and ends when that menu closes.
|
|
const holding = railMenu && menuOpen();
|
|
if (!holding) railMenu = false;
|
|
if (rail?.matches(':hover') || holding) { leaveTimer = win.setTimeout(tryLeave, LEAVE_DELAY); return; }
|
|
unpeek();
|
|
};
|
|
const peek = () => {
|
|
win.clearTimeout(peekTimer);
|
|
// Never over one of the site's modal sheets: the rail would sit above them.
|
|
if (!active || !rail || doc.querySelector(site.modalOpen)) return;
|
|
repaint();
|
|
setState('peek');
|
|
win.clearTimeout(leaveTimer);
|
|
leaveTimer = win.setTimeout(tryLeave, LEAVE_DELAY * 4);
|
|
};
|
|
|
|
const onEdgeEnter = () => { win.clearTimeout(peekTimer); peekTimer = win.setTimeout(peek, PEEK_DELAY); };
|
|
const onEdgeLeave = () => { if (state() !== 'peek') win.clearTimeout(peekTimer); };
|
|
const onRailEnter = () => win.clearTimeout(leaveTimer);
|
|
const onRailPress = () => { railMenu = true; };
|
|
const onRailLeave = () => { win.clearTimeout(leaveTimer); leaveTimer = win.setTimeout(tryLeave, LEAVE_DELAY); };
|
|
// A nav link navigates or opens a sheet: get out of the way.
|
|
const onRailClick = (event: Event) => {
|
|
if ((event.target as Element | null)?.closest?.('a[href]')) win.setTimeout(unpeek, 0);
|
|
};
|
|
const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape' && state() === 'peek') unpeek(); };
|
|
const onResize = () => {
|
|
if (resizeFrame) return;
|
|
resizeFrame = win.requestAnimationFrame(() => {
|
|
resizeFrame = 0;
|
|
if (active && rail) { paint = { ...paint, width: railWidth(rail) }; ensureStyle(); }
|
|
});
|
|
};
|
|
|
|
const bindRail = (next: HTMLElement | null) => {
|
|
if (next === rail) return;
|
|
if (rail) {
|
|
rail.removeAttribute(ids.rail);
|
|
rail.removeEventListener('pointerenter', onRailEnter);
|
|
rail.removeEventListener('pointerleave', onRailLeave);
|
|
rail.removeEventListener('pointerdown', onRailPress, true);
|
|
rail.removeEventListener('click', onRailClick, true);
|
|
}
|
|
rail = next;
|
|
if (!rail) return;
|
|
if (!rail.matches(site.rail)) rail.setAttribute(ids.rail, '');
|
|
rail.addEventListener('pointerenter', onRailEnter);
|
|
rail.addEventListener('pointerleave', onRailLeave);
|
|
rail.addEventListener('pointerdown', onRailPress, true);
|
|
rail.addEventListener('click', onRailClick, true);
|
|
paint = { ...paint, width: railWidth(rail), rtl: isRtl() };
|
|
ensureStyle();
|
|
};
|
|
|
|
const syncEdge = () => {
|
|
if (!doc.body) return;
|
|
if (edge?.isConnected) return;
|
|
edge = doc.createElement('div');
|
|
edge.id = ids.edge;
|
|
edge.setAttribute('aria-hidden', 'true');
|
|
edge.addEventListener('pointerenter', onEdgeEnter);
|
|
edge.addEventListener('pointerleave', onEdgeLeave);
|
|
edge.addEventListener('click', peek);
|
|
doc.body.appendChild(edge);
|
|
};
|
|
|
|
// Sites re-render their layout on route and breakpoint changes: keep the rail marked and the edge in place.
|
|
const sync = () => {
|
|
if (!active || disposed) return;
|
|
// The first run after <html> exists parks the rail (activation may predate the document).
|
|
ensureStyle();
|
|
if (state() !== 'peek') setState('hidden');
|
|
bindRail(findRail());
|
|
syncEdge();
|
|
// The page direction can switch with the display language, without a reload.
|
|
if (paint.rtl !== isRtl()) { paint = { ...paint, rtl: isRtl() }; ensureStyle(); }
|
|
};
|
|
|
|
const activate = () => {
|
|
// The rail's width is measured while it is still in the layout, before it is parked.
|
|
if (doc.body) bindRail(findRail());
|
|
ensureStyle();
|
|
setState('hidden');
|
|
win.addEventListener('keydown', onKey, true);
|
|
win.addEventListener('resize', onResize);
|
|
watch = watchDom(win as unknown as Parameters<typeof watchDom>[0], sync);
|
|
watch.schedule();
|
|
};
|
|
|
|
const deactivate = () => {
|
|
win.clearTimeout(peekTimer);
|
|
win.clearTimeout(leaveTimer);
|
|
railMenu = false;
|
|
if (resizeFrame) win.cancelAnimationFrame(resizeFrame);
|
|
resizeFrame = 0;
|
|
watch?.dispose();
|
|
watch = null;
|
|
win.removeEventListener('keydown', onKey, true);
|
|
win.removeEventListener('resize', onResize);
|
|
root()?.removeAttribute(ids.state);
|
|
bindRail(null);
|
|
edge?.remove();
|
|
edge = null;
|
|
doc.getElementById(ids.style)?.remove();
|
|
};
|
|
|
|
const apply = () => {
|
|
const next = enabled && redundant && !disposed;
|
|
if (next === active) return;
|
|
active = next;
|
|
if (active) activate();
|
|
else deactivate();
|
|
};
|
|
|
|
return {
|
|
set(value) {
|
|
const next = (value as { redundantNav?: unknown } | null)?.redundantNav;
|
|
if (typeof next !== 'boolean') return;
|
|
redundant = next;
|
|
apply();
|
|
},
|
|
setEnabled(value) {
|
|
enabled = value;
|
|
apply();
|
|
},
|
|
dispose() {
|
|
disposed = true;
|
|
apply();
|
|
},
|
|
};
|
|
}
|