Skip to content
This repository was archived by the owner on Oct 21, 2026. It is now read-only.

Commit ecce4f1

Browse files
authored
feat(desktop): add configurable window translucency (#3)
1 parent 2ed6c80 commit ecce4f1

15 files changed

Lines changed: 568 additions & 8 deletions

File tree

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function makeDesktopWindowLayer(
9797
dispatchSnapShotEvent: () => Effect.void,
9898
zoomMain: () => Effect.void,
9999
syncAppearance: Effect.void,
100+
syncTranslucency: Effect.void,
100101
});
101102
}
102103

‎apps/desktop/src/backend/DesktopBackendPool.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function makePoolLayer(
100100
dispatchSnapShotEvent: () => Effect.void,
101101
zoomMain: () => Effect.die("unexpected zoom"),
102102
syncAppearance: Effect.void,
103+
syncTranslucency: Effect.void,
103104
} satisfies DesktopWindow.DesktopWindow["Service"]),
104105
),
105106
),
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { parseMacWindowNumber } from "./MacWindowBlur.ts";
4+
5+
describe("parseMacWindowNumber", () => {
6+
it("reads the CGWindowID from a window media source id", () => {
7+
expect(parseMacWindowNumber("window:4821:0")).toBe(4821);
8+
});
9+
10+
it.each(["window:0:0", "screen:1:0", ""])("rejects a source without a window: %s", (id) => {
11+
expect(parseMacWindowNumber(id)).toBeNull();
12+
});
13+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type * as Electron from "electron";
2+
3+
// macOS has no public API for blurring the desktop behind a transparent window.
4+
// Vibrancy materials blur too, but paint their own tint over it, which makes
5+
// low opacities look grey. This calls the private WindowServer function that
6+
// MonoCode and other translucent apps use, so the only tint is the app's own.
7+
8+
type SetBlurRadius = (windowNumber: number, radius: number) => void;
9+
10+
let setBlurRadiusPromise: Promise<SetBlurRadius> | undefined;
11+
12+
function loadSetBlurRadius(): Promise<SetBlurRadius> {
13+
setBlurRadiusPromise ??= import("ffi-rs").then(({ DataType, load, open }) => {
14+
const coreGraphics = "t3-core-graphics";
15+
open({
16+
library: coreGraphics,
17+
path: "/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics",
18+
});
19+
const connection = load({
20+
library: coreGraphics,
21+
funcName: "CGSMainConnectionID",
22+
retType: DataType.I32,
23+
paramsType: [],
24+
paramsValue: [],
25+
});
26+
return (windowNumber, radius) => {
27+
load({
28+
library: coreGraphics,
29+
funcName: "CGSSetWindowBackgroundBlurRadius",
30+
retType: DataType.I32,
31+
paramsType: [DataType.I32, DataType.I32, DataType.I32],
32+
paramsValue: [connection, windowNumber, radius],
33+
});
34+
};
35+
});
36+
return setBlurRadiusPromise;
37+
}
38+
39+
// The media source id is `window:<CGWindowID>:0`, and the CGWindowID is the
40+
// NSWindow's window number.
41+
export function parseMacWindowNumber(mediaSourceId: string): number | null {
42+
const match = /^window:(\d+):/.exec(mediaSourceId);
43+
const windowNumber = match ? Number(match[1]) : 0;
44+
return windowNumber > 0 ? windowNumber : null;
45+
}
46+
47+
export async function setMacWindowBlurRadius(
48+
window: Electron.BrowserWindow,
49+
radius: number,
50+
): Promise<void> {
51+
if (window.isDestroyed()) return;
52+
const windowNumber = parseMacWindowNumber(window.getMediaSourceId());
53+
if (windowNumber === null) return;
54+
const setBlurRadius = await loadSetBlurRadius();
55+
if (window.isDestroyed()) return;
56+
setBlurRadius(windowNumber, radius);
57+
}

‎apps/desktop/src/ipc/methods/clientSettings.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Schema from "effect/Schema";
55

66
import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
77
import * as DesktopSnapShot from "../../snapShot/DesktopSnapShot.ts";
8+
import * as DesktopWindow from "../../window/DesktopWindow.ts";
89
import * as IpcChannels from "../channels.ts";
910
import * as DesktopIpc from "../DesktopIpc.ts";
1011

@@ -25,7 +26,9 @@ export const setClientSettings = DesktopIpc.makeIpcMethod({
2526
handler: Effect.fn("desktop.ipc.clientSettings.set")(function* (settings) {
2627
const clientSettings = yield* DesktopClientSettings.DesktopClientSettings;
2728
const snapShot = yield* DesktopSnapShot.DesktopSnapShot;
29+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
2830
yield* clientSettings.set(settings);
2931
yield* snapShot.configure(settings);
32+
yield* desktopWindow.syncTranslucency;
3033
}),
3134
});

‎apps/desktop/src/window/DesktopApplicationMenu.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const makeDesktopWindowLayer = (selectedAction: Deferred.Deferred<string>) =>
8989
zoomMain: (direction) =>
9090
Deferred.succeed(selectedAction, `zoom-${direction}`).pipe(Effect.asVoid),
9191
syncAppearance: Effect.void,
92+
syncTranslucency: Effect.void,
9293
} satisfies DesktopWindow.DesktopWindow["Service"]);
9394

9495
const makeElectronMenuLayer = (

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { getDesktopUrl } from "../electron/ElectronProtocol.ts";
1818
import * as ElectronShell from "../electron/ElectronShell.ts";
1919
import * as ElectronTheme from "../electron/ElectronTheme.ts";
2020
import * as ElectronWindow from "../electron/ElectronWindow.ts";
21+
import { setMacWindowBlurRadius } from "../electron/MacWindowBlur.ts";
2122
import {
2223
MENU_ACTION_CHANNEL,
2324
QUIT_SHORTCUT_CHANNEL,
@@ -136,6 +137,10 @@ export class DesktopWindow extends Context.Service<
136137
// the main window.
137138
readonly zoomMain: (direction: MainWindowZoomDirection) => Effect.Effect<void>;
138139
readonly syncAppearance: Effect.Effect<void>;
140+
// Re-applies the saved window translucency to the main window. macOS and
141+
// Linux can only turn it on or off when the window opens, so there only the
142+
// macOS blur radius changes live.
143+
readonly syncTranslucency: Effect.Effect<void>;
139144
}
140145
>()("@t3tools/desktop/window/DesktopWindow") {}
141146

@@ -158,6 +163,32 @@ function getInitialWindowBackgroundColor(shouldUseDarkColors: boolean): string {
158163
return shouldUseDarkColors ? "#0a0a0a" : "#ffffff";
159164
}
160165

166+
// Not fully clear: macOS draws a gap at the corners of a fully clear window
167+
// with a shadow.
168+
const TRANSPARENT_WINDOW_BACKGROUND_COLOR = "#03000000";
169+
170+
function getMainWindowBackgroundColor(shouldUseDarkColors: boolean, translucent: boolean): string {
171+
return translucent
172+
? TRANSPARENT_WINDOW_BACKGROUND_COLOR
173+
: getInitialWindowBackgroundColor(shouldUseDarkColors);
174+
}
175+
176+
// Only Windows can switch translucency on an open window. macOS and Linux
177+
// windows get an alpha channel only when they are created.
178+
function appliesTranslucencyLive(platform: NodeJS.Platform): boolean {
179+
return platform === "win32";
180+
}
181+
182+
function applyWindowsTranslucency(
183+
window: Electron.BrowserWindow,
184+
translucent: boolean,
185+
shouldUseDarkColors: boolean,
186+
): void {
187+
if (window.isDestroyed()) return;
188+
window.setBackgroundColor(getMainWindowBackgroundColor(shouldUseDarkColors, translucent));
189+
window.setBackgroundMaterial(translucent ? "acrylic" : "auto");
190+
}
191+
161192
type DisplayBounds = Pick<Electron.Rectangle, "x" | "y" | "width" | "height">;
162193

163194
function windowFitsWithinDisplay(
@@ -279,13 +310,14 @@ function syncWindowAppearance(
279310
window: Electron.BrowserWindow,
280311
shouldUseDarkColors: boolean,
281312
platform: NodeJS.Platform,
313+
translucent: boolean,
282314
): Effect.Effect<void> {
283315
return Effect.sync(() => {
284316
if (window.isDestroyed()) {
285317
return;
286318
}
287319

288-
window.setBackgroundColor(getInitialWindowBackgroundColor(shouldUseDarkColors));
320+
window.setBackgroundColor(getMainWindowBackgroundColor(shouldUseDarkColors, translucent));
289321
const { titleBarOverlay } = getWindowTitleBarOptions(shouldUseDarkColors, platform);
290322
if (typeof titleBarOverlay === "object") {
291323
window.setTitleBarOverlay(titleBarOverlay);
@@ -362,6 +394,38 @@ export const make = Effect.gen(function* () {
362394
const currentMainWindow = electronWindow.currentMainOrFirst.pipe(Effect.flatMap(withoutSplash));
363395
const focusedMainWindow = electronWindow.focusedMainOrFirst.pipe(Effect.flatMap(withoutSplash));
364396

397+
const readWindowTranslucency = clientSettings.get.pipe(
398+
Effect.map((settings) => {
399+
const { windowTranslucency, windowTranslucencyBlur } = Option.getOrElse(
400+
settings,
401+
() => DEFAULT_CLIENT_SETTINGS,
402+
);
403+
return { translucent: windowTranslucency, blurRadius: windowTranslucencyBlur };
404+
}),
405+
Effect.catch((error) =>
406+
logWindowWarning("failed to read window translucency; keeping the window opaque", {
407+
message: error.message,
408+
}).pipe(
409+
Effect.as({
410+
translucent: false,
411+
blurRadius: DEFAULT_CLIENT_SETTINGS.windowTranslucencyBlur,
412+
}),
413+
),
414+
),
415+
);
416+
// Whether the open main window was created translucent. Only Windows can
417+
// change that later.
418+
let mainWindowCreatedTranslucent = false;
419+
const resolveMainWindowTranslucency = Effect.map(readWindowTranslucency, ({ translucent }) =>
420+
appliesTranslucencyLive(environment.platform) ? translucent : mainWindowCreatedTranslucent,
421+
);
422+
const applyMacBlurRadius = (window: Electron.BrowserWindow, radius: number) =>
423+
Effect.tryPromise(() => setMacWindowBlurRadius(window, radius)).pipe(
424+
Effect.catch((error) =>
425+
logWindowWarning("failed to set the window blur radius", { message: error.message }),
426+
),
427+
);
428+
365429
const createWindow = Effect.fn("desktop.window.createWindow")(function* (): Effect.fn.Return<
366430
Electron.BrowserWindow,
367431
DesktopWindowError
@@ -371,6 +435,7 @@ export const make = Effect.gen(function* () {
371435
const iconPaths = yield* assets.iconPaths;
372436
const iconOption = getIconOption(iconPaths, environment.platform);
373437
const shouldUseDarkColors = yield* electronTheme.shouldUseDarkColors;
438+
const { translucent, blurRadius } = yield* readWindowTranslucency;
374439
const persistedSettings = yield* desktopSettings.get;
375440
const persistedBounds = persistedSettings.mainWindowBounds;
376441
const displayBoundsResult = yield* Effect.sync(() => {
@@ -401,7 +466,10 @@ export const make = Effect.gen(function* () {
401466
show: false,
402467
autoHideMenuBar: true,
403468
...(environment.platform === "darwin" ? { disableAutoHideCursor: true } : {}),
404-
backgroundColor: getInitialWindowBackgroundColor(shouldUseDarkColors),
469+
...(translucent && !appliesTranslucencyLive(environment.platform)
470+
? { transparent: true }
471+
: {}),
472+
backgroundColor: getMainWindowBackgroundColor(shouldUseDarkColors, translucent),
405473
...iconOption,
406474
title: environment.displayName,
407475
...getWindowTitleBarOptions(shouldUseDarkColors, environment.platform),
@@ -423,6 +491,21 @@ export const make = Effect.gen(function* () {
423491
if (environment.platform === "darwin") {
424492
window.setAutoHideCursor(false);
425493
}
494+
mainWindowCreatedTranslucent = translucent;
495+
if (translucent && environment.platform === "win32") {
496+
applyWindowsTranslucency(window, true, shouldUseDarkColors);
497+
} else if (translucent && environment.platform === "darwin") {
498+
// WindowServer can drop the blur while the window is hidden, and the
499+
// window number it targets is only certain once the window is on screen.
500+
window.on("show", () => {
501+
void runPromise(
502+
Effect.flatMap(readWindowTranslucency, (settings) =>
503+
applyMacBlurRadius(window, settings.blurRadius),
504+
),
505+
);
506+
});
507+
yield* applyMacBlurRadius(window, blurRadius);
508+
}
426509
let boundsPersistFiber: Fiber.Fiber<void, never> | undefined;
427510
let pendingBoundsPersistFiber: Fiber.Fiber<void, never> | undefined;
428511
let boundsPersistenceEnabled = persistedBounds === null || restoredPersistedBounds;
@@ -1023,10 +1106,28 @@ export const make = Effect.gen(function* () {
10231106
}),
10241107
syncAppearance: Effect.gen(function* () {
10251108
const shouldUseDarkColors = yield* electronTheme.shouldUseDarkColors;
1109+
const mainWindow = yield* currentMainWindow;
1110+
const mainWindowTranslucent = yield* resolveMainWindowTranslucency;
10261111
yield* electronWindow.syncAllAppearance((window) =>
1027-
syncWindowAppearance(window, shouldUseDarkColors, environment.platform),
1112+
syncWindowAppearance(
1113+
window,
1114+
shouldUseDarkColors,
1115+
environment.platform,
1116+
Option.contains(mainWindow, window) && mainWindowTranslucent,
1117+
),
10281118
);
10291119
}).pipe(Effect.withSpan("desktop.window.syncAppearance")),
1120+
syncTranslucency: Effect.gen(function* () {
1121+
const mainWindow = yield* currentMainWindow;
1122+
if (Option.isNone(mainWindow)) return;
1123+
const { translucent, blurRadius } = yield* readWindowTranslucency;
1124+
if (environment.platform === "win32") {
1125+
const shouldUseDarkColors = yield* electronTheme.shouldUseDarkColors;
1126+
applyWindowsTranslucency(mainWindow.value, translucent, shouldUseDarkColors);
1127+
} else if (environment.platform === "darwin" && mainWindowCreatedTranslucent) {
1128+
yield* applyMacBlurRadius(mainWindow.value, blurRadius);
1129+
}
1130+
}).pipe(Effect.withSpan("desktop.window.syncTranslucency")),
10301131
});
10311132
});
10321133

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import {
169169
useSettingsSearchTargetId,
170170
} from "./settingsLayout";
171171
import { searchableSetting } from "./settingsSearch";
172+
import { WindowTranslucencySection } from "./WindowTranslucencySettings";
172173
import { ProjectFavicon } from "../ProjectFavicon";
173174
import { PanelAnimationsPreview } from "./PanelAnimationsPreview";
174175

@@ -528,7 +529,15 @@ export function useSettingsRestore(onRestored?: () => void) {
528529
...(settings.appearanceContrast !== DEFAULT_UNIFIED_SETTINGS.appearanceContrast
529530
? ["Contrast"]
530531
: []),
531-
...(settings.glassOpacity !== DEFAULT_UNIFIED_SETTINGS.glassOpacity ? ["Glass opacity"] : []),
532+
...(settings.glassOpacity !== DEFAULT_UNIFIED_SETTINGS.glassOpacity
533+
? ["Menu & dialog opacity"]
534+
: []),
535+
...(settings.windowTranslucency !== DEFAULT_UNIFIED_SETTINGS.windowTranslucency ||
536+
settings.windowTranslucencyOpacity !== DEFAULT_UNIFIED_SETTINGS.windowTranslucencyOpacity ||
537+
settings.windowTranslucencyMainPane !== DEFAULT_UNIFIED_SETTINGS.windowTranslucencyMainPane ||
538+
settings.windowTranslucencyBlur !== DEFAULT_UNIFIED_SETTINGS.windowTranslucencyBlur
539+
? ["Window translucency"]
540+
: []),
532541
...(settings.diffColorScheme !== DEFAULT_UNIFIED_SETTINGS.diffColorScheme
533542
? ["Diff colors"]
534543
: []),
@@ -671,6 +680,10 @@ export function useSettingsRestore(onRestored?: () => void) {
671680
settings.fontSizePrompt,
672681
settings.fontSizeTerminal,
673682
settings.glassOpacity,
683+
settings.windowTranslucency,
684+
settings.windowTranslucencyOpacity,
685+
settings.windowTranslucencyMainPane,
686+
settings.windowTranslucencyBlur,
674687
settings.panelAnimationDurationMs,
675688
settings.responseStreamingMode,
676689
settings.enableProviderUpdateChecks,
@@ -772,6 +785,10 @@ export function useSettingsRestore(onRestored?: () => void) {
772785
contextWindowMeterEnabled: DEFAULT_UNIFIED_SETTINGS.contextWindowMeterEnabled,
773786
environmentIdentificationMode: DEFAULT_UNIFIED_SETTINGS.environmentIdentificationMode,
774787
glassOpacity: DEFAULT_UNIFIED_SETTINGS.glassOpacity,
788+
windowTranslucency: DEFAULT_UNIFIED_SETTINGS.windowTranslucency,
789+
windowTranslucencyOpacity: DEFAULT_UNIFIED_SETTINGS.windowTranslucencyOpacity,
790+
windowTranslucencyMainPane: DEFAULT_UNIFIED_SETTINGS.windowTranslucencyMainPane,
791+
windowTranslucencyBlur: DEFAULT_UNIFIED_SETTINGS.windowTranslucencyBlur,
775792
panelAnimationDurationMs: DEFAULT_UNIFIED_SETTINGS.panelAnimationDurationMs,
776793
sidebarThreadPreviewCount: DEFAULT_UNIFIED_SETTINGS.sidebarThreadPreviewCount,
777794
sidebarProjectGroupingMode: DEFAULT_UNIFIED_SETTINGS.sidebarProjectGroupingMode,
@@ -1257,7 +1274,7 @@ export function AppearanceSettingsPanel() {
12571274
resetAction={
12581275
settings.glassOpacity !== DEFAULT_UNIFIED_SETTINGS.glassOpacity ? (
12591276
<SettingResetButton
1260-
label="glass opacity"
1277+
label="menu and dialog opacity"
12611278
onClick={() =>
12621279
updateSettings({ glassOpacity: DEFAULT_UNIFIED_SETTINGS.glassOpacity })
12631280
}
@@ -1273,7 +1290,7 @@ export function AppearanceSettingsPanel() {
12731290
{settings.glassOpacity}%
12741291
</output>
12751292
<input
1276-
aria-label="Glass opacity"
1293+
aria-label="Menu and dialog opacity"
12771294
className="settings-slider min-w-0 flex-1"
12781295
id="glass-opacity"
12791296
max={MAX_GLASS_OPACITY}
@@ -1390,6 +1407,8 @@ export function AppearanceSettingsPanel() {
13901407
/>
13911408
</SettingsSection>
13921409

1410+
{isElectron ? <WindowTranslucencySection /> : null}
1411+
13931412
<SettingsSection id="motion" title="Motion">
13941413
<SettingsRow
13951414
{...searchableSetting("panel-animations")}

0 commit comments

Comments
 (0)