From dd8d50d4704cd33fd1bd36d80ec84e7692ff5829 Mon Sep 17 00:00:00 2001 From: CRT Date: Sun, 11 Oct 2026 02:39:08 +0000 Subject: [PATCH] desktop settings: the section nav follows scrolling (scroll spy), clicks hold the highlight until the scroll settles --- apps/desktop/src/screens/Settings.tsx | 7 ++-- apps/desktop/src/ui/useScrollSpy.ts | 59 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 apps/desktop/src/ui/useScrollSpy.ts diff --git a/apps/desktop/src/screens/Settings.tsx b/apps/desktop/src/screens/Settings.tsx index 8301db44..a93177b6 100644 --- a/apps/desktop/src/screens/Settings.tsx +++ b/apps/desktop/src/screens/Settings.tsx @@ -2,6 +2,7 @@ // (on launch, every N minutes, how many apps at once, icons), the offline cache, language and theme, notifications, // local tools, the CLI sidecar, privacy, keyboard shortcuts, updates and reset. A section list on the left jumps. import { useEffect, useState } from 'react'; +import { useScrollSpy } from '../ui/useScrollSpy'; import { api, type CacheInfo, errorText, onUpdateProgress, type Settings as S, type UpdateInfo } from '../api'; import { type Key, t } from '../i18n'; import { ago, bytes, date, num } from '../lib/format'; @@ -17,6 +18,7 @@ export const SECTIONS: [string, IconName, Key][] = [ ['appearance', 'sun', 'settings.appearance'], ['notifications', 'bell', 'set.notifications'], ['dependencies', 'package', 'nav.dependencies'], ['cli', 'terminal', 'set.cli'], ['privacy', 'shield', 'set.privacy'], ['shortcuts', 'keyboard', 'settings.shortcuts'], ['updates', 'download', 'set.updates'], ['reset', 'rotate', 'set.reset'], ]; +const SECTION_IDS = SECTIONS.map(([id]) => id); function Toggle({ id, label, hint, checked, onChange }: { id: string; label: string; hint?: string; checked: boolean; onChange: (v: boolean) => void }) { return ( @@ -58,14 +60,13 @@ export function Settings({ onTheme: _onTheme, section }: { onTheme: () => void; const { prefs, update } = usePrefs(); const [error, setError] = useState(null); const [cache, setCache] = useState(null); - const [active, setActive] = useState(section ?? 'studios'); + const { active, jump } = useScrollSpy(SECTION_IDS, 'set-', section ?? 'studios', prefs.motion !== 'reduce'); const s = d.settings; const tools = d.sidecar?.tools; const set = (patch: (x: S) => S) => d.updateSettings(patch(s)); const loadCache = () => api.cacheInfo().then(setCache, () => {}); useEffect(() => { loadCache(); }, []); - useEffect(() => { if (section) { setActive(section); document.getElementById(`set-${section}`)?.scrollIntoView({ block: 'start' }); } }, [section]); - const jump = (id: string) => { setActive(id); document.getElementById(`set-${id}`)?.scrollIntoView({ block: 'start', behavior: prefs.motion === 'reduce' ? 'auto' : 'smooth' }); }; + useEffect(() => { if (section) jump(section, { instant: true }); }, [section, jump]); const clear = () => api.clearCache().then(() => { d.toast({ kind: 'ok', title: t('settings.cacheCleared') }); loadCache(); }, e => setError(errorText(e))); const reset = async () => { try { diff --git a/apps/desktop/src/ui/useScrollSpy.ts b/apps/desktop/src/ui/useScrollSpy.ts new file mode 100644 index 00000000..13e51611 --- /dev/null +++ b/apps/desktop/src/ui/useScrollSpy.ts @@ -0,0 +1,59 @@ +import { useCallback, useEffect, useRef, useState } from 'react'; + +/** The nearest ancestor that scrolls vertically (the app's content pane), or the document. */ +function scrollParent(el: HTMLElement | null): HTMLElement | null { + for (let p = el?.parentElement ?? null; p; p = p.parentElement) { + const o = getComputedStyle(p).overflowY; + if ((o === 'auto' || o === 'scroll') && p.scrollHeight > p.clientHeight) return p; + } + return null; +} + +/** + * Keeps a section nav in step with scrolling: the active id is the last section whose top has passed the upper + * third of the scroll area, or the last section once the area is scrolled to the end. `jump` scrolls to a section + * and holds the highlight on it until that scroll settles, so the nav doesn't flicker through the sections in between. + */ +export function useScrollSpy(ids: readonly string[], prefix: string, initial: string, smooth: boolean) { + const [active, setActive] = useState(initial); + const hold = useRef<{ id: string; until: number } | null>(null); + const key = ids.join('|'); + + useEffect(() => { + const first = document.getElementById(`${prefix}${ids[0]}`); + const root = scrollParent(first); + const target: HTMLElement | Window = root ?? window; + let frame = 0; + const update = (): void => { + frame = 0; + const h = hold.current; + if (h && performance.now() < h.until) return; + hold.current = null; + const top = root ? root.getBoundingClientRect().top : 0; + const height = root ? root.clientHeight : window.innerHeight; + const atEnd = root ? root.scrollTop + root.clientHeight >= root.scrollHeight - 2 : window.scrollY + window.innerHeight >= document.documentElement.scrollHeight - 2; + let current = ids[0]!; + if (atEnd) current = ids[ids.length - 1]!; + else for (const id of ids) { + const el = document.getElementById(`${prefix}${id}`); + if (el && el.getBoundingClientRect().top - top <= height / 3) current = id; + } + setActive(current); + }; + const onScroll = (): void => { if (!frame) frame = requestAnimationFrame(update); }; + const onEnd = (): void => { hold.current = null; onScroll(); }; + target.addEventListener('scroll', onScroll, { passive: true }); + target.addEventListener('scrollend', onEnd); + update(); + return () => { target.removeEventListener('scroll', onScroll); target.removeEventListener('scrollend', onEnd); if (frame) cancelAnimationFrame(frame); }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [key, prefix]); + + const jump = useCallback((id: string, opts?: { instant?: boolean }) => { + setActive(id); + hold.current = { id, until: performance.now() + 1200 }; + document.getElementById(`${prefix}${id}`)?.scrollIntoView({ block: 'start', behavior: smooth && !opts?.instant ? 'smooth' : 'auto' }); + }, [prefix, smooth]); + + return { active, jump }; +}