@opencode/ui 2.2.4 → 2.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/dist/actions/button/button.d.ts +2 -2
  2. package/dist/actions/icon-button/icon-button.d.ts +1 -1
  3. package/dist/components/animated-number-machine.d.ts +95 -0
  4. package/dist/components/file-icon-machine.d.ts +40 -0
  5. package/dist/components/file-icon.d.ts +0 -2
  6. package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
  7. package/dist/components/image-flight-machine.d.ts +135 -0
  8. package/dist/components/image-zoom-machine.d.ts +95 -0
  9. package/dist/components/image-zoom.d.ts +6 -3
  10. package/dist/components/list.d.ts +2 -6
  11. package/dist/components/menu-scroll.d.ts +12 -0
  12. package/dist/components/popover-machine.d.ts +96 -0
  13. package/dist/components/popover.d.ts +2 -2
  14. package/dist/components/resize-handle-machine.d.ts +71 -0
  15. package/dist/components/resize-handle.d.ts +2 -0
  16. package/dist/components/scroll-view-machine.d.ts +68 -0
  17. package/dist/components/scroll-view.d.ts +2 -2
  18. package/dist/components/spinner.d.ts +11 -3
  19. package/dist/components/text-field-machine.d.ts +62 -0
  20. package/dist/components/text-reveal-machine.d.ts +90 -0
  21. package/dist/components/typewriter-machine.d.ts +62 -0
  22. package/dist/context/dialog-machine.d.ts +125 -0
  23. package/dist/context/dialog.d.ts +17 -16
  24. package/dist/data-display/line-comment/line-comment.d.ts +9 -0
  25. package/dist/data-display/line-comment/machine.d.ts +157 -0
  26. package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
  27. package/dist/data-display/project-avatar/animation.d.ts +1 -1
  28. package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
  29. package/dist/feedback/toast/machine.d.ts +57 -0
  30. package/dist/feedback/toast/toast.d.ts +1 -1
  31. package/dist/forms/checkbox/checkbox.d.ts +6 -1
  32. package/dist/forms/select/machine.d.ts +45 -0
  33. package/dist/forms/select/select.d.ts +4 -0
  34. package/dist/forms/text-input/text-input.d.ts +4 -2
  35. package/dist/forms/textarea/textarea.d.ts +1 -0
  36. package/dist/forms/typeahead/machine.d.ts +85 -0
  37. package/dist/hooks/auto-scroll-machine.d.ts +89 -0
  38. package/dist/hooks/create-auto-scroll.d.ts +1 -1
  39. package/dist/hooks/list-machine.d.ts +150 -0
  40. package/dist/hooks/use-filtered-list.d.ts +21 -35
  41. package/dist/i18n/en.d.ts +5 -0
  42. package/dist/icons/catalog.d.ts +8 -0
  43. package/dist/navigation/menu/machine.d.ts +49 -0
  44. package/dist/navigation/menu/menu.d.ts +5 -3
  45. package/dist/navigation/segmented-control/machine.d.ts +46 -0
  46. package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
  47. package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
  48. package/dist/overlays/tooltip/machine.d.ts +146 -0
  49. package/dist/theme/context.d.ts +38 -14
  50. package/dist/theme/machine.d.ts +174 -0
  51. package/dist/typography/text-shimmer/machine.d.ts +48 -0
  52. package/package.json +19 -3
  53. package/src/actions/button/button.tsx +11 -6
  54. package/src/actions/icon-button/icon-button.tsx +17 -4
  55. package/src/components/animated-number-machine.ts +214 -0
  56. package/src/components/animated-number.tsx +134 -83
  57. package/src/components/file-icon-machine.ts +78 -0
  58. package/src/components/file-icon.tsx +96 -579
  59. package/src/components/file-icons/choose-icon-name.ts +574 -0
  60. package/src/components/image-flight-machine.ts +247 -0
  61. package/src/components/image-preview.css +5 -2
  62. package/src/components/image-preview.tsx +390 -252
  63. package/src/components/image-zoom-machine.ts +191 -0
  64. package/src/components/image-zoom.ts +22 -14
  65. package/src/components/list.tsx +46 -137
  66. package/src/components/menu-scroll.css +70 -0
  67. package/src/components/menu-scroll.tsx +161 -0
  68. package/src/components/popover-machine.ts +198 -0
  69. package/src/components/popover.tsx +157 -89
  70. package/src/components/resize-handle-machine.ts +174 -0
  71. package/src/components/resize-handle.tsx +116 -67
  72. package/src/components/scroll-view-machine.ts +244 -0
  73. package/src/components/scroll-view.tsx +163 -116
  74. package/src/components/spinner.css +18 -5
  75. package/src/components/spinner.tsx +28 -14
  76. package/src/components/text-field-machine.ts +102 -0
  77. package/src/components/text-field.tsx +78 -24
  78. package/src/components/text-reveal-machine.ts +184 -0
  79. package/src/components/text-reveal.tsx +96 -93
  80. package/src/components/typewriter-machine.ts +134 -0
  81. package/src/components/typewriter.tsx +50 -39
  82. package/src/context/dialog-machine.ts +280 -0
  83. package/src/context/dialog.tsx +168 -177
  84. package/src/data-display/line-comment/line-comment.tsx +196 -132
  85. package/src/data-display/line-comment/machine.ts +391 -0
  86. package/src/data-display/project-avatar/animation-machine.ts +182 -0
  87. package/src/data-display/project-avatar/animation.ts +67 -38
  88. package/src/feedback/activity-ring/activity-ring.tsx +1 -1
  89. package/src/feedback/toast/machine.ts +115 -0
  90. package/src/feedback/toast/toast.tsx +55 -59
  91. package/src/forms/checkbox/checkbox-compact.css +169 -0
  92. package/src/forms/checkbox/checkbox.tsx +93 -31
  93. package/src/forms/search-list/search-list.css +5 -1
  94. package/src/forms/search-list/search-list.tsx +4 -4
  95. package/src/forms/select/machine.ts +81 -0
  96. package/src/forms/select/select.css +59 -17
  97. package/src/forms/select/select.tsx +67 -31
  98. package/src/forms/text-input/text-input.css +30 -16
  99. package/src/forms/text-input/text-input.tsx +6 -2
  100. package/src/forms/textarea/textarea.css +13 -8
  101. package/src/forms/textarea/textarea.tsx +3 -1
  102. package/src/forms/typeahead/machine.ts +253 -0
  103. package/src/forms/typeahead/typeahead.tsx +75 -66
  104. package/src/hooks/auto-scroll-machine.ts +261 -0
  105. package/src/hooks/create-auto-scroll.tsx +110 -205
  106. package/src/hooks/list-machine.ts +359 -0
  107. package/src/hooks/use-filtered-list.tsx +101 -112
  108. package/src/i18n/en.ts +6 -1
  109. package/src/icons/catalog.ts +8 -0
  110. package/src/navigation/menu/machine.ts +109 -0
  111. package/src/navigation/menu/menu.tsx +130 -22
  112. package/src/navigation/segmented-control/machine.ts +72 -0
  113. package/src/navigation/segmented-control/segmented-control.tsx +46 -46
  114. package/src/overlays/dialog/dialog.tsx +7 -1
  115. package/src/overlays/mcp-sign-in/machine.ts +343 -0
  116. package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
  117. package/src/overlays/tooltip/machine.ts +303 -0
  118. package/src/overlays/tooltip/tooltip.tsx +157 -92
  119. package/src/styles/theme.css +10 -2
  120. package/src/styles/tokens/theme.css +13 -0
  121. package/src/theme/context.tsx +192 -269
  122. package/src/theme/machine.ts +508 -0
  123. package/src/theme/themes/oc-2.json +2 -0
  124. package/src/theme/v2/mapping.ts +12 -8
  125. package/src/typography/text-shimmer/machine.ts +90 -0
  126. package/src/typography/text-shimmer/text-shimmer.css +71 -39
  127. package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
@@ -1,15 +1,29 @@
1
1
  // @refresh reload
2
2
 
3
- import { createEffect, onMount } from "solid-js"
4
- import { createStore } from "solid-js/store"
3
+ import { createMemo, createSignal, onCleanup, onMount, untrack } from "solid-js"
5
4
  import { makeEventListener } from "@solid-primitives/event-listener"
6
5
  import { createSimpleContext } from "../context/helper"
7
6
  import oc2ThemeJson from "./themes/oc-2.json"
8
7
  import { resolveThemeVariant, themeToCss } from "./resolve"
9
8
  import { resolveThemeVariantV2, themeV2ToCss } from "./v2/resolve"
10
9
  import type { DesktopTheme } from "./types"
11
-
12
- export type ColorScheme = "light" | "dark" | "system"
10
+ import {
11
+ createThemeState,
12
+ DEFAULT_THEME_ID,
13
+ parseColorScheme,
14
+ selectColorScheme,
15
+ selectMode,
16
+ selectThemeId,
17
+ selectThemesLoading,
18
+ transitionTheme,
19
+ type ColorScheme,
20
+ type ThemeEffect,
21
+ type ThemeEvent,
22
+ type ThemeMachineState,
23
+ type ThemeTransition,
24
+ } from "./machine"
25
+
26
+ export type { ColorScheme } from "./machine"
13
27
 
14
28
  const STORAGE_KEYS = {
15
29
  THEME_ID: "opencode-theme-id",
@@ -49,56 +63,53 @@ function knownThemes() {
49
63
  return known
50
64
  }
51
65
 
52
- const names: Record<string, string> = {
53
- "oc-2": "OpenCode",
54
- amoled: "AMOLED",
55
- aura: "Aura",
56
- ayu: "Ayu",
57
- carbonfox: "Carbonfox",
58
- catppuccin: "Catppuccin",
59
- "catppuccin-frappe": "Catppuccin Frappe",
60
- "catppuccin-macchiato": "Catppuccin Macchiato",
61
- cobalt2: "Cobalt2",
62
- cursor: "Cursor",
63
- dracula: "Dracula",
64
- everforest: "Everforest",
65
- flexoki: "Flexoki",
66
- github: "GitHub",
67
- gruvbox: "Gruvbox",
68
- kanagawa: "Kanagawa",
69
- "lucent-orng": "Lucent Orng",
70
- material: "Material",
71
- matrix: "Matrix",
72
- mercury: "Mercury",
73
- monokai: "Monokai",
74
- nightowl: "Night Owl",
75
- nord: "Nord",
76
- "one-dark": "One Dark",
77
- onedarkpro: "One Dark Pro",
78
- orng: "Orng",
79
- "osaka-jade": "Osaka Jade",
80
- palenight: "Palenight",
81
- rosepine: "Rose Pine",
82
- shadesofpurple: "Shades of Purple",
83
- solarized: "Solarized",
84
- synthwave84: "Synthwave '84",
85
- tokyonight: "Tokyonight",
86
- vercel: "Vercel",
87
- vesper: "Vesper",
88
- zenburn: "Zenburn",
89
- }
90
-
66
+ const names = new Map(
67
+ Object.entries({
68
+ "oc-2": "OpenCode",
69
+ amoled: "AMOLED",
70
+ aura: "Aura",
71
+ ayu: "Ayu",
72
+ carbonfox: "Carbonfox",
73
+ catppuccin: "Catppuccin",
74
+ "catppuccin-frappe": "Catppuccin Frappe",
75
+ "catppuccin-macchiato": "Catppuccin Macchiato",
76
+ cobalt2: "Cobalt2",
77
+ cursor: "Cursor",
78
+ dracula: "Dracula",
79
+ everforest: "Everforest",
80
+ flexoki: "Flexoki",
81
+ github: "GitHub",
82
+ gruvbox: "Gruvbox",
83
+ kanagawa: "Kanagawa",
84
+ "lucent-orng": "Lucent Orng",
85
+ material: "Material",
86
+ matrix: "Matrix",
87
+ mercury: "Mercury",
88
+ monokai: "Monokai",
89
+ nightowl: "Night Owl",
90
+ nord: "Nord",
91
+ "one-dark": "One Dark",
92
+ onedarkpro: "One Dark Pro",
93
+ orng: "Orng",
94
+ "osaka-jade": "Osaka Jade",
95
+ palenight: "Palenight",
96
+ rosepine: "Rose Pine",
97
+ shadesofpurple: "Shades of Purple",
98
+ solarized: "Solarized",
99
+ synthwave84: "Synthwave '84",
100
+ tokyonight: "Tokyonight",
101
+ vercel: "Vercel",
102
+ vesper: "Vesper",
103
+ zenburn: "Zenburn",
104
+ }),
105
+ )
106
+
107
+ // SAFETY: oc-2.json follows desktop-theme.schema.json; JSON modules only widen its colors to `string`, and
108
+ // v2/resolve.test.ts resolves the same file as a DesktopTheme.
91
109
  const oc2Theme = oc2ThemeJson as DesktopTheme
92
110
 
93
- function resolveStoredTheme(id: string | null | undefined, registered?: Record<string, DesktopTheme>) {
94
- if (id === "oc-2" || (id && (knownThemes().has(id) || registered?.[id]))) return id
95
-
96
- return "oc-2"
97
- }
98
-
111
+ // Storage access throws where localStorage is missing or blocked; every caller treats that as no stored value.
99
112
  function read(key: string) {
100
- if (typeof localStorage !== "object") return null
101
-
102
113
  try {
103
114
  return localStorage.getItem(key)
104
115
  } catch {
@@ -107,16 +118,12 @@ function read(key: string) {
107
118
  }
108
119
 
109
120
  function write(key: string, value: string) {
110
- if (typeof localStorage !== "object") return
111
-
112
121
  try {
113
122
  localStorage.setItem(key, value)
114
123
  } catch {}
115
124
  }
116
125
 
117
126
  function drop(key: string) {
118
- if (typeof localStorage !== "object") return
119
-
120
127
  try {
121
128
  localStorage.removeItem(key)
122
129
  } catch {}
@@ -128,9 +135,9 @@ function clear() {
128
135
  }
129
136
 
130
137
  function ensureThemeStyleElement(): HTMLStyleElement {
131
- const existing = document.getElementById(THEME_STYLE_ID) as HTMLStyleElement | null
138
+ const existing = document.getElementById(THEME_STYLE_ID)
132
139
 
133
- if (existing) return existing
140
+ if (existing instanceof HTMLStyleElement) return existing
134
141
  const element = document.createElement("style")
135
142
  element.id = THEME_STYLE_ID
136
143
  document.head.appendChild(element)
@@ -139,9 +146,7 @@ function ensureThemeStyleElement(): HTMLStyleElement {
139
146
  }
140
147
 
141
148
  function getSystemMode(): "light" | "dark" {
142
- if (typeof window !== "object") return "light"
143
-
144
- return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"
149
+ return globalThis.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light"
145
150
  }
146
151
 
147
152
  function applyThemeCss(theme: DesktopTheme, themeId: string, mode: "light" | "dark") {
@@ -151,10 +156,6 @@ function applyThemeCss(theme: DesktopTheme, themeId: string, mode: "light" | "da
151
156
  const css = themeToCss(tokens)
152
157
  const v2 = themeV2ToCss(resolveThemeVariantV2(variant, isDark))
153
158
 
154
- if (themeId !== "oc-2") {
155
- write(isDark ? STORAGE_KEYS.THEME_CSS_DARK : STORAGE_KEYS.THEME_CSS_LIGHT, `${css}\n ${v2}`)
156
- }
157
-
158
159
  const fullCss = `:root {
159
160
  color-scheme: ${mode};
160
161
  --text-mix-blend-mode: ${isDark ? "plus-lighter" : "multiply"};
@@ -174,9 +175,7 @@ function applyThemeCss(theme: DesktopTheme, themeId: string, mode: "light" | "da
174
175
  if (meta) meta.setAttribute("content", isDark ? "#080808" : "#fafafa")
175
176
  }
176
177
 
177
- function cacheThemeVariants(theme: DesktopTheme, themeId: string) {
178
- if (themeId === "oc-2") return
179
-
178
+ function cacheThemeVariants(theme: DesktopTheme) {
180
179
  for (const mode of ["light", "dark"] as const) {
181
180
  const isDark = mode === "dark"
182
181
  const variant = isDark ? theme.dark : theme.light
@@ -191,248 +190,172 @@ export const { use: useTheme, provider: ThemeProvider } = createSimpleContext({
191
190
  name: "Theme",
192
191
  init: (props: {
193
192
  defaultTheme?: string
194
- onThemeApplied?: (theme: DesktopTheme, mode: "light" | "dark", scheme: ColorScheme) => void
193
+ /** The window shows the theme: its colors, or until a registered theme arrives, those cached from the last run. */
194
+ onThemeApplied?: (mode: "light" | "dark", scheme: ColorScheme) => void
195
195
  }) => {
196
- const rawTheme = read(STORAGE_KEYS.THEME_ID) ?? props.defaultTheme
197
- const themeId = resolveStoredTheme(rawTheme)
198
-
199
- if (rawTheme && rawTheme !== themeId) {
200
- write(STORAGE_KEYS.THEME_ID, themeId)
201
- clear()
202
- }
203
-
204
- const colorScheme = (read(STORAGE_KEYS.COLOR_SCHEME) as ColorScheme | null) ?? "system"
205
- const mode = colorScheme === "system" ? getSystemMode() : colorScheme
206
-
207
- const [store, setStore] = createStore({
208
- themes: {
209
- "oc-2": oc2Theme,
210
- } as Record<string, DesktopTheme>,
211
- themeId,
212
- colorScheme,
213
- mode,
214
- previewThemeId: null as string | null,
215
- previewScheme: null as ColorScheme | null,
196
+ // An id no theme has yet stays selected until `settle`: a registered theme can arrive after startup.
197
+ const initial = createThemeState({
198
+ themeId: read(STORAGE_KEYS.THEME_ID) || props.defaultTheme || DEFAULT_THEME_ID,
199
+ scheme: parseColorScheme(read(STORAGE_KEYS.COLOR_SCHEME)),
200
+ systemMode: getSystemMode(),
201
+ defaultTheme: oc2Theme,
202
+ bundled: knownThemes(),
216
203
  })
217
204
 
218
- const loads = new Map<string, Promise<DesktopTheme | undefined>>()
205
+ const [state, setState] = createSignal(initial.state)
206
+ // Import timeouts by attempt.
207
+ const timers = new Map<number, ReturnType<typeof setTimeout>>()
208
+ // `loadThemes` calls waiting for every bundled import to settle.
209
+ const waiters = new Set<() => void>()
210
+ // Transitions an effect or `onThemeApplied` raises wait behind the one running.
211
+ const queue: ((current: ThemeMachineState) => ThemeTransition)[] = []
219
212
 
220
- const load = (id: string) => {
221
- const next = id
222
-
223
- if (!next) return Promise.resolve(undefined)
224
- const hit = store.themes[next]
225
-
226
- if (hit) return Promise.resolve(hit)
227
- const pending = loads.get(next)
228
-
229
- if (pending) return pending
230
- const file = getFiles()[`./themes/${next}.json`]
231
-
232
- if (!file) return Promise.resolve(undefined)
233
-
234
- const task = file()
235
- .then((mod) => {
236
- const theme = mod.default
237
- setStore("themes", next, theme)
238
-
239
- return theme
240
- })
241
- .finally(() => {
242
- loads.delete(next)
243
- })
244
-
245
- loads.set(next, task)
246
-
247
- return task
248
- }
249
-
250
- const applyTheme = (theme: DesktopTheme, themeId: string, mode: "light" | "dark", scheme: ColorScheme) => {
251
- applyThemeCss(theme, themeId, mode)
252
- props.onThemeApplied?.(theme, mode, scheme)
253
- }
254
-
255
- const ids = () => {
256
- const extra = Object.keys(store.themes)
257
- .filter((id) => !knownThemes().has(id))
258
- .sort()
259
-
260
- const all = themeIDs()
261
-
262
- if (extra.length === 0) return all
263
-
264
- return [...all, ...extra]
265
- }
213
+ const enqueue = (next: (current: ThemeMachineState) => ThemeTransition) => {
214
+ queue.push(next)
266
215
 
267
- const loadThemes = () => Promise.all(themeIDs().map(load)).then(() => store.themes)
216
+ if (queue.length > 1) return
268
217
 
269
- const onStorage = (e: StorageEvent) => {
270
- if (e.key === STORAGE_KEYS.THEME_ID && e.newValue) {
271
- const next = resolveStoredTheme(e.newValue, store.themes)
272
-
273
- if (next !== e.newValue) {
274
- write(STORAGE_KEYS.THEME_ID, next)
275
- clear()
218
+ untrack(() => {
219
+ while (queue[0]) {
220
+ const head = queue[0]
221
+ void Promise.try(() => step(head(state()))).catch((cause: unknown) => console.error(cause))
222
+ queue.shift()
276
223
  }
224
+ })
225
+ }
277
226
 
278
- setStore("themeId", next)
279
-
280
- if (next === "oc-2") {
281
- clear()
227
+ const dispatch = (event: ThemeEvent) => enqueue((current) => transitionTheme(current, event, knownThemes()))
282
228
 
283
- return
284
- }
229
+ // The CSS and storage change before the state, so effects that read computed styles see the new theme.
230
+ const step = (next: ThemeTransition) => {
231
+ next.effects.forEach(
232
+ (effect) => void Promise.try(() => run(effect)).catch((cause: unknown) => console.error(cause)),
233
+ )
285
234
 
286
- void load(next).then((theme) => {
287
- if (!theme || store.themeId !== next) return
288
- cacheThemeVariants(theme, next)
289
- })
290
- }
235
+ if (next.state !== state()) setState(next.state)
291
236
 
292
- if (e.key === STORAGE_KEYS.COLOR_SCHEME && e.newValue) {
293
- setStore("colorScheme", e.newValue as ColorScheme)
294
- setStore("mode", e.newValue === "system" ? getSystemMode() : (e.newValue as "light" | "dark"))
237
+ if (!selectThemesLoading(next.state)) {
238
+ waiters.forEach((resolve) => resolve())
239
+ waiters.clear()
295
240
  }
296
- }
297
-
298
- onMount(() => {
299
- makeEventListener(window, "storage", onStorage)
300
-
301
- const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
302
241
 
303
- const onMedia = () => {
304
- if (store.colorScheme !== "system") return
305
- setStore("mode", getSystemMode())
306
- }
242
+ const shown = next.effects.findLast(
243
+ (effect): effect is Extract<ThemeEffect, { type: "apply-theme" | "notify-applied" }> =>
244
+ effect.type === "apply-theme" || effect.type === "notify-applied",
245
+ )
307
246
 
308
- makeEventListener(mediaQuery, "change", onMedia)
247
+ if (shown) props.onThemeApplied?.(shown.mode, shown.scheme)
248
+ }
309
249
 
310
- const rawTheme = read(STORAGE_KEYS.THEME_ID) ?? props.defaultTheme
311
- const savedTheme = resolveStoredTheme(rawTheme, store.themes)
312
- const savedScheme = (read(STORAGE_KEYS.COLOR_SCHEME) as ColorScheme | null) ?? "system"
250
+ const run = (effect: ThemeEffect) => {
251
+ if (effect.type === "persist-theme") return write(STORAGE_KEYS.THEME_ID, effect.id)
313
252
 
314
- if (rawTheme && rawTheme !== savedTheme) {
315
- write(STORAGE_KEYS.THEME_ID, savedTheme)
316
- clear()
317
- }
253
+ if (effect.type === "persist-scheme") return write(STORAGE_KEYS.COLOR_SCHEME, effect.scheme)
318
254
 
319
- if (savedTheme !== store.themeId) setStore("themeId", savedTheme)
255
+ if (effect.type === "cache-theme") return cacheThemeVariants(effect.theme)
320
256
 
321
- if (savedScheme !== store.colorScheme) setStore("colorScheme", savedScheme)
322
- setStore("mode", savedScheme === "system" ? getSystemMode() : savedScheme)
323
- void load(savedTheme).then((theme) => {
324
- if (!theme || store.themeId !== savedTheme) return
325
- cacheThemeVariants(theme, savedTheme)
326
- })
327
- })
257
+ if (effect.type === "clear-cache") return clear()
328
258
 
329
- createEffect(() => {
330
- const theme = store.themes[store.themeId]
259
+ if (effect.type === "apply-theme") return applyThemeCss(effect.theme, effect.id, effect.mode)
331
260
 
332
- if (!theme) return
333
- applyTheme(theme, store.themeId, store.mode, store.colorScheme)
334
- })
261
+ if (effect.type === "warn-missing-theme") return console.warn(`Theme "${effect.id}" not found`)
335
262
 
336
- const setTheme = (id: string) => {
337
- const next = id
263
+ if (effect.type === "notify-applied") return
338
264
 
339
- if (!next) {
340
- console.warn(`Theme "${id}" not found`)
265
+ if (effect.type === "cancel-load-timer") {
266
+ clearTimeout(timers.get(effect.attempt))
267
+ timers.delete(effect.attempt)
341
268
 
342
269
  return
343
270
  }
344
271
 
345
- if (next !== "oc-2" && !knownThemes().has(next) && !store.themes[next]) {
346
- console.warn(`Theme "${id}" not found`)
272
+ if (effect.type === "cancel-all-loads") {
273
+ timers.forEach(clearTimeout)
274
+ timers.clear()
347
275
 
348
276
  return
349
277
  }
350
278
 
351
- setStore("themeId", next)
279
+ const file = getFiles()[`./themes/${effect.id}.json`]
280
+
281
+ if (!file) return dispatch({ type: "theme.load.failed", id: effect.id, attempt: effect.attempt })
282
+
283
+ timers.set(
284
+ effect.attempt,
285
+ setTimeout(() => {
286
+ timers.delete(effect.attempt)
287
+ dispatch({ type: "theme.load.timed-out", id: effect.id, attempt: effect.attempt })
288
+ }, effect.timeoutMs),
289
+ )
290
+ void Promise.try(file).then(
291
+ (mod) => dispatch({ type: "theme.loaded", id: effect.id, attempt: effect.attempt, theme: mod.default }),
292
+ () => dispatch({ type: "theme.load.failed", id: effect.id, attempt: effect.attempt }),
293
+ )
294
+ }
352
295
 
353
- if (next === "oc-2") {
354
- write(STORAGE_KEYS.THEME_ID, next)
355
- clear()
296
+ enqueue(() => initial)
356
297
 
357
- return
358
- }
298
+ onMount(() => {
299
+ makeEventListener(window, "storage", (e: StorageEvent) => {
300
+ if (e.key === STORAGE_KEYS.THEME_ID && e.newValue) dispatch({ type: "storage.theme.changed", id: e.newValue })
359
301
 
360
- void load(next).then((theme) => {
361
- if (!theme || store.themeId !== next) return
362
- cacheThemeVariants(theme, next)
363
- write(STORAGE_KEYS.THEME_ID, next)
302
+ if (e.key === STORAGE_KEYS.COLOR_SCHEME && e.newValue)
303
+ dispatch({ type: "storage.scheme.changed", scheme: parseColorScheme(e.newValue) })
364
304
  })
365
- }
366
305
 
367
- const setColorScheme = (scheme: ColorScheme) => {
368
- setStore("colorScheme", scheme)
369
- write(STORAGE_KEYS.COLOR_SCHEME, scheme)
370
- setStore("mode", scheme === "system" ? getSystemMode() : scheme)
371
- }
306
+ const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)")
372
307
 
373
- return {
374
- themeId: () => store.themeId,
375
- colorScheme: () => store.colorScheme,
376
- mode: () => store.mode,
377
- ids,
378
- name: (id: string) => store.themes[id]?.name ?? names[id] ?? id,
379
- loadThemes,
380
- themes: () => store.themes,
381
- setTheme,
382
- setColorScheme,
383
- registerTheme: (theme: DesktopTheme) => setStore("themes", theme.id, theme),
384
- previewTheme: (id: string) => {
385
- const next = id
386
-
387
- if (!next) return
388
-
389
- if (next !== "oc-2" && !knownThemes().has(next) && !store.themes[next]) return
390
- setStore("previewThemeId", next)
391
- void load(next).then((theme) => {
392
- if (!theme || store.previewThemeId !== next) return
393
-
394
- const mode = store.previewScheme
395
- ? store.previewScheme === "system"
396
- ? getSystemMode()
397
- : store.previewScheme
398
- : store.mode
399
-
400
- applyTheme(theme, next, mode, store.previewScheme ?? store.colorScheme)
401
- })
402
- },
403
- previewColorScheme: (scheme: ColorScheme) => {
404
- setStore("previewScheme", scheme)
405
- const mode = scheme === "system" ? getSystemMode() : scheme
406
- const id = store.previewThemeId ?? store.themeId
407
- void load(id).then((theme) => {
408
- if (!theme) return
409
-
410
- if ((store.previewThemeId ?? store.themeId) !== id) return
411
-
412
- if (store.previewScheme !== scheme) return
413
- applyTheme(theme, id, mode, scheme)
414
- })
415
- },
416
- commitPreview: () => {
417
- if (store.previewThemeId) {
418
- setTheme(store.previewThemeId)
419
- }
308
+ makeEventListener(mediaQuery, "change", () =>
309
+ dispatch({ type: "system.mode.changed", mode: mediaQuery.matches ? "dark" : "light" }),
310
+ )
311
+ })
420
312
 
421
- if (store.previewScheme) {
422
- setColorScheme(store.previewScheme)
423
- }
313
+ onCleanup(() => dispatch({ type: "disposed" }))
424
314
 
425
- setStore("previewThemeId", null)
426
- setStore("previewScheme", null)
315
+ const name = (id: string) => state().themes[id]?.name ?? names.get(id) ?? id
316
+
317
+ return {
318
+ /** The theme the window shows: a previewed one, the selected one, or the default while that one is not coming. */
319
+ themeId: createMemo(() => selectThemeId(state(), knownThemes())),
320
+ colorScheme: createMemo(() => selectColorScheme(state())),
321
+ /** The mode the window shows, a previewed scheme included. */
322
+ mode: createMemo(() => selectMode(state())),
323
+ // Bundled and registered themes in one list, alphabetical by name.
324
+ ids: () =>
325
+ [...themeIDs(), ...Object.keys(state().themes).filter((id) => !knownThemes().has(id))].toSorted((a, b) =>
326
+ name(a).localeCompare(name(b), undefined, { sensitivity: "base" }),
327
+ ),
328
+ name,
329
+ /** Loads every bundled theme; resolves once each import has loaded, failed or timed out. */
330
+ loadThemes: () => {
331
+ dispatch({ type: "themes.preload" })
332
+
333
+ if (!selectThemesLoading(untrack(state))) return Promise.resolve(untrack(state).themes)
334
+
335
+ return new Promise<Readonly<Record<string, DesktopTheme>>>((resolve) =>
336
+ waiters.add(() => resolve(untrack(state).themes)),
337
+ )
427
338
  },
428
- cancelPreview: () => {
429
- setStore("previewThemeId", null)
430
- setStore("previewScheme", null)
431
- void load(store.themeId).then((theme) => {
432
- if (!theme) return
433
- applyTheme(theme, store.themeId, store.mode, store.colorScheme)
434
- })
339
+ themes: () => state().themes,
340
+ setTheme: (id: string) => dispatch({ type: "theme.selected", id }),
341
+ setColorScheme: (scheme: ColorScheme) => dispatch({ type: "scheme.selected", scheme }),
342
+ /** Lists a theme beside the bundled ones until the returned function withdraws it. */
343
+ registerTheme: (theme: DesktopTheme) => {
344
+ dispatch({ type: "theme.registered", theme })
345
+
346
+ return () => dispatch({ type: "theme.unregistered", theme })
435
347
  },
348
+ /**
349
+ * Every theme source has registered what it will: a selected theme that is still missing is not coming, so the
350
+ * window shows the default theme and keeps the choice for when it returns.
351
+ */
352
+ settle: () => dispatch({ type: "settled" }),
353
+ /** Shows a theme without saving it, until `commitPreview` or `cancelPreview`. */
354
+ previewTheme: (id: string) => dispatch({ type: "preview.theme", id }),
355
+ /** Shows a color scheme without saving it, until `commitPreview` or `cancelPreview`. */
356
+ previewColorScheme: (scheme: ColorScheme) => dispatch({ type: "preview.scheme", scheme }),
357
+ commitPreview: () => dispatch({ type: "preview.committed" }),
358
+ cancelPreview: () => dispatch({ type: "preview.cancelled" }),
436
359
  }
437
360
  },
438
361
  })