@snap-rail/ui 0.0.0-stage → 0.1.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.
Files changed (60) hide show
  1. package/LICENSE +202 -0
  2. package/lib/charts.js +142 -0
  3. package/lib/contract.js +543 -0
  4. package/lib/fallback.js +400 -0
  5. package/lib/index.js +3345 -0
  6. package/lib/pages.js +68 -0
  7. package/lib/runtime.js +2064 -0
  8. package/lib/settings.js +1195 -0
  9. package/lib/skins.js +284 -0
  10. package/lib/slots.js +267 -0
  11. package/lib/types/charts.d.ts +62 -0
  12. package/lib/types/charts.js +41 -0
  13. package/lib/types/components/button.d.ts +39 -0
  14. package/lib/types/components/button.js +27 -0
  15. package/lib/types/components/controls.d.ts +66 -0
  16. package/lib/types/components/controls.js +66 -0
  17. package/lib/types/components/dialog.d.ts +58 -0
  18. package/lib/types/components/dialog.js +46 -0
  19. package/lib/types/components/feedback.d.ts +46 -0
  20. package/lib/types/components/feedback.js +20 -0
  21. package/lib/types/components/form.d.ts +31 -0
  22. package/lib/types/components/form.js +13 -0
  23. package/lib/types/components/index.d.ts +15 -0
  24. package/lib/types/components/index.js +15 -0
  25. package/lib/types/components/input.d.ts +37 -0
  26. package/lib/types/components/input.js +15 -0
  27. package/lib/types/components/layout.d.ts +100 -0
  28. package/lib/types/components/layout.js +80 -0
  29. package/lib/types/components/schema-form.d.ts +34 -0
  30. package/lib/types/components/schema-form.js +61 -0
  31. package/lib/types/components/surface.d.ts +49 -0
  32. package/lib/types/components/surface.js +59 -0
  33. package/lib/types/contract.d.ts +358 -0
  34. package/lib/types/contract.js +235 -0
  35. package/lib/types/fallback.d.ts +14 -0
  36. package/lib/types/fallback.js +50 -0
  37. package/lib/types/index.d.ts +19 -0
  38. package/lib/types/index.js +19 -0
  39. package/lib/types/pages.d.ts +40 -0
  40. package/lib/types/pages.js +69 -0
  41. package/lib/types/runtime.d.ts +71 -0
  42. package/lib/types/runtime.js +177 -0
  43. package/lib/types/settings-pages/appearance.d.ts +13 -0
  44. package/lib/types/settings-pages/appearance.js +131 -0
  45. package/lib/types/settings-pages/index.d.ts +3 -0
  46. package/lib/types/settings-pages/index.js +3 -0
  47. package/lib/types/settings-pages/plugins.d.ts +16 -0
  48. package/lib/types/settings-pages/plugins.js +109 -0
  49. package/lib/types/settings.d.ts +58 -0
  50. package/lib/types/settings.js +170 -0
  51. package/lib/types/skins.d.ts +143 -0
  52. package/lib/types/skins.js +268 -0
  53. package/lib/types/slots.d.ts +69 -0
  54. package/lib/types/slots.js +210 -0
  55. package/lib/types/utils.d.ts +15 -0
  56. package/lib/types/utils.js +26 -0
  57. package/package.json +105 -3
  58. package/src/styles/base.css +321 -0
  59. package/src/styles/default.css +734 -0
  60. package/README.md +0 -3
package/lib/runtime.js ADDED
@@ -0,0 +1,2064 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { rpcErrorText, subscribeFrame } from "@snap-rail/connection";
3
+ import { settingsChangedSchema } from "@snap-rail/station-rpc/contract";
4
+ import { Context } from "@snap-rail/cordis";
5
+ import { TimerService } from "@snap-rail/cordis-plugin-timer";
6
+ import { shellContract } from "@snap-rail/shell/contract";
7
+ import { Component, createContext, useContext, useEffect, useState, useSyncExternalStore } from "react";
8
+ import { createRoot } from "react-dom/client";
9
+ import { Slot as Slot$1 } from "@radix-ui/react-slot";
10
+ import "@radix-ui/react-checkbox";
11
+ import "@radix-ui/react-label";
12
+ import "@radix-ui/react-radio-group";
13
+ import "@radix-ui/react-select";
14
+ import * as SwitchPrimitive from "@radix-ui/react-switch";
15
+ import * as TabsPrimitive from "@radix-ui/react-tabs";
16
+ import * as DialogPrimitive from "@radix-ui/react-dialog";
17
+ import "@snap-rail/app-boot/contract";
18
+ //#region lib/types/utils.js
19
+ /**
20
+ * Small helpers shared by the headless semantic components: class merging and
21
+ * the stable `data-snap-*` anatomy that skins target.
22
+ *
23
+ * @module @snap-rail/ui/utils
24
+ */
25
+ /** Join truthy class names. */
26
+ function cn(...values) {
27
+ return values.filter((value) => typeof value === "string" && value.length > 0).join(" ");
28
+ }
29
+ /** The anatomy attributes every semantic component renders. */
30
+ function part(component, name) {
31
+ return {
32
+ "data-snap-component": component,
33
+ "data-snap-part": `${component}.${name}`
34
+ };
35
+ }
36
+ /** Map a semantic spacing token to a CSS variable. */
37
+ function space(token) {
38
+ return `var(--snap-space-${token})`;
39
+ }
40
+ //#endregion
41
+ //#region lib/types/components/button.js
42
+ /**
43
+ * Semantic buttons: one behaviour, skin-owned visuals. The DOM exposes
44
+ * `button.root`, `button.label`, `button.icon`, and `button.spinner` parts;
45
+ * intent/emphasis/size are data attributes a skin styles.
46
+ *
47
+ * @module @snap-rail/ui/components/button
48
+ */
49
+ /** A spinner that inherits the current text colour. */
50
+ function Spinner(props) {
51
+ return jsxs("svg", {
52
+ ...part("button", "spinner"),
53
+ className: cn("snap-spinner", props.className),
54
+ viewBox: "0 0 24 24",
55
+ "aria-hidden": "true",
56
+ children: [jsx("circle", {
57
+ cx: "12",
58
+ cy: "12",
59
+ r: "9",
60
+ fill: "none",
61
+ stroke: "currentColor",
62
+ strokeWidth: "3",
63
+ opacity: "0.25"
64
+ }), jsx("path", {
65
+ d: "M21 12a9 9 0 0 0-9-9",
66
+ fill: "none",
67
+ stroke: "currentColor",
68
+ strokeWidth: "3",
69
+ strokeLinecap: "round"
70
+ })]
71
+ });
72
+ }
73
+ /** A themed button; `asChild` composes the behaviour onto a child element. */
74
+ function Button({ intent = "neutral", emphasis = "solid", size = "md", loading = false, icon, iconPosition = "start", asChild = false, disabled, className, children, type, ...props }) {
75
+ const Component = asChild ? Slot$1 : "button";
76
+ const content = asChild ? children : jsxs(Fragment, { children: [
77
+ icon !== void 0 && iconPosition === "start" && jsx("span", {
78
+ ...part("button", "icon"),
79
+ children: icon
80
+ }),
81
+ children !== void 0 && jsx("span", {
82
+ ...part("button", "label"),
83
+ children
84
+ }),
85
+ icon !== void 0 && iconPosition === "end" && jsx("span", {
86
+ ...part("button", "icon"),
87
+ children: icon
88
+ }),
89
+ loading && jsx(Spinner, {})
90
+ ] });
91
+ return jsx(Component, {
92
+ ...part("button", "root"),
93
+ ...type !== void 0 ? { type } : !asChild ? { type: "button" } : {},
94
+ className: cn("snap-button", className),
95
+ "data-intent": intent,
96
+ "data-emphasis": emphasis,
97
+ "data-size": size,
98
+ "data-loading": loading || void 0,
99
+ "data-disabled": disabled === true || void 0,
100
+ "aria-busy": loading || void 0,
101
+ disabled: !asChild ? disabled === true || loading : void 0,
102
+ ...props,
103
+ children: content
104
+ });
105
+ }
106
+ /** A square glyph button with a mandatory accessible label. */
107
+ function IconButton({ icon, label, active = false, intent = "neutral", emphasis = "ghost", size = "md", className, ...props }) {
108
+ return jsx("button", {
109
+ ...part("icon-button", "root"),
110
+ type: "button",
111
+ className: cn("snap-icon-button", className),
112
+ "data-intent": intent,
113
+ "data-emphasis": emphasis,
114
+ "data-size": size,
115
+ "data-active": active || void 0,
116
+ "aria-label": label,
117
+ "aria-pressed": active || void 0,
118
+ ...props,
119
+ children: jsx("span", {
120
+ ...part("icon-button", "icon"),
121
+ "aria-hidden": "true",
122
+ children: icon
123
+ })
124
+ });
125
+ }
126
+ //#endregion
127
+ //#region lib/types/components/dialog.js
128
+ /**
129
+ * Dialog/sheet semantics over Radix's focus trap and portal. The skin chooses
130
+ * the presentation (modal, sheet, fullscreen) through the `presentation`
131
+ * attribute; the core owns dismissal and focus behaviour.
132
+ *
133
+ * @module @snap-rail/ui/components/dialog
134
+ */
135
+ /** The dialog root. */
136
+ const Dialog = DialogPrimitive.Root;
137
+ /** The portalled dialog content. */
138
+ function DialogContent({ children, className, presentation = "auto", size = "md", showClose = true }) {
139
+ const resolved = presentation === "auto" ? "modal" : presentation;
140
+ return jsxs(DialogPrimitive.Portal, { children: [jsx(DialogPrimitive.Overlay, {
141
+ ...part("dialog", "overlay"),
142
+ className: "snap-dialog-overlay"
143
+ }), jsxs(DialogPrimitive.Content, {
144
+ ...part("dialog", "panel"),
145
+ className: cn("snap-dialog-panel", className),
146
+ "data-presentation": resolved,
147
+ "data-size": size,
148
+ children: [children, showClose && jsx(DialogPrimitive.Close, {
149
+ ...part("dialog", "close"),
150
+ className: "snap-dialog-close",
151
+ "aria-label": "关闭",
152
+ children: "×"
153
+ })]
154
+ })] });
155
+ }
156
+ /** The dialog title. */
157
+ function DialogTitle({ children, className }) {
158
+ return jsx(DialogPrimitive.Title, {
159
+ ...part("dialog", "title"),
160
+ className: cn("snap-dialog-title", className),
161
+ children
162
+ });
163
+ }
164
+ //#endregion
165
+ //#region lib/types/components/controls.js
166
+ /** The tabs root. */
167
+ const Tabs = TabsPrimitive.Root;
168
+ /** The tabs list. */
169
+ function TabsList({ className, children, ...props }) {
170
+ return jsx(TabsPrimitive.List, {
171
+ ...part("tabs", "list"),
172
+ className: cn("snap-tabs-list", className),
173
+ ...props,
174
+ children
175
+ });
176
+ }
177
+ /** A tab trigger. */
178
+ function TabsTrigger({ className, children, ...props }) {
179
+ return jsx(TabsPrimitive.Trigger, {
180
+ ...part("tabs", "trigger"),
181
+ className: cn("snap-tabs-trigger", className),
182
+ ...props,
183
+ children
184
+ });
185
+ }
186
+ /** A boolean switch. */
187
+ function Switch({ className, label, ...props }) {
188
+ return jsxs("label", {
189
+ className: "snap-switch-label",
190
+ children: [jsx(SwitchPrimitive.Root, {
191
+ ...part("switch", "root"),
192
+ className: cn("snap-switch", className),
193
+ ...props,
194
+ children: jsx(SwitchPrimitive.Thumb, {
195
+ ...part("switch", "thumb"),
196
+ className: "snap-switch-thumb"
197
+ })
198
+ }), label !== void 0 && jsx("span", {
199
+ ...part("switch", "label"),
200
+ children: label
201
+ })]
202
+ });
203
+ }
204
+ //#endregion
205
+ //#region lib/types/components/feedback.js
206
+ /** A compact status/count label. */
207
+ function Badge({ children, intent = "neutral", className }) {
208
+ return jsx("span", {
209
+ ...part("badge", "root"),
210
+ className: cn("snap-badge", className),
211
+ "data-intent": intent,
212
+ children
213
+ });
214
+ }
215
+ /** A centered empty/zero-state block. */
216
+ function EmptyState({ title, description, icon, action, className }) {
217
+ return jsxs("div", {
218
+ ...part("empty-state", "root"),
219
+ className: cn("snap-empty-state", className),
220
+ children: [
221
+ icon !== void 0 && jsx("div", {
222
+ ...part("empty-state", "icon"),
223
+ children: icon
224
+ }),
225
+ jsx("div", {
226
+ ...part("empty-state", "title"),
227
+ children: title
228
+ }),
229
+ description !== void 0 && jsx("div", {
230
+ ...part("empty-state", "description"),
231
+ children: description
232
+ }),
233
+ action !== void 0 && jsx("div", {
234
+ ...part("empty-state", "action"),
235
+ children: action
236
+ })
237
+ ]
238
+ });
239
+ }
240
+ //#endregion
241
+ //#region lib/types/components/layout.js
242
+ /** A vertical flow. */
243
+ function Stack({ gap = "md", padding, align, className, style, children }) {
244
+ return jsx("div", {
245
+ ...part("stack", "root"),
246
+ className: cn("snap-stack", className),
247
+ style: {
248
+ display: "flex",
249
+ flexDirection: "column",
250
+ gap: space(gap),
251
+ ...padding !== void 0 ? { padding: space(padding) } : {},
252
+ ...align !== void 0 ? { alignItems: align } : {},
253
+ ...style
254
+ },
255
+ children
256
+ });
257
+ }
258
+ /** A horizontal flow. */
259
+ function Row({ gap = "sm", padding, align, justify, wrap = false, className, style, children }) {
260
+ return jsx("div", {
261
+ ...part("row", "root"),
262
+ className: cn("snap-row", className),
263
+ style: {
264
+ display: "flex",
265
+ flexDirection: "row",
266
+ gap: space(gap),
267
+ ...padding !== void 0 ? { padding: space(padding) } : {},
268
+ ...align !== void 0 ? { alignItems: align } : {},
269
+ ...justify !== void 0 ? { justifyContent: justify } : {},
270
+ ...wrap ? { flexWrap: "wrap" } : {},
271
+ ...style
272
+ },
273
+ children
274
+ });
275
+ }
276
+ /** The operator-window shell frame. */
277
+ function AppShell({ header, sidebar, footer, className, style, children }) {
278
+ return jsxs("div", {
279
+ ...part("app-shell", "root"),
280
+ className: cn("snap-app-shell", className),
281
+ style: {
282
+ display: "flex",
283
+ flexDirection: "column",
284
+ height: "100%",
285
+ minHeight: 0,
286
+ ...style
287
+ },
288
+ children: [
289
+ header !== void 0 && jsx("div", {
290
+ ...part("app-shell", "header"),
291
+ style: { flexShrink: 0 },
292
+ children: header
293
+ }),
294
+ jsxs("div", {
295
+ style: {
296
+ display: "flex",
297
+ flex: 1,
298
+ minHeight: 0
299
+ },
300
+ children: [sidebar !== void 0 && jsx("div", {
301
+ ...part("app-shell", "sidebar"),
302
+ style: {
303
+ flexShrink: 0,
304
+ minHeight: 0
305
+ },
306
+ children: sidebar
307
+ }), jsx("div", {
308
+ ...part("app-shell", "main"),
309
+ style: {
310
+ flex: 1,
311
+ minWidth: 0,
312
+ minHeight: 0,
313
+ overflow: "auto"
314
+ },
315
+ children
316
+ })]
317
+ }),
318
+ footer !== void 0 && jsx("div", {
319
+ ...part("app-shell", "footer"),
320
+ style: { flexShrink: 0 },
321
+ children: footer
322
+ })
323
+ ]
324
+ });
325
+ }
326
+ /** A page body with its own header/actions anatomy. */
327
+ function Page({ header, actions, title, description, className, style, children }) {
328
+ return jsxs("div", {
329
+ ...part("page", "root"),
330
+ className: cn("snap-page", className),
331
+ style: {
332
+ display: "flex",
333
+ flexDirection: "column",
334
+ minHeight: 0,
335
+ ...style
336
+ },
337
+ children: [(header !== void 0 || title !== void 0 || description !== void 0 || actions !== void 0) && jsxs("div", {
338
+ ...part("page", "header"),
339
+ style: {
340
+ display: "flex",
341
+ flexDirection: "column",
342
+ gap: space("sm"),
343
+ flexShrink: 0
344
+ },
345
+ children: [
346
+ jsxs("div", {
347
+ style: {
348
+ display: "flex",
349
+ alignItems: "flex-start",
350
+ justifyContent: "space-between",
351
+ gap: space("md")
352
+ },
353
+ children: [title !== void 0 && jsx("div", {
354
+ ...part("page", "title"),
355
+ children: title
356
+ }), actions !== void 0 && jsx("div", {
357
+ ...part("page", "actions"),
358
+ children: actions
359
+ })]
360
+ }),
361
+ description !== void 0 && jsx("div", {
362
+ ...part("page", "description"),
363
+ children: description
364
+ }),
365
+ header
366
+ ]
367
+ }), jsx("div", {
368
+ ...part("page", "body"),
369
+ style: {
370
+ display: "flex",
371
+ flexDirection: "column",
372
+ flex: 1,
373
+ minHeight: 0
374
+ },
375
+ children
376
+ })]
377
+ });
378
+ }
379
+ /** A titled content section. */
380
+ function Section({ title, description, actions, className, style, children }) {
381
+ return jsxs("section", {
382
+ ...part("section", "root"),
383
+ className: cn("snap-section", className),
384
+ style: {
385
+ display: "flex",
386
+ flexDirection: "column",
387
+ gap: space("sm"),
388
+ ...style
389
+ },
390
+ children: [(title !== void 0 || actions !== void 0) && jsxs("div", {
391
+ ...part("section", "header"),
392
+ style: {
393
+ display: "flex",
394
+ alignItems: "center",
395
+ justifyContent: "space-between",
396
+ gap: space("md")
397
+ },
398
+ children: [jsxs("div", { children: [title !== void 0 && jsx("div", {
399
+ ...part("section", "title"),
400
+ children: title
401
+ }), description !== void 0 && jsx("div", { children: description })] }), actions !== void 0 && jsx("div", {
402
+ ...part("section", "actions"),
403
+ children: actions
404
+ })]
405
+ }), jsx("div", {
406
+ ...part("section", "body"),
407
+ children
408
+ })]
409
+ });
410
+ }
411
+ //#endregion
412
+ //#region lib/types/components/surface.js
413
+ /** A semantic table. */
414
+ function Table({ className, children, ...props }) {
415
+ return jsx("div", {
416
+ ...part("table", "root"),
417
+ className: cn("snap-table", className),
418
+ children: jsx("table", {
419
+ ...props,
420
+ children
421
+ })
422
+ });
423
+ }
424
+ /** The table head. */
425
+ function TableHeader({ className, children, ...props }) {
426
+ return jsx("thead", {
427
+ ...part("table", "header"),
428
+ className: cn("snap-table-header", className),
429
+ ...props,
430
+ children
431
+ });
432
+ }
433
+ /** The table body. */
434
+ function TableBody({ className, children, ...props }) {
435
+ return jsx("tbody", {
436
+ ...part("table", "body"),
437
+ className: cn("snap-table-body", className),
438
+ ...props,
439
+ children
440
+ });
441
+ }
442
+ /** A table row. */
443
+ function TableRow({ className, children, ...props }) {
444
+ return jsx("tr", {
445
+ ...part("table", "row"),
446
+ className: cn("snap-table-row", className),
447
+ ...props,
448
+ children
449
+ });
450
+ }
451
+ /** A table header cell. */
452
+ function TableHead({ className, children, ...props }) {
453
+ return jsx("th", {
454
+ ...part("table", "head"),
455
+ className: cn("snap-table-head", className),
456
+ ...props,
457
+ children
458
+ });
459
+ }
460
+ /** A table body cell. */
461
+ function TableCell({ className, children, ...props }) {
462
+ return jsx("td", {
463
+ ...part("table", "cell"),
464
+ className: cn("snap-table-cell", className),
465
+ ...props,
466
+ children
467
+ });
468
+ }
469
+ //#endregion
470
+ //#region lib/types/contract.js
471
+ /** The stable region slots every layout may provide. */
472
+ const UI_SLOT_IDS = {
473
+ /** The one active layout of the operator window. */
474
+ layout: "shell.layout",
475
+ /** The operator titlebar. */
476
+ titlebar: "shell.titlebar",
477
+ /** Contributed titlebar actions. */
478
+ titlebarActions: "shell.titlebar.actions",
479
+ /** Sidebar extension area. */
480
+ sidebar: "shell.sidebar",
481
+ /** Statusbar extension area. */
482
+ statusbar: "shell.statusbar",
483
+ /** The global overlay stack (dialogs, confirms). */
484
+ overlay: "shell.overlay",
485
+ /** The floating ball surface face. */
486
+ ball: "shell.ball",
487
+ /** The settings trigger. */
488
+ settingsEntry: "settings.entry",
489
+ /** The settings surface itself. */
490
+ settingsSurface: "settings.surface"
491
+ };
492
+ /** A named custom slot is owned by a publisher package. */
493
+ const CUSTOM_SLOT_PATTERN = /^custom:[a-z0-9][a-z0-9-]*\.[a-z0-9][a-z0-9-]*$/;
494
+ /** Check a region-slot id at runtime. */
495
+ function isValidSlotId(value) {
496
+ return Object.values(UI_SLOT_IDS).includes(value) || CUSTOM_SLOT_PATTERN.test(value);
497
+ }
498
+ /** The occupancy mode of every built-in slot. */
499
+ const UI_SLOT_MODES = {
500
+ [UI_SLOT_IDS.layout]: "one",
501
+ [UI_SLOT_IDS.titlebar]: "one",
502
+ [UI_SLOT_IDS.titlebarActions]: "many",
503
+ [UI_SLOT_IDS.sidebar]: "many",
504
+ [UI_SLOT_IDS.statusbar]: "many",
505
+ [UI_SLOT_IDS.overlay]: "many",
506
+ [UI_SLOT_IDS.ball]: "one",
507
+ [UI_SLOT_IDS.settingsEntry]: "one",
508
+ [UI_SLOT_IDS.settingsSurface]: "one"
509
+ };
510
+ //#endregion
511
+ //#region lib/types/pages.js
512
+ /**
513
+ * The main-page registry: business views contribute a page, a layout renders
514
+ * the active one. This is deliberately separate from settings pages and from
515
+ * the chrome slots; gating/workflows can wrap it later without changing the
516
+ * page authoring surface.
517
+ *
518
+ * @module @snap-rail/ui/pages
519
+ */
520
+ /** Mount the page registry. */
521
+ const pagesPlugin = {
522
+ name: "snap-rail ui pages",
523
+ apply(ctx) {
524
+ const pages = /* @__PURE__ */ new Map();
525
+ const listeners = /* @__PURE__ */ new Set();
526
+ let revision = 0;
527
+ let seq = 0;
528
+ let activeId;
529
+ const ordered = () => [...pages.values()].sort((a, b) => a.page.order - b.page.order || a.seq - b.seq).map((entry) => entry.page);
530
+ const notify = () => {
531
+ revision += 1;
532
+ for (const listener of listeners) listener();
533
+ ctx.emit("pages/changed");
534
+ };
535
+ const service = {
536
+ registerPage(caller, page) {
537
+ pages.set(page.id, {
538
+ page,
539
+ seq: seq++
540
+ });
541
+ activeId ??= page.id;
542
+ notify();
543
+ const remove = () => {
544
+ if (!pages.delete(page.id)) return;
545
+ if (activeId === page.id) activeId = ordered()[0]?.id;
546
+ notify();
547
+ };
548
+ caller.effect(() => remove);
549
+ return remove;
550
+ },
551
+ list: ordered,
552
+ active() {
553
+ if (activeId === void 0) return void 0;
554
+ return pages.get(activeId)?.page ?? ordered()[0];
555
+ },
556
+ setActive(id) {
557
+ if (!pages.has(id) || activeId === id) return;
558
+ activeId = id;
559
+ notify();
560
+ },
561
+ subscribe(listener) {
562
+ listeners.add(listener);
563
+ return () => {
564
+ listeners.delete(listener);
565
+ };
566
+ },
567
+ version: () => revision
568
+ };
569
+ ctx.provide("pages", service);
570
+ }
571
+ };
572
+ /** Re-render a layout when the page registry or active page changes. */
573
+ function usePagesVersion(ctx) {
574
+ return useSyncExternalStore(ctx.pages.subscribe, ctx.pages.version, ctx.pages.version);
575
+ }
576
+ //#endregion
577
+ //#region lib/types/slots.js
578
+ const MAX_DIAGNOSTICS = 100;
579
+ /** The default definition of a built-in slot. */
580
+ function builtinDefinition(slot) {
581
+ return {
582
+ id: slot,
583
+ mode: UI_SLOT_MODES[slot] ?? "many"
584
+ };
585
+ }
586
+ /** Mount the `ctx.ui` registry. */
587
+ const slotsPlugin = {
588
+ name: "snap-rail ui slots",
589
+ apply(ctx) {
590
+ const entries = /* @__PURE__ */ new Map();
591
+ const layouts = /* @__PURE__ */ new Map();
592
+ const listeners = /* @__PURE__ */ new Set();
593
+ const diagnostics = [];
594
+ let revision = 0;
595
+ let layoutSeq = 0;
596
+ for (const slot of Object.values(UI_SLOT_IDS)) entries.set(slot, {
597
+ def: builtinDefinition(slot),
598
+ contributions: /* @__PURE__ */ new Map(),
599
+ nextSeq: 0
600
+ });
601
+ const notify = (slot) => {
602
+ revision += 1;
603
+ for (const listener of listeners) listener();
604
+ ctx.emit("ui/changed");
605
+ if (slot !== void 0) ctx.emit("ui/slot-changed", slot);
606
+ };
607
+ const diagnose = (diagnostic) => {
608
+ diagnostics.push({
609
+ ...diagnostic,
610
+ time: Date.now()
611
+ });
612
+ if (diagnostics.length > MAX_DIAGNOSTICS) diagnostics.shift();
613
+ };
614
+ const activeLayoutOf = () => {
615
+ return [...layouts.values()].sort((a, b) => {
616
+ const pa = a.layout.precedence ?? 0;
617
+ return (b.layout.precedence ?? 0) - pa || a.layout.order - b.layout.order || b.seq - a.seq;
618
+ })[0]?.layout;
619
+ };
620
+ const ensureEntry = (slot) => {
621
+ const existing = entries.get(slot);
622
+ if (existing !== void 0) return existing;
623
+ const entry = {
624
+ def: {
625
+ id: slot,
626
+ mode: "many"
627
+ },
628
+ contributions: /* @__PURE__ */ new Map(),
629
+ nextSeq: 0
630
+ };
631
+ entries.set(slot, entry);
632
+ return entry;
633
+ };
634
+ ctx.provide("ui", {
635
+ defineSlot(caller, def) {
636
+ if (!isValidSlotId(def.id)) {
637
+ diagnose({
638
+ kind: "unknown-slot",
639
+ slot: def.id,
640
+ message: `插槽 id 不合法:${def.id}`
641
+ });
642
+ return () => {};
643
+ }
644
+ if (Object.values(UI_SLOT_IDS).includes(def.id)) {
645
+ diagnose({
646
+ kind: "duplicate-id",
647
+ slot: def.id,
648
+ message: `内置插槽不能被重新定义:${def.id}`
649
+ });
650
+ return () => {};
651
+ }
652
+ const entry = ensureEntry(def.id);
653
+ entry.def = def;
654
+ notify(def.id);
655
+ const remove = () => {
656
+ if (entries.delete(def.id)) notify(def.id);
657
+ };
658
+ caller.effect(() => remove);
659
+ return remove;
660
+ },
661
+ registerLayout(caller, layout) {
662
+ for (const slot of layout.provides) if (!isValidSlotId(slot)) diagnose({
663
+ kind: "unknown-slot",
664
+ id: layout.id,
665
+ slot,
666
+ message: `布局 ${layout.id} 声明了非法插槽:${slot}`
667
+ });
668
+ else if (!entries.has(slot)) diagnose({
669
+ kind: "unknown-slot",
670
+ id: layout.id,
671
+ slot,
672
+ message: `布局 ${layout.id} 声明了未定义的插槽:${slot}`
673
+ });
674
+ layouts.set(layout.id, {
675
+ layout,
676
+ seq: layoutSeq++
677
+ });
678
+ notify();
679
+ const remove = () => {
680
+ if (layouts.delete(layout.id)) notify();
681
+ };
682
+ caller.effect(() => remove);
683
+ return remove;
684
+ },
685
+ contribute(caller, slot, contribution) {
686
+ if (!isValidSlotId(slot)) {
687
+ diagnose({
688
+ kind: "unknown-slot",
689
+ id: contribution.id,
690
+ slot,
691
+ message: `贡献 ${contribution.id} 指向非法插槽:${slot}`
692
+ });
693
+ return () => {};
694
+ }
695
+ const entry = entries.get(slot);
696
+ if (entry === void 0) diagnose({
697
+ kind: "unknown-slot",
698
+ id: contribution.id,
699
+ slot,
700
+ message: `贡献 ${contribution.id} 指向未定义的插槽:${slot}`
701
+ });
702
+ const target = entry ?? ensureEntry(slot);
703
+ target.contributions.set(contribution.id, {
704
+ contribution,
705
+ seq: target.nextSeq++
706
+ });
707
+ notify(slot);
708
+ const remove = () => {
709
+ if (target.contributions.delete(contribution.id)) notify(slot);
710
+ };
711
+ caller.effect(() => remove);
712
+ return remove;
713
+ },
714
+ list(slot) {
715
+ const entry = entries.get(slot);
716
+ if (entry === void 0) return [];
717
+ const ranked = [...entry.contributions.values()].sort((a, b) => a.contribution.order - b.contribution.order || a.seq - b.seq);
718
+ if (entry.def.mode !== "one" || ranked.length === 0) return ranked.map((item) => item.contribution);
719
+ return [ranked.reduce((best, item) => {
720
+ const delta = (item.contribution.precedence ?? 0) - (best.contribution.precedence ?? 0);
721
+ return delta > 0 || delta === 0 && item.seq > best.seq ? item : best;
722
+ }).contribution];
723
+ },
724
+ definition: (slot) => entries.get(slot)?.def,
725
+ activeLayout: activeLayoutOf,
726
+ hasProvider(slot) {
727
+ const active = activeLayoutOf();
728
+ return active !== void 0 && active.provides.includes(slot);
729
+ },
730
+ diagnostics: () => diagnostics,
731
+ subscribe(listener) {
732
+ listeners.add(listener);
733
+ return () => {
734
+ listeners.delete(listener);
735
+ };
736
+ },
737
+ version: () => revision
738
+ });
739
+ }
740
+ };
741
+ /** The React context carrying the active UI service. */
742
+ const UiContext = createContext(null);
743
+ /** Provide the UI service to a slot tree. */
744
+ function UiProvider(props) {
745
+ return jsx(UiContext.Provider, {
746
+ value: props.service,
747
+ children: props.children
748
+ });
749
+ }
750
+ /** Read the UI service; throws when a slot tree is mounted outside the runtime. */
751
+ function useUi() {
752
+ const service = useContext(UiContext);
753
+ if (service === null) throw new Error("useUi: no UiProvider in the tree");
754
+ return service;
755
+ }
756
+ /** Re-render on every registry change. */
757
+ function useUiVersion(service) {
758
+ return useSyncExternalStore(service.subscribe, service.version, service.version);
759
+ }
760
+ /** Keep one broken contribution from taking down the whole shell. */
761
+ var SlotBoundary = class extends Component {
762
+ state = { failed: false };
763
+ static getDerivedStateFromError() {
764
+ return { failed: true };
765
+ }
766
+ componentDidCatch(error) {
767
+ this.props.onError(error);
768
+ }
769
+ render() {
770
+ return this.state.failed ? null : this.props.children;
771
+ }
772
+ };
773
+ /** A region-slot outlet rendered by a layout. */
774
+ function Slot(props) {
775
+ const service = useUi();
776
+ useUiVersion(service);
777
+ const provided = service.hasProvider(props.name);
778
+ const entry = service.list(props.name);
779
+ const def = service.definition(props.name);
780
+ const context = props.context ?? {};
781
+ if (!provided) return null;
782
+ if (entry.length === 0) {
783
+ const fallback = def?.fallback;
784
+ return fallback?.(context) ?? null;
785
+ }
786
+ return jsx(Fragment, { children: entry.map((item) => jsx(SlotBoundary, {
787
+ onError: (error) => {
788
+ console.error(`[snap-rail ui] slot ${props.name} contribution ${item.id} failed`, error);
789
+ },
790
+ children: item.render(context)
791
+ }, item.id)) });
792
+ }
793
+ /** Re-render when the active layout changes. */
794
+ function useActiveLayout() {
795
+ const service = useUi();
796
+ useUiVersion(service);
797
+ return service.activeLayout();
798
+ }
799
+ //#endregion
800
+ //#region lib/types/fallback.js
801
+ /** A compact glyph for the window controls. */
802
+ function Glyph(props) {
803
+ return jsx("span", {
804
+ "aria-hidden": "true",
805
+ style: { lineHeight: 1 },
806
+ children: props.children
807
+ });
808
+ }
809
+ /** The fallback body. */
810
+ function FallbackShell(props) {
811
+ useUiVersion(props.ctx.ui);
812
+ usePagesVersion(props.ctx);
813
+ const active = props.ctx.pages.active();
814
+ const control = (action) => {
815
+ props.ctx.client.link.call("window.control", { action }).catch(() => {});
816
+ };
817
+ const header = jsxs("div", {
818
+ className: "snap-fallback-titlebar",
819
+ style: {
820
+ display: "flex",
821
+ alignItems: "center",
822
+ justifyContent: "space-between",
823
+ minHeight: 48,
824
+ paddingLeft: 16
825
+ },
826
+ children: [jsxs("span", {
827
+ style: {
828
+ color: "var(--snap-color-muted)",
829
+ fontSize: "var(--snap-text-sm)"
830
+ },
831
+ children: ["snap-rail", active !== void 0 ? ` · ${active.title}` : ""]
832
+ }), jsxs("div", {
833
+ style: {
834
+ display: "flex",
835
+ alignItems: "center",
836
+ gap: 4
837
+ },
838
+ children: [
839
+ jsx(Slot, {
840
+ name: UI_SLOT_IDS.settingsEntry,
841
+ context: {
842
+ presentation: "icon",
843
+ open: () => props.ctx.settings.open(),
844
+ isOpen: props.ctx.settings.isOpen()
845
+ }
846
+ }),
847
+ jsx(IconButton, {
848
+ icon: jsx(Glyph, { children: "—" }),
849
+ label: "最小化",
850
+ onClick: () => control("minimize")
851
+ }),
852
+ jsx(IconButton, {
853
+ icon: jsx(Glyph, { children: "□" }),
854
+ label: "最大化切换",
855
+ onClick: () => control("toggle-maximize")
856
+ }),
857
+ jsx(IconButton, {
858
+ icon: jsx(Glyph, { children: "×" }),
859
+ label: "关闭",
860
+ onClick: () => control("close")
861
+ })
862
+ ]
863
+ })]
864
+ });
865
+ return jsx(AppShell, {
866
+ header,
867
+ children: active !== void 0 ? active.render() : jsx("div", {
868
+ style: {
869
+ display: "flex",
870
+ minHeight: "100%",
871
+ alignItems: "center",
872
+ justifyContent: "center",
873
+ padding: 24
874
+ },
875
+ children: jsx(EmptyState, {
876
+ title: "尚未激活业务套件",
877
+ description: "打开设置的「插件管理」页,安装并启用一个业务套件;驱动也在同一处安装。",
878
+ action: jsx(Button, {
879
+ intent: "primary",
880
+ onClick: () => props.ctx.settings.open("plugins"),
881
+ children: "打开设置"
882
+ })
883
+ })
884
+ })
885
+ });
886
+ }
887
+ /** The fallback layout occupant. */
888
+ const fallbackPlugin = {
889
+ name: "snap-rail fallback layout",
890
+ inject: [
891
+ "ui",
892
+ "settings",
893
+ "pages",
894
+ "client"
895
+ ],
896
+ apply(ctx) {
897
+ ctx.ui.registerLayout(ctx, {
898
+ id: "fallback",
899
+ order: 200,
900
+ precedence: 0,
901
+ provides: [
902
+ UI_SLOT_IDS.titlebar,
903
+ UI_SLOT_IDS.titlebarActions,
904
+ UI_SLOT_IDS.sidebar,
905
+ UI_SLOT_IDS.statusbar,
906
+ UI_SLOT_IDS.settingsEntry
907
+ ],
908
+ window: {
909
+ width: 1280,
910
+ height: 800,
911
+ minWidth: 960,
912
+ minHeight: 600
913
+ },
914
+ ball: { size: 128 },
915
+ render: () => jsx(FallbackShell, { ctx })
916
+ });
917
+ }
918
+ };
919
+ //#endregion
920
+ //#region lib/types/settings-pages/appearance.js
921
+ const THEME_KEY = "ui.theme";
922
+ const WINDOW_KEY = "ui.window";
923
+ const BALL_KEY = "ui.ball";
924
+ const WINDOW_PRESETS = [
925
+ {
926
+ label: "1280 × 800",
927
+ width: 1280,
928
+ height: 800
929
+ },
930
+ {
931
+ label: "1440 × 900",
932
+ width: 1440,
933
+ height: 900
934
+ },
935
+ {
936
+ label: "1600 × 900",
937
+ width: 1600,
938
+ height: 900
939
+ },
940
+ {
941
+ label: "1920 × 1080",
942
+ width: 1920,
943
+ height: 1080
944
+ }
945
+ ];
946
+ const BALL_SIZES = [
947
+ 96,
948
+ 112,
949
+ 128,
950
+ 160,
951
+ 192
952
+ ];
953
+ /** Read a settings value through the host link. */
954
+ async function readSetting(link, key) {
955
+ const result = await link.call("settings.get", { key });
956
+ return result.ok ? result.value.value : void 0;
957
+ }
958
+ /** Write a settings value; `null` clears the row. */
959
+ async function writeSetting(link, key, value) {
960
+ await link.call("settings.set", {
961
+ key,
962
+ value
963
+ });
964
+ }
965
+ /** The appearance/display page. */
966
+ function AppearancePage() {
967
+ const { link } = useClient();
968
+ const [mode, setMode] = useState("dark");
969
+ const [windowPreset, setWindowPreset] = useState(void 0);
970
+ const [ballSize, setBallSize] = useState(128);
971
+ const [ballEnabled, setBallEnabled] = useState(true);
972
+ const [ballDockable, setBallDockable] = useState(true);
973
+ const [ballCollapse, setBallCollapse] = useState(true);
974
+ const [skins, setSkins] = useState([]);
975
+ const [error, setError] = useState(void 0);
976
+ const [busy, setBusy] = useState(false);
977
+ useEffect(() => {
978
+ Promise.all([
979
+ readSetting(link, THEME_KEY),
980
+ readSetting(link, WINDOW_KEY),
981
+ readSetting(link, BALL_KEY),
982
+ link.call("plugins.list", {})
983
+ ]).then(([theme, windowRow, ballRow, plugins]) => {
984
+ const themeMode = theme?.mode;
985
+ if (themeMode === "system" || themeMode === "light" || themeMode === "dark") setMode(themeMode);
986
+ const windowProfile = windowRow;
987
+ if (windowProfile !== null && windowProfile !== void 0 && typeof windowProfile.width === "number" && typeof windowProfile.height === "number") {
988
+ const preset = WINDOW_PRESETS.find((item) => item.width === windowProfile.width && item.height === windowProfile.height);
989
+ setWindowPreset(preset?.label);
990
+ }
991
+ const ballProfile = ballRow;
992
+ if (ballProfile !== null && ballProfile !== void 0 && typeof ballProfile.size === "number") setBallSize(ballProfile.size);
993
+ if (ballProfile !== null && ballProfile !== void 0 && typeof ballProfile.enabled === "boolean") setBallEnabled(ballProfile.enabled);
994
+ if (ballProfile !== null && ballProfile !== void 0 && typeof ballProfile.dockable === "boolean") setBallDockable(ballProfile.dockable);
995
+ if (ballProfile !== null && ballProfile !== void 0 && typeof ballProfile.collapse === "boolean") setBallCollapse(ballProfile.collapse);
996
+ if (plugins.ok) setSkins(plugins.value.plugins.filter((plugin) => plugin.kind === "skin" && plugin.source !== "builtin").map((plugin) => ({
997
+ name: plugin.packageName,
998
+ title: plugin.title ?? plugin.packageName,
999
+ enabled: plugin.enabled
1000
+ })));
1001
+ }).catch((cause) => {
1002
+ setError(cause instanceof Error ? cause.message : String(cause));
1003
+ });
1004
+ return subscribeFrame(link, "settings/changed", settingsChangedSchema, (change) => {
1005
+ if (change.key === THEME_KEY) {
1006
+ const next = change.value?.mode;
1007
+ if (next === "system" || next === "light" || next === "dark") setMode(next);
1008
+ }
1009
+ });
1010
+ }, [link]);
1011
+ const saveTheme = (next) => {
1012
+ setMode(next);
1013
+ writeSetting(link, THEME_KEY, { mode: next }).catch((cause) => {
1014
+ setError(cause instanceof Error ? cause.message : String(cause));
1015
+ });
1016
+ };
1017
+ const saveWindow = (width, height, label) => {
1018
+ setWindowPreset(label);
1019
+ writeSetting(link, WINDOW_KEY, {
1020
+ width,
1021
+ height,
1022
+ minWidth: 960,
1023
+ minHeight: 600
1024
+ }).catch((cause) => {
1025
+ setError(cause instanceof Error ? cause.message : String(cause));
1026
+ });
1027
+ };
1028
+ const resetWindow = () => {
1029
+ setWindowPreset(void 0);
1030
+ writeSetting(link, WINDOW_KEY, null).catch((cause) => {
1031
+ setError(cause instanceof Error ? cause.message : String(cause));
1032
+ });
1033
+ };
1034
+ const saveBall = (patch) => {
1035
+ const next = {
1036
+ size: patch.size ?? ballSize,
1037
+ enabled: patch.enabled ?? ballEnabled,
1038
+ dockable: patch.dockable ?? ballDockable,
1039
+ collapse: patch.collapse ?? ballCollapse
1040
+ };
1041
+ if (patch.size !== void 0) setBallSize(patch.size);
1042
+ if (patch.enabled !== void 0) setBallEnabled(patch.enabled);
1043
+ if (patch.dockable !== void 0) setBallDockable(patch.dockable);
1044
+ if (patch.collapse !== void 0) setBallCollapse(patch.collapse);
1045
+ writeSetting(link, BALL_KEY, next).catch((cause) => {
1046
+ setError(cause instanceof Error ? cause.message : String(cause));
1047
+ });
1048
+ };
1049
+ const toggleSkin = (skin) => {
1050
+ setBusy(true);
1051
+ link.call("plugins.set-enabled", {
1052
+ name: skin.name,
1053
+ enabled: !skin.enabled
1054
+ }).then((result) => {
1055
+ if (!result.ok) {
1056
+ setError(rpcErrorText(result.error));
1057
+ return;
1058
+ }
1059
+ setSkins((current) => current.map((item) => item.name === skin.name ? {
1060
+ ...item,
1061
+ enabled: !item.enabled
1062
+ } : item));
1063
+ }).catch((cause) => {
1064
+ setError(cause instanceof Error ? cause.message : String(cause));
1065
+ }).finally(() => {
1066
+ setBusy(false);
1067
+ });
1068
+ };
1069
+ return jsx(Page, {
1070
+ title: "外观与显示",
1071
+ description: "主题、皮肤、主窗口尺寸和悬浮球外观;保存后立即生效。",
1072
+ children: jsxs(Stack, {
1073
+ gap: "lg",
1074
+ children: [
1075
+ error !== void 0 && jsx("div", {
1076
+ role: "alert",
1077
+ style: { color: "var(--snap-color-danger)" },
1078
+ children: error
1079
+ }),
1080
+ jsx(Section, {
1081
+ title: "主题模式",
1082
+ children: jsx(Tabs, {
1083
+ value: mode,
1084
+ onValueChange: (value) => saveTheme(value),
1085
+ children: jsxs(TabsList, {
1086
+ "aria-label": "主题模式",
1087
+ children: [
1088
+ jsx(TabsTrigger, {
1089
+ value: "system",
1090
+ children: "跟随系统"
1091
+ }),
1092
+ jsx(TabsTrigger, {
1093
+ value: "light",
1094
+ children: "白天"
1095
+ }),
1096
+ jsx(TabsTrigger, {
1097
+ value: "dark",
1098
+ children: "黑夜"
1099
+ })
1100
+ ]
1101
+ })
1102
+ })
1103
+ }),
1104
+ jsx(Section, {
1105
+ title: "界面皮肤",
1106
+ description: "皮肤由独立插件提供;启用后需要重启终端。",
1107
+ children: jsxs(Stack, {
1108
+ gap: "sm",
1109
+ children: [jsxs(Row, {
1110
+ gap: "sm",
1111
+ align: "center",
1112
+ children: [jsx(Badge, {
1113
+ intent: "success",
1114
+ children: "默认(内置)"
1115
+ }), jsx("span", {
1116
+ style: {
1117
+ color: "var(--snap-color-muted)",
1118
+ fontSize: "var(--snap-text-sm)"
1119
+ },
1120
+ children: "工业钴蓝,始终可用"
1121
+ })]
1122
+ }), skins.length === 0 ? jsx("span", {
1123
+ style: { color: "var(--snap-color-muted)" },
1124
+ children: "尚未安装其他皮肤插件。"
1125
+ }) : skins.map((skin) => jsxs(Row, {
1126
+ gap: "sm",
1127
+ align: "center",
1128
+ children: [
1129
+ jsx("span", { children: skin.title }),
1130
+ jsx(Badge, {
1131
+ intent: skin.enabled ? "success" : "neutral",
1132
+ children: skin.enabled ? "已启用" : "未启用"
1133
+ }),
1134
+ jsx(Button, {
1135
+ size: "sm",
1136
+ emphasis: "outline",
1137
+ disabled: busy,
1138
+ onClick: () => toggleSkin(skin),
1139
+ children: skin.enabled ? "停用" : "启用"
1140
+ })
1141
+ ]
1142
+ }, skin.name))]
1143
+ })
1144
+ }),
1145
+ jsx(Section, {
1146
+ title: "主窗口",
1147
+ description: "布局/套件会声明推荐尺寸;这里的选择会覆盖布局推荐。",
1148
+ children: jsxs(Stack, {
1149
+ gap: "sm",
1150
+ children: [jsxs(Row, {
1151
+ gap: "sm",
1152
+ wrap: true,
1153
+ children: [WINDOW_PRESETS.map((preset) => jsx(Button, {
1154
+ size: "sm",
1155
+ intent: windowPreset === preset.label ? "primary" : "neutral",
1156
+ emphasis: windowPreset === preset.label ? "solid" : "outline",
1157
+ onClick: () => saveWindow(preset.width, preset.height, preset.label),
1158
+ children: preset.label
1159
+ }, preset.label)), jsx(Button, {
1160
+ size: "sm",
1161
+ emphasis: "ghost",
1162
+ onClick: resetWindow,
1163
+ children: "跟随布局"
1164
+ })]
1165
+ }), jsxs("span", {
1166
+ style: {
1167
+ color: "var(--snap-color-muted)",
1168
+ fontSize: "var(--snap-text-sm)"
1169
+ },
1170
+ children: ["当前:", windowPreset ?? "跟随布局推荐 / 默认 1280 × 800"]
1171
+ })]
1172
+ })
1173
+ }),
1174
+ jsx(Section, {
1175
+ title: "悬浮球",
1176
+ description: "球面尺寸与停泊行为;球内显示内容由业务套件决定。",
1177
+ children: jsxs(Stack, {
1178
+ gap: "md",
1179
+ children: [jsx(Row, {
1180
+ gap: "sm",
1181
+ wrap: true,
1182
+ align: "center",
1183
+ children: BALL_SIZES.map((size) => jsxs(Button, {
1184
+ size: "sm",
1185
+ intent: ballSize === size ? "primary" : "neutral",
1186
+ emphasis: ballSize === size ? "solid" : "outline",
1187
+ onClick: () => saveBall({ size }),
1188
+ children: [size, "px"]
1189
+ }, size))
1190
+ }), jsxs(Row, {
1191
+ gap: "lg",
1192
+ wrap: true,
1193
+ align: "center",
1194
+ children: [
1195
+ jsx(Switch, {
1196
+ label: "启用悬浮球",
1197
+ checked: ballEnabled,
1198
+ onCheckedChange: (enabled) => saveBall({ enabled })
1199
+ }),
1200
+ jsx(Switch, {
1201
+ label: "允许贴边",
1202
+ checked: ballDockable,
1203
+ onCheckedChange: (dockable) => saveBall({ dockable })
1204
+ }),
1205
+ jsx(Switch, {
1206
+ label: "贴边后收起",
1207
+ checked: ballCollapse,
1208
+ onCheckedChange: (collapse) => saveBall({ collapse })
1209
+ })
1210
+ ]
1211
+ })]
1212
+ })
1213
+ })
1214
+ ]
1215
+ })
1216
+ });
1217
+ }
1218
+ //#endregion
1219
+ //#region lib/types/settings-pages/plugins.js
1220
+ /** Human labels for the package kinds. */
1221
+ const KIND_LABELS = {
1222
+ skin: "界面风格",
1223
+ driver: "通讯驱动",
1224
+ suite: "业务套件",
1225
+ plugin: "插件"
1226
+ };
1227
+ const PROFILE_LABELS = {
1228
+ native: "皮肤原生",
1229
+ integrated: "图表桥接",
1230
+ custom: "自定义界面"
1231
+ };
1232
+ /** Collapse entry rows into package rows (one master switch per package). */
1233
+ function packageRows(plugins) {
1234
+ const byPackage = /* @__PURE__ */ new Map();
1235
+ for (const plugin of plugins) {
1236
+ if (plugin.source === "builtin") continue;
1237
+ const existing = byPackage.get(plugin.packageName);
1238
+ if (existing !== void 0) {
1239
+ existing.enabled = existing.enabled || plugin.enabled;
1240
+ continue;
1241
+ }
1242
+ byPackage.set(plugin.packageName, {
1243
+ packageName: plugin.packageName,
1244
+ title: plugin.title ?? plugin.packageName,
1245
+ category: plugin.category ?? "其他",
1246
+ kind: plugin.kind ?? "plugin",
1247
+ ...plugin.version !== void 0 ? { version: plugin.version } : {},
1248
+ enabled: plugin.enabled,
1249
+ source: plugin.source,
1250
+ builtinDefault: plugin.source === "builtin-default",
1251
+ clientOnly: plugin.clientOnly === true,
1252
+ ...plugin.uiProfile !== void 0 ? { uiProfile: plugin.uiProfile } : {}
1253
+ });
1254
+ }
1255
+ return [...byPackage.values()].sort((a, b) => a.category.localeCompare(b.category) || a.title.localeCompare(b.title));
1256
+ }
1257
+ /** The plugin management page. */
1258
+ function PluginsPage({ ctx }) {
1259
+ const [rows, setRows] = useState([]);
1260
+ const [error, setError] = useState(void 0);
1261
+ const [busy, setBusy] = useState(void 0);
1262
+ const [touched, setTouched] = useState(/* @__PURE__ */ new Set());
1263
+ const load = () => {
1264
+ setError(void 0);
1265
+ ctx.client.link.call("plugins.list", {}).then((result) => {
1266
+ if (!result.ok) {
1267
+ setError(rpcErrorText(result.error));
1268
+ return;
1269
+ }
1270
+ setRows(packageRows(result.value.plugins));
1271
+ }).catch((cause) => {
1272
+ setError(cause instanceof Error ? cause.message : String(cause));
1273
+ });
1274
+ };
1275
+ useEffect(load, []);
1276
+ const toggle = (row, enabled) => {
1277
+ setBusy(row.packageName);
1278
+ ctx.client.link.call("plugins.set-enabled", {
1279
+ name: row.packageName,
1280
+ enabled
1281
+ }).then((result) => {
1282
+ if (!result.ok) {
1283
+ setError(rpcErrorText(result.error));
1284
+ return;
1285
+ }
1286
+ setRows((current) => current.map((item) => item.packageName === row.packageName ? {
1287
+ ...item,
1288
+ enabled
1289
+ } : item));
1290
+ setTouched((current) => new Set(current).add(row.packageName));
1291
+ }).catch((cause) => {
1292
+ setError(cause instanceof Error ? cause.message : String(cause));
1293
+ }).finally(() => {
1294
+ setBusy(void 0);
1295
+ });
1296
+ };
1297
+ const uninstall = (row) => {
1298
+ if (!window.confirm(`卸载 ${row.title}?该包的数据也会一起删除。`)) return;
1299
+ setBusy(row.packageName);
1300
+ ctx.client.link.call("plugins.uninstall", { name: row.packageName }).then((result) => {
1301
+ if (!result.ok) {
1302
+ setError(rpcErrorText(result.error));
1303
+ return;
1304
+ }
1305
+ setTouched((current) => new Set(current).add(row.packageName));
1306
+ load();
1307
+ }).catch((cause) => {
1308
+ setError(cause instanceof Error ? cause.message : String(cause));
1309
+ }).finally(() => {
1310
+ setBusy(void 0);
1311
+ });
1312
+ };
1313
+ const groups = /* @__PURE__ */ new Map();
1314
+ for (const row of rows) {
1315
+ const bucket = groups.get(row.category);
1316
+ if (bucket === void 0) groups.set(row.category, [row]);
1317
+ else bucket.push(row);
1318
+ }
1319
+ return jsx(Page, {
1320
+ title: "插件管理",
1321
+ description: "安装驱动、业务页面和界面风格。启用/停用写入 plugins.yml,渲染端在下一次重启后生效。",
1322
+ actions: jsx(Button, {
1323
+ intent: "neutral",
1324
+ emphasis: "outline",
1325
+ onClick: load,
1326
+ children: "刷新"
1327
+ }),
1328
+ children: jsxs(Stack, {
1329
+ gap: "lg",
1330
+ children: [error !== void 0 && jsx("div", {
1331
+ role: "alert",
1332
+ style: { color: "var(--snap-color-danger)" },
1333
+ children: error
1334
+ }), rows.length === 0 ? jsx(EmptyState, {
1335
+ title: "还没有安装插件",
1336
+ description: "把插件 zip 拖入插件安装入口,或从插件源搜索安装。"
1337
+ }) : [...groups.entries()].map(([category, entries]) => jsx(Section, {
1338
+ title: category,
1339
+ children: jsxs(Table, { children: [jsx(TableHeader, { children: jsxs(TableRow, { children: [
1340
+ jsx(TableHead, { children: "名称" }),
1341
+ jsx(TableHead, { children: "类型" }),
1342
+ jsx(TableHead, { children: "版本" }),
1343
+ jsx(TableHead, { children: "状态" }),
1344
+ jsx(TableHead, { children: "操作" })
1345
+ ] }) }), jsx(TableBody, { children: entries.map((row) => jsxs(TableRow, { children: [
1346
+ jsx(TableCell, { children: jsxs("div", {
1347
+ style: {
1348
+ display: "flex",
1349
+ flexDirection: "column"
1350
+ },
1351
+ children: [jsxs("span", {
1352
+ style: {
1353
+ display: "flex",
1354
+ alignItems: "center",
1355
+ gap: "var(--snap-space-sm)"
1356
+ },
1357
+ children: [row.title, row.builtinDefault && jsx(Badge, {
1358
+ intent: "neutral",
1359
+ children: "内置默认"
1360
+ })]
1361
+ }), jsx("span", {
1362
+ style: {
1363
+ color: "var(--snap-color-muted)",
1364
+ fontSize: "var(--snap-text-xs)"
1365
+ },
1366
+ children: row.packageName
1367
+ })]
1368
+ }) }),
1369
+ jsxs(TableCell, { children: [KIND_LABELS[row.kind] ?? row.kind, row.uiProfile !== void 0 && jsx(Badge, {
1370
+ intent: "neutral",
1371
+ children: PROFILE_LABELS[row.uiProfile] ?? row.uiProfile
1372
+ })] }),
1373
+ jsx(TableCell, { children: row.version ?? "—" }),
1374
+ jsx(TableCell, { children: row.builtinDefault ? jsx(Badge, {
1375
+ intent: "success",
1376
+ children: "始终随底座"
1377
+ }) : jsxs("div", {
1378
+ style: {
1379
+ display: "flex",
1380
+ alignItems: "center",
1381
+ gap: "var(--snap-space-sm)"
1382
+ },
1383
+ children: [jsx(Switch, {
1384
+ checked: row.enabled,
1385
+ disabled: busy === row.packageName,
1386
+ onCheckedChange: (next) => toggle(row, next),
1387
+ "aria-label": `启用 ${row.title}`
1388
+ }), touched.has(row.packageName) && jsx(Badge, {
1389
+ intent: "warning",
1390
+ children: "待重启"
1391
+ })]
1392
+ }) }),
1393
+ jsx(TableCell, { children: row.builtinDefault ? jsx("span", {
1394
+ style: { color: "var(--snap-color-muted)" },
1395
+ children: "内置默认不可卸载"
1396
+ }) : jsx(Button, {
1397
+ intent: "danger",
1398
+ emphasis: "ghost",
1399
+ size: "sm",
1400
+ disabled: busy === row.packageName,
1401
+ onClick: () => uninstall(row),
1402
+ children: "卸载"
1403
+ }) })
1404
+ ] }, row.packageName)) })] })
1405
+ }, category))]
1406
+ })
1407
+ });
1408
+ }
1409
+ //#endregion
1410
+ //#region lib/types/settings.js
1411
+ /** Sort pages by order, then registration time. */
1412
+ function orderedPages(pages) {
1413
+ return [...pages].sort((a, b) => a.page.order - b.page.order || a.seq - b.seq).map((entry) => entry.page);
1414
+ }
1415
+ /** A page-level boundary: one broken settings page must not take down the shell. */
1416
+ var SettingsPageBoundary = class extends Component {
1417
+ state = { failed: false };
1418
+ static getDerivedStateFromError() {
1419
+ return { failed: true };
1420
+ }
1421
+ componentDidCatch(error) {
1422
+ console.error(`[snap-rail ui] settings page ${this.props.page} failed`, error);
1423
+ }
1424
+ render() {
1425
+ return this.state.failed ? jsx(EmptyState, {
1426
+ title: "设置页加载失败",
1427
+ description: "该页面抛出了异常,其他设置页仍可使用。"
1428
+ }) : this.props.children;
1429
+ }
1430
+ };
1431
+ /** The gear glyph used by the default trigger. */
1432
+ function GearIcon() {
1433
+ return jsxs("svg", {
1434
+ viewBox: "0 0 24 24",
1435
+ width: "18",
1436
+ height: "18",
1437
+ fill: "none",
1438
+ stroke: "currentColor",
1439
+ strokeWidth: "1.8",
1440
+ "aria-hidden": "true",
1441
+ children: [jsx("circle", {
1442
+ cx: "12",
1443
+ cy: "12",
1444
+ r: "3"
1445
+ }), jsx("path", { d: "M19.4 15a1.7 1.7 0 0 0 .34 1.87l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.7 1.7 0 0 0-1.87-.34 1.7 1.7 0 0 0-1 1.55V21a2 2 0 1 1-4 0v-.09a1.7 1.7 0 0 0-1-1.55 1.7 1.7 0 0 0-1.87.34l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.7 1.7 0 0 0 .34-1.87 1.7 1.7 0 0 0-1.55-1H3a2 2 0 1 1 0-4h.09a1.7 1.7 0 0 0 1.55-1 1.7 1.7 0 0 0-.34-1.87l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.7 1.7 0 0 0 1.87.34h0a1.7 1.7 0 0 0 1-1.55V3a2 2 0 1 1 4 0v.09a1.7 1.7 0 0 0 1 1.55h0a1.7 1.7 0 0 0 1.87-.34l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.7 1.7 0 0 0-.34 1.87v0a1.7 1.7 0 0 0 1.55 1H21a2 2 0 1 1 0 4h-.09a1.7 1.7 0 0 0-1.55 1Z" })]
1446
+ });
1447
+ }
1448
+ /** The default settings trigger; the layout decides placement/presentation. */
1449
+ function SettingsEntry(props) {
1450
+ if (props.presentation === "label") return jsx(Button, {
1451
+ intent: "neutral",
1452
+ emphasis: "ghost",
1453
+ icon: jsx(GearIcon, {}),
1454
+ onClick: props.open,
1455
+ children: "设置"
1456
+ });
1457
+ return jsx(IconButton, {
1458
+ icon: jsx(GearIcon, {}),
1459
+ label: "设置",
1460
+ active: props.isOpen,
1461
+ onClick: props.open
1462
+ });
1463
+ }
1464
+ /** The default settings surface. */
1465
+ function SettingsSurface(props) {
1466
+ const { service } = props;
1467
+ useSyncExternalStore(service.subscribe, service.version, service.version);
1468
+ const open = service.isOpen();
1469
+ const pages = service.listPages();
1470
+ const activeId = service.activePage();
1471
+ const active = pages.find((page) => page.id === activeId);
1472
+ const groups = /* @__PURE__ */ new Map();
1473
+ for (const page of pages) {
1474
+ const group = page.group ?? "其他";
1475
+ const bucket = groups.get(group);
1476
+ if (bucket === void 0) groups.set(group, [page]);
1477
+ else bucket.push(page);
1478
+ }
1479
+ return jsx(Dialog, {
1480
+ open,
1481
+ onOpenChange: (next) => {
1482
+ if (!next) service.close();
1483
+ },
1484
+ children: jsx(DialogContent, {
1485
+ presentation: "fullscreen",
1486
+ showClose: false,
1487
+ children: jsxs("div", {
1488
+ ...part("settings", "root"),
1489
+ className: "snap-settings",
1490
+ children: [jsxs("header", {
1491
+ ...part("settings", "header"),
1492
+ className: "snap-settings-header",
1493
+ children: [jsx(DialogTitle, { children: "设置" }), jsx(Button, {
1494
+ intent: "neutral",
1495
+ emphasis: "ghost",
1496
+ onClick: () => service.close(),
1497
+ children: "关闭"
1498
+ })]
1499
+ }), jsxs("div", {
1500
+ ...part("settings", "body"),
1501
+ className: "snap-settings-body",
1502
+ children: [jsx("nav", {
1503
+ ...part("settings", "nav"),
1504
+ className: "snap-settings-nav",
1505
+ "aria-label": "设置页",
1506
+ children: [...groups.entries()].map(([group, entries]) => jsxs("div", {
1507
+ ...part("settings", "group"),
1508
+ className: "snap-settings-group",
1509
+ children: [jsx("div", {
1510
+ ...part("settings", "group-title"),
1511
+ className: "snap-settings-group-title",
1512
+ children: group
1513
+ }), entries.map((page) => jsxs("button", {
1514
+ type: "button",
1515
+ ...part("settings", "nav-item"),
1516
+ className: cn("snap-settings-nav-item", page.id === activeId && "is-active"),
1517
+ "data-active": page.id === activeId || void 0,
1518
+ "aria-current": page.id === activeId ? "page" : void 0,
1519
+ onClick: () => service.open(page.id),
1520
+ children: [page.icon !== void 0 && jsx("span", {
1521
+ ...part("settings", "nav-icon"),
1522
+ children: page.icon
1523
+ }), jsx("span", { children: page.title })]
1524
+ }, page.id))]
1525
+ }, group))
1526
+ }), jsx("main", {
1527
+ ...part("settings", "content"),
1528
+ className: "snap-settings-content",
1529
+ "data-active-page": activeId,
1530
+ children: active === void 0 ? jsx(EmptyState, {
1531
+ title: "暂无设置页",
1532
+ description: "安装或启用一个带设置页的插件后,它会出现在这里。"
1533
+ }) : jsx(SettingsPageBoundary, {
1534
+ page: active.id,
1535
+ children: active.render()
1536
+ })
1537
+ })]
1538
+ })]
1539
+ })
1540
+ })
1541
+ });
1542
+ }
1543
+ /** Mount the settings registry and its default slot contributions. */
1544
+ const settingsPlugin = {
1545
+ name: "snap-rail settings",
1546
+ inject: ["ui"],
1547
+ apply(ctx) {
1548
+ const pages = /* @__PURE__ */ new Map();
1549
+ const listeners = /* @__PURE__ */ new Set();
1550
+ let open = false;
1551
+ let requestedId;
1552
+ let revision = 0;
1553
+ let seq = 0;
1554
+ const notify = (kind) => {
1555
+ revision += 1;
1556
+ for (const listener of listeners) listener();
1557
+ ctx.emit(kind === "pages" ? "settings/pages-changed" : "settings/open-changed");
1558
+ };
1559
+ const firstId = () => orderedPages(pages.values())[0]?.id;
1560
+ const activeId = () => {
1561
+ if (!open) return void 0;
1562
+ if (requestedId !== void 0 && pages.has(requestedId)) return requestedId;
1563
+ return firstId();
1564
+ };
1565
+ const service = {
1566
+ registerPage(caller, page) {
1567
+ pages.set(page.id, {
1568
+ page,
1569
+ seq: seq++
1570
+ });
1571
+ notify("pages");
1572
+ const remove = () => {
1573
+ if (pages.delete(page.id)) notify("pages");
1574
+ };
1575
+ caller.effect(() => remove);
1576
+ return remove;
1577
+ },
1578
+ listPages: () => orderedPages(pages.values()),
1579
+ open(pageId) {
1580
+ if (pageId !== void 0) requestedId = pageId;
1581
+ if (open && pageId === void 0) return;
1582
+ open = true;
1583
+ notify("open");
1584
+ },
1585
+ close() {
1586
+ if (!open) return;
1587
+ open = false;
1588
+ notify("open");
1589
+ },
1590
+ toggle() {
1591
+ if (open) service.close();
1592
+ else service.open();
1593
+ },
1594
+ isOpen: () => open,
1595
+ activePage: activeId,
1596
+ subscribe(listener) {
1597
+ listeners.add(listener);
1598
+ return () => {
1599
+ listeners.delete(listener);
1600
+ };
1601
+ },
1602
+ version: () => revision
1603
+ };
1604
+ ctx.provide("settings", service);
1605
+ ctx.settings.registerPage(ctx, {
1606
+ id: "plugins",
1607
+ title: "插件管理",
1608
+ group: "系统",
1609
+ order: 0,
1610
+ keywords: [
1611
+ "安装",
1612
+ "启用",
1613
+ "卸载",
1614
+ "驱动",
1615
+ "皮肤"
1616
+ ],
1617
+ render: () => jsx(PluginsPage, { ctx })
1618
+ });
1619
+ ctx.settings.registerPage(ctx, {
1620
+ id: "appearance",
1621
+ title: "外观与显示",
1622
+ group: "系统",
1623
+ order: 1,
1624
+ keywords: [
1625
+ "主题",
1626
+ "皮肤",
1627
+ "窗口",
1628
+ "悬浮球",
1629
+ "尺寸"
1630
+ ],
1631
+ render: () => jsx(AppearancePage, {})
1632
+ });
1633
+ ctx.ui.contribute(ctx, UI_SLOT_IDS.settingsEntry, {
1634
+ id: "settings.default-entry",
1635
+ order: 0,
1636
+ render: (context) => jsx(SettingsEntry, {
1637
+ presentation: context.presentation,
1638
+ open: context.open,
1639
+ isOpen: context.isOpen
1640
+ })
1641
+ });
1642
+ ctx.ui.contribute(ctx, UI_SLOT_IDS.settingsSurface, {
1643
+ id: "settings.default-surface",
1644
+ order: 0,
1645
+ render: () => jsx(SettingsSurface, { service })
1646
+ });
1647
+ }
1648
+ };
1649
+ //#endregion
1650
+ //#region lib/types/skins.js
1651
+ /** The built-in default skin id. */
1652
+ const DEFAULT_SKIN_ID = "default";
1653
+ /** The factory cobalt/steel skin. Extra skins override it through the registry. */
1654
+ const DEFAULT_SKIN = {
1655
+ id: DEFAULT_SKIN_ID,
1656
+ title: "工业钴蓝",
1657
+ presentations: {
1658
+ dialog: "modal",
1659
+ select: "popover",
1660
+ tabs: "inline",
1661
+ toast: "inline"
1662
+ },
1663
+ tokens: {
1664
+ dark: {
1665
+ colorBg: "#0b0f14",
1666
+ colorSurface: "#11161d",
1667
+ colorSurfaceRaised: "#161b22",
1668
+ colorText: "#ced8e3",
1669
+ colorMuted: "#8b949e",
1670
+ colorPrimary: "#4a6cff",
1671
+ colorOnPrimary: "#ffffff",
1672
+ colorDanger: "#f85149",
1673
+ colorSuccess: "#3fb950",
1674
+ colorWarning: "#d29922",
1675
+ colorBorder: "#1f2933",
1676
+ colorFocus: "#4a6cff",
1677
+ chart1: "#4a6cff",
1678
+ chart2: "#3147a1",
1679
+ chart3: "#667fcd",
1680
+ chart4: "#7d8fa5",
1681
+ chart5: "#55677c",
1682
+ chart6: "#33414f",
1683
+ fontSans: "system-ui, \"Segoe UI\", \"Microsoft YaHei\", sans-serif",
1684
+ fontMono: "\"Cascadia Code\", \"Cascadia Mono\", Consolas, ui-monospace, monospace",
1685
+ textXs: "13px",
1686
+ textSm: "15px",
1687
+ textMd: "17px",
1688
+ textLg: "20px",
1689
+ textXl: "24px",
1690
+ weightNormal: "400",
1691
+ weightMedium: "500",
1692
+ weightStrong: "600",
1693
+ leadingTight: "1.2",
1694
+ leadingNormal: "1.55",
1695
+ spaceXs: "4px",
1696
+ spaceSm: "8px",
1697
+ spaceMd: "12px",
1698
+ spaceLg: "20px",
1699
+ spaceXl: "32px",
1700
+ radiusSm: "6px",
1701
+ radiusMd: "8px",
1702
+ radiusLg: "12px",
1703
+ radiusFull: "999px",
1704
+ borderWidth: "1px",
1705
+ shadowSm: "0 1px 2px rgb(0 0 0 / 0.25)",
1706
+ shadowMd: "0 8px 24px rgb(0 0 0 / 0.28)",
1707
+ shadowLg: "0 24px 64px rgb(0 0 0 / 0.36)",
1708
+ durationFast: "120ms",
1709
+ durationNormal: "180ms",
1710
+ durationSlow: "280ms",
1711
+ easeStandard: "cubic-bezier(0.2, 0, 0, 1)",
1712
+ zOverlay: "50",
1713
+ zModal: "60",
1714
+ zToast: "70",
1715
+ controlHeightSm: "40px",
1716
+ controlHeightMd: "48px",
1717
+ controlHeightLg: "56px",
1718
+ density: "comfortable"
1719
+ },
1720
+ light: {
1721
+ colorBg: "#e4e9ef",
1722
+ colorSurface: "#f3f6f9",
1723
+ colorSurfaceRaised: "#ffffff",
1724
+ colorText: "#26323e",
1725
+ colorMuted: "#5d6d7e",
1726
+ colorPrimary: "#3857e8",
1727
+ colorOnPrimary: "#ffffff",
1728
+ colorDanger: "#dc2626",
1729
+ colorSuccess: "#15803d",
1730
+ colorWarning: "#b45309",
1731
+ colorBorder: "#c7d0da",
1732
+ colorFocus: "#3857e8",
1733
+ chart1: "#3857e8",
1734
+ chart2: "#7088ee",
1735
+ chart3: "#667dd0",
1736
+ chart4: "#94a3b8",
1737
+ chart5: "#b3c0cd",
1738
+ chart6: "#ccd6e0",
1739
+ fontSans: "system-ui, \"Segoe UI\", \"Microsoft YaHei\", sans-serif",
1740
+ fontMono: "\"Cascadia Code\", \"Cascadia Mono\", Consolas, ui-monospace, monospace",
1741
+ textXs: "13px",
1742
+ textSm: "15px",
1743
+ textMd: "17px",
1744
+ textLg: "20px",
1745
+ textXl: "24px",
1746
+ weightNormal: "400",
1747
+ weightMedium: "500",
1748
+ weightStrong: "600",
1749
+ leadingTight: "1.2",
1750
+ leadingNormal: "1.55",
1751
+ spaceXs: "4px",
1752
+ spaceSm: "8px",
1753
+ spaceMd: "12px",
1754
+ spaceLg: "20px",
1755
+ spaceXl: "32px",
1756
+ radiusSm: "6px",
1757
+ radiusMd: "8px",
1758
+ radiusLg: "12px",
1759
+ radiusFull: "999px",
1760
+ borderWidth: "1px",
1761
+ shadowSm: "0 1px 2px rgb(0 0 0 / 0.08)",
1762
+ shadowMd: "0 8px 24px rgb(0 0 0 / 0.12)",
1763
+ shadowLg: "0 24px 64px rgb(0 0 0 / 0.18)",
1764
+ durationFast: "120ms",
1765
+ durationNormal: "180ms",
1766
+ durationSlow: "280ms",
1767
+ easeStandard: "cubic-bezier(0.2, 0, 0, 1)",
1768
+ zOverlay: "50",
1769
+ zModal: "60",
1770
+ zToast: "70",
1771
+ controlHeightSm: "40px",
1772
+ controlHeightMd: "48px",
1773
+ controlHeightLg: "56px",
1774
+ density: "comfortable"
1775
+ }
1776
+ }
1777
+ };
1778
+ /** Turn a token key into its CSS custom property (`chart1` → `--snap-chart-1`). */
1779
+ function skinTokenVar(key) {
1780
+ return `--snap-${key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`).replace(/(\d+)$/, "-$1")}`;
1781
+ }
1782
+ /** Apply the active skin to the document root. */
1783
+ function applySkin(skin, mode) {
1784
+ if (typeof document === "undefined") return;
1785
+ const root = document.documentElement;
1786
+ root.dataset["snapSkin"] = skin.id;
1787
+ root.dataset["snapMode"] = mode;
1788
+ const tokens = skin.tokens[mode];
1789
+ for (const [key, value] of Object.entries(tokens)) root.style.setProperty(skinTokenVar(key), value);
1790
+ }
1791
+ /** Mount the `ctx.skins` registry. */
1792
+ const skinsPlugin = {
1793
+ name: "snap-rail ui skins",
1794
+ apply(ctx) {
1795
+ const skins = /* @__PURE__ */ new Map();
1796
+ const listeners = /* @__PURE__ */ new Set();
1797
+ const styleTags = /* @__PURE__ */ new Map();
1798
+ let activeId = DEFAULT_SKIN_ID;
1799
+ let mode = "dark";
1800
+ let revision = 0;
1801
+ skins.set(DEFAULT_SKIN_ID, DEFAULT_SKIN);
1802
+ const notify = () => {
1803
+ revision += 1;
1804
+ for (const listener of listeners) listener();
1805
+ ctx.emit("skins/changed");
1806
+ };
1807
+ const ensureStyleTag = (skin) => {
1808
+ if (typeof document === "undefined" || skin.css === void 0) return;
1809
+ if (styleTags.has(skin.id)) return;
1810
+ const tag = document.createElement("style");
1811
+ tag.dataset["snapSkinStyle"] = skin.id;
1812
+ tag.textContent = skin.css;
1813
+ document.head.append(tag);
1814
+ styleTags.set(skin.id, tag);
1815
+ };
1816
+ const refresh = () => {
1817
+ const skin = skins.get(activeId) ?? DEFAULT_SKIN;
1818
+ activeId = skin.id;
1819
+ ensureStyleTag(skin);
1820
+ applySkin(skin, mode);
1821
+ notify();
1822
+ };
1823
+ ctx.provide("skins", {
1824
+ register(caller, skin) {
1825
+ skins.set(skin.id, skin);
1826
+ if (skin.id !== "default") activeId = skin.id;
1827
+ refresh();
1828
+ const remove = () => {
1829
+ if (!skins.delete(skin.id)) return;
1830
+ const tag = styleTags.get(skin.id);
1831
+ if (tag !== void 0) {
1832
+ tag.remove();
1833
+ styleTags.delete(skin.id);
1834
+ }
1835
+ if (activeId === skin.id) activeId = DEFAULT_SKIN_ID;
1836
+ refresh();
1837
+ };
1838
+ caller.effect(() => remove);
1839
+ return remove;
1840
+ },
1841
+ activate(skinId) {
1842
+ if (!skins.has(skinId)) return false;
1843
+ activeId = skinId;
1844
+ refresh();
1845
+ return true;
1846
+ },
1847
+ list: () => [skins.get("default") ?? DEFAULT_SKIN, ...[...skins.values()].filter((skin) => skin.id !== DEFAULT_SKIN_ID)],
1848
+ active: () => skins.get(activeId) ?? DEFAULT_SKIN,
1849
+ setMode(next) {
1850
+ if (mode === next) return;
1851
+ mode = next;
1852
+ refresh();
1853
+ },
1854
+ mode: () => mode,
1855
+ presentation(target) {
1856
+ return (skins.get(activeId) ?? DEFAULT_SKIN).presentations?.[target];
1857
+ },
1858
+ tokens: () => (skins.get(activeId) ?? DEFAULT_SKIN).tokens[mode],
1859
+ subscribe(listener) {
1860
+ listeners.add(listener);
1861
+ return () => {
1862
+ listeners.delete(listener);
1863
+ };
1864
+ },
1865
+ version: () => revision
1866
+ });
1867
+ refresh();
1868
+ }
1869
+ };
1870
+ /** The React context carrying the skin service. */
1871
+ const SkinsContext = createContext(null);
1872
+ /** Provide the skin service to a renderer tree. */
1873
+ function SkinsProvider(props) {
1874
+ return jsx(SkinsContext.Provider, {
1875
+ value: props.service,
1876
+ children: props.children
1877
+ });
1878
+ }
1879
+ //#endregion
1880
+ //#region lib/types/runtime.js
1881
+ /** The React context carrying the host link/surface into builtin pages. */
1882
+ const ClientContext = createContext(null);
1883
+ /** Provide the client services to builtin page components. */
1884
+ function ClientProvider(props) {
1885
+ return jsx(ClientContext.Provider, {
1886
+ value: props.service,
1887
+ children: props.children
1888
+ });
1889
+ }
1890
+ /** Read the client services (host link + surface) from a builtin page. */
1891
+ function useClient() {
1892
+ const service = useContext(ClientContext);
1893
+ if (service === null) throw new Error("useClient: no ClientProvider in the tree");
1894
+ return service;
1895
+ }
1896
+ /** Re-render on settings open/page changes. */
1897
+ function useSettingsVersion(ctx) {
1898
+ return useSyncExternalStore(ctx.settings.subscribe, ctx.settings.version, ctx.settings.version);
1899
+ }
1900
+ /** Apply the active layout's stage profiles to the host. */
1901
+ function StageProfiles(props) {
1902
+ const link = props.ctx.client.link;
1903
+ const layout = props.layout;
1904
+ useEffect(() => {
1905
+ if (layout?.window !== void 0) link.call("shell.window.configure", { profile: layout.window }).catch(() => {});
1906
+ if (layout?.ball !== void 0) link.call("shell.ball.configure", { profile: layout.ball }).catch(() => {});
1907
+ }, [layout?.id]);
1908
+ return null;
1909
+ }
1910
+ /** The global overlay host: registry-owned surfaces do not depend on a layout. */
1911
+ function OverlayHost(props) {
1912
+ useUiVersion(props.ctx.ui);
1913
+ useSettingsVersion(props.ctx);
1914
+ const overlays = props.ctx.ui.list(UI_SLOT_IDS.overlay);
1915
+ const settingsSurfaces = props.ctx.settings.isOpen() ? props.ctx.ui.list(UI_SLOT_IDS.settingsSurface) : [];
1916
+ const context = {
1917
+ pages: props.ctx.settings.listPages().map((page) => ({
1918
+ id: page.id,
1919
+ title: page.title,
1920
+ ...page.group !== void 0 ? { group: page.group } : {},
1921
+ order: page.order
1922
+ })),
1923
+ ...props.ctx.settings.activePage() !== void 0 ? { activeId: props.ctx.settings.activePage() } : {},
1924
+ close: () => props.ctx.settings.close()
1925
+ };
1926
+ return jsxs(Fragment, { children: [overlays.map((item) => jsx("span", { children: item.render({ surface: "main" }) }, item.id)), settingsSurfaces.map((item) => jsx("span", { children: item.render(context) }, item.id))] });
1927
+ }
1928
+ /** The floating settings fallback, rendered only when no layout provides the seat. */
1929
+ function SettingsFallbackHost(props) {
1930
+ useUiVersion(props.ctx.ui);
1931
+ useSettingsVersion(props.ctx);
1932
+ if (props.ctx.ui.hasProvider(UI_SLOT_IDS.settingsEntry)) return null;
1933
+ const entry = props.ctx.ui.list(UI_SLOT_IDS.settingsEntry)[0];
1934
+ if (entry === void 0) return null;
1935
+ return jsx("div", {
1936
+ "data-snap-part": "settings.fallback",
1937
+ style: {
1938
+ position: "fixed",
1939
+ right: 16,
1940
+ bottom: 16,
1941
+ zIndex: "var(--snap-z-overlay)"
1942
+ },
1943
+ children: entry.render({
1944
+ presentation: "icon",
1945
+ open: () => props.ctx.settings.open(),
1946
+ isOpen: props.ctx.settings.isOpen()
1947
+ })
1948
+ });
1949
+ }
1950
+ /** The operator-window root. */
1951
+ function MainRoot(props) {
1952
+ const layout = useActiveLayout();
1953
+ return jsxs(Fragment, { children: [
1954
+ jsx(StageProfiles, {
1955
+ ctx: props.ctx,
1956
+ layout
1957
+ }),
1958
+ layout === void 0 ? jsx("div", {
1959
+ style: { padding: 24 },
1960
+ children: "没有已加载的布局插件。"
1961
+ }) : layout.render(),
1962
+ jsx(OverlayHost, { ctx: props.ctx }),
1963
+ jsx(SettingsFallbackHost, { ctx: props.ctx })
1964
+ ] });
1965
+ }
1966
+ /** The fallback ball face used when no plugin occupies `shell.ball`. */
1967
+ function BallRoot(props) {
1968
+ useUiVersion(props.ctx.ui);
1969
+ const occupant = props.ctx.ui.list(UI_SLOT_IDS.ball)[0];
1970
+ if (occupant !== void 0) return jsx("div", {
1971
+ style: {
1972
+ width: "100%",
1973
+ height: "100%"
1974
+ },
1975
+ children: occupant.render({ surface: "ball" })
1976
+ });
1977
+ return jsx("div", {
1978
+ "data-snap-part": "ball.root",
1979
+ style: {
1980
+ display: "flex",
1981
+ alignItems: "center",
1982
+ justifyContent: "center",
1983
+ width: "100%",
1984
+ height: "100%",
1985
+ color: "var(--snap-color-muted)",
1986
+ fontSize: "var(--snap-text-xs)"
1987
+ },
1988
+ children: "snap-rail"
1989
+ });
1990
+ }
1991
+ /** The root that mounts the layout (or the ball face) and the global overlays. */
1992
+ function UiRoot(props) {
1993
+ return jsx(ClientProvider, {
1994
+ service: props.ctx.client,
1995
+ children: jsx(UiProvider, {
1996
+ service: props.ctx.ui,
1997
+ children: jsx(SkinsProvider, {
1998
+ service: props.ctx.skins,
1999
+ children: props.surface === "main" ? jsx(MainRoot, { ctx: props.ctx }) : jsx(BallRoot, { ctx: props.ctx })
2000
+ })
2001
+ })
2002
+ });
2003
+ }
2004
+ /**
2005
+ * Take over the kernel root and mount the UI substrate.
2006
+ *
2007
+ * @param handle - the kernel boot result.
2008
+ * @param options - see {@link RuntimeOptions}.
2009
+ */
2010
+ async function createClientRuntime(handle, options) {
2011
+ const surface = options.surface ?? "main";
2012
+ const ctx = new Context();
2013
+ ctx.provide("client", {
2014
+ link: handle.link,
2015
+ surface
2016
+ });
2017
+ await ctx.plugin(TimerService);
2018
+ await ctx.plugin(slotsPlugin);
2019
+ await ctx.plugin(pagesPlugin);
2020
+ await ctx.plugin(skinsPlugin);
2021
+ await ctx.plugin(settingsPlugin);
2022
+ const applyThemeRow = (value) => {
2023
+ const mode = value?.mode;
2024
+ const choice = mode === "light" || mode === "dark" || mode === "system" ? mode : "dark";
2025
+ const prefersLight = typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-color-scheme: light)").matches;
2026
+ const resolved = choice === "system" ? prefersLight ? "light" : "dark" : choice;
2027
+ ctx.skins.setMode(resolved);
2028
+ if (typeof document !== "undefined") document.documentElement.dataset["mode"] = resolved;
2029
+ };
2030
+ handle.link.call("settings.get", { key: "ui.theme" }).then((result) => {
2031
+ if (result.ok) applyThemeRow(result.value.value);
2032
+ }).catch(() => {});
2033
+ const dropTheme = subscribeFrame(handle.link, "settings/changed", settingsChangedSchema, (change) => {
2034
+ if (change.key === "ui.theme") applyThemeRow(change.value);
2035
+ });
2036
+ const dropOpenSettings = subscribeFrame(handle.link, "shell/settings-open-requested", shellContract.frames["shell/settings-open-requested"], () => {
2037
+ ctx.settings.open();
2038
+ });
2039
+ ctx.effect(() => () => {
2040
+ dropTheme();
2041
+ dropOpenSettings();
2042
+ });
2043
+ for (const page of options.builtinPages ?? []) ctx.settings.registerPage(ctx, page);
2044
+ for (const seat of options.plugins) {
2045
+ const spec = "plugin" in seat ? seat : { plugin: seat };
2046
+ const plugin = spec.plugin;
2047
+ await ctx.plugin(plugin, spec.config === void 0 ? {} : spec.config);
2048
+ }
2049
+ if (surface === "main") await ctx.plugin(fallbackPlugin);
2050
+ handle.root.unmount();
2051
+ const shellRoot = createRoot(handle.element);
2052
+ shellRoot.render(jsx(UiRoot, {
2053
+ ctx,
2054
+ surface
2055
+ }));
2056
+ return {
2057
+ ctx,
2058
+ dispose: () => ctx.fiber.dispose().then(() => {
2059
+ shellRoot.unmount();
2060
+ })
2061
+ };
2062
+ }
2063
+ //#endregion
2064
+ export { ClientProvider, Slot, createClientRuntime, fallbackPlugin, pagesPlugin, settingsPlugin, skinsPlugin, slotsPlugin, useClient };