The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): one owner for the translucency mapping
Brooklyn Nicholsond6adef6991c6
diff --git a/apps/desktop/electron/translucency.test.ts b/apps/desktop/electron/translucency.test.tsindex ef69b2a90ac..53f43f603cc 100644--- a/apps/desktop/electron/translucency.test.ts+++ b/apps/desktop/electron/translucency.test.ts@@ -1,31 +1,69 @@+/**+ * The translucency contract both processes read (`@hermes/shared/translucency`)+ * plus the one piece that needs a BrowserWindow to mean anything: which backing+ * a chat window is born with.+ *+ * The ramp assertions carry over from the clear-mode fix — its endpoints are+ * load-bearing, because a persisted intensity has to keep looking the same+ * across the upgrade that curved the middle of the lever.+ */+import { describe, expect, it } from 'vitest'-import { clampIntensity, glassActive, normalizeMode, normalizePayload, windowBackingOptions, windowOpacityFor } from './translucency'+import {+ clampIntensity,+ glassActive,+ glassSurfaceKeep,+ normalizeMode,+ normalizeState,+ TRANSLUCENCY_CURVE,+ TRANSLUCENCY_MAX,+ TRANSLUCENCY_MIN,+ TRANSLUCENCY_OPACITY_FLOOR,+ windowBackingOptions,+ windowOpacityFor+} from './translucency'++/** The linear ramp the curve replaced. Endpoints must still agree with it. */+const legacyOpacity = (intensity: number) => 1 - (intensity / 100) * 0.7++const clear = (intensity: number) => ({ intensity, mode: 'clear' as const })+const glass = (intensity: number) => ({ intensity, mode: 'glass' as const })++describe('lever bounds', () => {+ it('keeps the bounds and floor stable so persisted settings survive upgrades', () => {+ expect(TRANSLUCENCY_MIN).toBe(0)+ expect(TRANSLUCENCY_MAX).toBe(100)+ expect(TRANSLUCENCY_OPACITY_FLOOR).toBe(0.3)+ })+})describe('clampIntensity', () => {- it('clamps to 0-100 and rounds', () => {- expect(clampIntensity(-5)).toBe(0)+ it('clamps to the lever bounds and rounds to a whole percent', () => {+ expect(clampIntensity(-5)).toBe(TRANSLUCENCY_MIN)expect(clampIntensity(0)).toBe(0)expect(clampIntensity(49.6)).toBe(50)- expect(clampIntensity(100)).toBe(100)- expect(clampIntensity(250)).toBe(100)+ expect(clampIntensity(100)).toBe(TRANSLUCENCY_MAX)+ expect(clampIntensity(250)).toBe(TRANSLUCENCY_MAX)+ expect(clampIntensity('35')).toBe(35)})- it('treats junk as 0 (opaque)', () => {+ it('treats junk as 0 (opaque) rather than letting it reach setOpacity', () => {expect(clampIntensity(undefined)).toBe(0)+ expect(clampIntensity(null)).toBe(0)expect(clampIntensity(NaN)).toBe(0)+ expect(clampIntensity(Infinity)).toBe(0)expect(clampIntensity('glass')).toBe(0)- expect(clampIntensity(null)).toBe(0)})})describe('normalizeMode', () => {- it('accepts glass on macOS only', () => {+ it('accepts glass on macOS only — there is no vibrancy to ride elsewhere', () => {expect(normalizeMode('glass', true)).toBe('glass')expect(normalizeMode('glass', false)).toBe('clear')})- it('falls back to clear for legacy and junk values', () => {+ it('falls back to clear for absent, legacy and junk values', () => {expect(normalizeMode(undefined, true)).toBe('clear')expect(normalizeMode('clear', true)).toBe('clear')expect(normalizeMode('acrylic', true)).toBe('clear')@@ -34,53 +72,118 @@ describe('normalizeMode', () => {})describe('windowOpacityFor', () => {- it('keeps the legacy clear-mode ramp with its 0.3 floor', () => {- expect(windowOpacityFor(0, 'clear')).toBe(1)- expect(windowOpacityFor(50, 'clear')).toBeCloseTo(0.65)- expect(windowOpacityFor(100, 'clear')).toBeCloseTo(0.3)+ it('leaves both endpoints bit-identical to the linear ramp it replaced', () => {+ expect(windowOpacityFor(clear(TRANSLUCENCY_MIN))).toBe(legacyOpacity(0))+ expect(windowOpacityFor(clear(TRANSLUCENCY_MAX))).toBe(legacyOpacity(100))+ })++ it('is fully opaque at 0 and exactly the floor at 100', () => {+ expect(windowOpacityFor(clear(0))).toBe(1)+ expect(windowOpacityFor(clear(100))).toBe(1 - (1 - TRANSLUCENCY_OPACITY_FLOOR))+ })++ it('decreases monotonically and never sinks below the floor', () => {+ let previous = windowOpacityFor(clear(TRANSLUCENCY_MIN))++ for (let intensity = TRANSLUCENCY_MIN + 1; intensity <= TRANSLUCENCY_MAX; intensity += 1) {+ const opacity = windowOpacityFor(clear(intensity))++ expect(opacity, `${intensity} should be more see-through than ${intensity - 1}`).toBeLessThan(previous)+ expect(opacity, `${intensity} should stay at or above the floor`).toBeGreaterThanOrEqual(+ TRANSLUCENCY_OPACITY_FLOOR+ )++ previous = opacity+ }+ })++ // The bug the curve fixes: setOpacity fades text too, so anything under ~0.95+ // is where reading gets hard. A linear ramp put that boundary at intensity 7,+ // leaving almost the whole lever unusable.+ it('spends a useful stretch of the lever on readable settings, not its first few percent', () => {+ const readable = []++ for (let intensity = TRANSLUCENCY_MIN; intensity <= TRANSLUCENCY_MAX; intensity += 1) {+ if (windowOpacityFor(clear(intensity)) >= 0.95) {+ readable.push(intensity)+ }+ }++ expect(readable.length, `only ${readable.length} readable settings`).toBeGreaterThan(20)+ })++ it('keeps fine control near the opaque end', () => {+ // Linear gave 0.965 here — a visible jump off "off" on the very first step.+ expect(windowOpacityFor(clear(5))).toBeGreaterThan(0.99)+ expect(windowOpacityFor(clear(1))).toBeGreaterThan(legacyOpacity(1))+ expect(TRANSLUCENCY_CURVE).toBeGreaterThan(1)+ })++ it('clamps before mapping so corrupt state cannot escape the range', () => {+ expect(windowOpacityFor(clear(-40))).toBe(1)+ expect(windowOpacityFor(clear(240))).toBe(windowOpacityFor(clear(TRANSLUCENCY_MAX)))+ })++ it('never fades the native window in glass mode — the renderer paints that effect', () => {+ expect(windowOpacityFor(glass(0))).toBe(1)+ expect(windowOpacityFor(glass(60))).toBe(1)+ expect(windowOpacityFor(glass(100))).toBe(1)+ })+})++describe('glassSurfaceKeep', () => {+ it('mirrors the clear-mode ramp floor so glass stays matte, never bare desktop', () => {+ expect(glassSurfaceKeep(0)).toBe(100)+ expect(glassSurfaceKeep(50)).toBe(65)+ expect(glassSurfaceKeep(100)).toBeCloseTo(30)})- it('never fades the native window in glass mode', () => {- expect(windowOpacityFor(0, 'glass')).toBe(1)- expect(windowOpacityFor(60, 'glass')).toBe(1)- expect(windowOpacityFor(100, 'glass')).toBe(1)+ it('clamps its input like every other consumer of the lever', () => {+ expect(glassSurfaceKeep(-40)).toBe(100)+ expect(glassSurfaceKeep(240)).toBeCloseTo(30)})})-describe('normalizePayload', () => {+describe('normalizeState', () => {it('parses a modern payload', () => {- expect(normalizePayload({ intensity: 40, mode: 'glass' }, true)).toEqual({ intensity: 40, mode: 'glass' })+ expect(normalizeState({ intensity: 40, mode: 'glass' }, true)).toEqual({ intensity: 40, mode: 'glass' })})- it('parses a legacy intensity-only payload as clear', () => {- expect(normalizePayload({ intensity: 70 }, true)).toEqual({ intensity: 70, mode: 'clear' })+ // The migration contract: a pre-glass translucency.json is intensity-only and+ // always meant clear. It must NOT silently become glass on update.+ it('keeps a legacy intensity-only payload on clear', () => {+ expect(normalizeState({ intensity: 70 }, true)).toEqual({ intensity: 70, mode: 'clear' })})it('survives junk payloads', () => {- expect(normalizePayload(null, true)).toEqual({ intensity: 0, mode: 'clear' })- expect(normalizePayload('nope', true)).toEqual({ intensity: 0, mode: 'clear' })- expect(normalizePayload({ intensity: 'x', mode: 'glass' }, false)).toEqual({ intensity: 0, mode: 'clear' })+ expect(normalizeState(null, true)).toEqual({ intensity: 0, mode: 'clear' })+ expect(normalizeState('nope', true)).toEqual({ intensity: 0, mode: 'clear' })+ expect(normalizeState({ intensity: 'x', mode: 'glass' }, false)).toEqual({ intensity: 0, mode: 'clear' })})})describe('glassActive', () => {it('is on only for glass with nonzero intensity', () => {- expect(glassActive({ intensity: 60, mode: 'glass' })).toBe(true)- expect(glassActive({ intensity: 0, mode: 'glass' })).toBe(false)- expect(glassActive({ intensity: 60, mode: 'clear' })).toBe(false)+ expect(glassActive(glass(60))).toBe(true)+ expect(glassActive(glass(0))).toBe(false)+ expect(glassActive(clear(60))).toBe(false)})})describe('windowBackingOptions', () => {+ // The cold-launch bug: `backgroundColor: '#00000000'` on a non-transparent+ // window is silently treated as OPAQUE, so a window born while glass was+ // persisted blocked the vibrancy material no matter how clear the page went.+ // Omitting the key entirely is the only shape that works, and a runtime+ // setBackgroundColor fixup is lost in a fresh window's first seconds.it('omits backgroundColor entirely while glass is active', () => {- // Spreading a themed color (even alpha-0) would be silently treated as- // opaque on a non-transparent window and block the vibrancy material.- expect(windowBackingOptions({ intensity: 60, mode: 'glass' }, '#111111')).toEqual({})+ expect(windowBackingOptions(glass(60), '#111111')).toEqual({})+ expect('backgroundColor' in windowBackingOptions(glass(60), '#111111')).toBe(false)})- it('keeps the themed anti-flash backing otherwise', () => {- expect(windowBackingOptions({ intensity: 0, mode: 'glass' }, '#111111')).toEqual({ backgroundColor: '#111111' })- expect(windowBackingOptions({ intensity: 60, mode: 'clear' }, '#111111')).toEqual({ backgroundColor: '#111111' })- expect(windowBackingOptions({ intensity: 0, mode: 'clear' }, '#f7f7f7')).toEqual({ backgroundColor: '#f7f7f7' })+ it('keeps the themed anti-flash backing in every other state', () => {+ expect(windowBackingOptions(glass(0), '#111111')).toEqual({ backgroundColor: '#111111' })+ expect(windowBackingOptions(clear(60), '#111111')).toEqual({ backgroundColor: '#111111' })+ expect(windowBackingOptions(clear(0), '#f7f7f7')).toEqual({ backgroundColor: '#f7f7f7' })})})diff --git a/apps/desktop/electron/translucency.ts b/apps/desktop/electron/translucency.tsindex 017c3660780..59d486e456f 100644--- a/apps/desktop/electron/translucency.ts+++ b/apps/desktop/electron/translucency.ts@@ -1,64 +1,33 @@/**- * Window translucency mapping shared by main-process call sites.+ * Main-process side of window translucency.*- * Two modes ride one persisted lever (see src/store/translucency.ts):- * - 'clear': the 0-100 intensity maps to native window opacity — the whole- * window (text included) fades so the desktop shows through unblurred.- * - 'glass': the window stays fully opaque at the native level; the renderer- * thins its background surfaces instead so the macOS vibrancy material- * (already attached to every chat window) shows through as a matte blur- * while text keeps full contrast. macOS only — other platforms fall back- * to 'clear'.- */--export type TranslucencyMode = 'clear' | 'glass'--export function clampIntensity(value: unknown): number {- const n = Math.round(Number(value))-- return Number.isFinite(n) ? Math.min(100, Math.max(0, n)) : 0-}--/** Unknown or unsupported values fall back to 'clear' (the legacy behavior). */-export function normalizeMode(value: unknown, isMac: boolean): TranslucencyMode {- return value === 'glass' && isMac ? 'glass' : 'clear'-}--/**- * Native window opacity for a mode + intensity. Glass never fades the native- * window — the see-through effect is painted by the renderer over vibrancy.- * Clear keeps the historical ramp: floor at 0.3 so the most see-through- * setting is still usable rather than nearly invisible. 0 → fully opaque.+ * The mapping itself (modes, clamping, the clear-mode opacity ramp) lives in+ * apps/shared so the renderer and the main process cannot drift; this module+ * adds only what needs a BrowserWindow to mean anything.+ *+ * The import is relative rather than `@hermes/shared/translucency`: the+ * electron bundle is built by esbuild with no tsconfig path resolution (see+ * scripts/bundle-electron-main.mjs), so a bare specifier would typecheck and+ * then fail to bundle.*/-export function windowOpacityFor(intensity: number, mode: TranslucencyMode): number {- if (mode === 'glass') {- return 1- }-- return 1 - (clampIntensity(intensity) / 100) * 0.7-}-/** Parse a persisted translucency.json / IPC payload into a safe shape. */-export function normalizePayload(payload: unknown, isMac: boolean): { intensity: number; mode: TranslucencyMode } {- const record = payload && typeof payload === 'object' ? (payload as Record<string, unknown>) : {}-- return {- intensity: clampIntensity(record.intensity),- mode: normalizeMode(record.mode, isMac)- }-}--/**- * Whether glass is visually active. Decides the chat windows' webContents- * backing: Chromium composites the page against the window's backgroundColor- * BEFORE macOS composites the window, so an opaque backing (the normal- * anti-white-flash paint) blocks the vibrancy material even under a fully- * transparent page. Glass needs the backing gone; any other state keeps the- * opaque themed backing.- */-export function glassActive(state: { intensity: number; mode: TranslucencyMode }): boolean {- return state.mode === 'glass' && state.intensity > 0-}+import { glassActive, type TranslucencyState } from '../../shared/src/translucency'++export {+ clampIntensity,+ glassActive,+ glassSurfaceKeep,+ normalizeMode,+ normalizeState,+ TRANSLUCENCY_CURVE,+ TRANSLUCENCY_MAX,+ TRANSLUCENCY_MIN,+ TRANSLUCENCY_OPACITY_FLOOR,+ TRANSLUCENCY_STEP,+ type TranslucencyMode,+ type TranslucencyState,+ windowOpacityFor+} from '../../shared/src/translucency'/*** BrowserWindow constructor options for a chat window's backing, given the@@ -66,7 +35,7 @@ export function glassActive(state: { intensity: number; mode: TranslucencyMode }** Glass active → OMIT `backgroundColor` entirely: on a `vibrancy` window the* NSVisualEffectView then shows through a transparent page from the first- * frame. Passing an alpha color instead does NOT work — the docs only support+ * frame. Passing an alpha color instead does NOT work — Electron only supports* constructor alpha with `transparent: true`, and `#00000000` on a normal* window is quietly treated as opaque.*@@ -75,13 +44,10 @@ export function glassActive(state: { intensity: number; mode: TranslucencyMode }** A runtime `setBackgroundColor` swap (see applyWindowTranslucency in main)* only settles reliably on a window that has been compositing for a while —- * measured on macOS 26/Electron 40: swaps issued during roughly the first+ * measured on macOS 26 / Electron 40, swaps issued during roughly the first* seconds of a fresh process were lost, including from 'ready-to-show' and* 'did-finish-load' — so creation must not rely on a post-creation fixup.*/-export function windowBackingOptions(- state: { intensity: number; mode: TranslucencyMode },- themedColor: string-): { backgroundColor?: string } {+export function windowBackingOptions(state: TranslucencyState, themedColor: string): { backgroundColor?: string } {return glassActive(state) ? {} : { backgroundColor: themedColor }}diff --git a/apps/desktop/electron/window-opacity.test.ts b/apps/desktop/electron/window-opacity.test.tsdeleted file mode 100644index dd2f1ba3e56..00000000000--- a/apps/desktop/electron/window-opacity.test.ts+++ /dev/null@@ -1,93 +0,0 @@-/**- * Unit tests for the pure translucency helpers: the intensity -> opacity ramp- * the window options and the runtime IPC toggle share, its endpoints, and the- * clamping that keeps a corrupt translucency.json from reaching setOpacity.- */--import assert from 'node:assert/strict'--import { test } from 'vitest'--import {- clampIntensity,- TRANSLUCENCY_CURVE,- TRANSLUCENCY_MAX,- TRANSLUCENCY_MIN,- TRANSLUCENCY_OPACITY_FLOOR,- windowOpacity-} from './window-opacity'--/** The linear ramp this curve replaced. Endpoints must still agree with it. */-const legacyOpacity = (intensity: number) => 1 - (intensity / 100) * 0.7--test('lever bounds and floor stay stable so persisted settings survive upgrades', () => {- assert.equal(TRANSLUCENCY_MIN, 0)- assert.equal(TRANSLUCENCY_MAX, 100)- assert.equal(TRANSLUCENCY_OPACITY_FLOOR, 0.3)-})--test('endpoints are unchanged from the linear ramp', () => {- assert.equal(windowOpacity(TRANSLUCENCY_MIN), legacyOpacity(0))- assert.equal(windowOpacity(TRANSLUCENCY_MAX), legacyOpacity(100))-})--test('off is fully opaque and max is exactly the floor', () => {- assert.equal(windowOpacity(0), 1)- assert.equal(windowOpacity(100), 1 - (1 - TRANSLUCENCY_OPACITY_FLOOR))-})--test('opacity decreases monotonically and never sinks below the floor', () => {- let previous = windowOpacity(TRANSLUCENCY_MIN)-- for (let intensity = TRANSLUCENCY_MIN + 1; intensity <= TRANSLUCENCY_MAX; intensity += 1) {- const opacity = windowOpacity(intensity)-- assert.ok(opacity < previous, `expected ${intensity} to be more see-through than ${intensity - 1}`)- assert.ok(opacity >= TRANSLUCENCY_OPACITY_FLOOR, `expected ${intensity} to stay at or above the floor`)-- previous = opacity- }-})--// The bug this module fixes: setOpacity fades text too, so anything under ~0.95-// is where reading gets hard. A linear ramp put that boundary at intensity 7,-// leaving almost the whole lever unusable.-test('the readable band covers a useful stretch of the lever, not just its first few percent', () => {- const readable = []-- for (let intensity = TRANSLUCENCY_MIN; intensity <= TRANSLUCENCY_MAX; intensity += 1) {- if (windowOpacity(intensity) >= 0.95) {- readable.push(intensity)- }- }-- assert.ok(- readable.length > 20,- `expected more than 20 readable settings, got ${readable.length} (max ${readable[readable.length - 1]})`- )-})--test('a curved ramp keeps fine control near the opaque end', () => {- // Linear gave 0.965 here — a visible jump off "off" on the very first step.- assert.ok(windowOpacity(5) > 0.99, `expected 5% to stay near-opaque, got ${windowOpacity(5)}`)- assert.ok(windowOpacity(1) > legacyOpacity(1))- assert.ok(TRANSLUCENCY_CURVE > 1)-})--test('clampIntensity rejects garbage and enforces bounds', () => {- assert.equal(clampIntensity(NaN), TRANSLUCENCY_MIN)- assert.equal(clampIntensity(Infinity), TRANSLUCENCY_MIN)- assert.equal(clampIntensity(undefined), TRANSLUCENCY_MIN)- assert.equal(clampIntensity(null), TRANSLUCENCY_MIN)- assert.equal(clampIntensity('nope'), TRANSLUCENCY_MIN)- assert.equal(clampIntensity(-40), TRANSLUCENCY_MIN)- assert.equal(clampIntensity(240), TRANSLUCENCY_MAX)- assert.equal(clampIntensity('35'), 35)- assert.equal(clampIntensity(35.6), 36)-})--test('windowOpacity clamps before mapping so corrupt state cannot escape the range', () => {- assert.equal(windowOpacity(-40), 1)- assert.equal(windowOpacity(240), windowOpacity(TRANSLUCENCY_MAX))- assert.equal(windowOpacity('nope'), 1)-})diff --git a/apps/desktop/electron/window-opacity.ts b/apps/desktop/electron/window-opacity.tsdeleted file mode 100644index 3b87f4496c5..00000000000--- a/apps/desktop/electron/window-opacity.ts+++ /dev/null@@ -1,37 +0,0 @@-/**- * Pure helpers for window translucency. The main process owns- * BrowserWindow#setOpacity, so both window creation and the runtime IPC toggle- * funnel through this one mapping. Intensity is the user-facing unit- * (0 = opaque, 100 = most see-through); Electron's unit is window opacity.- *- * `setOpacity` fades the whole window, text included, so the band a user can- * actually read in sits just under opacity 1. A linear ramp spends that band in- * its first few percent and the rest of the lever on settings nobody can use.- * Curving the ramp keeps both endpoints exactly where they have always been and- * gives the readable band most of the travel instead.- */--export const TRANSLUCENCY_MIN = 0-export const TRANSLUCENCY_MAX = 100--/** Most see-through setting — floored so it stays usable, not near-invisible. */-export const TRANSLUCENCY_OPACITY_FLOOR = 0.3--/**- * Exponent for the intensity -> opacity ramp. 1 is the linear ramp this- * replaces. 2 holds the readable settings across roughly the first third of the- * lever while leaving windowOpacity(0) and windowOpacity(100) bit-identical.- */-export const TRANSLUCENCY_CURVE = 2--export function clampIntensity(value) {- const n = Math.round(Number(value))-- return Number.isFinite(n) ? Math.min(TRANSLUCENCY_MAX, Math.max(TRANSLUCENCY_MIN, n)) : TRANSLUCENCY_MIN-}--export function windowOpacity(intensity) {- const ratio = clampIntensity(intensity) / TRANSLUCENCY_MAX-- return 1 - (1 - TRANSLUCENCY_OPACITY_FLOOR) * Math.pow(ratio, TRANSLUCENCY_CURVE)-}diff --git a/apps/desktop/tsconfig.electron.json b/apps/desktop/tsconfig.electron.jsonindex 0d25682602b..3499d7a8e13 100644--- a/apps/desktop/tsconfig.electron.json+++ b/apps/desktop/tsconfig.electron.json@@ -13,8 +13,9 @@"ignoreDeprecations": "6.0","composite": true,"declaration": true,+ "rootDir": "..","outDir": "build/electron-types"},- "include": ["electron"],+ "include": ["electron", "../shared/src/translucency.ts"],"exclude": ["src"]}diff --git a/apps/desktop/tsconfig.json b/apps/desktop/tsconfig.jsonindex a00bad75fb0..96a133f2704 100644--- a/apps/desktop/tsconfig.json+++ b/apps/desktop/tsconfig.json@@ -19,10 +19,10 @@"@/*": ["./src/*"],"@hermes/plugin-sdk": ["./src/sdk/index.ts"],"@hermes/shared/billing": ["../shared/src/billing-types.ts"],+ "@hermes/shared/translucency": ["../shared/src/translucency.ts"],"@hermes/shared": ["../shared/src/index.ts"]}},"include": ["src", "../shared/src"],- "exclude": ["e2e", "electron", "playwright.config.ts"],- "references": [{ "path": "./tsconfig.electron.json" }]+ "exclude": ["e2e", "electron", "playwright.config.ts"]}diff --git a/apps/shared/src/index.ts b/apps/shared/src/index.tsindex 1cb4220e0db..bd4edf5fff3 100644--- a/apps/shared/src/index.ts+++ b/apps/shared/src/index.ts@@ -1,9 +1,4 @@-export {- backendScopeKey,- backendScopePrefix,- LOCAL_CONNECTION_ID,- registryBackendScopeKey-} from './backend-scope'+export { backendScopeKey, backendScopePrefix, LOCAL_CONNECTION_ID, registryBackendScopeKey } from './backend-scope'export {BILLING_REFUSAL_POLICY,type BillingRecovery,@@ -67,6 +62,21 @@ export {type SkinColors,type SkinColorToken} from './skin'+export {+ clampIntensity,+ glassActive,+ glassSurfaceKeep,+ normalizeMode,+ normalizeState,+ TRANSLUCENCY_CURVE,+ TRANSLUCENCY_MAX,+ TRANSLUCENCY_MIN,+ TRANSLUCENCY_OPACITY_FLOOR,+ TRANSLUCENCY_STEP,+ type TranslucencyMode,+ type TranslucencyState,+ windowOpacityFor+} from './translucency'export {buildHermesWebSocketUrl,type GatewayAuthMode,diff --git a/apps/shared/src/translucency.ts b/apps/shared/src/translucency.tsnew file mode 100644index 00000000000..0bc825dcd72--- /dev/null+++ b/apps/shared/src/translucency.ts@@ -0,0 +1,100 @@+/**+ * Window translucency — the one place the main process and the renderer agree+ * on what the setting means.+ *+ * One lever, 0–100 (0 = off, the default). Two modes decide HOW the desktop+ * shows through:+ *+ * - 'clear' — the main process maps the lever to native window opacity+ * (`setOpacity`), so the whole window fades, text included. macOS + Windows;+ * `setOpacity` is a no-op on Linux.+ * - 'glass' — macOS only. The window stays fully opaque at the native level and+ * the renderer thins its page surfaces instead, letting the vibrancy material+ * every chat window already carries read as a matte blur while text keeps+ * full contrast.+ *+ * The renderer owns the value and mirrors it to main over IPC; main persists it+ * so a cold launch can apply it at window creation, before the renderer reports+ * anything.+ */++export type TranslucencyMode = 'clear' | 'glass'++export interface TranslucencyState {+ intensity: number+ mode: TranslucencyMode+}++export const TRANSLUCENCY_MIN = 0+export const TRANSLUCENCY_MAX = 100++/** Renderer slider granularity. Main accepts any integer in range. */+export const TRANSLUCENCY_STEP = 1++/** Most see-through clear setting — floored so it stays usable, not invisible. */+export const TRANSLUCENCY_OPACITY_FLOOR = 0.3++/**+ * Exponent for the clear intensity → opacity ramp. 1 is a linear ramp, which+ * spends the whole readable band (opacity ≳ 0.95) in the first few percent of+ * the lever. 2 holds that band across roughly the first third while leaving+ * both endpoints bit-identical to the linear ramp.+ */+export const TRANSLUCENCY_CURVE = 2++export function clampIntensity(value: unknown): number {+ const n = Math.round(Number(value))++ return Number.isFinite(n) ? Math.min(TRANSLUCENCY_MAX, Math.max(TRANSLUCENCY_MIN, n)) : TRANSLUCENCY_MIN+}++/**+ * Glass rides on the macOS vibrancy material, so it is macOS-only and 'clear'+ * is the fallback everywhere else. Clear is also the default: a profile with no+ * mode recorded predates this setting and always behaved as clear, so it keeps+ * behaving as clear rather than silently changing look on update.+ */+export function normalizeMode(value: unknown, isMac: boolean): TranslucencyMode {+ return value === 'glass' && isMac ? 'glass' : 'clear'+}++/** Parse a persisted translucency.json / IPC payload into a safe state. */+export function normalizeState(payload: unknown, isMac: boolean): TranslucencyState {+ const record = payload && typeof payload === 'object' ? (payload as Record<string, unknown>) : {}++ return {+ intensity: clampIntensity(record.intensity),+ mode: normalizeMode(record.mode, isMac)+ }+}++/**+ * Native window opacity for a state. Glass never fades the native window — its+ * see-through effect is painted by the renderer over the vibrancy material.+ */+export function windowOpacityFor({ intensity, mode }: TranslucencyState): number {+ if (mode === 'glass') {+ return 1+ }++ const ratio = clampIntensity(intensity) / TRANSLUCENCY_MAX++ return 1 - (1 - TRANSLUCENCY_OPACITY_FLOOR) * Math.pow(ratio, TRANSLUCENCY_CURVE)+}++/**+ * Whether glass is visually active. Both processes branch on this: main to+ * decide a window's backing, the renderer to decide whether to thin surfaces.+ */+export function glassActive({ intensity, mode }: TranslucencyState): boolean {+ return mode === 'glass' && intensity > 0+}++/**+ * Percent of the surface tint the renderer KEEPS at a given intensity. Mirrors+ * clear mode's opacity floor: at 100 the field still holds a 30% wash, so glass+ * reads as tinted blur rather than bare desktop.+ */+export function glassSurfaceKeep(intensity: number): number {+ return TRANSLUCENCY_MAX - clampIntensity(intensity) * (1 - TRANSLUCENCY_OPACITY_FLOOR)+}