|
| 1 | +import type { BrowserWindow, View } from 'electron' |
| 2 | + |
| 3 | +import { |
| 4 | + WINDOW_BACKGROUND_BLUR_MAX_RADIUS, |
| 5 | + WINDOW_BACKGROUND_BLUR_MIN_RADIUS, |
| 6 | +} from '@/shared/settings' |
| 7 | + |
| 8 | +/** |
| 9 | + * Opaque launch color matching the app's dark background token. |
| 10 | + * Exported so `BrowserWindow` construction and post-create blur updates |
| 11 | + * use the same fallback color. |
| 12 | + */ |
| 13 | +export const MAIN_WINDOW_OPAQUE_BACKGROUND = 'rgb(10, 15, 28)' |
| 14 | + |
| 15 | +/** |
| 16 | + * Alpha mirrors the renderer's `bg-background/85` class so Chromium and the |
| 17 | + * native Electron contentView expose the same glass strength. |
| 18 | + */ |
| 19 | +export const MAIN_WINDOW_BLURRED_BACKGROUND = 'rgba(10, 15, 28, 0.85)' |
| 20 | + |
| 21 | +type BackgroundBlurCapableView = View & { |
| 22 | + setBackgroundBlur?: (blurRadius: number) => void |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Clamp a persisted blur radius before it touches Electron APIs. |
| 27 | + * @param blurRadius - User setting from `settings.json` or IPC. |
| 28 | + * @returns Whole-pixel radius inside the app-supported range. |
| 29 | + * @example |
| 30 | + * normalizeWindowBackgroundBlurRadius(99) // => 48 |
| 31 | + */ |
| 32 | +export function normalizeWindowBackgroundBlurRadius( |
| 33 | + blurRadius: number, |
| 34 | +): number { |
| 35 | + return Math.min( |
| 36 | + WINDOW_BACKGROUND_BLUR_MAX_RADIUS, |
| 37 | + Math.max(WINDOW_BACKGROUND_BLUR_MIN_RADIUS, Math.trunc(blurRadius)), |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Pick the window backplate color required by Electron's blur renderer. |
| 43 | + * @param blurRadius - Normalized or raw blur radius. |
| 44 | + * @returns Opaque color when blur is off; alpha color when blur is on. |
| 45 | + * @example |
| 46 | + * getMainWindowBackgroundColor(12) // => 'rgba(10, 15, 28, 0.82)' |
| 47 | + */ |
| 48 | +export function getMainWindowBackgroundColor(blurRadius: number): string { |
| 49 | + const normalizedRadius = normalizeWindowBackgroundBlurRadius(blurRadius) |
| 50 | + // Electron only shows `setBackgroundBlur` through an alpha background. |
| 51 | + if (normalizedRadius > 0) { |
| 52 | + return MAIN_WINDOW_BLURRED_BACKGROUND |
| 53 | + } |
| 54 | + return MAIN_WINDOW_OPAQUE_BACKGROUND |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Apply the persisted Appearance blur setting to the live main window. |
| 59 | + * @param window - Main BrowserWindow instance. |
| 60 | + * @param blurRadius - Requested Electron 42 blur radius in CSS pixels. |
| 61 | + * @example |
| 62 | + * applyWindowBackgroundBlur(mainWindow, settings.windowBackgroundBlurRadius) |
| 63 | + */ |
| 64 | +export function applyWindowBackgroundBlur( |
| 65 | + window: BrowserWindow, |
| 66 | + blurRadius: number, |
| 67 | +): void { |
| 68 | + const normalizedRadius = normalizeWindowBackgroundBlurRadius(blurRadius) |
| 69 | + const backgroundColor = getMainWindowBackgroundColor(normalizedRadius) |
| 70 | + |
| 71 | + window.setBackgroundColor(backgroundColor) |
| 72 | + |
| 73 | + const contentView = window.contentView as BackgroundBlurCapableView |
| 74 | + if (typeof contentView.setBackgroundColor === 'function') { |
| 75 | + contentView.setBackgroundColor(backgroundColor) |
| 76 | + } |
| 77 | + |
| 78 | + // Electron 42 exposes this at runtime, but its TypeScript declarations may |
| 79 | + // lag behind. Guarding keeps older local Electron builds from crashing. |
| 80 | + if (typeof contentView.setBackgroundBlur !== 'function') return |
| 81 | + |
| 82 | + contentView.setBackgroundBlur(normalizedRadius) |
| 83 | +} |
0 commit comments