diff --git a/packages/desktop/src/main/windows/defaults.ts b/packages/desktop/src/main/windows/defaults.ts index 7248ca5a53b..fd7d0379387 100644 --- a/packages/desktop/src/main/windows/defaults.ts +++ b/packages/desktop/src/main/windows/defaults.ts @@ -1,7 +1,7 @@ import { resolveThemeVariant } from "@opencode/ui/theme/resolve" import type { DesktopTheme } from "@opencode/ui/theme/types" import { desktopZoomLevels } from "@opencode/app/desktop-menu" -import { nativeTheme, type BrowserWindowConstructorOptions } from "electron" +import { nativeTheme, systemPreferences, type BrowserWindowConstructorOptions } from "electron" import { Predicate } from "effect" import oc2ThemeJson from "../../../../ui/src/theme/themes/oc-2.json" import { BACKGROUND_COLOR_KEY, ZOOM_LEVEL_KEY } from "../storage/keys" @@ -49,11 +49,20 @@ export function storedZoomLevel() { export function titlebarOverlay(mode: "light" | "dark" = tone(), zoom = 1) { return { color: "#00000000", - symbolColor: mode === "dark" ? "white" : "black", + symbolColor: captionSymbolColor(mode), height: Math.max(titlebarHeight, Math.round(titlebarHeight * zoom)), } } +// A Windows contrast theme repaints the page in system colours (`forced-colors`), whatever theme the app picked, so the +// caption buttons draw in the theme's window text colour too instead of the app theme's black or white. +function captionSymbolColor(mode: "light" | "dark") { + if (process.platform === "win32" && nativeTheme.shouldUseHighContrastColors) + return systemPreferences.getColor("window-text") + + return mode === "dark" ? "white" : "black" +} + // macOS and Windows hide the titlebar; Windows draws its window controls in an overlay sized to the zoom. export function platformFrame(mode: "light" | "dark", zoom: number) { const frame: BrowserWindowConstructorOptions = {} diff --git a/packages/desktop/src/main/windows/index.ts b/packages/desktop/src/main/windows/index.ts index e96afec7af9..5e622b0a8db 100644 --- a/packages/desktop/src/main/windows/index.ts +++ b/packages/desktop/src/main/windows/index.ts @@ -1,5 +1,5 @@ import { randomUUID } from "node:crypto" -import { app, BrowserWindow, screen } from "electron" +import { app, BrowserWindow, nativeTheme, screen } from "electron" import { Effect, FileSystem, Path } from "effect" import { openExternalURL } from "../files" import { scoped } from "../native/logging" @@ -123,6 +123,13 @@ export const mainWindows = Effect.fn("Window.make")(function* (input: { const runFork = Effect.runForkWith(yield* Effect.context()) const wireWindowRecovery = yield* windowRecovery + // Turning a Windows contrast theme on or off changes the caption buttons' colour (`titlebarOverlay`). + if (process.platform === "win32") { + const contrastChanged = () => getMainWindows().forEach(updateTitlebar) + nativeTheme.on("updated", contrastChanged) + yield* Effect.addFinalizer(() => Effect.sync(() => nativeTheme.off("updated", contrastChanged))) + } + // The entry module created and showed the first restored window on ready; it is adopted here, // before any renderer loads, so the user never waited for the layers to see a window. const restore = () =>