The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): curve window translucency so the whole lever is usable
Nicky Molina374779dae8d4
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 06a1eb91811..ad14397dd14 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -318,6 +318,7 @@ import {import { fetchMarketplaceThemes, searchMarketplaceThemes } from './vscode-marketplace'import { createWakeIndicatorWindowController } from './wake-indicator-window'import { readWindowBelow } from './window-below'+import { clampIntensity, windowOpacity } from './window-opacity'import { installWindowRendererLifecycle } from './window-renderer-lifecycle'import { createWindowRevealController } from './window-reveal'import {@@ -874,15 +875,9 @@ nativeTheme.themeSource = readPersistedThemeSource()// default). Mapped to the native window opacity so the desktop shows through// the whole window. Persisted so a cold launch applies it at window creation,// before the renderer reports its value. macOS + Windows only; `setOpacity` is-// a no-op on Linux. See store/translucency.+// a no-op on Linux. See store/translucency, and window-opacity for the ramp.const TRANSLUCENCY_CONFIG_PATH = path.join(app.getPath('userData'), 'translucency.json')-function clampIntensity(value) {- const n = Math.round(Number(value))-- return Number.isFinite(n) ? Math.min(100, Math.max(0, n)) : 0-}-function readPersistedTranslucency() {try {return clampIntensity(JSON.parse(fs.readFileSync(TRANSLUCENCY_CONFIG_PATH, 'utf8')).intensity)@@ -902,12 +897,6 @@ function writePersistedTranslucency(intensity) {let translucencyIntensity = readPersistedTranslucency()-// Map the 0–100 lever to a window opacity. Floor at 0.3 so the most see-through-// setting is still usable rather than nearly invisible. 0 → fully opaque.-function windowOpacity() {- return 1 - (translucencyIntensity / 100) * 0.7-}-// Re-apply translucency to a live window (runtime toggle, no recreation).// `setOpacity` is a no-op on Linux, which is fine — it just stays opaque there.function applyWindowTranslucency(win) {@@ -916,7 +905,7 @@ function applyWindowTranslucency(win) {}try {- win.setOpacity(windowOpacity())+ win.setOpacity(windowOpacity(translucencyIntensity))} catch (error) {rememberLog(`[translucency] apply failed: ${error.message}`)}@@ -10646,7 +10635,7 @@ function spawnSecondaryWindow({ sessionId, watch }: { sessionId?: string; watch?titleBarOverlay: getTitleBarOverlayOptions(),trafficLightPosition: IS_MAC ? WINDOW_BUTTON_POSITION : undefined,vibrancy: IS_MAC ? 'sidebar' : undefined,- opacity: windowOpacity(),+ opacity: windowOpacity(translucencyIntensity),icon,// Don't show until the renderer's first themed paint is ready. macOS// `vibrancy` ignores `backgroundColor` and paints a translucent OS@@ -10746,7 +10735,7 @@ function createInstanceWindow() {titleBarOverlay: getTitleBarOverlayOptions(),trafficLightPosition: IS_MAC ? WINDOW_BUTTON_POSITION : undefined,vibrancy: IS_MAC ? 'sidebar' : undefined,- opacity: windowOpacity(),+ opacity: windowOpacity(translucencyIntensity),icon,show: false,backgroundColor: getWindowBackgroundColor(),@@ -11607,7 +11596,7 @@ function createWindow() {titleBarOverlay: getTitleBarOverlayOptions(),trafficLightPosition: IS_MAC ? WINDOW_BUTTON_POSITION : undefined,vibrancy: IS_MAC ? 'sidebar' : undefined,- opacity: windowOpacity(),+ opacity: windowOpacity(translucencyIntensity),icon,// Hidden until the first themed paint so macOS `vibrancy` (which ignores// `backgroundColor` and follows the OS appearance) can't flash a lightdiff --git a/apps/desktop/electron/window-opacity.test.ts b/apps/desktop/electron/window-opacity.test.tsnew file mode 100644index 00000000000..dd2f1ba3e56--- /dev/null+++ b/apps/desktop/electron/window-opacity.test.ts@@ -0,0 +1,93 @@+/**+ * 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.tsnew file mode 100644index 00000000000..3b87f4496c5--- /dev/null+++ b/apps/desktop/electron/window-opacity.ts@@ -0,0 +1,37 @@+/**+ * 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)+}