/** * Toolbar visibility modes. * * visible – always shown (default) * autohide – toolbar and side arrows fade out after a moment without * activity; moving the mouse, touching or a key brings them back * minimize – when idle the toolbar shrinks to a small pill with the page * number; hovering, tapping or focusing it expands it again * * Never hides while the toolbar is hovered or focused, or while a panel, * menu or card from the viewer is open. */ export type ToolbarMode = 'visible' | 'autohide' | 'minimize'; const IDLE_MS = 2500; export function resolveToolbarMode(value: unknown): ToolbarMode { return value === 'autohide' || value === 'minimize' ? value : 'visible'; } /** Something the reader is using is open: keep the toolbar. */ const BUSY = '.pdfrack-share-panel, .pdfrack-hs-popover, .pdfrack-hs-modal, .pdfrack-resume, .pdfrack-notice, .pdfrack-lock'; export function applyToolbarMode(container: HTMLElement, mode: ToolbarMode): () => void { if (mode === 'visible') return () => {}; const idleClass = mode === 'autohide' ? 'pdfrack-tb-hidden' : 'pdfrack-tb-collapsed'; container.classList.add(mode === 'autohide' ? 'pdfrack-tb-autohide' : 'pdfrack-tb-minimize'); let timer = 0; let pointerOverToolbar = false; const busy = () => pointerOverToolbar || container.classList.contains('pdfrack-sidebar-open') || !!container.querySelector('.pdfrack-toolbar.pdfrack-more-open, .pdfrack-toolbar:focus-within') || !!container.querySelector(BUSY); const show = () => { container.classList.remove(idleClass); window.clearTimeout(timer); timer = window.setTimeout(hide, IDLE_MS); }; const hide = () => { if (busy()) { timer = window.setTimeout(hide, IDLE_MS); return; } container.classList.add(idleClass); }; // Activity anywhere in the viewer const onActivity = () => show(); const events = ['pointermove', 'pointerdown', 'touchstart', 'wheel', 'keydown', 'focusin'] as const; events.forEach((type) => container.addEventListener(type, onActivity, { passive: true })); // Hovering the toolbar keeps it (and expands the minimized pill) const onOver = (e: Event) => { if ((e.target as Element).closest?.('.pdfrack-toolbar')) { pointerOverToolbar = true; show(); } }; const onOut = (e: PointerEvent) => { const from = (e.target as Element).closest?.('.pdfrack-toolbar'); const to = (e.relatedTarget as Element | null)?.closest?.('.pdfrack-toolbar'); if (from && !to) pointerOverToolbar = false; }; container.addEventListener('pointerover', onOver); container.addEventListener('pointerout', onOut); // Minimized pill: a tap on it expands the toolbar instead of pressing // whichever button happens to sit under the finger. const onPillClick = (e: MouseEvent) => { if (mode !== 'minimize' || !container.classList.contains(idleClass)) return; if (!(e.target as Element).closest?.('.pdfrack-toolbar')) return; e.preventDefault(); e.stopPropagation(); show(); }; container.addEventListener('click', onPillClick, true); // Start visible, then settle timer = window.setTimeout(hide, IDLE_MS * 1.6); return () => { window.clearTimeout(timer); events.forEach((type) => container.removeEventListener(type, onActivity)); container.removeEventListener('pointerover', onOver); container.removeEventListener('pointerout', onOut); container.removeEventListener('click', onPillClick, true); container.classList.remove(idleClass, 'pdfrack-tb-autohide', 'pdfrack-tb-minimize'); }; }