diff --git a/apps/desktop/src/app/right-sidebar/terminal/selection.ts b/apps/desktop/src/app/right-sidebar/terminal/selection.ts index 2aa2f891d3..239f549591 100644 --- a/apps/desktop/src/app/right-sidebar/terminal/selection.ts +++ b/apps/desktop/src/app/right-sidebar/terminal/selection.ts @@ -1,6 +1,7 @@ import type { ITheme, Terminal } from '@xterm/xterm' import type { CSSProperties } from 'react' +import { isMacPlatform } from '@/lib/platform' import type { DesktopTerminalPalette } from '@/themes/types' // VS Code's default integrated-terminal palette (terminalColorRegistry.ts) — a @@ -97,7 +98,7 @@ export function resolveSurfaceColor(fallback: string): string { return resolved && resolved !== 'rgba(0, 0, 0, 0)' ? resolved : fallback } -export const isMacPlatform = () => navigator.platform.toLowerCase().includes('mac') +export { isMacPlatform } export function isAddSelectionShortcut(event: KeyboardEvent) { const mod = isMacPlatform() ? event.metaKey : event.ctrlKey diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts index 276c6b1661..aafc15d77f 100644 --- a/apps/desktop/src/global.d.ts +++ b/apps/desktop/src/global.d.ts @@ -1,4 +1,5 @@ import type { GatewayWsUrlResult } from '@hermes/shared' +import type { TranslucencyState } from '@hermes/shared/translucency' import type { WakeIndicatorState } from './lib/wake-indicator' import type { @@ -226,7 +227,7 @@ declare global { setActiveWork?: (payload: HermesActiveWork) => void setTitleBarTheme?: (payload: HermesTitleBarTheme) => void setNativeTheme?: (mode: 'dark' | 'light' | 'system') => void - setTranslucency?: (payload: { intensity: number; mode?: 'clear' | 'glass' }) => void + setTranslucency?: (payload: TranslucencyState) => void setKeepAwake?: (on: boolean) => void setDisableF12?: (blocked: boolean) => void setPreviewShortcutActive?: (active: boolean) => void diff --git a/apps/desktop/src/lib/platform.ts b/apps/desktop/src/lib/platform.ts new file mode 100644 index 0000000000..011d3afc4e --- /dev/null +++ b/apps/desktop/src/lib/platform.ts @@ -0,0 +1,10 @@ +/** + * Platform detection for the renderer. + * + * The renderer has no `process.platform`, and several surfaces need to know + * whether they're on a Mac — keybind glyphs, terminal shortcuts, and the + * macOS-only vibrancy features. One definition so they can't disagree. + */ + +export const isMacPlatform = (): boolean => + typeof navigator !== 'undefined' && /mac/i.test(navigator.platform || navigator.userAgent || '') diff --git a/apps/desktop/src/store/translucency.test.ts b/apps/desktop/src/store/translucency.test.ts index 8e73c5374f..9e78bdbe34 100644 --- a/apps/desktop/src/store/translucency.test.ts +++ b/apps/desktop/src/store/translucency.test.ts @@ -5,9 +5,8 @@ import { onPersistenceEvent, type PersistenceEvent } from '@/lib/storage' import { $translucency, - $translucencyMode, GLASS_SUPPORTED, - glassSurfaceKeep, + isChatWindow, setTranslucency, setTranslucencyMode, TRANSLUCENCY_MAX, @@ -17,10 +16,14 @@ import { const KEY = 'hermes.desktop.translucency.v1' +const glassAttr = () => document.documentElement.hasAttribute('data-hermes-glass') +const clearAttr = () => document.documentElement.hasAttribute('data-hermes-clear') +const keep = () => document.documentElement.style.getPropertyValue('--translucency-glass-keep') + describe('window translucency lever', () => { beforeEach(() => { - setTranslucency(TRANSLUCENCY_MIN) setTranslucencyMode('clear') + setTranslucency(TRANSLUCENCY_MIN) }) it('steps in single percent so the readable low end is reachable', () => { @@ -29,31 +32,38 @@ describe('window translucency lever', () => { expect(TRANSLUCENCY_MAX).toBe(100) }) - it('defaults to off', () => { - expect($translucency.get()).toBe(TRANSLUCENCY_MIN) + it('defaults to off and clear', () => { + expect($translucency.get()).toEqual({ intensity: TRANSLUCENCY_MIN, mode: 'clear' }) }) it('accepts every step the slider can emit', () => { for (let intensity = TRANSLUCENCY_MIN; intensity <= TRANSLUCENCY_MAX; intensity += TRANSLUCENCY_STEP) { setTranslucency(intensity) - expect($translucency.get()).toBe(intensity) + expect($translucency.get().intensity).toBe(intensity) } }) - it('clamps out-of-range input to the lever bounds', () => { + it('clamps out-of-range input and rounds fractions', () => { setTranslucency(-40) - expect($translucency.get()).toBe(TRANSLUCENCY_MIN) + expect($translucency.get().intensity).toBe(TRANSLUCENCY_MIN) setTranslucency(240) - expect($translucency.get()).toBe(TRANSLUCENCY_MAX) - }) + expect($translucency.get().intensity).toBe(TRANSLUCENCY_MAX) - it('rounds fractional input to a whole percent', () => { setTranslucency(35.6) - expect($translucency.get()).toBe(36) + expect($translucency.get().intensity).toBe(36) }) - it('persists under a stable key so a saved setting survives upgrades', () => { + it('changing one half of the state leaves the other alone', () => { + setTranslucency(42) + setTranslucencyMode('glass') + expect($translucency.get().intensity).toBe(42) + + setTranslucency(43) + expect($translucency.get().mode).toBe(GLASS_SUPPORTED ? 'glass' : 'clear') + }) + + it('persists intensity and mode together under one stable key', () => { const writes: PersistenceEvent[] = [] const stop = onPersistenceEvent(event => { @@ -68,65 +78,87 @@ describe('window translucency lever', () => { stop() } - expect(writes).toContainEqual({ key: KEY, op: 'write', value: '23' }) + expect(writes.at(-1)).toEqual({ key: KEY, op: 'write', value: JSON.stringify({ intensity: 23, mode: 'clear' }) }) }) - it('mirrors intensity and mode to the desktop bridge', () => { - const calls: Array<{ intensity: number; mode?: string }> = [] + it('mirrors the whole state to the desktop bridge', () => { + const calls: Array<{ intensity: number; mode: string }> = [] window.hermesDesktop = { - setTranslucency: (payload: { intensity: number; mode?: 'clear' | 'glass' }) => calls.push(payload) + setTranslucency: (payload: { intensity: number; mode: 'clear' | 'glass' }) => calls.push(payload) } as never setTranslucency(40) expect(calls.at(-1)).toEqual({ intensity: 40, mode: 'clear' }) }) +}) + +describe('glass mode', () => { + beforeEach(() => { + setTranslucencyMode('clear') + setTranslucency(TRANSLUCENCY_MIN) + }) it('rejects glass off macOS and applies it on macOS', () => { setTranslucency(50) setTranslucencyMode('glass') if (GLASS_SUPPORTED) { - expect($translucencyMode.get()).toBe('glass') - expect(document.documentElement.hasAttribute('data-hermes-glass')).toBe(true) - expect(document.documentElement.style.getPropertyValue('--translucency-glass-keep')).toBe('65%') + expect($translucency.get().mode).toBe('glass') + expect(glassAttr()).toBe(true) + expect(keep()).toBe('65%') } else { - expect($translucencyMode.get()).toBe('clear') - expect(document.documentElement.hasAttribute('data-hermes-glass')).toBe(false) + expect($translucency.get().mode).toBe('clear') + expect(glassAttr()).toBe(false) } }) - it('removes the glass attribute at zero intensity or back on clear', () => { + it('drops the glass attribute at zero intensity or back on clear', () => { setTranslucency(50) setTranslucencyMode('glass') + setTranslucency(0) - expect(document.documentElement.hasAttribute('data-hermes-glass')).toBe(false) + expect(glassAttr()).toBe(false) + expect(keep()).toBe('') setTranslucency(50) setTranslucencyMode('clear') - expect(document.documentElement.hasAttribute('data-hermes-glass')).toBe(false) + expect(glassAttr()).toBe(false) }) - it('marks clear mode on so the overlay scrim can compensate', () => { + // Clear and glass are mutually exclusive page states: clear strengthens the + // overlay scrim, glass thins the field. Both at once would fight. + it('marks clear mode separately, and never both at once', () => { setTranslucency(50) - setTranslucencyMode('clear') - expect(document.documentElement.hasAttribute('data-hermes-clear')).toBe(true) + expect(clearAttr()).toBe(true) + expect(glassAttr()).toBe(false) - setTranslucency(0) - expect(document.documentElement.hasAttribute('data-hermes-clear')).toBe(false) - - setTranslucency(50) + setTranslucencyMode('glass') if (GLASS_SUPPORTED) { - setTranslucencyMode('glass') - expect(document.documentElement.hasAttribute('data-hermes-clear')).toBe(false) + expect(clearAttr()).toBe(false) + expect(glassAttr()).toBe(true) + } + + setTranslucency(0) + expect(clearAttr()).toBe(false) + expect(glassAttr()).toBe(false) + }) +}) + +// Glass rewrites page surfaces, which is only correct in a real chat window. +// The HUD, pet overlay, quick entry and wake indicator are transparent +// special-purpose windows that own their own backgrounds. +describe('isChatWindow', () => { + it('accepts the primary window and secondary session windows', () => { + expect(isChatWindow('')).toBe(true) + expect(isChatWindow('?theme=dark')).toBe(true) + expect(isChatWindow('?win=secondary')).toBe(true) + expect(isChatWindow('?win=secondary&session=abc')).toBe(true) + }) + + it('rejects every special-purpose window kind', () => { + for (const win of ['hud', 'pet', 'quick-entry', 'wake', 'anything-new']) { + expect(isChatWindow(`?win=${win}`), win).toBe(false) } }) }) - -describe('glassSurfaceKeep', () => { - it('mirrors the clear-mode opacity ramp with its 30% floor', () => { - expect(glassSurfaceKeep(0)).toBe(100) - expect(glassSurfaceKeep(50)).toBe(65) - expect(glassSurfaceKeep(100)).toBeCloseTo(30) - }) -}) diff --git a/apps/desktop/src/store/translucency.ts b/apps/desktop/src/store/translucency.ts index 587617dd6e..eee1e9681e 100644 --- a/apps/desktop/src/store/translucency.ts +++ b/apps/desktop/src/store/translucency.ts @@ -1,123 +1,110 @@ /** * Window translucency (see-through window). * - * One lever, 0–100. 0 = off (fully opaque, the default). Higher = more of the - * desktop shows through. Two modes decide HOW it shows through: + * One lever, 0–100. 0 = off (fully opaque, the default). Two modes decide HOW + * the desktop shows through — see `@hermes/shared/translucency`, which owns the + * mapping both this store and the main process read. * - * - 'clear' — the main process maps the lever to native window opacity - * (`setOpacity`), the same effect as the Windows shift-scroll trick. The - * whole window fades, text included. macOS + Windows; Linux has no runtime - * window opacity, so it's a no-op there. - * - 'glass' — macOS only. The window stays opaque at the native level; this - * store thins the renderer's page surfaces instead (see the - * `[data-hermes-glass]` block in styles.css), so the vibrancy material every - * chat window already carries shows through as a matte blur while text keeps - * full contrast. - * - * The renderer owns both values and mirrors them to the main process over IPC. + * The renderer owns the value and mirrors it to the main process over IPC. + * Glass additionally needs page-level work, which lives here: the field + * surfaces have to get out of the way for the vibrancy material underneath the + * web contents to read (see the `[data-hermes-glass]` block in styles.css). */ +import { + clampIntensity, + glassSurfaceKeep, + TRANSLUCENCY_MAX, + TRANSLUCENCY_MIN, + TRANSLUCENCY_STEP, + type TranslucencyMode +} from '@hermes/shared/translucency' import { atom } from 'nanostores' -import { persistString, storedString } from '@/lib/storage' +import { isMacPlatform } from '@/lib/platform' +import { readJson, writeJson } from '@/lib/storage' -export type TranslucencyMode = 'clear' | 'glass' +export { TRANSLUCENCY_MAX, TRANSLUCENCY_MIN, TRANSLUCENCY_STEP, type TranslucencyMode } + +/** Glass rides on the macOS vibrancy material; other platforms only have Clear. */ +export const GLASS_SUPPORTED = isMacPlatform() const KEY = 'hermes.desktop.translucency.v1' -const MODE_KEY = 'hermes.desktop.translucency-mode.v1' -/** Glass rides on macOS vibrancy; other platforms only have Clear. */ -export const GLASS_SUPPORTED = typeof navigator !== 'undefined' && /mac/i.test(navigator.platform || navigator.userAgent || '') - -// Slider bounds — mirror `electron/window-opacity.ts` (TRANSLUCENCY_MIN / MAX) -// so the control and the main-process clamp agree on the same range. The step -// is renderer-only: the main process takes any integer in range. -export const TRANSLUCENCY_MIN = 0 -export const TRANSLUCENCY_MAX = 100 -export const TRANSLUCENCY_STEP = 1 - -const clamp = (n: number): number => Math.min(TRANSLUCENCY_MAX, Math.max(TRANSLUCENCY_MIN, Math.round(n))) - -const read = (): number => { - const n = Number(storedString(KEY)) - - return Number.isFinite(n) ? clamp(n) : 0 +interface PersistedTranslucency { + intensity: number + mode: TranslucencyMode } -const readMode = (): TranslucencyMode => (GLASS_SUPPORTED && storedString(MODE_KEY) === 'glass' ? 'glass' : 'clear') +// The v1 key used to hold a bare intensity (`"23"`). Anything without a mode +// predates glass and always behaved as clear, so it stays clear — a saved +// setting must not change how the window looks on update. +const read = (): PersistedTranslucency => { + const stored = readJson(KEY) -export const $translucency = atom(typeof window === 'undefined' ? 0 : read()) + const record: Record = + stored && typeof stored === 'object' ? (stored as Record) : { intensity: stored } -export const $translucencyMode = atom(typeof window === 'undefined' ? 'clear' : readMode()) + return { + intensity: clampIntensity(record.intensity), + mode: record.mode === 'glass' && GLASS_SUPPORTED ? 'glass' : 'clear' + } +} + +const initial: PersistedTranslucency = + typeof window === 'undefined' ? { intensity: TRANSLUCENCY_MIN, mode: 'clear' } : read() + +export const $translucency = atom(initial) export function setTranslucency(intensity: number): void { - $translucency.set(clamp(intensity)) + $translucency.set({ ...$translucency.get(), intensity: clampIntensity(intensity) }) } export function setTranslucencyMode(mode: TranslucencyMode): void { - $translucencyMode.set(mode === 'glass' && GLASS_SUPPORTED ? 'glass' : 'clear') + $translucency.set({ ...$translucency.get(), mode: mode === 'glass' && GLASS_SUPPORTED ? 'glass' : 'clear' }) } -// Glass thins surfaces only in real chat windows (primary + secondary session -// windows). The HUD, pet overlay, quick entry and wake indicator are -// transparent special-purpose windows that manage their own backgrounds — a -// page-surface rewrite there would fight them. -const isChatWindow = (): boolean => { - try { - const win = new URLSearchParams(window.location.search).get('win') +// Glass thins surfaces only in real chat windows (the primary window and +// secondary session windows). The HUD, pet overlay, quick entry and wake +// indicator are transparent special-purpose windows that manage their own +// backgrounds — a page-surface rewrite there would fight them. +const CHAT_WINDOW_KINDS = new Set([null, 'secondary']) - return win === null || win === 'secondary' +export const isChatWindow = (search = typeof window === 'undefined' ? '' : window.location.search): boolean => { + try { + return CHAT_WINDOW_KINDS.has(new URLSearchParams(search).get('win')) } catch { return false } } -/** - * Percent of the surface tint KEPT at a given intensity. Mirrors clear mode's - * opacity ramp (floor 0.3): at 100 the surfaces keep a 30% wash so the glass - * stays matte — tinted blur, not bare desktop. - */ -export const glassSurfaceKeep = (intensity: number): number => 100 - clamp(intensity) * 0.7 - -const applyGlassSurfaces = (intensity: number, mode: TranslucencyMode): void => { +const applyGlassSurfaces = ({ intensity, mode }: PersistedTranslucency): void => { if (typeof document === 'undefined') { return } const root = document.documentElement - const on = mode === 'glass' && intensity > 0 && GLASS_SUPPORTED && isChatWindow() + const glassOn = mode === 'glass' && intensity > 0 && GLASS_SUPPORTED && isChatWindow() // Clear mode fades the whole window uniformly, so overlay text and the // covered transcript blend; styles.css strengthens the overlay scrim while - // this attribute is present. Native opacity applies in every window kind, - // so no chat-window gate. + // this attribute is present. Native opacity applies in every window kind, so + // no chat-window gate. const clearOn = mode === 'clear' && intensity > 0 - if (on) { - root.setAttribute('data-hermes-glass', '') + root.toggleAttribute('data-hermes-glass', glassOn) + root.toggleAttribute('data-hermes-clear', clearOn) + + if (glassOn) { root.style.setProperty('--translucency-glass-keep', `${glassSurfaceKeep(intensity)}%`) } else { - root.removeAttribute('data-hermes-glass') root.style.removeProperty('--translucency-glass-keep') } - - if (clearOn) { - root.setAttribute('data-hermes-clear', '') - } else { - root.removeAttribute('data-hermes-clear') - } } if (typeof window !== 'undefined') { - const sync = () => { - const intensity = $translucency.get() - const mode = $translucencyMode.get() - - persistString(KEY, String(intensity)) - persistString(MODE_KEY, mode) - applyGlassSurfaces(intensity, mode) - window.hermesDesktop?.setTranslucency?.({ intensity, mode }) - } - - $translucency.subscribe(sync) - $translucencyMode.subscribe(sync) + $translucency.subscribe(state => { + writeJson(KEY, state) + applyGlassSurfaces(state) + window.hermesDesktop?.setTranslucency?.(state) + }) }