Repository navigation
Muse [ T3 Code ] feat(desktop): persist theme source and restore it at startup (#855) #25902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ffc73c4
8737bb0
43ac139
b3b9327
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -246,6 +246,14 @@ export const make = DesktopLifecycle.of({ | |||||||||||||||||||||||||||||
| const context = yield* Effect.context<DesktopLifecycleRegistrationServices>(); | ||||||||||||||||||||||||||||||
| const runEffect = Effect.runPromiseWith(context); | ||||||||||||||||||||||||||||||
| const quit: QuitState = { phase: "running", waitingSinceNanos: 0n }; | ||||||||||||||||||||||||||||||
| // Restore the persisted theme before the first window is created so the | ||||||||||||||||||||||||||||||
| // initial window background and title bar match the saved preference. | ||||||||||||||||||||||||||||||
| yield* electronTheme.setSource(yield* electronTheme.source).pipe( | ||||||||||||||||||||||||||||||
| Effect.catchCause((cause) => | ||||||||||||||||||||||||||||||
| logLifecycleError("failed to restore the persisted theme", { cause }), | ||||||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||||||
| Effect.withSpan("desktop.lifecycle.themeRestore"), | ||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High Malformed
Suggested change
🤖 Copy this AI Prompt to have your agent fix this: |
||||||||||||||||||||||||||||||
| yield* electronTheme.onUpdated(() => { | ||||||||||||||||||||||||||||||
| void runEffect( | ||||||||||||||||||||||||||||||
| desktopWindow.syncAppearance.pipe(Effect.withSpan("desktop.lifecycle.themeUpdated")), | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import { beforeEach, vi } from "vite-plus/test"; | ||
|
|
||
| const { nativeTheme, storeValues } = vi.hoisted(() => { | ||
| const listeners = new Map<string, Set<() => void>>(); | ||
| const nativeTheme = { | ||
| themeSource: "system", | ||
| shouldUseDarkColors: false, | ||
| on: (event: string, listener: () => void) => { | ||
| const set = listeners.get(event) ?? new Set<() => void>(); | ||
| set.add(listener); | ||
| listeners.set(event, set); | ||
| }, | ||
| removeListener: (event: string, listener: () => void) => { | ||
| listeners.get(event)?.delete(listener); | ||
| }, | ||
| emitUpdated: () => { | ||
| for (const listener of listeners.get("updated") ?? []) listener(); | ||
| }, | ||
| updatedListenerCount: () => listeners.get("updated")?.size ?? 0, | ||
| }; | ||
| return { nativeTheme, storeValues: new Map<string, unknown>() }; | ||
| }); | ||
|
|
||
| vi.mock("electron", () => ({ nativeTheme })); | ||
|
|
||
| vi.mock("electron-store", () => ({ | ||
| default: class FakeStore { | ||
| defaults: Record<string, unknown>; | ||
| constructor(options?: { defaults?: Record<string, unknown> }) { | ||
| this.defaults = options?.defaults ?? {}; | ||
| } | ||
| get(key: string) { | ||
| return storeValues.has(key) ? storeValues.get(key) : this.defaults[key]; | ||
| } | ||
| set(key: string, value: unknown) { | ||
| storeValues.set(key, value); | ||
| } | ||
| }, | ||
| })); | ||
|
|
||
| import * as ElectronTheme from "./ElectronTheme.ts"; | ||
|
|
||
| describe("ElectronTheme", () => { | ||
| beforeEach(() => { | ||
| storeValues.clear(); | ||
| nativeTheme.themeSource = "system"; | ||
| nativeTheme.shouldUseDarkColors = false; | ||
| }); | ||
|
|
||
| it.effect("defaults the persisted source to system", () => | ||
| Effect.gen(function* () { | ||
| const theme = yield* ElectronTheme.ElectronTheme; | ||
| assert.equal(yield* theme.source, "system"); | ||
| }).pipe(Effect.provide(ElectronTheme.layer)), | ||
| ); | ||
|
|
||
| it.effect("falls back to system for an unrecognized persisted value", () => | ||
| Effect.gen(function* () { | ||
| storeValues.set("theme", "neon"); | ||
| const theme = yield* ElectronTheme.ElectronTheme; | ||
| assert.equal(yield* theme.source, "system"); | ||
| }).pipe(Effect.provide(ElectronTheme.layer)), | ||
| ); | ||
|
|
||
| it.effect("setSource updates nativeTheme and persists the choice", () => | ||
| Effect.gen(function* () { | ||
| const theme = yield* ElectronTheme.ElectronTheme; | ||
| yield* theme.setSource("dark"); | ||
| assert.equal(nativeTheme.themeSource, "dark"); | ||
| assert.equal(storeValues.get("theme"), "dark"); | ||
| assert.equal(yield* theme.source, "dark"); | ||
| }).pipe(Effect.provide(ElectronTheme.layer)), | ||
| ); | ||
|
|
||
| it.effect("onUpdated forwards native theme updates until the scope closes", () => | ||
| Effect.gen(function* () { | ||
| const theme = yield* ElectronTheme.ElectronTheme; | ||
| let calls = 0; | ||
| yield* Effect.scoped( | ||
| Effect.gen(function* () { | ||
| yield* theme.onUpdated(() => { | ||
| calls += 1; | ||
| }); | ||
| nativeTheme.emitUpdated(); | ||
| assert.equal(calls, 1); | ||
| }), | ||
| ); | ||
| nativeTheme.emitUpdated(); | ||
| assert.equal(calls, 1); | ||
| assert.equal(nativeTheme.updatedListenerCount(), 0); | ||
| }).pipe(Effect.provide(ElectronTheme.layer)), | ||
| ); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.