Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(theme): refresh plugin appearance after system changes

Linuxdazhao1db9f9b83c7b
diff --git a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
index f222bfaf5..850dcd377 100644
--- a/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
+++ b/apps/desktop/electron/main/bootstrap/app-lifecycle.ts
@@ -739,6 +739,14 @@ export function createApplicationLifecycle({
plugins.broadcastEvent("appearance:changed", [appearance]);
}
+ function refreshSystemAppearance(): void {
+ if (nativeTheme.themeSource !== "system") return;
+ appearanceState.pluginPanelTheme = nativeTheme.shouldUseDarkColors ? "dark" : "light";
+ broadcastAppearance();
+ }
+ nativeTheme.on("updated", refreshSystemAppearance);
+ app.once("will-quit", () => nativeTheme.removeListener("updated", refreshSystemAppearance));
+
function flushPendingApplicationMenuCommands() {
const commands = appState.pendingApplicationMenuCommands.splice(0);
void (async () => {
diff --git a/apps/desktop/test/browser-system-appearance.test.mjs b/apps/desktop/test/browser-system-appearance.test.mjs
new file mode 100644
index 000000000..0a012c374
--- /dev/null
+++ b/apps/desktop/test/browser-system-appearance.test.mjs
@@ -0,0 +1,75 @@
+import assert from "node:assert/strict";
+import { EventEmitter } from "node:events";
+import { readFileSync } from "node:fs";
+import test from "node:test";
+import { runInNewContext } from "node:vm";
+import ts from "typescript";
+
+function harness() {
+ const source = readFileSync(new URL("../electron/main/bootstrap/app-lifecycle.ts", import.meta.url), "utf8");
+ const ast = ts.createSourceFile("lifecycle.ts", source, ts.ScriptTarget.Latest, true);
+ const names = new Set(["applyNativeThemeSource", "applyAppThemePreference", "resolveAppearance", "broadcastAppearance", "refreshSystemAppearance"]);
+ const fragments = [];
+ function visit(node) {
+ if (ts.isFunctionDeclaration(node) && names.has(node.name?.text)) fragments.push(node.getText(ast));
+ if (ts.isExpressionStatement(node) && /^(nativeTheme\.on\("updated"|app\.once\("will-quit")/.test(node.getText(ast))) fragments.push(node.getText(ast));
+ ts.forEachChild(node, visit);
+ }
+ visit(ast);
+ const nativeTheme = new EventEmitter();
+ nativeTheme.themeSource = "system";
+ nativeTheme.systemDark = false;
+ Object.defineProperty(nativeTheme, "shouldUseDarkColors", { get: () => nativeTheme.themeSource === "dark" || (nativeTheme.themeSource === "system" && nativeTheme.systemDark) });
+ const app = new EventEmitter();
+ const state = { appThemePreference: "system", pluginPanelTheme: "light", updaterLocale: "en", broadcastAppearanceSignature: "" };
+ const elements = new Map();
+ const root = { dataset: {} };
+ const callbacks = new Map();
+ const html = readFileSync(new URL("../resources/plugins/pi.browser/views/browser.html", import.meta.url), "utf8");
+ runInNewContext(html.match(/<script>([\s\S]*?)<\/script>/)[1], {
+ document: { documentElement: root, getElementById(id) {
+ if (!elements.has(id)) elements.set(id, { addEventListener() {}, setAttribute() {}, getBoundingClientRect: () => ({ x: 0, y: 36, width: 400, height: 300 }) });
+ return elements.get(id);
+ } },
+ window: { pluginBridge: { on: (name, fn) => callbacks.set(name, fn), invoke: async () => null }, matchMedia: () => ({ matches: !nativeTheme.shouldUseDarkColors }), addEventListener() {} },
+ navigator: { language: "en" }, ResizeObserver: class { observe() {} },
+ });
+ const context = {
+ nativeTheme, app, appearanceState: state, appState: state,
+ isThemeColorScheme: value => value === "light" || value === "dark",
+ plugins: { getThemes: () => [], broadcastEvent() {} },
+ broadcastPluginPanelEvent: (name, value) => callbacks.get(name)?.(value),
+ process: { platform: "linux" }, windowState: {}, state: {}, logger: { app() {} },
+ };
+ const code = ts.transpileModule(fragments.join("\n") + "\nglobalThis.apply=applyAppThemePreference;", { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
+ runInNewContext(code, context);
+ return { nativeTheme, app, root, apply: context.apply, state };
+}
+
+test("open Browser chrome follows OS light/dark changes without another settings write", async () => {
+ const h = harness();
+ await new Promise(setImmediate);
+ h.apply("system");
+ assert.equal(h.root.dataset.base, "light");
+ for (const dark of [true, false]) {
+ h.nativeTheme.systemDark = dark;
+ h.nativeTheme.emit("updated");
+ assert.equal(h.root.dataset.base, dark ? "dark" : "light");
+ assert.equal(h.state.pluginPanelTheme, dark ? "dark" : "light");
+ }
+});
+
+test("OS appearance changes preserve an explicit app palette", async () => {
+ const h = harness();
+ await new Promise(setImmediate);
+ h.apply("dark");
+ h.nativeTheme.emit("updated");
+ assert.equal(h.root.dataset.base, "dark");
+});
+
+test("application shutdown removes its native appearance listener", () => {
+ const h = harness();
+ assert.equal(h.nativeTheme.listenerCount("updated"), 1);
+ h.app.emit("will-quit");
+ assert.equal(h.nativeTheme.listenerCount("updated"), 0);
+});
diff --git a/docs/spec/04-ux/06-settings-ia.md b/docs/spec/04-ux/06-settings-ia.md
index df17f708f..6deeba0be 100644
--- a/docs/spec/04-ux/06-settings-ia.md
+++ b/docs/spec/04-ux/06-settings-ia.md
@@ -141,7 +141,10 @@ D684.
settings control column, and shows the current name. The menu pins System,
Light, and Dark at the top, then lists plugin themes after a divider with a
"Provided by …" hint. Search matches labels, descriptions, ids, and plugin
- ids. Selection updates `settings.theme`.
+ ids. Selection updates `settings.theme`. With System selected, OS appearance
+ changes refresh the resolved plugin palette and broadcast it to open plugin
+ surfaces, including Browser chrome, without another settings write or restart.
+ Explicit light/dark and registered plugin palettes remain fixed.
- **Language**: a searchable picker row (not a card grid). The closed trigger
sizes to the current label, capped by the settings control column, and
shows the current native name, or Match system. The menu pins Auto at the
diff --git a/docs/spec/06-delivery/04-e2e-test-plan.md b/docs/spec/06-delivery/04-e2e-test-plan.md
index 3344c904c..0a60b0c46 100644
--- a/docs/spec/06-delivery/04-e2e-test-plan.md
+++ b/docs/spec/06-delivery/04-e2e-test-plan.md
@@ -3856,7 +3856,11 @@ identify the platform validation still needed.
2) In the Appearance card, open the Theme picker. Confirm System, Light, and
Dark are pinned at the top; select Dark and confirm the trigger shows Dark
and the UI switches to dark.
- 3) Select Light and confirm the UI switches to light.
+ 3) Select Light and confirm the UI switches to light. With a Browser tab open,
+ select System and change OS appearance light → dark → light. Confirm the
+ shell and Browser address/navigation toolbar update together without a
+ settings write or restart. Repeat with explicit Light and Dark: OS changes
+ must not override the selected app palette.
4) In the Language row, open the searchable picker. Confirm Auto is pinned
at the top with the detected native name and that English, 简体中文,
繁體中文, Türkçe, Deutsch, Español, Français, 한국어, and Português (Brasil)