@opencode/ui 2.2.5 → 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.
- package/dist/actions/button/button.d.ts +2 -2
- package/dist/actions/icon-button/icon-button.d.ts +1 -1
- package/dist/components/animated-number-machine.d.ts +95 -0
- package/dist/components/file-icon-machine.d.ts +40 -0
- package/dist/components/file-icon.d.ts +0 -2
- package/dist/components/file-icons/choose-icon-name.d.ts +2 -0
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/components/list.d.ts +2 -6
- package/dist/components/menu-scroll.d.ts +9 -3
- package/dist/components/popover-machine.d.ts +96 -0
- package/dist/components/popover.d.ts +2 -2
- package/dist/components/resize-handle-machine.d.ts +71 -0
- package/dist/components/resize-handle.d.ts +2 -0
- package/dist/components/scroll-view-machine.d.ts +68 -0
- package/dist/components/scroll-view.d.ts +2 -2
- package/dist/components/spinner.d.ts +11 -3
- package/dist/components/text-field-machine.d.ts +62 -0
- package/dist/components/text-reveal-machine.d.ts +90 -0
- package/dist/components/typewriter-machine.d.ts +62 -0
- package/dist/context/dialog-machine.d.ts +125 -0
- package/dist/context/dialog.d.ts +17 -16
- package/dist/data-display/line-comment/machine.d.ts +157 -0
- package/dist/data-display/project-avatar/animation-machine.d.ts +62 -0
- package/dist/data-display/project-avatar/animation.d.ts +1 -1
- package/dist/feedback/activity-ring/activity-ring.d.ts +2 -1
- package/dist/feedback/toast/machine.d.ts +57 -0
- package/dist/feedback/toast/toast.d.ts +1 -1
- package/dist/forms/checkbox/checkbox.d.ts +6 -1
- package/dist/forms/select/machine.d.ts +45 -0
- package/dist/forms/select/select.d.ts +4 -0
- package/dist/forms/typeahead/machine.d.ts +85 -0
- package/dist/hooks/auto-scroll-machine.d.ts +89 -0
- package/dist/hooks/create-auto-scroll.d.ts +1 -1
- package/dist/hooks/list-machine.d.ts +150 -0
- package/dist/hooks/use-filtered-list.d.ts +21 -35
- package/dist/i18n/en.d.ts +5 -0
- package/dist/icons/catalog.d.ts +8 -0
- package/dist/navigation/menu/machine.d.ts +49 -0
- package/dist/navigation/menu/menu.d.ts +5 -3
- package/dist/navigation/segmented-control/machine.d.ts +46 -0
- package/dist/overlays/mcp-sign-in/machine.d.ts +184 -0
- package/dist/overlays/mcp-sign-in/mcp-sign-in.d.ts +40 -0
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/dist/theme/context.d.ts +38 -14
- package/dist/theme/machine.d.ts +174 -0
- package/dist/typography/text-shimmer/machine.d.ts +48 -0
- package/package.json +19 -3
- package/src/actions/button/button.tsx +11 -6
- package/src/actions/icon-button/icon-button.tsx +17 -4
- package/src/components/animated-number-machine.ts +214 -0
- package/src/components/animated-number.tsx +134 -83
- package/src/components/file-icon-machine.ts +78 -0
- package/src/components/file-icon.tsx +96 -579
- package/src/components/file-icons/choose-icon-name.ts +574 -0
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/components/list.tsx +46 -137
- package/src/components/menu-scroll.css +19 -8
- package/src/components/menu-scroll.tsx +111 -34
- package/src/components/popover-machine.ts +198 -0
- package/src/components/popover.tsx +157 -89
- package/src/components/resize-handle-machine.ts +174 -0
- package/src/components/resize-handle.tsx +116 -67
- package/src/components/scroll-view-machine.ts +244 -0
- package/src/components/scroll-view.tsx +163 -116
- package/src/components/spinner.css +18 -5
- package/src/components/spinner.tsx +28 -14
- package/src/components/text-field-machine.ts +102 -0
- package/src/components/text-field.tsx +78 -24
- package/src/components/text-reveal-machine.ts +184 -0
- package/src/components/text-reveal.tsx +96 -93
- package/src/components/typewriter-machine.ts +134 -0
- package/src/components/typewriter.tsx +50 -39
- package/src/context/dialog-machine.ts +280 -0
- package/src/context/dialog.tsx +168 -177
- package/src/data-display/line-comment/line-comment.tsx +157 -178
- package/src/data-display/line-comment/machine.ts +391 -0
- package/src/data-display/project-avatar/animation-machine.ts +182 -0
- package/src/data-display/project-avatar/animation.ts +67 -38
- package/src/feedback/activity-ring/activity-ring.tsx +1 -1
- package/src/feedback/toast/machine.ts +115 -0
- package/src/feedback/toast/toast.tsx +55 -59
- package/src/forms/checkbox/checkbox-compact.css +169 -0
- package/src/forms/checkbox/checkbox.tsx +93 -31
- package/src/forms/search-list/search-list.css +5 -1
- package/src/forms/search-list/search-list.tsx +4 -4
- package/src/forms/select/machine.ts +81 -0
- package/src/forms/select/select.css +59 -17
- package/src/forms/select/select.tsx +67 -31
- package/src/forms/typeahead/machine.ts +253 -0
- package/src/forms/typeahead/typeahead.tsx +70 -62
- package/src/hooks/auto-scroll-machine.ts +261 -0
- package/src/hooks/create-auto-scroll.tsx +110 -205
- package/src/hooks/list-machine.ts +359 -0
- package/src/hooks/use-filtered-list.tsx +101 -112
- package/src/i18n/en.ts +6 -1
- package/src/icons/catalog.ts +8 -0
- package/src/navigation/menu/machine.ts +109 -0
- package/src/navigation/menu/menu.tsx +130 -22
- package/src/navigation/segmented-control/machine.ts +72 -0
- package/src/navigation/segmented-control/segmented-control.tsx +46 -46
- package/src/overlays/dialog/dialog.tsx +7 -1
- package/src/overlays/mcp-sign-in/machine.ts +343 -0
- package/src/overlays/mcp-sign-in/mcp-sign-in.ts +159 -0
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +157 -92
- package/src/styles/theme.css +10 -2
- package/src/theme/context.tsx +192 -269
- package/src/theme/machine.ts +508 -0
- package/src/typography/text-shimmer/machine.ts +90 -0
- package/src/typography/text-shimmer/text-shimmer.css +71 -39
- package/src/typography/text-shimmer/text-shimmer.tsx +41 -24
package/src/theme/context.tsx
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
// @refresh reload
|
|
2
2
|
|
|
3
|
-
import {
|
|
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
|
-
|
|
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
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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)
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
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
|
|
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
|
|
221
|
-
|
|
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
|
-
|
|
216
|
+
if (queue.length > 1) return
|
|
268
217
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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
|
-
|
|
279
|
-
|
|
280
|
-
if (next === "oc-2") {
|
|
281
|
-
clear()
|
|
227
|
+
const dispatch = (event: ThemeEvent) => enqueue((current) => transitionTheme(current, event, knownThemes()))
|
|
282
228
|
|
|
283
|
-
|
|
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
|
-
|
|
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 (
|
|
293
|
-
|
|
294
|
-
|
|
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
|
|
304
|
-
|
|
305
|
-
|
|
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
|
-
|
|
247
|
+
if (shown) props.onThemeApplied?.(shown.mode, shown.scheme)
|
|
248
|
+
}
|
|
309
249
|
|
|
310
|
-
|
|
311
|
-
|
|
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 (
|
|
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 (
|
|
255
|
+
if (effect.type === "cache-theme") return cacheThemeVariants(effect.theme)
|
|
320
256
|
|
|
321
|
-
if (
|
|
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
|
-
|
|
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 (
|
|
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
|
-
|
|
337
|
-
const next = id
|
|
263
|
+
if (effect.type === "notify-applied") return
|
|
338
264
|
|
|
339
|
-
if (
|
|
340
|
-
|
|
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 (
|
|
346
|
-
|
|
272
|
+
if (effect.type === "cancel-all-loads") {
|
|
273
|
+
timers.forEach(clearTimeout)
|
|
274
|
+
timers.clear()
|
|
347
275
|
|
|
348
276
|
return
|
|
349
277
|
}
|
|
350
278
|
|
|
351
|
-
|
|
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
|
-
|
|
354
|
-
write(STORAGE_KEYS.THEME_ID, next)
|
|
355
|
-
clear()
|
|
296
|
+
enqueue(() => initial)
|
|
356
297
|
|
|
357
|
-
|
|
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
|
-
|
|
361
|
-
|
|
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
|
-
|
|
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
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
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
|
-
|
|
422
|
-
setColorScheme(store.previewScheme)
|
|
423
|
-
}
|
|
313
|
+
onCleanup(() => dispatch({ type: "disposed" }))
|
|
424
314
|
|
|
425
|
-
|
|
426
|
-
|
|
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
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
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
|
})
|