/** * Vertical tab strip for the right context rail: one sideways tab per rail * panel, the open panel beside the strip, at most one panel open. The panels' * `collapsed` classes remain the only state; the strip mirrors them and asks * the owner (setPanelCollapsed) for every change, so persistence, share links * and the rail layout pass are untouched. Phones hide the strip and keep their * sheet shell (mobileShell.js). * * The open tab and its panel are one surface: the strip measures both, traces * their joined silhouette as an SVG path (railJoinPath) and keeps a join * element under the panels whose layers blur, tint, outline and shadow that * shape (styles/right-rail-tabs.css). * * The strip sits in a column with a tool slot under it. The compact Radio * launcher lives in the slot on desktop (radioPanelPlacement.js), where the * collapsed Context that carries it otherwise is hidden. */ import { msg, t } from '../i18n/index.js'; export const RIGHT_RAIL_TABS_ID = 'right-rail-tabs'; export const RIGHT_RAIL_COLUMN_ID = 'right-rail-column'; export const RIGHT_RAIL_TOOLS_ID = 'right-rail-tools'; /** * Marks the rail while its strip is mounted; styles/right-rail-tabs.css keys * the tab geometry on it, so a rail without a strip keeps its plain layout. */ export const RIGHT_RAIL_TABS_ATTRIBUTE = 'data-rail-tabs'; const SVG_NS = 'http://www.w3.org/2000/svg'; /** * The tab keyboard navigation moves to in a vertical tablist: Down and Up * wrap, Home and End jump. Any other key keeps the current index. * @param {string} key KeyboardEvent.key. * @param {number} index Index of the focused tab. * @param {number} count Visible tab count. * @returns {number} Target index, or -1 when there is no tab. */ export function nextRailTabIndex(key, index, count) { if (!(count > 0)) return -1; const current = Math.max(0, Math.min(count - 1, index)); switch (key) { case 'ArrowDown': return (current + 1) % count; case 'ArrowUp': return (current - 1 + count) % count; case 'Home': return 0; case 'End': return count - 1; default: return current; } } /** The panels a rail presents as tabs, in rail order. */ export function railTabPanels(rail) { return [...(rail?.children || [])].filter( (panel) => panel.matches?.('[data-panel-id]') && !panel.hidden, ); } /** * SVG path of the panel with the open tab attached to its right edge, traced * clockwise from the top-left corner, in rail pixels. * * Round: the panel's corners and the tab's outer corners are convex arcs and * the two inner corners where the tab's top and bottom edges meet the panel's * right edge are concave fillets. A tab level with the panel's top shares its * top edge, so that corner and the upper fillet are dropped. Chamfer (Cyber): * the frame's 10px chamfers and 6px calibration bay (cyber.css, * --cyber-panel-frame) and a square tab. `inset` moves the whole outline * inward; 0.5 centres a one-pixel stroke just inside the edge. * * @param {object} g * @param {{x0: number, y0: number, x1: number, y1: number}} g.panel * @param {{top: number, bottom: number, right: number}} g.tab The tab's * extent; its left edge is the panel's right edge. * @param {number} [g.corner] Panel corner radius, or the chamfer size. * @param {number} [g.tabRadius] The tab's outer corner radius. * @param {number} [g.fillet] Concave fillet radius at the junction. * @param {'round'|'chamfer'} [g.style] * @param {number} [g.inset] * @returns {string} */ export function railJoinPath({ panel, tab, corner = 16, tabRadius = 10, fillet = 8, style = 'round', inset = 0, }) { const n = (value) => Math.round(value * 100) / 100; const P = (x, y) => `${n(x)} ${n(y)}`; const x0 = panel.x0 + inset; const y0 = panel.y0 + inset; const x1 = panel.x1 - inset; const y1 = panel.y1 - inset; const ty0 = tab.top + inset; const ty1 = tab.bottom - inset; const tx1 = tab.right - inset; if (style === 'chamfer') { const c = corner; const cx = (panel.x0 + panel.x1) / 2; // The tab's edges along the panel stay between the chamfers. const tyA = Math.max(ty0, y0 + c); const tyB = Math.max(tyA, Math.min(ty1, y1 - c)); const edge = [ `L ${P(x1, tyA)}`, `L ${P(tx1, tyA)}`, `L ${P(tx1, tyB)}`, `L ${P(x1, tyB)}`, ]; return [ `M ${P(x0, y0 + c)}`, `L ${P(x0 + c, y0)}`, `L ${P(cx - 25, y0)}`, `L ${P(cx - 18, y0 + 6)}`, `L ${P(cx + 18, y0 + 6)}`, `L ${P(cx + 25, y0)}`, `L ${P(x1 - c, y0)}`, `L ${P(x1, y0 + c)}`, ...edge, `L ${P(x1, y1 - c)}`, `L ${P(x1 - c, y1)}`, `L ${P(x0 + c, y1)}`, `L ${P(x0, y1 - c)}`, 'Z', ].join(' '); } const R = Math.max(0, corner - inset); const r = Math.max(0, tabRadius - inset); const f = fillet + inset; const d = []; let at = null; const same = (x, y) => at && at[0] === n(x) && at[1] === n(y); const move = (x, y) => { d.push(`M ${P(x, y)}`); at = [n(x), n(y)]; }; const line = (x, y) => { if (same(x, y)) return; d.push(`L ${P(x, y)}`); at = [n(x), n(y)]; }; // sweep 1 turns clockwise (convex), 0 counter-clockwise (concave); a zero // radius is a plain corner. const arc = (radius, sweep, x, y) => { if (!(radius > 0)) return line(x, y); d.push(`A ${n(radius)} ${n(radius)} 0 0 ${sweep} ${P(x, y)}`); at = [n(x), n(y)]; }; // The run of panel edge above or below the tab may be shorter than a fillet // plus a corner: shrink them to what fits, down to a straight edge shared // with the panel's top or bottom. const fit = (room) => { const fillet = Math.min(f, Math.max(0, room - R)); return { fillet, corner: Math.min(R, Math.max(0, room - fillet)) }; }; move(x0 + R, y0); const first = tab.top - panel.y0 < 0.5; const last = panel.y1 - tab.bottom < 0.5; const above = fit(ty0 - y0); const below = fit(y1 - ty1); if (first) { line(tx1 - r, y0); arc(r, 1, tx1, y0 + r); } else { line(x1 - above.corner, y0); arc(above.corner, 1, x1, y0 + above.corner); line(x1, ty0 - above.fillet); arc(above.fillet, 0, x1 + above.fillet, ty0); line(tx1 - r, ty0); arc(r, 1, tx1, ty0 + r); } line(tx1, ty1 - r); arc(r, 1, tx1 - r, ty1); if (!last) { line(x1 + below.fillet, ty1); arc(below.fillet, 0, x1, ty1 + below.fillet); line(x1, y1 - below.corner); arc(below.corner, 1, x1 - below.corner, y1); } line(x0 + R, y1); arc(R, 1, x0, y1 - R); line(x0, y0 + R); arc(R, 1, x0 + R, y0); d.push('Z'); return d.join(' '); } /** The tab label: the panel's own header title, already translated. */ function panelTitle(panel) { return ( panel .querySelector?.('.panel-title, .pp-header-label') ?.textContent?.trim() || panel.id ); } /** * Build the strip inside `rail` and keep it in step with the rail's panels. * @param {object} input * @param {HTMLElement} input.rail The right context rail. * @param {(panelId: string, collapsed: boolean, options: object) => void} input.setPanelCollapsed * Owner callback; the strip never touches panel classes itself. * @param {Document} [input.documentRef] * @returns {{ element: HTMLElement, column: HTMLElement, tools: HTMLElement, * join: HTMLElement, sync: () => void, destroy: () => void }} */ export function createRightRailTabs({ rail, setPanelCollapsed, documentRef = rail?.ownerDocument, }) { if (!rail || !documentRef || typeof setPanelCollapsed !== 'function') { throw new TypeError( 'Right rail tabs require a rail, a document and setPanelCollapsed', ); } const column = documentRef.createElement('div'); column.id = RIGHT_RAIL_COLUMN_ID; const strip = documentRef.createElement('div'); strip.id = RIGHT_RAIL_TABS_ID; strip.setAttribute('role', 'tablist'); strip.setAttribute('aria-orientation', 'vertical'); strip.setAttribute('aria-label', t('Panels')); const tools = documentRef.createElement('div'); tools.id = RIGHT_RAIL_TOOLS_ID; column.append(strip, tools); rail.setAttribute(RIGHT_RAIL_TABS_ATTRIBUTE, 'vertical'); // The join: shadow under, glass, outline over; all shaped by one path. const svg = (tag) => documentRef.createElementNS?.(SVG_NS, tag) ?? documentRef.createElement(tag); const join = documentRef.createElement('div'); join.className = 'right-rail-join'; join.setAttribute('aria-hidden', 'true'); join.hidden = true; const shadow = svg('svg'); shadow.setAttribute('class', 'right-rail-join-shadow'); const shadowPath = svg('path'); shadow.append(shadowPath); const glass = documentRef.createElement('div'); glass.setAttribute('class', 'right-rail-join-glass'); const outline = svg('svg'); outline.setAttribute('class', 'right-rail-join-outline'); const outlinePath = svg('path'); outline.append(outlinePath); join.append(shadow, glass, outline); /** @type {Map} */ const entries = new Map(); const observed = new WeakSet(); let destroyed = false; const entryFor = (panel) => { let entry = entries.get(panel.id); if (entry) return entry; const tab = documentRef.createElement('button'); tab.type = 'button'; tab.className = 'right-rail-tab'; tab.dataset.railTab = panel.id; tab.setAttribute('role', 'tab'); tab.setAttribute('aria-controls', panel.id); const label = documentRef.createElement('span'); label.className = 'right-rail-tab-label'; tab.append(label); entry = { tab, label }; entries.set(panel.id, entry); return entry; }; const Observer = documentRef.defaultView?.MutationObserver ?? globalThis.MutationObserver; const observer = typeof Observer === 'function' ? new Observer(() => sync()) : null; // Tab heights follow their labels (fonts, locale) and panel heights their // content and allocation: re-measure the join when the strip or a panel // changes size. const Resize = documentRef.defaultView?.ResizeObserver; const resizer = typeof Resize === 'function' ? new Resize(() => sync()) : null; resizer?.observe(strip); const observe = (panel) => { if (observed.has(panel)) return; observed.add(panel); resizer?.observe(panel); observer?.observe(panel, { attributes: true, attributeFilter: ['class', 'hidden'], }); }; /** A length from a computed style, in pixels. */ const token = (computed, name, fallback) => { const value = parseFloat(computed?.getPropertyValue?.(name)); return Number.isFinite(value) ? value : fallback; }; /** * Share a strip shorter than its tabs. The lane (--right-stack-max-height) * less the tool slot and the strip's gaps is divided equally; a tab shorter * than its share keeps its height and the rest is shared again, so the long * labels end up the same height and clip alike (styles/right-rail-tabs.css). * Tabs that fit keep their natural height. */ const heights = new WeakMap(); const setHeight = (tab, value) => { if (heights.get(tab) === value) return; heights.set(tab, value); if (value) tab.style.setProperty('height', value); else tab.style.removeProperty('height'); }; function fitTabs(tabs) { const styleOf = (el) => documentRef.defaultView?.getComputedStyle?.(el); const stripStyle = styleOf(strip); const lane = token(styleOf(rail), '--right-stack-max-height', NaN); const chrome = token(stripStyle, 'row-gap', 0) * Math.max(0, tabs.length - 1) + token(stripStyle, 'padding-top', 0) + token(stripStyle, 'padding-bottom', 0); const overhead = (column.offsetHeight || 0) - (strip.offsetHeight || 0); const room = lane - overhead - chrome; const entries = tabs.map((tab) => { const tabStyle = styleOf(tab); return { tab, natural: (tab.firstElementChild?.offsetHeight || 0) + token(tabStyle, 'padding-top', 0) + token(tabStyle, 'padding-bottom', 0), }; }); const natural = entries.reduce((sum, entry) => sum + entry.natural, 0); if ( !(room > 0) || natural <= room + 0.5 || entries.some((entry) => !(entry.natural > 0)) ) { for (const entry of entries) setHeight(entry.tab, ''); return; } let remaining = room; let count = entries.length; for (const entry of [...entries].sort((a, b) => a.natural - b.natural)) { const share = remaining / count; if (entry.natural <= share) { remaining -= entry.natural; count -= 1; setHeight(entry.tab, ''); } else { setHeight(entry.tab, `${Math.round(share * 100) / 100}px`); } } } /** * Publish the join: the open tab's bottom on the rail (--rail-tab-bottom, * rail pixels; the stylesheet sets the panel's minimum height from it) and * the joined silhouette on the join's layers. The rail can be taller than * its panel in the focus layout, so the join is sized from the panel, * extended down to cover the tab if that is shorter, but never past the * rail's lane (--right-stack-max-height). */ function placeJoin(tab, panel) { if (!tab) { rail.style.removeProperty('--rail-tab-bottom'); join.hidden = true; return; } const top = tab.offsetTop; rail.style.setProperty('--rail-tab-bottom', `${top + tab.offsetHeight}px`); const computed = documentRef.defaultView?.getComputedStyle?.(rail); const corner = token(computed, '--right-rail-corner', 16); const tabRadius = token(computed, '--right-rail-tab-radius', 10); const fillet = token(computed, '--right-rail-fillet', 8); const lane = token(computed, '--right-stack-max-height', Infinity); const chamfer = computed?.getPropertyValue?.('--right-rail-join-style').trim() === 'chamfer'; // Tab offsets are relative to the strip, the strip's to the column, the // column's to the rail. const stripTop = (column.offsetTop || 0) + (strip.offsetTop || 0); const stripLeft = (column.offsetLeft || 0) + (strip.offsetLeft || 0); const tabTop = stripTop + top; const tabBottom = tabTop + tab.offsetHeight; const geometry = { panel: { x0: panel.offsetLeft, y0: panel.offsetTop, x1: panel.offsetLeft + panel.offsetWidth, y1: Math.max( panel.offsetTop + panel.offsetHeight, Math.min(lane, tabBottom + fillet + 4 + corner), ), }, tab: { top: tabTop, bottom: tabBottom, right: stripLeft + tab.offsetLeft + tab.offsetWidth, }, corner, // A tab shrunk by a short strip keeps corners no larger than half of it. tabRadius: Math.min(tabRadius, (tabBottom - tabTop) / 2), fillet, style: chamfer ? 'chamfer' : 'round', }; const shape = railJoinPath(geometry); const width = rail.clientWidth; const height = geometry.panel.y1; join.style.height = `${height}px`; glass.style.clipPath = `path('${shape}')`; shadowPath.setAttribute('d', shape); // Everything but the silhouette: the shadow it casts stays, the fill goes. shadow.style.clipPath = `path(evenodd, 'M -80 -80 H ${width + 80} V ${height + 80} H -80 Z ${shape}')`; outlinePath.setAttribute('d', railJoinPath({ ...geometry, inset: 0.5 })); join.hidden = false; } /** Reconcile tabs with the rail: order, visibility, selection, labels. */ function sync() { if (destroyed) return; // The column precedes its panels in the tab order, so Tab from the open // tab reaches the tool slot and then the open panel. The rail owner moves // panels in at start-up; keep the column first afterwards too. if (rail.firstElementChild !== column) rail.prepend(column); if (join.parentElement !== rail) rail.append(join); // Hidden panels too: Weather and Recent Imagery arrive hidden and their // owners reveal them by attribute, which must create their tabs. for (const panel of rail.children) if (panel.matches?.('[data-panel-id]')) observe(panel); const panels = railTabPanels(rail); const focusOwner = panels.find((panel) => !panel.classList.contains('collapsed')) || panels[0]; // Move a tab only when it is out of place: reinserting the focused tab // would drop keyboard focus on every class change. let cursor = strip.firstElementChild; let open = null; const tabs = []; for (const panel of panels) { const { tab, label } = entryFor(panel); tabs.push(tab); if (tab !== cursor) strip.insertBefore(tab, cursor); else cursor = cursor.nextElementSibling; const collapsed = panel.classList.contains('collapsed'); if (!collapsed && !open) open = { tab, panel }; const name = panelTitle(panel); if (label.textContent !== name) label.textContent = name; tab.setAttribute('aria-selected', String(!collapsed)); tab.tabIndex = panel === focusOwner ? 0 : -1; tab.title = t(collapsed ? msg('Expand {name}') : msg('Collapse {name}'), { name, }); } const live = new Set(panels.map((panel) => panel.id)); for (const [id, entry] of entries) { if (live.has(id)) continue; entry.tab.remove(); entries.delete(id); } fitTabs(tabs); placeJoin(open?.tab, open?.panel); } const onClick = (event) => { const tab = event.target?.closest?.('[data-rail-tab]'); if (!tab) return; const panel = documentRef.getElementById(tab.dataset.railTab); if (!panel) return; setPanelCollapsed(panel.id, !panel.classList.contains('collapsed'), { explicit: true, }); }; const onKeydown = (event) => { const current = event.target?.closest?.('[data-rail-tab]'); if (!current) return; const visible = [...strip.children].filter((tab) => !tab.hidden); const index = visible.indexOf(current); const next = nextRailTabIndex(event.key, index, visible.length); if (next < 0 || next === index) return; event.preventDefault(); visible[next].focus(); }; strip.addEventListener('click', onClick); strip.addEventListener('keydown', onKeydown); observer?.observe(rail, { childList: true }); // Tabs holding shared heights do not resize with the lane: watch the rail's // style for a new lane. The join's own writes to that style change no lane. let seenLane = rail.style?.getPropertyValue?.('--right-stack-max-height'); const laneObserver = typeof Observer === 'function' ? new Observer(() => { const lane = rail.style.getPropertyValue('--right-stack-max-height'); if (lane === seenLane) return; seenLane = lane; sync(); }) : null; laneObserver?.observe(rail, { attributes: true, attributeFilter: ['style'] }); rail.prepend(column); rail.append(join); sync(); return { element: strip, column, tools, join, sync, destroy() { if (destroyed) return; destroyed = true; observer?.disconnect(); laneObserver?.disconnect(); resizer?.disconnect(); strip.removeEventListener('click', onClick); strip.removeEventListener('keydown', onKeydown); column.remove(); join.remove(); rail.removeAttribute(RIGHT_RAIL_TABS_ATTRIBUTE); placeJoin(null, null); entries.clear(); }, }; }