sideby 0.22.0 → 0.23.0
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/ai-tasks.json +14 -0
- package/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +7 -0
- package/dist/src/cli/commands.js +326 -21
- package/dist/src/cli/json-schemas.js +76 -1
- package/dist/src/cli/main.js +32 -4
- package/dist/src/core/ask.js +6 -1
- package/dist/src/core/config.d.ts +5 -1
- package/dist/src/core/config.js +18 -3
- package/dist/src/core/quota-levels.d.ts +7 -2
- package/dist/src/core/quota-levels.js +13 -3
- package/dist/src/core/themes.d.ts +107 -0
- package/dist/src/core/themes.js +463 -0
- package/dist/src/menubar/snapshot.d.ts +10 -0
- package/dist/src/menubar/snapshot.js +14 -6
- package/dist/src/menubar/strings.d.ts +6 -0
- package/dist/src/menubar/strings.js +6 -0
- package/dist/src/panel/handler.d.ts +2 -0
- package/dist/src/panel/handler.js +125 -1
- package/dist/src/panel/i18n.d.ts +49 -0
- package/dist/src/panel/i18n.js +104 -6
- package/dist/src/panel/page-logic.d.ts +20 -1
- package/dist/src/panel/page-logic.js +41 -0
- package/dist/src/panel/page-script.js +264 -35
- package/dist/src/panel/page-styles.d.ts +2 -0
- package/dist/src/panel/page-styles.js +57 -31
- package/dist/src/panel/theme.d.ts +6 -1
- package/dist/src/panel/theme.js +4 -0
- package/dist/src/panel/themes.d.ts +14 -0
- package/dist/src/panel/themes.js +162 -0
- package/dist/src/runtime.d.ts +4 -0
- package/package.json +1 -1
- package/schemas/config.json +29 -1
- package/schemas/families.json +5 -0
- package/schemas/hide.json +157 -0
- package/schemas/list.json +10 -0
- package/schemas/order.json +26 -1
- package/schemas/pin.json +17 -0
- package/schemas/theme-check.json +140 -0
- package/schemas/theme-export.json +86 -0
- package/schemas/theme-import.json +92 -0
- package/schemas/theme-preview.json +86 -0
- package/schemas/theme-remove.json +87 -0
- package/schemas/theme-use.json +88 -0
- package/schemas/theme.json +137 -0
- package/schemas/unhide.json +157 -0
- package/schemas/unpin.json +17 -0
- package/skills/sideby/SKILL.md +3 -1
- package/skills/sideby/references/ai-tasks.md +1 -1
- package/skills/sideby/references/panel.md +9 -0
- package/skills/sideby/references/quota.md +1 -1
- package/skills/sideby/references/themes.md +47 -0
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
// Themes (spec §3.34, ADR-0050): the two Built-in Themes plus the user's Theme Files in <config>/themes/<id>.json.
|
|
2
|
+
// A Theme File is data only: a base Built-in Theme, an optional accent that regenerates the accent roles, and token
|
|
3
|
+
// overrides checked like a Portal Theme's. This module lists, checks, resolves and switches them; it never runs code.
|
|
4
|
+
import { lstat, mkdir, readdir, readFile, rm } from 'node:fs/promises';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { THEME_TOKENS, themeValueProblem } from '../panel/theme.js';
|
|
7
|
+
import { BUILTIN_THEMES, DEFAULT_THEME_ID } from '../panel/themes.js';
|
|
8
|
+
import { editConfigPanel, loadConfig } from './config.js';
|
|
9
|
+
import { writeFileAtomic } from './fs-safe.js';
|
|
10
|
+
export const THEME_ID_RE = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
11
|
+
const MAX_FILE = 64 * 1024;
|
|
12
|
+
const HEX_RE = /^#[0-9a-fA-F]{6}$/;
|
|
13
|
+
export const themesDir = (paths) => join(paths.configDir, 'themes');
|
|
14
|
+
const undoFile = (paths) => join(paths.stateDir, 'theme-undo.json');
|
|
15
|
+
const previewFile = (paths) => join(paths.stateDir, 'theme-preview.json');
|
|
16
|
+
const builtin = (id) => BUILTIN_THEMES.find((t) => t.id === id);
|
|
17
|
+
/**
|
|
18
|
+
* The name `sideby theme use` takes for "no Theme chosen": the Panel then shows the Default Theme on its own and,
|
|
19
|
+
* inside a Portal, the Portal's colors. It names no Theme File.
|
|
20
|
+
*/
|
|
21
|
+
export const NO_THEME = 'default';
|
|
22
|
+
const reserved = (id) => Boolean(builtin(id)) || id === NO_THEME;
|
|
23
|
+
const DEFAULT = builtin(DEFAULT_THEME_ID);
|
|
24
|
+
// ---- Reading and checking a Theme File
|
|
25
|
+
/** Checks parsed JSON as a Theme File. Problems make it unusable; warnings are keys that were ignored. */
|
|
26
|
+
export function checkThemeData(data) {
|
|
27
|
+
const problems = [];
|
|
28
|
+
const warnings = [];
|
|
29
|
+
if (typeof data !== 'object' || data === null || Array.isArray(data))
|
|
30
|
+
return { problems: ['must be a JSON object, like {"schemaVersion": 1, "name": "My theme"}'], warnings };
|
|
31
|
+
const d = data;
|
|
32
|
+
if (d.schemaVersion !== 1)
|
|
33
|
+
problems.push('schemaVersion must be 1');
|
|
34
|
+
if (typeof d.name !== 'string' || !d.name.trim() || d.name.length > 60)
|
|
35
|
+
problems.push('name must be a short text (1 to 60 characters)');
|
|
36
|
+
if (d.base !== undefined && (typeof d.base !== 'string' || !builtin(d.base)))
|
|
37
|
+
problems.push(`base must be one of ${BUILTIN_THEMES.map((t) => t.id).join(', ')}`);
|
|
38
|
+
if (d.accent !== undefined && (typeof d.accent !== 'string' || !HEX_RE.test(d.accent)))
|
|
39
|
+
problems.push('accent must be a color like #3f8fc4');
|
|
40
|
+
const tokens = { light: {}, dark: {} };
|
|
41
|
+
for (const mode of ['light', 'dark']) {
|
|
42
|
+
const v = d[mode];
|
|
43
|
+
if (v === undefined)
|
|
44
|
+
continue;
|
|
45
|
+
if (typeof v !== 'object' || v === null || Array.isArray(v)) {
|
|
46
|
+
problems.push(`${mode} must be an object of tokens`);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
for (const [k, value] of Object.entries(v)) {
|
|
50
|
+
if (!Object.hasOwn(THEME_TOKENS, k)) {
|
|
51
|
+
warnings.push(`${mode}.${k} is not a token sideby knows; it was ignored`);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const problem = themeValueProblem(value);
|
|
55
|
+
if (problem)
|
|
56
|
+
problems.push(`${mode}.${k} ${problem}`);
|
|
57
|
+
else
|
|
58
|
+
tokens[mode][k] = value.trim();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const k of Object.keys(d))
|
|
62
|
+
if (!['$schema', 'schemaVersion', 'name', 'base', 'accent', 'light', 'dark'].includes(k))
|
|
63
|
+
warnings.push(`${k} is not a Theme File key; it was ignored`);
|
|
64
|
+
if (problems.length)
|
|
65
|
+
return { problems, warnings };
|
|
66
|
+
return {
|
|
67
|
+
theme: {
|
|
68
|
+
schemaVersion: 1,
|
|
69
|
+
name: d.name.trim(),
|
|
70
|
+
...(d.base ? { base: d.base } : {}),
|
|
71
|
+
...(d.accent ? { accent: d.accent.toLowerCase() } : {}),
|
|
72
|
+
light: tokens.light,
|
|
73
|
+
dark: tokens.dark,
|
|
74
|
+
},
|
|
75
|
+
problems,
|
|
76
|
+
warnings,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/** Reads one Theme File: a regular file of at most 64 KB, never followed through a link. */
|
|
80
|
+
export async function readThemeFile(path) {
|
|
81
|
+
try {
|
|
82
|
+
const st = await lstat(path);
|
|
83
|
+
if (!st.isFile())
|
|
84
|
+
return { problems: ['is not a regular file (links are not followed)'], warnings: [] };
|
|
85
|
+
if (st.size > MAX_FILE)
|
|
86
|
+
return { problems: ['is over 64 KB'], warnings: [] };
|
|
87
|
+
const text = await readFile(path, 'utf8');
|
|
88
|
+
let data;
|
|
89
|
+
try {
|
|
90
|
+
data = JSON.parse(text);
|
|
91
|
+
}
|
|
92
|
+
catch (e) {
|
|
93
|
+
return {
|
|
94
|
+
problems: [`is not valid JSON (${e.message}); fix it or run \`sideby theme check\``],
|
|
95
|
+
warnings: [],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
return checkThemeData(data);
|
|
99
|
+
}
|
|
100
|
+
catch (e) {
|
|
101
|
+
return { problems: [`cannot be read (${e.code ?? 'error'})`], warnings: [] };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// ---- Listing
|
|
105
|
+
export async function listThemes(paths) {
|
|
106
|
+
const out = BUILTIN_THEMES.map((t) => ({
|
|
107
|
+
id: t.id,
|
|
108
|
+
name: t.name.en,
|
|
109
|
+
source: 'builtin',
|
|
110
|
+
swatches: [...t.swatches],
|
|
111
|
+
problems: [],
|
|
112
|
+
warnings: [],
|
|
113
|
+
}));
|
|
114
|
+
let names = [];
|
|
115
|
+
try {
|
|
116
|
+
names = (await readdir(themesDir(paths))).filter((n) => n.endsWith('.json')).sort();
|
|
117
|
+
}
|
|
118
|
+
catch { }
|
|
119
|
+
for (const n of names) {
|
|
120
|
+
const id = n.slice(0, -5);
|
|
121
|
+
const path = join(themesDir(paths), n);
|
|
122
|
+
if (!THEME_ID_RE.test(id) || reserved(id)) {
|
|
123
|
+
out.push({
|
|
124
|
+
id,
|
|
125
|
+
name: id,
|
|
126
|
+
source: 'file',
|
|
127
|
+
path,
|
|
128
|
+
swatches: [],
|
|
129
|
+
warnings: [],
|
|
130
|
+
problems: [
|
|
131
|
+
builtin(id)
|
|
132
|
+
? `uses the name of a Built-in Theme; rename the file`
|
|
133
|
+
: 'file name must be lowercase letters, digits and hyphens (up to 40), then .json',
|
|
134
|
+
],
|
|
135
|
+
});
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const r = await readThemeFile(path);
|
|
139
|
+
const colors = r.theme ? themeColors(r.theme) : undefined;
|
|
140
|
+
out.push({
|
|
141
|
+
id,
|
|
142
|
+
name: r.theme?.name ?? id,
|
|
143
|
+
source: 'file',
|
|
144
|
+
path,
|
|
145
|
+
swatches: colors ? swatchesOf(colors.light, colors.dark) : [],
|
|
146
|
+
problems: r.problems,
|
|
147
|
+
warnings: r.warnings,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
const swatchesOf = (l, d) => [l.bg, l.accentSoft, l.primary, d.bg].filter((c) => typeof c === 'string');
|
|
153
|
+
// ---- Resolving a Theme to tokens
|
|
154
|
+
/** A Theme File's colors before the contrast check: base, then accent roles, then the file's own tokens. */
|
|
155
|
+
export function themeColors(theme) {
|
|
156
|
+
const base = builtin(theme.base ?? DEFAULT_THEME_ID);
|
|
157
|
+
const accent = theme.accent ? accentTokens(theme.accent) : { light: {}, dark: {} };
|
|
158
|
+
return {
|
|
159
|
+
light: { ...base.light, ...accent.light, ...theme.light },
|
|
160
|
+
dark: { ...base.dark, ...accent.dark, ...theme.dark },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/** The colors for a Theme id (Built-in or file), with failing pairs moved until they pass. Unknown or broken: Sky. */
|
|
164
|
+
export async function resolveColors(paths, id) {
|
|
165
|
+
const want = id === undefined || id === NO_THEME ? DEFAULT_THEME_ID : id;
|
|
166
|
+
const b = builtin(want);
|
|
167
|
+
if (b)
|
|
168
|
+
return { id: want, light: { ...b.light }, dark: { ...b.dark }, adjusted: 0 };
|
|
169
|
+
if (!THEME_ID_RE.test(want))
|
|
170
|
+
return { ...defaults(), fallback: `${want} is not a Theme name; using ${DEFAULT.name.en}` };
|
|
171
|
+
const r = await readThemeFile(join(themesDir(paths), `${want}.json`));
|
|
172
|
+
if (!r.theme)
|
|
173
|
+
return {
|
|
174
|
+
...defaults(),
|
|
175
|
+
fallback: `Theme ${want} cannot be used (${r.problems[0]}); using ${DEFAULT.name.en}`,
|
|
176
|
+
};
|
|
177
|
+
return adjust(want, themeColors(r.theme));
|
|
178
|
+
}
|
|
179
|
+
function defaults() {
|
|
180
|
+
return { id: DEFAULT_THEME_ID, light: { ...DEFAULT.light }, dark: { ...DEFAULT.dark }, adjusted: 0 };
|
|
181
|
+
}
|
|
182
|
+
function adjust(id, c) {
|
|
183
|
+
const light = { ...c.light };
|
|
184
|
+
const dark = { ...c.dark };
|
|
185
|
+
let adjusted = 0;
|
|
186
|
+
for (const p of contrastPairs({ light, dark }))
|
|
187
|
+
if (!p.ok && p.suggest) {
|
|
188
|
+
;
|
|
189
|
+
(p.mode === 'light' ? light : dark)[p.fg] = p.suggest;
|
|
190
|
+
adjusted++;
|
|
191
|
+
}
|
|
192
|
+
return { id, light, dark, adjusted };
|
|
193
|
+
}
|
|
194
|
+
// ---- Accent roles from one color
|
|
195
|
+
// Lightness and chroma of the maintainer's leaf scales, step 1 to 12; step 9 becomes the accent itself.
|
|
196
|
+
const SCALE_LIGHT = [
|
|
197
|
+
[0.988, 0.014],
|
|
198
|
+
[0.973, 0.029],
|
|
199
|
+
[0.948, 0.055],
|
|
200
|
+
[0.923, 0.08],
|
|
201
|
+
[0.894, 0.101],
|
|
202
|
+
[0.857, 0.118],
|
|
203
|
+
[0.809, 0.14],
|
|
204
|
+
[0.746, 0.161],
|
|
205
|
+
[0.735, 0.18],
|
|
206
|
+
[0.69, 0.178],
|
|
207
|
+
[0.5, 0.126],
|
|
208
|
+
[0.305, 0.072],
|
|
209
|
+
];
|
|
210
|
+
const SCALE_DARK = [
|
|
211
|
+
[0.18, 0.015],
|
|
212
|
+
[0.207, 0.026],
|
|
213
|
+
[0.25, 0.045],
|
|
214
|
+
[0.289, 0.06],
|
|
215
|
+
[0.33, 0.074],
|
|
216
|
+
[0.378, 0.09],
|
|
217
|
+
[0.44, 0.11],
|
|
218
|
+
[0.521, 0.136],
|
|
219
|
+
[0.735, 0.18],
|
|
220
|
+
[0.776, 0.172],
|
|
221
|
+
[0.841, 0.14],
|
|
222
|
+
[0.94, 0.061],
|
|
223
|
+
];
|
|
224
|
+
/** A 12-step scale around one color: step 9 is the color, the others follow the template's lightness. */
|
|
225
|
+
export function accentScale(hex, dark) {
|
|
226
|
+
const [sL, sC, sH] = toOklch(hex);
|
|
227
|
+
const k = sC / 0.18;
|
|
228
|
+
return (dark ? SCALE_DARK : SCALE_LIGHT).map(([L, C], i) => i === 8
|
|
229
|
+
? hex.toLowerCase()
|
|
230
|
+
: i === 9
|
|
231
|
+
? fromOklch(dark ? sL + 0.04 : sL - 0.045, sC, sH)
|
|
232
|
+
: fromOklch(i === 10 && !dark ? Math.min(L, sL - 0.12) : L, Math.min(C * k, C * 1.3), sH));
|
|
233
|
+
}
|
|
234
|
+
/** The accent roles of a Theme generated from one color; neutrals and warning colors stay the base Theme's. */
|
|
235
|
+
export function accentTokens(hex) {
|
|
236
|
+
const a = accentScale(hex, false);
|
|
237
|
+
const d = accentScale(hex, true);
|
|
238
|
+
// Main button: the accent if white text reaches 3.2:1 on it, else the accent darkened until it does.
|
|
239
|
+
const [L, C, H] = toOklch(hex);
|
|
240
|
+
let l = L;
|
|
241
|
+
let p = hex.toLowerCase();
|
|
242
|
+
while (contrast(p, '#ffffff') < 3.2 && l > 0.3) {
|
|
243
|
+
l -= 0.005;
|
|
244
|
+
p = fromOklch(l, C, H);
|
|
245
|
+
}
|
|
246
|
+
const pHover = fromOklch(toOklch(p)[0] - 0.045, C, H);
|
|
247
|
+
const button = {
|
|
248
|
+
primary: p,
|
|
249
|
+
primaryHover: pHover,
|
|
250
|
+
primaryActive: pHover,
|
|
251
|
+
primaryBorder: p,
|
|
252
|
+
primaryHoverBorder: pHover,
|
|
253
|
+
onPrimary: '#ffffff',
|
|
254
|
+
};
|
|
255
|
+
return {
|
|
256
|
+
light: {
|
|
257
|
+
...button,
|
|
258
|
+
accent: a[10],
|
|
259
|
+
accentSoft: a[2],
|
|
260
|
+
focus: a[10],
|
|
261
|
+
buttonHoverBorder: a[5],
|
|
262
|
+
buttonHoverText: a[10],
|
|
263
|
+
checkColor: a[8],
|
|
264
|
+
ok: a[11],
|
|
265
|
+
okSoft: a[2],
|
|
266
|
+
okBar: a[8],
|
|
267
|
+
},
|
|
268
|
+
dark: {
|
|
269
|
+
...button,
|
|
270
|
+
accent: d[10],
|
|
271
|
+
accentSoft: d[2],
|
|
272
|
+
focus: d[10],
|
|
273
|
+
buttonHoverBorder: d[5],
|
|
274
|
+
buttonHoverText: d[10],
|
|
275
|
+
checkColor: d[8],
|
|
276
|
+
ok: d[10],
|
|
277
|
+
okSoft: d[2],
|
|
278
|
+
okBar: d[8],
|
|
279
|
+
},
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
// Text pairs need 4.5:1; the main button (bold text, ADR-0050) and the focus outline need 3:1.
|
|
283
|
+
const PAIRS = [
|
|
284
|
+
['text', 'bg', 4.5],
|
|
285
|
+
['text', 'surface', 4.5],
|
|
286
|
+
['muted', 'bg', 4.5],
|
|
287
|
+
['muted', 'surface', 4.5],
|
|
288
|
+
['accent', 'bg', 4.5],
|
|
289
|
+
['accent', 'surface', 4.5],
|
|
290
|
+
['ok', 'okSoft', 4.5],
|
|
291
|
+
['warn', 'warnSoft', 4.5],
|
|
292
|
+
['fail', 'failSoft', 4.5],
|
|
293
|
+
['fail', 'surface', 4.5],
|
|
294
|
+
['onPrimary', 'primary', 3],
|
|
295
|
+
['focus', 'surface', 3],
|
|
296
|
+
];
|
|
297
|
+
export function contrastPairs(c) {
|
|
298
|
+
const out = [];
|
|
299
|
+
for (const mode of ['light', 'dark'])
|
|
300
|
+
for (const [fg, bg, min] of PAIRS) {
|
|
301
|
+
const a = c[mode][fg];
|
|
302
|
+
const b = c[mode][bg];
|
|
303
|
+
if (!a || !b || !HEX_RE.test(a) || !HEX_RE.test(b)) {
|
|
304
|
+
out.push({ mode, fg, bg, min, ok: true });
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const ratio = Math.round(contrast(a, b) * 100) / 100;
|
|
308
|
+
const ok = ratio >= min;
|
|
309
|
+
out.push({ mode, fg, bg, ratio, min, ok, ...(ok ? {} : { suggest: passing(a, b, min) }) });
|
|
310
|
+
}
|
|
311
|
+
return out;
|
|
312
|
+
}
|
|
313
|
+
/** Moves `fg` along OKLCH lightness, away from `bg`, until it reaches `min`. */
|
|
314
|
+
function passing(fg, bg, min) {
|
|
315
|
+
const [L, C, H] = toOklch(fg);
|
|
316
|
+
const darker = toOklch(bg)[0] > 0.6;
|
|
317
|
+
for (let l = L; l >= 0 && l <= 1; l += darker ? -0.01 : 0.01) {
|
|
318
|
+
const c = fromOklch(l, C, H);
|
|
319
|
+
if (contrast(c, bg) >= min)
|
|
320
|
+
return c;
|
|
321
|
+
}
|
|
322
|
+
return darker ? '#000000' : '#ffffff';
|
|
323
|
+
}
|
|
324
|
+
// ---- Switching
|
|
325
|
+
/** Sets `panel.colorTheme`, keeping what it was so `undo` can put it back. */
|
|
326
|
+
export async function useTheme(paths, id) {
|
|
327
|
+
if (id !== NO_THEME) {
|
|
328
|
+
const entry = (await listThemes(paths)).find((t) => t.id === id);
|
|
329
|
+
if (!entry)
|
|
330
|
+
throw new Error(`no Theme named ${id}; run \`sideby theme list\` to see the names`);
|
|
331
|
+
if (entry.problems.length)
|
|
332
|
+
throw new Error(`Theme ${id} cannot be used: ${entry.problems[0]}; fix ${entry.path}`);
|
|
333
|
+
}
|
|
334
|
+
// A Theme picked by name is kept even when it is the Default Theme: inside a Portal it wins over the Portal's colors.
|
|
335
|
+
const before = (await loadConfig(paths.configFile)).panel?.colorTheme ?? NO_THEME;
|
|
336
|
+
const r = await editConfigPanel(paths.configFile, { colorTheme: id === NO_THEME ? null : id });
|
|
337
|
+
if (r.changed) {
|
|
338
|
+
await mkdir(paths.stateDir, { recursive: true });
|
|
339
|
+
await writeFileAtomic(undoFile(paths), `${JSON.stringify({ before, after: id })}\n`);
|
|
340
|
+
}
|
|
341
|
+
return { before, after: id, changed: r.changed };
|
|
342
|
+
}
|
|
343
|
+
/** Puts back the Theme from before the last `use`, only while config still holds what that `use` wrote. */
|
|
344
|
+
export async function undoTheme(paths) {
|
|
345
|
+
let rec;
|
|
346
|
+
try {
|
|
347
|
+
rec = JSON.parse(await readFile(undoFile(paths), 'utf8'));
|
|
348
|
+
}
|
|
349
|
+
catch {
|
|
350
|
+
throw new Error('nothing to undo: no Theme was switched with sideby yet');
|
|
351
|
+
}
|
|
352
|
+
const now = (await loadConfig(paths.configFile)).panel?.colorTheme ?? NO_THEME;
|
|
353
|
+
if (typeof rec.before !== 'string' || rec.after !== now)
|
|
354
|
+
throw new Error(`the Theme was changed since (now ${now}); pick one with \`sideby theme use <name>\``);
|
|
355
|
+
await editConfigPanel(paths.configFile, { colorTheme: rec.before === NO_THEME ? null : rec.before });
|
|
356
|
+
await rm(undoFile(paths), { force: true });
|
|
357
|
+
return { before: now, after: rec.before };
|
|
358
|
+
}
|
|
359
|
+
// ---- Files
|
|
360
|
+
/** Writes a checked Theme File into the themes folder. */
|
|
361
|
+
export async function saveThemeFile(paths, id, theme) {
|
|
362
|
+
if (!THEME_ID_RE.test(id) || reserved(id))
|
|
363
|
+
throw new Error(`${id} cannot be a Theme name: use lowercase letters, digits and hyphens, not ${[...BUILTIN_THEMES.map((t) => t.id), NO_THEME].join(', ')}`);
|
|
364
|
+
await mkdir(themesDir(paths), { recursive: true });
|
|
365
|
+
const path = join(themesDir(paths), `${id}.json`);
|
|
366
|
+
await writeFileAtomic(path, `${JSON.stringify(theme, null, 2)}\n`);
|
|
367
|
+
return path;
|
|
368
|
+
}
|
|
369
|
+
export async function removeThemeFile(paths, id) {
|
|
370
|
+
if (!THEME_ID_RE.test(id) || reserved(id))
|
|
371
|
+
throw new Error(`${id} is not a Theme File; Built-in Themes cannot be removed`);
|
|
372
|
+
const path = join(themesDir(paths), `${id}.json`);
|
|
373
|
+
const st = await lstat(path).catch(() => undefined);
|
|
374
|
+
if (!st)
|
|
375
|
+
throw new Error(`no Theme File ${path}`);
|
|
376
|
+
const wasActive = (await loadConfig(paths.configFile)).panel?.colorTheme === id;
|
|
377
|
+
if (wasActive)
|
|
378
|
+
await editConfigPanel(paths.configFile, { colorTheme: null });
|
|
379
|
+
await rm(path);
|
|
380
|
+
return { path, wasActive };
|
|
381
|
+
}
|
|
382
|
+
/** A full Theme File for an id: a Built-in Theme gets every token, so it can be the start of the user's own. */
|
|
383
|
+
export async function exportTheme(paths, id) {
|
|
384
|
+
const b = builtin(id);
|
|
385
|
+
if (b)
|
|
386
|
+
return {
|
|
387
|
+
schemaVersion: 1,
|
|
388
|
+
name: `My ${b.name.en}`,
|
|
389
|
+
base: b.id,
|
|
390
|
+
light: { ...b.light },
|
|
391
|
+
dark: { ...b.dark },
|
|
392
|
+
};
|
|
393
|
+
const r = await readThemeFile(join(themesDir(paths), `${id}.json`));
|
|
394
|
+
if (!r.theme)
|
|
395
|
+
throw new Error(`Theme ${id} cannot be exported: ${r.problems[0] ?? 'not found'}`);
|
|
396
|
+
return r.theme;
|
|
397
|
+
}
|
|
398
|
+
/** Keeps a Theme for one look at the Panel without choosing it (`sideby theme preview`). */
|
|
399
|
+
export async function setPreview(paths, theme) {
|
|
400
|
+
await mkdir(paths.stateDir, { recursive: true });
|
|
401
|
+
await writeFileAtomic(previewFile(paths), `${JSON.stringify({ at: Date.now(), theme })}\n`);
|
|
402
|
+
}
|
|
403
|
+
/** The previewed Theme's colors, if one was set in the last ten minutes. */
|
|
404
|
+
export async function previewColors(paths) {
|
|
405
|
+
try {
|
|
406
|
+
const rec = JSON.parse(await readFile(previewFile(paths), 'utf8'));
|
|
407
|
+
if (typeof rec.at !== 'number' || Date.now() - rec.at > 10 * 60_000)
|
|
408
|
+
return undefined;
|
|
409
|
+
const r = checkThemeData(rec.theme);
|
|
410
|
+
return r.theme ? adjust('preview', themeColors(r.theme)) : undefined;
|
|
411
|
+
}
|
|
412
|
+
catch {
|
|
413
|
+
return undefined;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
// ---- OKLCH and WCAG contrast
|
|
417
|
+
const lin = (c) => (c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4);
|
|
418
|
+
const unlin = (c) => (c <= 0.0031308 ? 12.92 * c : 1.055 * c ** (1 / 2.4) - 0.055);
|
|
419
|
+
const rgb = (hex) => [1, 3, 5].map((i) => Number.parseInt(hex.slice(i, i + 2), 16) / 255);
|
|
420
|
+
export function toOklch(hex) {
|
|
421
|
+
const [r, g, b] = rgb(hex).map(lin);
|
|
422
|
+
const l = Math.cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b);
|
|
423
|
+
const m = Math.cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b);
|
|
424
|
+
const s = Math.cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b);
|
|
425
|
+
const L = 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s;
|
|
426
|
+
const A = 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s;
|
|
427
|
+
const B = 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s;
|
|
428
|
+
return [L, Math.hypot(A, B), ((Math.atan2(B, A) * 180) / Math.PI + 360) % 360];
|
|
429
|
+
}
|
|
430
|
+
function oklchRgb(L, C, H) {
|
|
431
|
+
const a = C * Math.cos((H * Math.PI) / 180);
|
|
432
|
+
const b = C * Math.sin((H * Math.PI) / 180);
|
|
433
|
+
const l = (L + 0.3963377774 * a + 0.2158037573 * b) ** 3;
|
|
434
|
+
const m = (L - 0.1055613458 * a - 0.0638541728 * b) ** 3;
|
|
435
|
+
const s = (L - 0.0894841775 * a - 1.291485548 * b) ** 3;
|
|
436
|
+
return [
|
|
437
|
+
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
|
|
438
|
+
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
|
|
439
|
+
-0.0041960863 * l - 0.7034186147 * m + 1.707614701 * s,
|
|
440
|
+
];
|
|
441
|
+
}
|
|
442
|
+
/** OKLCH to #rrggbb, lowering chroma until the color fits sRGB. */
|
|
443
|
+
export function fromOklch(L, C, H) {
|
|
444
|
+
const l = Math.min(1, Math.max(0, L));
|
|
445
|
+
let c = C;
|
|
446
|
+
while (c > 0 && !oklchRgb(l, c, H).every((v) => v >= -1e-4 && v <= 1 + 1e-4))
|
|
447
|
+
c -= 0.003;
|
|
448
|
+
return `#${oklchRgb(l, Math.max(c, 0), H)
|
|
449
|
+
.map((v) => Math.round(unlin(Math.min(1, Math.max(0, v))) * 255)
|
|
450
|
+
.toString(16)
|
|
451
|
+
.padStart(2, '0'))
|
|
452
|
+
.join('')}`;
|
|
453
|
+
}
|
|
454
|
+
const luminance = (hex) => {
|
|
455
|
+
const [r, g, b] = rgb(hex).map(lin);
|
|
456
|
+
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
457
|
+
};
|
|
458
|
+
/** WCAG 2 contrast ratio of two #rrggbb colors. */
|
|
459
|
+
export function contrast(a, b) {
|
|
460
|
+
const x = luminance(a);
|
|
461
|
+
const y = luminance(b);
|
|
462
|
+
return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
|
|
463
|
+
}
|
|
@@ -90,6 +90,11 @@ export interface MenubarFamilyInput {
|
|
|
90
90
|
id: string;
|
|
91
91
|
title: string;
|
|
92
92
|
logo?: FamilyLogo | null;
|
|
93
|
+
/**
|
|
94
|
+
* Whether "Refresh" asks the Host for new numbers (the Family has `fetchQuota`). False for Claude Code, whose Quota
|
|
95
|
+
* changes only when a session runs in the account; old readings then say so. Default true.
|
|
96
|
+
*/
|
|
97
|
+
refreshable?: boolean;
|
|
93
98
|
/** Its Handoff ranking (`sideby next`); null when the Family has no Quota source. */
|
|
94
99
|
plan: HandoffPlan | null;
|
|
95
100
|
/** In Display Order. */
|
|
@@ -101,6 +106,11 @@ export interface MenubarFamilyInput {
|
|
|
101
106
|
quota: QuotaResult;
|
|
102
107
|
/** The other refs of its Same Login: the same subscription, so one quota. */
|
|
103
108
|
sameLogin?: readonly string[];
|
|
109
|
+
/**
|
|
110
|
+
* The newest use of its login on this computer (its own and its Same Login's sessions), when known. A reading
|
|
111
|
+
* older than an hour with no use since is not marked old (`quotaIsStale`).
|
|
112
|
+
*/
|
|
113
|
+
lastUseAt?: string;
|
|
104
114
|
}[];
|
|
105
115
|
}
|
|
106
116
|
/** A shortcut as the menu bar script registers it: a macOS virtual key code and Carbon modifier flags. */
|
|
@@ -147,7 +147,7 @@ function observedText(observedAt, now, s) {
|
|
|
147
147
|
return s.observedJustNow;
|
|
148
148
|
return fill(s.observedAgo, { time: durationText(minutes, s) });
|
|
149
149
|
}
|
|
150
|
-
function row(a, plan, now, s) {
|
|
150
|
+
function row(a, plan, now, s, refreshable) {
|
|
151
151
|
const recommended = Boolean(plan?.hasQuota && plan.pick === a.ref);
|
|
152
152
|
const startTip = fill(s.startTip, { account: a.ref });
|
|
153
153
|
const base = {
|
|
@@ -169,11 +169,14 @@ function row(a, plan, now, s) {
|
|
|
169
169
|
const quota = a.quota;
|
|
170
170
|
if (quota.status !== 'ok')
|
|
171
171
|
return noted(s.noData);
|
|
172
|
-
const stale = quotaIsStale(quota, now.getTime());
|
|
172
|
+
const stale = quotaIsStale(quota, now.getTime(), a.lastUseAt);
|
|
173
|
+
// Over an hour old but unused here since: the numbers stand, and the hover text says what it cannot see.
|
|
174
|
+
const quiet = !stale && now.getTime() - Date.parse(quota.observedAt) > 3600_000;
|
|
173
175
|
// The head of the hover text: when the numbers were read, and why they may be old.
|
|
174
176
|
const head = [
|
|
175
177
|
observedText(quota.observedAt, now, s),
|
|
176
|
-
...(stale ? [s.olderReadingTip] : []),
|
|
178
|
+
...(stale ? [refreshable ? s.olderReadingTip : s.olderReadingSessionTip] : []),
|
|
179
|
+
...(quiet ? [s.quietTip] : []),
|
|
177
180
|
...(quota.fetchError ? [fill(s.fetchFailed, { error: quota.fetchError })] : []),
|
|
178
181
|
];
|
|
179
182
|
const groups = quotaGroups(quota);
|
|
@@ -240,7 +243,7 @@ function section(f, now, s) {
|
|
|
240
243
|
...(f.logo.gradients?.length ? { gradients: f.logo.gradients } : {}),
|
|
241
244
|
}
|
|
242
245
|
: null;
|
|
243
|
-
const all = f.accounts.map((a) => ({ a, r: row(a, f.plan, now, s) }));
|
|
246
|
+
const all = f.accounts.map((a) => ({ a, r: row(a, f.plan, now, s, f.refreshable !== false) }));
|
|
244
247
|
const aside = (a) => a.kind === 'api' || a.login === 'logged-out';
|
|
245
248
|
// A Same Login is one quota, so one row, named like the Panel's: after the directory first in Display Order (the
|
|
246
249
|
// Panel's "Set as shown name" moves one there). It is recommended when any of its directories is. The other
|
|
@@ -320,6 +323,11 @@ export function menubarSnapshot(families, now, lang, hotkey = null) {
|
|
|
320
323
|
: {};
|
|
321
324
|
const low = worst !== null && quotaLevel(worst.value) !== 'ok';
|
|
322
325
|
const old = sections.some((s) => s.rows.some((r) => r.stale));
|
|
326
|
+
// "Refresh" brings new numbers only for a Family that can be asked; otherwise a session in that account must run.
|
|
327
|
+
const oldSays = old &&
|
|
328
|
+
sections.every((sec) => !sec.rows.some((r) => r.stale) || families.find((f) => f.id === sec.family)?.refreshable === false)
|
|
329
|
+
? s.detailOldSession
|
|
330
|
+
: s.detailOld;
|
|
323
331
|
const { recommended, empty, loading, addAccount, resetsHeader, updatedJustNow, updatedMinutes, refreshing, refreshFailed, refresh, openPanel, hide, hideTitle, hideBody, hideYes, cancel, hotkeyTaken, } = s;
|
|
324
332
|
return {
|
|
325
333
|
schemaVersion: 1,
|
|
@@ -327,7 +335,7 @@ export function menubarSnapshot(families, now, lang, hotkey = null) {
|
|
|
327
335
|
level: worst && !old ? quotaLevel(worst.value) : 'none',
|
|
328
336
|
tooltip: [
|
|
329
337
|
old
|
|
330
|
-
? `${s.statusOld} · ${
|
|
338
|
+
? `${s.statusOld} · ${oldSays}`
|
|
331
339
|
: worst
|
|
332
340
|
? fill(low ? s.tooltipLow : s.tooltipOk, values)
|
|
333
341
|
: s.tooltipEmpty,
|
|
@@ -338,7 +346,7 @@ export function menubarSnapshot(families, now, lang, hotkey = null) {
|
|
|
338
346
|
card: worst
|
|
339
347
|
? {
|
|
340
348
|
status: old ? s.statusOld : fill(low ? s.statusLow : s.statusOk, values),
|
|
341
|
-
detail: old ?
|
|
349
|
+
detail: old ? oldSays : fill(low ? s.detailLow : s.detailOk, values),
|
|
342
350
|
level: old ? 'none' : quotaLevel(worst.value),
|
|
343
351
|
}
|
|
344
352
|
: null,
|
|
@@ -10,6 +10,7 @@ export declare const MENUBAR_STRINGS: {
|
|
|
10
10
|
readonly statusOk: 'Plenty of quota';
|
|
11
11
|
readonly statusOld: 'Quota needs refreshing';
|
|
12
12
|
readonly detailOld: 'Some readings are old · refresh to check';
|
|
13
|
+
readonly detailOldSession: 'Some readings are old · they update when a session runs in that account';
|
|
13
14
|
readonly statusLow: '{tool} is running low';
|
|
14
15
|
readonly detailOk: 'Highest next account: {account} ({p}% used)';
|
|
15
16
|
readonly detailLow: '{account} is at {p}% · switch tools or wait';
|
|
@@ -24,6 +25,8 @@ export declare const MENUBAR_STRINGS: {
|
|
|
24
25
|
readonly observedJustNow: 'Quota read just now';
|
|
25
26
|
readonly observedAgo: 'Quota read {time} ago';
|
|
26
27
|
readonly olderReadingTip: 'These numbers may be old · click Refresh to check again';
|
|
28
|
+
readonly olderReadingSessionTip: 'These numbers may be old · they update when a session runs in this account';
|
|
29
|
+
readonly quietTip: 'Not used on this computer since · use on the web or other computers is not included';
|
|
27
30
|
readonly fetchFailed: 'The last check failed: {error}';
|
|
28
31
|
readonly windowTip: '{label}: {p}% used';
|
|
29
32
|
readonly windowResetsTip: '{label}: {p}% used · resets in {time}';
|
|
@@ -65,6 +68,7 @@ export declare const MENUBAR_STRINGS: {
|
|
|
65
68
|
readonly statusOk: '各工具额度充足';
|
|
66
69
|
readonly statusOld: '额度需要更新';
|
|
67
70
|
readonly detailOld: '部分额度是旧数据 · 请刷新确认';
|
|
71
|
+
readonly detailOldSession: '部分额度是旧数据 · 在对应账号开一次会话就会更新';
|
|
68
72
|
readonly statusLow: '{tool} 额度吃紧';
|
|
69
73
|
readonly detailOk: '首选账号中最高:{account}(已用 {p}%)';
|
|
70
74
|
readonly detailLow: '首选 {account} 已用 {p}% · 可换工具或等重置';
|
|
@@ -79,6 +83,8 @@ export declare const MENUBAR_STRINGS: {
|
|
|
79
83
|
readonly observedJustNow: '额度刚刚读取';
|
|
80
84
|
readonly observedAgo: '额度读于 {time}前';
|
|
81
85
|
readonly olderReadingTip: '这些数字可能已经过时 · 点“刷新”重新检查';
|
|
86
|
+
readonly olderReadingSessionTip: '这些数字可能已经过时 · 在这个账号开一次会话就会更新';
|
|
87
|
+
readonly quietTip: '之后这台电脑没用过这个账号 · 网页或其他电脑上的用量不在内';
|
|
82
88
|
readonly fetchFailed: '上次检查没成功:{error}';
|
|
83
89
|
readonly windowTip: '{label}:已用 {p}%';
|
|
84
90
|
readonly windowResetsTip: '{label}:已用 {p}% · {time}后重置';
|
|
@@ -10,6 +10,7 @@ export const MENUBAR_STRINGS = {
|
|
|
10
10
|
statusOk: 'Plenty of quota',
|
|
11
11
|
statusOld: 'Quota needs refreshing',
|
|
12
12
|
detailOld: 'Some readings are old · refresh to check',
|
|
13
|
+
detailOldSession: 'Some readings are old · they update when a session runs in that account',
|
|
13
14
|
statusLow: '{tool} is running low',
|
|
14
15
|
detailOk: 'Highest next account: {account} ({p}% used)',
|
|
15
16
|
detailLow: '{account} is at {p}% · switch tools or wait',
|
|
@@ -24,6 +25,8 @@ export const MENUBAR_STRINGS = {
|
|
|
24
25
|
observedJustNow: 'Quota read just now',
|
|
25
26
|
observedAgo: 'Quota read {time} ago',
|
|
26
27
|
olderReadingTip: 'These numbers may be old · click Refresh to check again',
|
|
28
|
+
olderReadingSessionTip: 'These numbers may be old · they update when a session runs in this account',
|
|
29
|
+
quietTip: 'Not used on this computer since · use on the web or other computers is not included',
|
|
27
30
|
fetchFailed: 'The last check failed: {error}',
|
|
28
31
|
windowTip: '{label}: {p}% used',
|
|
29
32
|
windowResetsTip: '{label}: {p}% used · resets in {time}',
|
|
@@ -65,6 +68,7 @@ export const MENUBAR_STRINGS = {
|
|
|
65
68
|
statusOk: '各工具额度充足',
|
|
66
69
|
statusOld: '额度需要更新',
|
|
67
70
|
detailOld: '部分额度是旧数据 · 请刷新确认',
|
|
71
|
+
detailOldSession: '部分额度是旧数据 · 在对应账号开一次会话就会更新',
|
|
68
72
|
statusLow: '{tool} 额度吃紧',
|
|
69
73
|
detailOk: '首选账号中最高:{account}(已用 {p}%)',
|
|
70
74
|
detailLow: '首选 {account} 已用 {p}% · 可换工具或等重置',
|
|
@@ -79,6 +83,8 @@ export const MENUBAR_STRINGS = {
|
|
|
79
83
|
observedJustNow: '额度刚刚读取',
|
|
80
84
|
observedAgo: '额度读于 {time}前',
|
|
81
85
|
olderReadingTip: '这些数字可能已经过时 · 点“刷新”重新检查',
|
|
86
|
+
olderReadingSessionTip: '这些数字可能已经过时 · 在这个账号开一次会话就会更新',
|
|
87
|
+
quietTip: '之后这台电脑没用过这个账号 · 网页或其他电脑上的用量不在内',
|
|
82
88
|
fetchFailed: '上次检查没成功:{error}',
|
|
83
89
|
windowTip: '{label}:已用 {p}%',
|
|
84
90
|
windowResetsTip: '{label}:已用 {p}% · {time}后重置',
|
|
@@ -149,6 +149,8 @@ export type PanelFamily = FamilyInfo & {
|
|
|
149
149
|
tryCommand?: string[];
|
|
150
150
|
/** AI Tasks start this Host with its permission checks on (`aiTaskArgs`, ADR-0031), so the AI dialog says so. */
|
|
151
151
|
aiTaskSafe?: true;
|
|
152
|
+
/** "Refresh" can ask the Host for its Quota (`fetchQuota`); without it old numbers wait for a session (spec §3.31). */
|
|
153
|
+
quotaRefresh?: true;
|
|
152
154
|
};
|
|
153
155
|
/** What the Panel shows about updates (spec §3.23); absent when the handler was created with `updates: false`. */
|
|
154
156
|
export interface PanelUpdate {
|