@@ -18,6 +18,7 @@ import { getDesktopUrl } from "../electron/ElectronProtocol.ts";
1818import * as ElectronShell from "../electron/ElectronShell.ts" ;
1919import * as ElectronTheme from "../electron/ElectronTheme.ts" ;
2020import * as ElectronWindow from "../electron/ElectronWindow.ts" ;
21+ import { setMacWindowBlurRadius } from "../electron/MacWindowBlur.ts" ;
2122import {
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+
161192type DisplayBounds = Pick < Electron . Rectangle , "x" | "y" | "width" | "height" > ;
162193
163194function 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
0 commit comments