Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/tui/src/context/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
upsertTheme,
type ThemeJson,
} from "../theme"
import { win32SystemTheme } from "../terminal-win32"
import { createEffect, createMemo, onCleanup, onMount } from "solid-js"
import { createStore, produce } from "solid-js/store"
import { createSimpleContext } from "./helper"
Expand Down Expand Up @@ -162,7 +163,15 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
if (store.active === "system") setStore("active", "opencode")
return
}
const next = store.lock ?? terminalMode(colors) ?? mode
// On Windows Terminal the buffer background (OSC 11) is independent
// of the system dark/light mode. The title bar follows the system
// theme but the background colour does not change. Use the Windows
// registry value as the authoritative source when available.
let next = store.lock ?? terminalMode(colors) ?? mode
if (process.platform === "win32") {
const winMode = win32SystemTheme()
if (winMode) next = winMode
}
if (store.mode !== next) setStore("mode", next)
const signature = JSON.stringify(colors)
hasResolvedSystemTheme = true
Expand Down Expand Up @@ -245,12 +254,26 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
let unsubscribeRefresh: (() => void) | undefined
unsubscribeRefresh = themes.subscribeRefresh?.(refresh)

let winThemeWatcher: ReturnType<typeof setInterval> | undefined
if (process.platform === "win32") {
let lastWinTheme = win32SystemTheme()
winThemeWatcher = setInterval(() => {
const current = win32SystemTheme()
if (current && current !== lastWinTheme) {
lastWinTheme = current
if (!store.lock) apply(current)
}
}, 3000)
winThemeWatcher.unref()
}

onCleanup(() => {
renderer.off(CliRenderEvents.THEME_MODE, handle)
renderer.removeInputHandler(handleThemeNotification)
unsubscribeRefresh?.()
for (const timeout of themeRefreshTimeouts) clearTimeout(timeout)
themeRefreshTimeouts.length = 0
if (winThemeWatcher) clearInterval(winThemeWatcher)
})

const values = createMemo(() => {
Expand Down
27 changes: 27 additions & 0 deletions packages/tui/src/terminal-win32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,30 @@ export function win32InstallCtrlCGuard() {

return unhook
}

/**
* Detect Windows system dark/light mode from the registry.
*
* Reads `AppsUseLightTheme` from:
* HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize
*
* Returns `"dark"` when the value is `0`, `"light"` when `1`, or `undefined`
* if the query fails (registry key absent on older Windows, non-Windows platform).
*/
export function win32SystemTheme(): "dark" | "light" | undefined {
if (process.platform !== "win32") return undefined
if (typeof Bun === "undefined") return undefined
try {
const match = (
Bun.spawnSync([
"reg", "query",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
"/v", "AppsUseLightTheme",
], { encoding: "utf8" }).stdout?.toString() ?? ""
).match(/0x([0-9a-f]+)/i)
if (match) return parseInt(match[1], 16) === 0 ? "dark" : "light"
} catch {
// Registry key may not exist on Windows 8 or earlier.
}
return undefined
}
19 changes: 19 additions & 0 deletions packages/tui/test/win32-theme.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect, test } from "bun:test"
import { win32SystemTheme } from "../src/terminal-win32"

test("win32SystemTheme returns a valid dark/light value on Windows, undefined elsewhere", () => {
const result = win32SystemTheme()
if (process.platform === "win32") {
// On Windows the registry may or may not be readable; both outcomes are valid.
// If readable, it must be "dark" or "light".
expect(["dark", "light", undefined]).toContain(result)
} else {
expect(result).toBeUndefined()
}
})

test("win32SystemTheme is idempotent - second call returns same value as first", () => {
const a = win32SystemTheme()
const b = win32SystemTheme()
expect(a).toBe(b)
})
Loading