|
| 1 | +/** |
| 2 | + * ZoomContext |
| 3 | + * |
| 4 | + * App-wide "interface zoom" preference — scales the whole renderer (sidebar, |
| 5 | + * navigator, chat, settings, dialogs) up or down, like the ⌘+ / ⌘- / ⌘0 zoom |
| 6 | + * in Claude Desktop, VS Code, and other desktop apps. |
| 7 | + * |
| 8 | + * The zoom factor is one of a fixed set of Chrome-like steps and is applied by |
| 9 | + * setting `document.documentElement.style.zoom` — a Chromium-native property |
| 10 | + * that scales the entire tree, including portalled dialogs rendered into |
| 11 | + * `<body>`. At 100% the inline style is cleared so the DOM stays clean. |
| 12 | + * |
| 13 | + * The preference is persisted in `localStorage` (renderer-only, no backend), |
| 14 | + * mirroring the other lightweight UI prefs in `lib/local-storage.ts` and the |
| 15 | + * merged `ReduceMotionProvider`. |
| 16 | + */ |
| 17 | + |
| 18 | +import React, { |
| 19 | + createContext, |
| 20 | + useContext, |
| 21 | + useState, |
| 22 | + useEffect, |
| 23 | + useCallback, |
| 24 | + useMemo, |
| 25 | + type ReactNode, |
| 26 | +} from 'react' |
| 27 | +import * as storage from '@/lib/local-storage' |
| 28 | + |
| 29 | +/** Discrete zoom steps (factors), mirroring a browser's zoom ladder. */ |
| 30 | +export const ZOOM_STEPS: readonly number[] = [0.5, 0.67, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2] |
| 31 | + |
| 32 | +/** The neutral / "actual size" factor. */ |
| 33 | +export const DEFAULT_ZOOM = 1 |
| 34 | + |
| 35 | +interface ZoomContextType { |
| 36 | + /** Current zoom factor (e.g. 1 = 100%, 1.1 = 110%). */ |
| 37 | + zoom: number |
| 38 | + /** Whole-number percentage for display (e.g. 110). */ |
| 39 | + zoomPercent: number |
| 40 | + /** Step up to the next larger factor (no-op at the max). */ |
| 41 | + zoomIn: () => void |
| 42 | + /** Step down to the next smaller factor (no-op at the min). */ |
| 43 | + zoomOut: () => void |
| 44 | + /** Return to 100%. */ |
| 45 | + resetZoom: () => void |
| 46 | + /** Whether a larger step is available. */ |
| 47 | + canZoomIn: boolean |
| 48 | + /** Whether a smaller step is available. */ |
| 49 | + canZoomOut: boolean |
| 50 | +} |
| 51 | + |
| 52 | +const ZoomContext = createContext<ZoomContextType | null>(null) |
| 53 | + |
| 54 | +/** Clamp an arbitrary stored value onto the nearest valid step. */ |
| 55 | +function normalizeZoom(value: number): number { |
| 56 | + if (!Number.isFinite(value)) return DEFAULT_ZOOM |
| 57 | + let nearest = ZOOM_STEPS[0] |
| 58 | + let bestDelta = Math.abs(value - nearest) |
| 59 | + for (const step of ZOOM_STEPS) { |
| 60 | + const delta = Math.abs(value - step) |
| 61 | + if (delta < bestDelta) { |
| 62 | + nearest = step |
| 63 | + bestDelta = delta |
| 64 | + } |
| 65 | + } |
| 66 | + return nearest |
| 67 | +} |
| 68 | + |
| 69 | +/** Reflect the factor onto <html> so the whole renderer scales. */ |
| 70 | +function applyZoom(factor: number): void { |
| 71 | + const root = document.documentElement |
| 72 | + if (factor === DEFAULT_ZOOM) { |
| 73 | + root.style.removeProperty('zoom') |
| 74 | + } else { |
| 75 | + root.style.zoom = String(factor) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export function ZoomProvider({ children }: { children: ReactNode }) { |
| 80 | + const [zoom, setZoomState] = useState<number>(() => |
| 81 | + normalizeZoom(storage.get<number>(storage.KEYS.zoomLevel, DEFAULT_ZOOM)), |
| 82 | + ) |
| 83 | + |
| 84 | + // Keep the DOM in sync (also covers the initial value on mount). |
| 85 | + useEffect(() => { |
| 86 | + applyZoom(zoom) |
| 87 | + }, [zoom]) |
| 88 | + |
| 89 | + const setZoom = useCallback((factor: number) => { |
| 90 | + const next = normalizeZoom(factor) |
| 91 | + setZoomState(next) |
| 92 | + storage.set(storage.KEYS.zoomLevel, next) |
| 93 | + applyZoom(next) |
| 94 | + }, []) |
| 95 | + |
| 96 | + const zoomIn = useCallback(() => { |
| 97 | + setZoomState((current) => { |
| 98 | + const idx = ZOOM_STEPS.indexOf(normalizeZoom(current)) |
| 99 | + const next = ZOOM_STEPS[Math.min(idx + 1, ZOOM_STEPS.length - 1)] |
| 100 | + storage.set(storage.KEYS.zoomLevel, next) |
| 101 | + applyZoom(next) |
| 102 | + return next |
| 103 | + }) |
| 104 | + }, []) |
| 105 | + |
| 106 | + const zoomOut = useCallback(() => { |
| 107 | + setZoomState((current) => { |
| 108 | + const idx = ZOOM_STEPS.indexOf(normalizeZoom(current)) |
| 109 | + const next = ZOOM_STEPS[Math.max(idx - 1, 0)] |
| 110 | + storage.set(storage.KEYS.zoomLevel, next) |
| 111 | + applyZoom(next) |
| 112 | + return next |
| 113 | + }) |
| 114 | + }, []) |
| 115 | + |
| 116 | + const resetZoom = useCallback(() => setZoom(DEFAULT_ZOOM), [setZoom]) |
| 117 | + |
| 118 | + const value = useMemo<ZoomContextType>(() => { |
| 119 | + const idx = ZOOM_STEPS.indexOf(zoom) |
| 120 | + return { |
| 121 | + zoom, |
| 122 | + zoomPercent: Math.round(zoom * 100), |
| 123 | + zoomIn, |
| 124 | + zoomOut, |
| 125 | + resetZoom, |
| 126 | + canZoomIn: idx < ZOOM_STEPS.length - 1, |
| 127 | + canZoomOut: idx > 0, |
| 128 | + } |
| 129 | + }, [zoom, zoomIn, zoomOut, resetZoom]) |
| 130 | + |
| 131 | + return <ZoomContext.Provider value={value}>{children}</ZoomContext.Provider> |
| 132 | +} |
| 133 | + |
| 134 | +export function useZoom(): ZoomContextType { |
| 135 | + const ctx = useContext(ZoomContext) |
| 136 | + if (!ctx) { |
| 137 | + throw new Error('useZoom must be used within a ZoomProvider') |
| 138 | + } |
| 139 | + return ctx |
| 140 | +} |
0 commit comments