appearance.service.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { app, nativeTheme, type BrowserWindow, type BrowserWindowConstructorOptions } from 'electron'
  2. import { isTranslucent, launchAppearanceKey, wantsBlur, type Appearance } from '@shared/appearance'
  3. import { getAppearance } from './config.service'
  4. import { detectDesktopEnv } from '../utils/platform'
  5. import { relaunchApp } from '../utils/relaunch'
  6. type Koffi = typeof import('koffi')
  7. // transparency and X11/Wayland are fixed at window creation
  8. let launchAppearance: Appearance | null = null
  9. let mainWindow: BrowserWindow | null = null
  10. function onX11(): boolean {
  11. const ozone = app.commandLine.getSwitchValue('ozone-platform')
  12. if (ozone) return ozone === 'x11'
  13. return !process.env.WAYLAND_DISPLAY || process.env.ELECTRON_OZONE_PLATFORM_HINT === 'x11'
  14. }
  15. /** Not in a Flatpak on Wayland, fallback-x11 hides the X socket */
  16. function xwaylandReachable(): boolean {
  17. return !!process.env.DISPLAY
  18. }
  19. /**
  20. * Before app ready: KWin only blurs X11 windows, so Wayland goes through XWayland. The platform is picked before any
  21. * JS runs and an appended switch only reaches child processes, so the app relaunches with it.
  22. */
  23. export function prepareAppearance(): void {
  24. launchAppearance = getAppearance()
  25. if (wantsBlur(launchAppearance) && detectDesktopEnv() === 'kde' && !onX11() && xwaylandReachable()) {
  26. relaunchApp([...process.argv.slice(1), '--ozone-platform=x11'], 'exit')
  27. }
  28. }
  29. export function getLaunchAppearance(): Appearance {
  30. return launchAppearance ?? getAppearance()
  31. }
  32. export function isAppearanceRestartNeeded(): boolean {
  33. const current = getAppearance()
  34. const launch = getLaunchAppearance()
  35. if (isTranslucent(current) !== isTranslucent(launch)) return true
  36. if (onX11() || !xwaylandReachable()) return false
  37. return launchAppearanceKey(current) !== launchAppearanceKey(launch)
  38. }
  39. function darkBackground(theme: Appearance['theme']): boolean {
  40. return theme === 'dark' || (theme === 'system' && nativeTheme.shouldUseDarkColors)
  41. }
  42. export function windowAppearanceOptions(): Pick<BrowserWindowConstructorOptions, 'transparent' | 'backgroundColor'> {
  43. const appearance = getLaunchAppearance()
  44. nativeTheme.themeSource = appearance.theme
  45. if (isTranslucent(appearance)) return { transparent: true, backgroundColor: '#00000000' }
  46. return { backgroundColor: darkBackground(appearance.theme) ? '#0e0f12' : '#f3f4f7' }
  47. }
  48. export function attachAppearance(win: BrowserWindow): void {
  49. mainWindow = win
  50. win.once('ready-to-show', () => { updateBlur(getAppearance()) })
  51. win.on('show', () => { updateBlur(getAppearance()) })
  52. }
  53. export function applyLiveAppearance(appearance: Appearance): void {
  54. nativeTheme.themeSource = appearance.theme
  55. updateBlur(appearance)
  56. }
  57. let x11: {
  58. display: unknown
  59. blurAtom: bigint | number
  60. change: (...args: unknown[]) => number
  61. remove: (...args: unknown[]) => number
  62. flush: (display: unknown) => number
  63. } | null = null
  64. function getX11(): typeof x11 {
  65. if (x11) return x11
  66. // eslint-disable-next-line @typescript-eslint/no-require-imports
  67. const koffi: Koffi = require('koffi')
  68. const lib = koffi.load('libX11.so.6')
  69. const openDisplay = lib.func('void *XOpenDisplay(const char *name)')
  70. const internAtom = lib.func('unsigned long XInternAtom(void *display, const char *name, int onlyIfExists)')
  71. const display = openDisplay(null)
  72. if (!display) throw new Error('XOpenDisplay failed')
  73. x11 = {
  74. display,
  75. blurAtom: internAtom(display, '_KDE_NET_WM_BLUR_BEHIND_REGION', 0),
  76. change: lib.func(
  77. 'int XChangeProperty(void *display, unsigned long w, unsigned long property, unsigned long type, int format, int mode, const uint8_t *data, int nelements)'
  78. ),
  79. remove: lib.func('int XDeleteProperty(void *display, unsigned long w, unsigned long property)'),
  80. flush: lib.func('int XFlush(void *display)')
  81. }
  82. return x11
  83. }
  84. const XA_CARDINAL = 6
  85. const PROP_MODE_REPLACE = 0
  86. function nativeWindowId(win: BrowserWindow): bigint {
  87. const handle = win.getNativeWindowHandle()
  88. return handle.length >= 8 ? handle.readBigUInt64LE(0) : BigInt(handle.readUInt32LE(0))
  89. }
  90. // an empty _KDE_NET_WM_BLUR_BEHIND_REGION blurs the whole window
  91. function updateBlur(appearance: Appearance): void {
  92. const win = mainWindow
  93. if (!win || win.isDestroyed() || !onX11() || !isTranslucent(getLaunchAppearance())) return
  94. try {
  95. const lib = getX11()!
  96. const id = nativeWindowId(win)
  97. if (id === 0n) return
  98. if (wantsBlur(appearance)) {
  99. lib.change(lib.display, id, lib.blurAtom, XA_CARDINAL, 32, PROP_MODE_REPLACE, Buffer.alloc(8), 0)
  100. } else {
  101. lib.remove(lib.display, id, lib.blurAtom)
  102. }
  103. lib.flush(lib.display)
  104. } catch (err) {
  105. console.warn('[Appearance] Could not set the KDE blur property:', (err as Error)?.message ?? err)
  106. }
  107. }