From e18ec754a05231fb82a7cbdb80632d79a6232872 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 19 Aug 2024 14:52:56 +0800 Subject: [PATCH] fix: electron window frame and `backgroundMaterial` Signed-off-by: Innei --- src/main/window.ts | 52 +++++++++++++----- .../src/components/ui/background/vibrancy.tsx | 55 +++++++++++++------ src/renderer/src/constants/ui.ts | 2 +- src/renderer/src/main.tsx | 10 ++-- src/renderer/src/modules/app/Titlebar.tsx | 13 +++-- .../src/modules/feed-column/index.tsx | 6 +- src/renderer/src/pages/settings/layout.tsx | 6 +- 7 files changed, 93 insertions(+), 51 deletions(-) diff --git a/src/main/window.ts b/src/main/window.ts index 42a92022b..0154f7791 100644 --- a/src/main/window.ts +++ b/src/main/window.ts @@ -31,35 +31,59 @@ export function createWindow( } & BrowserWindowConstructorOptions, ) { const { extraPath, height, width, ...configs } = options - // Create the browser window. - const window = new BrowserWindow({ + + const baseWindowConfig: Electron.BrowserWindowConstructorOptions = { width, height, show: false, resizable: configs?.resizable ?? true, autoHideMenuBar: true, - ...(platform !== "darwin" ? { icon: getIconPath() } : {}), webPreferences: { preload: path.join(__dirname, "../preload/index.mjs"), sandbox: false, webviewTag: true, }, + } - // @windows - backgroundMaterial: "mica", - titleBarStyle: platform === "win32" ? "hidden" : "hiddenInset", - trafficLightPosition: { - x: 18, - y: 18, - }, + switch (platform) { + case "darwin": { + Object.assign(baseWindowConfig, { + titleBarStyle: "hiddenInset", + trafficLightPosition: { + x: 18, + y: 18, + }, + vibrancy: "sidebar", + visualEffectState: "active", + transparent: true, + } as Electron.BrowserWindowConstructorOptions) + break + } - transparent: true, - backgroundColor: "#00000000", // transparent hexadecimal or anything with transparency, - vibrancy: "sidebar", - visualEffectState: "active", + case "win32": { + Object.assign(baseWindowConfig, { + icon: getIconPath(), + backgroundMaterial: "mica", + titleBarStyle: "hidden", + // titleBarOverlay: { + // height: 30, + // }, + } as Electron.BrowserWindowConstructorOptions) + break + } + + default: { + baseWindowConfig.icon = getIconPath() + } + } + + // Create the browser window. + const window = new BrowserWindow({ + ...baseWindowConfig, ...configs, }) + window.webContents.openDevTools() function refreshBound(timeout = 0) { setTimeout(() => { const mainWindow = getMainWindow() diff --git a/src/renderer/src/components/ui/background/vibrancy.tsx b/src/renderer/src/components/ui/background/vibrancy.tsx index d639b0bff..21f25c131 100644 --- a/src/renderer/src/components/ui/background/vibrancy.tsx +++ b/src/renderer/src/components/ui/background/vibrancy.tsx @@ -1,25 +1,44 @@ import { useUISettingKey } from "@renderer/atoms/settings/ui" import { cn } from "@renderer/lib/utils" -export const Vibrancy: Component< +type Props = Component< React.DetailedHTMLProps, HTMLDivElement> -> = ({ className, children, ...rest }) => { +> +const MacOSVibrancy: Props = ({ className, children, ...rest }) => ( +
+ {children} +
+) + +const Noop: Props = ({ children, className, ...rest }) => ( +
+ {children} +
+) + +const Win32Material: Props = ({ className, children, ...rest }) => ( +
+ {children} +
+) +export const WindowUnderBlur: Props = (props) => { const opaqueSidebar = useUISettingKey("opaqueSidebar") - const canVibrancy = - window.electron && - window.electron.process.platform === "darwin" && - !opaqueSidebar + if (opaqueSidebar) { + return + } - return ( -
- {children} -
- ) + if (!window.electron) { + return + } + switch (window.electron.process.platform) { + case "darwin": { + return + } + case "win32": { + return + } + default: { + return + } + } } diff --git a/src/renderer/src/constants/ui.ts b/src/renderer/src/constants/ui.ts index 9598edfa2..62793b760 100644 --- a/src/renderer/src/constants/ui.ts +++ b/src/renderer/src/constants/ui.ts @@ -1,2 +1,2 @@ export const ElECTRON_CUSTOM_TITLEBAR_HEIGHT = 30 -export const ELECTRON_WINDOWS_RADIUS = 12 +// export const ELECTRON_WINDOWS_RADIUS = 12 diff --git a/src/renderer/src/main.tsx b/src/renderer/src/main.tsx index 9e5378d60..becb32cdf 100644 --- a/src/renderer/src/main.tsx +++ b/src/renderer/src/main.tsx @@ -6,7 +6,9 @@ import ReactDOM from "react-dom/client" import { RouterProvider } from "react-router-dom" import { setAppIsReady } from "./atoms/app" -import { ElECTRON_CUSTOM_TITLEBAR_HEIGHT, ELECTRON_WINDOWS_RADIUS } from "./constants" +import { + ElECTRON_CUSTOM_TITLEBAR_HEIGHT, +} from "./constants" import { initializeApp } from "./initialize" import { getOS } from "./lib/utils" import { router } from "./router" @@ -18,11 +20,7 @@ await initializeApp().finally(() => { const $container = document.querySelector("#root") as HTMLElement if (window.electron && getOS() === "Windows") { - $container.style.borderRadius = `${ELECTRON_WINDOWS_RADIUS}px` - $container.style.overflow = "hidden" - $container.style.background = "var(--fo-background)" - - document.body.style.cssText += `--fo-window-padding-top: ${ElECTRON_CUSTOM_TITLEBAR_HEIGHT}px; --fo-window-radius: ${ELECTRON_WINDOWS_RADIUS}px;` + document.body.style.cssText += `--fo-window-padding-top: ${ElECTRON_CUSTOM_TITLEBAR_HEIGHT}px;` } ReactDOM.createRoot($container).render( diff --git a/src/renderer/src/modules/app/Titlebar.tsx b/src/renderer/src/modules/app/Titlebar.tsx index 2a5a3ab8e..6ec989c3e 100644 --- a/src/renderer/src/modules/app/Titlebar.tsx +++ b/src/renderer/src/modules/app/Titlebar.tsx @@ -1,4 +1,4 @@ -import { ElECTRON_CUSTOM_TITLEBAR_HEIGHT, ELECTRON_WINDOWS_RADIUS } from "@renderer/constants" +import { ElECTRON_CUSTOM_TITLEBAR_HEIGHT } from "@renderer/constants" import { tipcClient } from "@renderer/lib/client" import { useQuery } from "@tanstack/react-query" @@ -12,10 +12,7 @@ export const Titlebar = () => {
{children} - + ) } diff --git a/src/renderer/src/pages/settings/layout.tsx b/src/renderer/src/pages/settings/layout.tsx index b2fd1a57c..892da599b 100644 --- a/src/renderer/src/pages/settings/layout.tsx +++ b/src/renderer/src/pages/settings/layout.tsx @@ -1,5 +1,5 @@ import { Logo } from "@renderer/components/icons/logo" -import { Vibrancy } from "@renderer/components/ui/background" +import { WindowUnderBlur } from "@renderer/components/ui/background" import { isElectronBuild } from "@renderer/constants" import { preventDefault } from "@renderer/lib/dom" import { settings } from "@renderer/modules/settings/constants" @@ -13,7 +13,7 @@ function Layout() { return (
- +
{settings.map((t) => ( {APP_NAME}
-
+