@stacksjs/components 0.2.890 → 0.2.891
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{AppearancePicker-61r20jpp.stx → AppearancePicker-30fetv0a.stx} +45 -0
- package/dist/{Button-r8q6v9m3.stx → Button-ghdfqwg0.stx} +54 -1
- package/dist/{Checkbox-52mhrzkz.stx → Checkbox-355qsbwd.stx} +13 -3
- package/dist/{PasswordInput-z2g5ha65.stx → PasswordInput-hhq64qzr.stx} +15 -1
- package/dist/{SegmentedControl-7q0t8c9s.stx → SegmentedControl-b93n9n2v.stx} +9 -1
- package/dist/{Select-ygzchedx.stx → Select-p8mgxthn.stx} +13 -2
- package/dist/{SettingsGroup-dkae46k5.stx → SettingsGroup-g1hnxp1p.stx} +52 -0
- package/dist/{SettingsPane-jbd1gbdt.stx → SettingsPane-9svk1w3e.stx} +21 -2
- package/dist/{SettingsPaneHeader-f0cvkqs7.stx → SettingsPaneHeader-am42srhp.stx} +24 -0
- package/dist/{SettingsRow-4cxbsf1q.stx → SettingsRow-kgda3nv1.stx} +63 -0
- package/dist/{SettingsSidebarGroup-p2ygv5yy.stx → SettingsSidebarGroup-4ag5qjpw.stx} +17 -0
- package/dist/{SettingsSidebarItem-x8fsn3w5.stx → SettingsSidebarItem-ga2jxq53.stx} +33 -0
- package/dist/SettingsUpdateRow-ajpnqx8a.stx +143 -0
- package/dist/{SettingsWindow-mm490ymt.stx → SettingsWindow-586aps08.stx} +186 -19
- package/dist/{Switch-ga9ap8n8.stx → Switch-vd8k23sr.stx} +4 -1
- package/dist/{TextInput-m4a4kpfy.stx → TextInput-492d1c3n.stx} +15 -2
- package/dist/component-api/component-api.js +21 -3
- package/dist/index.js +24 -24
- package/dist/settings/settings.js +371 -15
- package/dist/ui/appearance-picker/AppearancePicker.d.ts +1 -0
- package/dist/ui/button/Button.d.ts +16 -0
- package/dist/ui/checkbox/Checkbox.d.ts +7 -0
- package/dist/ui/input/PasswordInput.d.ts +10 -0
- package/dist/ui/input/TextInput.d.ts +11 -0
- package/dist/ui/segmented-control/SegmentedControl.d.ts +7 -0
- package/dist/ui/settings/SettingsGroup.d.ts +5 -0
- package/dist/ui/settings/SettingsPane.d.ts +4 -0
- package/dist/ui/settings/SettingsPaneHeader.d.ts +10 -1
- package/dist/ui/settings/SettingsRow.d.ts +20 -1
- package/dist/ui/settings/SettingsSidebarGroup.d.ts +5 -0
- package/dist/ui/settings/SettingsSidebarItem.d.ts +11 -1
- package/dist/ui/settings/SettingsUpdateRow.d.ts +34 -0
- package/dist/ui/settings/SettingsWindow.d.ts +12 -0
- package/dist/ui/settings/index.d.ts +18 -0
- package/dist/ui/settings/native-theme.d.ts +130 -0
- package/dist/ui/settings/native.d.ts +51 -0
- package/dist/ui/settings/settings.d.ts +5 -2
- package/dist/ui/settings/update.d.ts +45 -0
- package/dist/utils/component-api.d.ts +2 -0
- package/package.json +3 -3
- package/src/ui/appearance-picker/AppearancePicker.stx +45 -0
- package/src/ui/appearance-picker/README.md +4 -0
- package/src/ui/button/Button.stx +54 -1
- package/src/ui/checkbox/Checkbox.stx +13 -3
- package/src/ui/input/PasswordInput.stx +15 -1
- package/src/ui/input/TextInput.stx +15 -2
- package/src/ui/segmented-control/SegmentedControl.stx +9 -1
- package/src/ui/select/Select.stx +13 -2
- package/src/ui/settings/README.md +210 -15
- package/src/ui/settings/SettingsGroup.stx +52 -0
- package/src/ui/settings/SettingsPane.stx +21 -2
- package/src/ui/settings/SettingsPaneHeader.stx +24 -0
- package/src/ui/settings/SettingsRow.stx +63 -0
- package/src/ui/settings/SettingsSidebarGroup.stx +17 -0
- package/src/ui/settings/SettingsSidebarItem.stx +33 -0
- package/src/ui/settings/SettingsUpdateRow.stx +143 -0
- package/src/ui/settings/SettingsWindow.stx +186 -19
- package/src/ui/settings/index.ts +31 -3
- package/src/ui/settings/native-theme.ts +169 -0
- package/src/ui/settings/native.ts +456 -0
- package/src/ui/settings/settings.ts +5 -2
- package/src/ui/settings/update.ts +119 -0
- package/src/ui/switch/Switch.stx +4 -1
- package/src/utils/component-api.ts +36 -3
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the Settings components are drawn with on a native host.
|
|
3
|
+
*
|
|
4
|
+
* On the web the look is the `--stx-mac-*` custom properties and each
|
|
5
|
+
* component's stylesheet. A native host (Craft's AppKit host) has neither: it
|
|
6
|
+
* draws views from class lists. Every size, padding, radius and colour the
|
|
7
|
+
* native branches use is in this file and nowhere else, so matching a real
|
|
8
|
+
* System Settings window is a matter of editing one place.
|
|
9
|
+
*
|
|
10
|
+
* The numbers are the web look's own (`src/utils/mac-theme.ts` and the
|
|
11
|
+
* components' stylesheets), which were measured from System Settings on
|
|
12
|
+
* macOS 26. A colour is written as a hex with its dark twin, the way the
|
|
13
|
+
* other shared components spell a native colour: `#000000/85` is black at
|
|
14
|
+
* 85%, which the host receives as `#000000d9`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** The tile colours, light and dark. The same ten tones as the web. */
|
|
18
|
+
export const SETTINGS_NATIVE_TONES: Readonly<Record<string, string>> = {
|
|
19
|
+
grey: 'bg-[#8e8e93] dark:bg-[#8e8e93]',
|
|
20
|
+
blue: 'bg-[#007aff] dark:bg-[#0a84ff]',
|
|
21
|
+
red: 'bg-[#ff3b30] dark:bg-[#ff453a]',
|
|
22
|
+
orange: 'bg-[#ff9500] dark:bg-[#ff9f0a]',
|
|
23
|
+
yellow: 'bg-[#ffcc00] dark:bg-[#ffd60a]',
|
|
24
|
+
green: 'bg-[#34c759] dark:bg-[#30d158]',
|
|
25
|
+
indigo: 'bg-[#5856d6] dark:bg-[#5e5ce6]',
|
|
26
|
+
purple: 'bg-[#af52de] dark:bg-[#bf5af2]',
|
|
27
|
+
pink: 'bg-[#ff2d55] dark:bg-[#ff375f]',
|
|
28
|
+
// Ink is the page's own ink: near black on light, light on dark.
|
|
29
|
+
ink: 'bg-[#1d1d1f] dark:bg-[#dcdce0]',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** The glyph on a tile: white, and dark on the light `ink` tile of a dark window. */
|
|
33
|
+
export function settingsNativeGlyph(tone: string): string {
|
|
34
|
+
return tone === 'ink' ? 'text-[#ffffff] dark:text-[#1d1d1f]' : 'text-[#ffffff]'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Text colours. */
|
|
38
|
+
const INK = 'text-[#000000]/85 dark:text-[#ffffff]/85'
|
|
39
|
+
const INK_SECONDARY = 'text-[#000000]/50 dark:text-[#ffffff]/55'
|
|
40
|
+
const INK_TERTIARY = 'text-[#000000]/26 dark:text-[#ffffff]/25'
|
|
41
|
+
const DANGER = 'text-[#ff3b30] dark:text-[#ff453a]'
|
|
42
|
+
const ACCENT_TEXT = 'text-[#007aff] dark:text-[#0a84ff]'
|
|
43
|
+
/** The selection: the system blue, and the ink on it. */
|
|
44
|
+
const SELECTED_FILL = 'aria-selected:bg-[#007aff] dark:aria-selected:bg-[#0a84ff]'
|
|
45
|
+
const SELECTED_INK = 'aria-selected:text-[#ffffff] dark:aria-selected:text-[#ffffff]'
|
|
46
|
+
/** The hairline between rows and under the toolbar. */
|
|
47
|
+
const SEPARATOR = 'bg-[#000000]/10 dark:bg-[#ffffff]/10'
|
|
48
|
+
/** The slightly raised card a group of rows sits on. */
|
|
49
|
+
const CARD = 'bg-[#000000]/4 dark:bg-[#ffffff]/5'
|
|
50
|
+
|
|
51
|
+
export const SETTINGS_NATIVE = {
|
|
52
|
+
// ---------- The window: two columns ----------
|
|
53
|
+
/** The root. Not named `window`: a template's evaluator refuses that word. */
|
|
54
|
+
shell: 'flex-1 flex-row',
|
|
55
|
+
/** The sidebar column. Its width is `sidebarWidth` (226) and its fill sits under the host's sidebar material. */
|
|
56
|
+
sidebar: 'flex-col bg-[#f2f1f0] dark:bg-[#2a2a2a]',
|
|
57
|
+
sidebarWidth: 226,
|
|
58
|
+
/** The AppKit material drawn behind the sidebar (`material` on the host's box). */
|
|
59
|
+
sidebarMaterial: 'sidebar',
|
|
60
|
+
/** Room above the search field: the window buttons sit over the sidebar of a titlebar-less window. */
|
|
61
|
+
sidebarTop: 'pt-[38px] px-[9px] pb-[6px]',
|
|
62
|
+
/** The search field: the system's own search field at this height. */
|
|
63
|
+
search: 'h-[28px] text-[13px]',
|
|
64
|
+
/** The scrolling list under the search field. */
|
|
65
|
+
nav: 'flex-1 flex-col px-[9px] pb-[12px]',
|
|
66
|
+
account: 'mt-[7px]',
|
|
67
|
+
list: 'flex-col pt-[7px]',
|
|
68
|
+
/** A setting a search found: its label over the name of its pane. */
|
|
69
|
+
results: 'flex-col gap-[2px] pt-[7px]',
|
|
70
|
+
result: 'flex-col justify-center min-h-[38px] px-[8px] py-[4px] rounded-[8px] hover:bg-[#000000]/5 dark:hover:bg-[#ffffff]/6',
|
|
71
|
+
resultLabel: `text-[13px] ${INK}`,
|
|
72
|
+
resultPane: `text-[11px] ${INK_SECONDARY}`,
|
|
73
|
+
empty: `py-[24px] px-[8px] text-[13px] text-center ${INK_SECONDARY}`,
|
|
74
|
+
|
|
75
|
+
// ---------- The sidebar's rows ----------
|
|
76
|
+
group: 'flex-col gap-[2px]',
|
|
77
|
+
/** The gap above every group but the first one showing: with the 2pt between a group's rows, 13pt. */
|
|
78
|
+
groupGap: 'h-[11px]',
|
|
79
|
+
groupLabel: `px-[8px] pb-[3px] text-[11px] font-semibold ${INK_SECONDARY}`,
|
|
80
|
+
/** One row: 30pt tall, the selected one a blue pill with 8pt corners. */
|
|
81
|
+
item: `flex-row items-center gap-[8px] h-[30px] px-[8px] rounded-[8px] ${SELECTED_FILL}`,
|
|
82
|
+
itemTile: 'h-[20px] w-[20px] rounded-[5.5px] items-center justify-center',
|
|
83
|
+
itemGlyph: 'h-[13px] w-[13px]',
|
|
84
|
+
itemLabel: `flex-1 text-[13px] ${INK} ${SELECTED_INK}`,
|
|
85
|
+
itemBadge: `text-[11px] ${INK_SECONDARY} ${SELECTED_INK}`,
|
|
86
|
+
|
|
87
|
+
// ---------- The detail column ----------
|
|
88
|
+
detail: 'flex-1 flex-col bg-[#ffffff] dark:bg-[#1e1e1e]',
|
|
89
|
+
/** Back, forward and the open pane's name. */
|
|
90
|
+
toolbar: 'flex-row items-center gap-[12px] h-[52px] pl-[8px] pr-[16px]',
|
|
91
|
+
history: 'flex-row items-center',
|
|
92
|
+
historyButton: `text-[15px] ${INK_SECONDARY}`,
|
|
93
|
+
/** The pane's name, large, as System Settings titles a pane. */
|
|
94
|
+
title: `flex-1 text-[20px] font-semibold ${INK}`,
|
|
95
|
+
body: 'flex-1 flex-col',
|
|
96
|
+
/** One pane: it scrolls on its own, and its content is inset by this padding. */
|
|
97
|
+
pane: 'flex-1',
|
|
98
|
+
paneContent: 'flex-col px-[20px] pt-[11px] pb-[20px]',
|
|
99
|
+
|
|
100
|
+
// ---------- The card at the top of a pane ----------
|
|
101
|
+
hero: `flex-col items-center mb-[10px] p-[22px] rounded-[12px] ${CARD}`,
|
|
102
|
+
heroTile: 'h-[48px] w-[48px] rounded-[12px] items-center justify-center',
|
|
103
|
+
heroGlyph: 'h-[30px] w-[30px]',
|
|
104
|
+
heroTitle: `mt-[10px] text-[17px] font-semibold text-center ${INK}`,
|
|
105
|
+
heroDescription: `mt-[4px] text-[13px] text-center ${INK_SECONDARY}`,
|
|
106
|
+
heroExtra: 'mt-[8px] flex-col items-center',
|
|
107
|
+
|
|
108
|
+
// ---------- A group of rows ----------
|
|
109
|
+
section: 'flex-col mb-[10px]',
|
|
110
|
+
sectionHeader: `pt-[18px] px-[10px] pb-[8px] text-[13px] font-bold ${INK}`,
|
|
111
|
+
card: `flex-col rounded-[12px] ${CARD}`,
|
|
112
|
+
sectionFooter: `pt-[6px] px-[10px] text-[11px] ${INK_SECONDARY}`,
|
|
113
|
+
|
|
114
|
+
// ---------- One row ----------
|
|
115
|
+
/** The hairline above every row but the first, inset on both sides. */
|
|
116
|
+
rowRule: `mx-[10px] h-[0.5px] ${SEPARATOR}`,
|
|
117
|
+
row: 'flex-row items-center gap-[10px] min-h-[38px] px-[10px]',
|
|
118
|
+
/** A navigation row with a tile is a little taller, as System Settings draws it. */
|
|
119
|
+
rowTall: 'min-h-[44px]',
|
|
120
|
+
rowMultiline: 'items-start',
|
|
121
|
+
rowStacked: 'flex-col items-stretch gap-[4px] min-h-[38px] px-[10px]',
|
|
122
|
+
rowPressed: 'active:bg-[#000000]/5 dark:active:bg-[#ffffff]/6',
|
|
123
|
+
/** The wash a found row wears for a moment: 20% of the accent. */
|
|
124
|
+
rowWash: { light: '#007aff33', dark: '#0a84ff33' },
|
|
125
|
+
rowTile: 'h-[24px] w-[24px] rounded-[6px] items-center justify-center',
|
|
126
|
+
rowTileMultiline: 'mt-[10px]',
|
|
127
|
+
rowGlyph: 'h-[15px] w-[15px]',
|
|
128
|
+
rowText: 'flex-1 flex-col justify-center py-[7px]',
|
|
129
|
+
rowTextMultiline: 'flex-1 flex-col py-[10px]',
|
|
130
|
+
rowTextStacked: 'flex-col pt-[10px]',
|
|
131
|
+
rowLabel: `text-[13px] ${INK}`,
|
|
132
|
+
rowLabelDestructive: `text-[13px] ${DANGER}`,
|
|
133
|
+
rowDescription: `mt-[1px] text-[11px] ${INK_SECONDARY}`,
|
|
134
|
+
rowControl: 'flex-row items-center justify-end gap-[8px]',
|
|
135
|
+
rowControlMultiline: 'flex-row items-center justify-end gap-[8px] py-[10px]',
|
|
136
|
+
rowControlStacked: 'flex-col items-stretch pb-[7px]',
|
|
137
|
+
rowValue: `text-[13px] ${INK_SECONDARY}`,
|
|
138
|
+
rowChevron: `h-[11px] w-[11px] ${INK_TERTIARY}`,
|
|
139
|
+
rowDisabled: 'opacity-50',
|
|
140
|
+
|
|
141
|
+
// ---------- The appearance picker ----------
|
|
142
|
+
appearance: 'flex-row gap-[14px]',
|
|
143
|
+
appearanceOption: 'flex-col items-center gap-[5px]',
|
|
144
|
+
/** The picture's frame: a ring in the accent around the chosen one. */
|
|
145
|
+
appearanceFrame: 'p-[3px] rounded-[9px] border-2 border-transparent aria-checked:border-[#007aff] dark:aria-checked:border-[#0a84ff]',
|
|
146
|
+
appearanceThumb: 'h-[44px] w-[67px] flex-row rounded-[6px] overflow-hidden',
|
|
147
|
+
appearanceLight: 'flex-1 bg-[#e9e9eb]',
|
|
148
|
+
appearanceDark: 'flex-1 bg-[#2c2c2e]',
|
|
149
|
+
appearanceCaption: `text-[11px] ${INK}`,
|
|
150
|
+
|
|
151
|
+
// ---------- The update row ----------
|
|
152
|
+
updateStatus: `mt-[1px] text-[11px] ${INK_SECONDARY}`,
|
|
153
|
+
updateError: `mt-[1px] text-[11px] ${DANGER}`,
|
|
154
|
+
updateProgress: `text-[11px] ${INK_SECONDARY}`,
|
|
155
|
+
|
|
156
|
+
/** Text colours, for a component that needs one outside the lists above. */
|
|
157
|
+
ink: INK,
|
|
158
|
+
inkSecondary: INK_SECONDARY,
|
|
159
|
+
inkTertiary: INK_TERTIARY,
|
|
160
|
+
danger: DANGER,
|
|
161
|
+
accentText: ACCENT_TEXT,
|
|
162
|
+
} as const
|
|
163
|
+
|
|
164
|
+
/** SF Symbols the components draw themselves. */
|
|
165
|
+
export const SETTINGS_NATIVE_SYMBOLS = {
|
|
166
|
+
back: 'chevron.left',
|
|
167
|
+
forward: 'chevron.right',
|
|
168
|
+
disclosure: 'chevron.right',
|
|
169
|
+
} as const
|
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Settings window on a native host.
|
|
3
|
+
*
|
|
4
|
+
* `./view` is the window in a DOM. A native host (Craft's AppKit host) has no
|
|
5
|
+
* DOM: its screen is a tree of views behind stx's host adapter
|
|
6
|
+
* (`window.stx._host`), which answers the same few questions a DOM does: what
|
|
7
|
+
* is under a node, what attributes it carries, show it, hide it, hear a press
|
|
8
|
+
* on it. This is the window written against that adapter.
|
|
9
|
+
*
|
|
10
|
+
* The pieces meet the way they do on the web. A sidebar item is a node
|
|
11
|
+
* carrying `data-settings-item`, a pane `data-settings-pane`, a row
|
|
12
|
+
* `data-settings-row`; this reads them off the tree each time it needs them
|
|
13
|
+
* (so a row a `:for` added later is found), asks `./core` what to do, and
|
|
14
|
+
* writes the answer back: which pane is shown, which item is selected
|
|
15
|
+
* (`aria-selected`, which the item's classes draw), what a search leaves.
|
|
16
|
+
*
|
|
17
|
+
* What it cannot draw itself (the pane's name, whether back leads anywhere,
|
|
18
|
+
* the settings a search found) it reports through `onState`, and the shell's
|
|
19
|
+
* template draws from signals.
|
|
20
|
+
*/
|
|
21
|
+
import type { SettingsHistory, SettingsItemInfo, SettingsPaneInfo, SettingsRowInfo, SettingsSearchHit } from './core'
|
|
22
|
+
import type { SettingsNavigateDetail, SettingsPaneDetail, SettingsSearchDetail } from './view'
|
|
23
|
+
import { emptySettingsHistory, findSettingsPane, resolveSettingsPane, searchSettings, SETTINGS_HIGHLIGHT_MS, settingsCanGoBack, settingsCanGoForward, settingsHistoryBack, settingsHistoryCurrent, settingsHistoryForward, settingsHistoryVisit, settingsParentPane, settingsSidebarPane, settingsStorageName } from './core'
|
|
24
|
+
|
|
25
|
+
/** The part of stx's host adapter (`window.stx._host`) the window uses. */
|
|
26
|
+
export interface SettingsTreeHost<N = unknown> {
|
|
27
|
+
parent: (node: N) => N | null
|
|
28
|
+
descendants: (node: N) => N[]
|
|
29
|
+
getAttribute: (node: N, name: string) => string | null
|
|
30
|
+
setAttribute: (node: N, name: string, value: unknown) => void
|
|
31
|
+
removeAttribute: (node: N, name: string) => void
|
|
32
|
+
setStyle: (node: N, value: unknown) => void
|
|
33
|
+
listen: (node: N, event: string, handler: (payload?: unknown) => void) => (() => void)
|
|
34
|
+
observeChildren: (node: N, callback: () => void) => (() => void)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Where the open pane is remembered. On a native host, the app's storage. */
|
|
38
|
+
export interface SettingsTreeStorage {
|
|
39
|
+
get: (name: string) => string | null
|
|
40
|
+
set: (name: string, value: string) => void
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SettingsTreeOptions {
|
|
44
|
+
/** The pane to open on. A stored pane wins over it; the first pane is the fallback. */
|
|
45
|
+
pane?: string
|
|
46
|
+
/** Remembers the open pane under `stx:settings:<key>`. */
|
|
47
|
+
storageKey?: string
|
|
48
|
+
storage?: SettingsTreeStorage
|
|
49
|
+
/** The fill a found row wears for a moment, or nothing to leave it undrawn. */
|
|
50
|
+
wash?: () => string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** What the shell draws from signals. */
|
|
54
|
+
export interface SettingsTreeState {
|
|
55
|
+
pane: string
|
|
56
|
+
title: string
|
|
57
|
+
canGoBack: boolean
|
|
58
|
+
canGoForward: boolean
|
|
59
|
+
query: string
|
|
60
|
+
searching: boolean
|
|
61
|
+
/** The settings a search found, best first. */
|
|
62
|
+
results: Array<{ position: number, label: string, pane: string }>
|
|
63
|
+
/** A search that found nothing at all. */
|
|
64
|
+
empty: boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface SettingsTreeCallbacks {
|
|
68
|
+
onPane?: (detail: SettingsPaneDetail) => void
|
|
69
|
+
onSearch?: (detail: SettingsSearchDetail) => void
|
|
70
|
+
onNavigate?: (detail: SettingsNavigateDetail) => void
|
|
71
|
+
onState?: (state: SettingsTreeState) => void
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const ROOT = 'data-stx-settings'
|
|
75
|
+
const ITEM = 'data-settings-item'
|
|
76
|
+
const PANE = 'data-settings-pane'
|
|
77
|
+
const ROW = 'data-settings-row'
|
|
78
|
+
|
|
79
|
+
export class SettingsWindowTree<N = unknown> {
|
|
80
|
+
private history: SettingsHistory = emptySettingsHistory()
|
|
81
|
+
private current = ''
|
|
82
|
+
private query = ''
|
|
83
|
+
private hits: Array<{ hit: SettingsSearchHit, row: N }> = []
|
|
84
|
+
private highlighted: N | null = null
|
|
85
|
+
private highlightTimer: ReturnType<typeof setTimeout> | null = null
|
|
86
|
+
private readonly pressed = new Map<N, () => void>()
|
|
87
|
+
private readonly visible = new Map<N, boolean>()
|
|
88
|
+
private readonly disposers: Array<() => void> = []
|
|
89
|
+
private destroyed = false
|
|
90
|
+
|
|
91
|
+
constructor(
|
|
92
|
+
private readonly tree: SettingsTreeHost<N>,
|
|
93
|
+
private readonly root: N,
|
|
94
|
+
private readonly options: SettingsTreeOptions = {},
|
|
95
|
+
private readonly callbacks: SettingsTreeCallbacks = {},
|
|
96
|
+
) {
|
|
97
|
+
this.hear()
|
|
98
|
+
// A row or an item a `:for` or an `:if` adds later is heard too.
|
|
99
|
+
this.disposers.push(tree.observeChildren(root, () => {
|
|
100
|
+
if (this.destroyed)
|
|
101
|
+
return
|
|
102
|
+
this.hear()
|
|
103
|
+
this.show(false)
|
|
104
|
+
this.applySearch(false)
|
|
105
|
+
}))
|
|
106
|
+
|
|
107
|
+
const panes = this.panes()
|
|
108
|
+
const shown = this.paneNodes().find(pane => this.tree.getAttribute(pane, 'data-active') != null)
|
|
109
|
+
this.history = settingsHistoryVisit(this.history, resolveSettingsPane(panes, this.stored(), options.pane, shown ? this.attribute(shown, PANE) : ''))
|
|
110
|
+
this.show(true)
|
|
111
|
+
// The gaps between the sidebar's groups, which only the window can place.
|
|
112
|
+
this.applySearch(false)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The id of the open pane. */
|
|
116
|
+
getPane(): string {
|
|
117
|
+
return this.current
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Open a pane, as a press on its sidebar item would. False when there is no such pane. */
|
|
121
|
+
showPane(id: string): boolean {
|
|
122
|
+
if (!findSettingsPane(this.panes(), id))
|
|
123
|
+
return false
|
|
124
|
+
this.open(id)
|
|
125
|
+
return true
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Open the pane holding the row whose `data-setting` is `id` and highlight
|
|
130
|
+
* the row. False when no row has that id. The web view also scrolls the row
|
|
131
|
+
* into view; a native host offers no way to scroll to a view, so a row far
|
|
132
|
+
* down a long pane is highlighted where it is.
|
|
133
|
+
*/
|
|
134
|
+
revealSetting(id: string): boolean {
|
|
135
|
+
if (!id)
|
|
136
|
+
return false
|
|
137
|
+
const row = this.rowNodes().find(node => this.attribute(node, 'data-setting') === id)
|
|
138
|
+
const pane = row ? this.paneOf(row) : ''
|
|
139
|
+
if (!row || !pane)
|
|
140
|
+
return false
|
|
141
|
+
this.open(pane)
|
|
142
|
+
this.reveal(row)
|
|
143
|
+
return true
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
back(): void {
|
|
147
|
+
this.travel(settingsHistoryBack(this.history, this.panes()))
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
forward(): void {
|
|
151
|
+
this.travel(settingsHistoryForward(this.history))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Search for `query`, as typing it into the field would. */
|
|
155
|
+
search(query: string): void {
|
|
156
|
+
this.query = String(query ?? '')
|
|
157
|
+
this.applySearch(true)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Open the setting a search found, by its place in `state.results`. */
|
|
161
|
+
chooseResult(position: number): void {
|
|
162
|
+
const found = this.hits[position]
|
|
163
|
+
if (!found || this.tree.parent(found.row) === null)
|
|
164
|
+
return
|
|
165
|
+
this.open(found.hit.pane)
|
|
166
|
+
this.reveal(found.row)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
destroy(): void {
|
|
170
|
+
this.destroyed = true
|
|
171
|
+
for (const dispose of this.disposers) dispose()
|
|
172
|
+
this.disposers.length = 0
|
|
173
|
+
for (const stop of this.pressed.values()) stop()
|
|
174
|
+
this.pressed.clear()
|
|
175
|
+
this.visible.clear()
|
|
176
|
+
this.clearHighlight()
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// -------------------------------------------------------------------------
|
|
180
|
+
// Reading the tree
|
|
181
|
+
// -------------------------------------------------------------------------
|
|
182
|
+
|
|
183
|
+
private attribute(node: N, name: string): string {
|
|
184
|
+
return this.tree.getAttribute(node, name) ?? ''
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Show or hide a node by the `hidden` class, as `classList.toggle` would.
|
|
189
|
+
* What the server left hidden (every pane but the open one, so a window
|
|
190
|
+
* never draws them all for a frame) is hidden by that class, and a class
|
|
191
|
+
* is not something the host's own show and hide can lift. The adapter
|
|
192
|
+
* answers `class` with what the template wrote, not what was set since, so
|
|
193
|
+
* what each node was last told is kept here.
|
|
194
|
+
*/
|
|
195
|
+
private toggle(node: N, shown: boolean): void {
|
|
196
|
+
const authored = this.attribute(node, 'class').split(/\s+/).filter(Boolean)
|
|
197
|
+
const was = this.visible.has(node) ? this.visible.get(node)! : !authored.includes('hidden')
|
|
198
|
+
this.visible.set(node, shown)
|
|
199
|
+
if (was === shown)
|
|
200
|
+
return
|
|
201
|
+
const classes = authored.filter(name => name !== 'hidden')
|
|
202
|
+
this.tree.setAttribute(node, 'class', (shown ? classes : [...classes, 'hidden']).join(' '))
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
private has(node: N, name: string): boolean {
|
|
206
|
+
return this.tree.getAttribute(node, name) != null
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The nearest node at or above `node` carrying `name`, or null. */
|
|
210
|
+
private closest(node: N | null, name: string): N | null {
|
|
211
|
+
for (let current = node; current; current = this.tree.parent(current)) {
|
|
212
|
+
if (this.has(current, name))
|
|
213
|
+
return current
|
|
214
|
+
}
|
|
215
|
+
return null
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Every node carrying `name` that belongs to this window and not to one nested in a pane. */
|
|
219
|
+
private all(name: string, within: N = this.root): N[] {
|
|
220
|
+
return this.tree.descendants(within).filter(node => this.has(node, name) && this.closest(this.tree.parent(node), ROOT) === this.root)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private paneNodes(): N[] {
|
|
224
|
+
return this.all(PANE)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private panes(): SettingsPaneInfo[] {
|
|
228
|
+
return this.paneNodes().map((pane) => {
|
|
229
|
+
const id = this.attribute(pane, PANE)
|
|
230
|
+
return { id, title: this.attribute(pane, 'data-title') || id, keywords: this.attribute(pane, 'data-keywords'), parent: this.attribute(pane, 'data-parent') }
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private itemNodes(): N[] {
|
|
235
|
+
return this.all(ITEM)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private items(): SettingsItemInfo[] {
|
|
239
|
+
return this.itemNodes().map(item => ({
|
|
240
|
+
id: this.attribute(item, ITEM),
|
|
241
|
+
label: this.attribute(item, 'data-label').trim(),
|
|
242
|
+
keywords: this.attribute(item, 'data-keywords'),
|
|
243
|
+
}))
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private rowNodes(): N[] {
|
|
247
|
+
return this.all(ROW).filter(row => this.paneOf(row) !== '')
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
private paneOf(node: N): string {
|
|
251
|
+
const pane = this.closest(node, PANE)
|
|
252
|
+
return pane ? this.attribute(pane, PANE) : ''
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
private stored(): string {
|
|
256
|
+
if (!this.options.storageKey || !this.options.storage)
|
|
257
|
+
return ''
|
|
258
|
+
try {
|
|
259
|
+
return this.options.storage.get(settingsStorageName(this.options.storageKey)) || ''
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
// Storage can be missing, and a window that forgets still opens.
|
|
263
|
+
return ''
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private store(): void {
|
|
268
|
+
if (!this.options.storageKey || !this.options.storage || !this.current)
|
|
269
|
+
return
|
|
270
|
+
try {
|
|
271
|
+
this.options.storage.set(settingsStorageName(this.options.storageKey), this.current)
|
|
272
|
+
}
|
|
273
|
+
catch {}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// -------------------------------------------------------------------------
|
|
277
|
+
// Presses
|
|
278
|
+
// -------------------------------------------------------------------------
|
|
279
|
+
|
|
280
|
+
/** Hear a press on every sidebar item and navigation row, once each. */
|
|
281
|
+
private hear(): void {
|
|
282
|
+
const live = new Set<N>()
|
|
283
|
+
for (const item of this.itemNodes()) {
|
|
284
|
+
live.add(item)
|
|
285
|
+
if (!this.pressed.has(item))
|
|
286
|
+
this.pressed.set(item, this.tree.listen(item, 'click', () => this.open(this.attribute(item, ITEM))))
|
|
287
|
+
}
|
|
288
|
+
for (const row of this.rowNodes()) {
|
|
289
|
+
if (!this.has(row, 'data-navigate'))
|
|
290
|
+
continue
|
|
291
|
+
live.add(row)
|
|
292
|
+
if (!this.pressed.has(row))
|
|
293
|
+
this.pressed.set(row, this.tree.listen(row, 'click', () => this.navigate(row)))
|
|
294
|
+
}
|
|
295
|
+
for (const [node, stop] of this.pressed) {
|
|
296
|
+
if (live.has(node))
|
|
297
|
+
continue
|
|
298
|
+
stop()
|
|
299
|
+
this.pressed.delete(node)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private navigate(row: N): void {
|
|
304
|
+
if (this.attribute(row, 'aria-disabled') === 'true')
|
|
305
|
+
return
|
|
306
|
+
const id = this.attribute(row, 'data-navigate')
|
|
307
|
+
if (!findSettingsPane(this.panes(), id))
|
|
308
|
+
return
|
|
309
|
+
const from = this.current
|
|
310
|
+
this.open(id)
|
|
311
|
+
this.callbacks.onNavigate?.({ id, from, setting: this.attribute(row, 'data-setting') })
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// -------------------------------------------------------------------------
|
|
315
|
+
// Panes and history
|
|
316
|
+
// -------------------------------------------------------------------------
|
|
317
|
+
|
|
318
|
+
private open(id: string): void {
|
|
319
|
+
if (!id)
|
|
320
|
+
return
|
|
321
|
+
this.history = settingsHistoryVisit(this.history, id)
|
|
322
|
+
this.show(true)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
private travel(next: SettingsHistory): void {
|
|
326
|
+
if (next === this.history)
|
|
327
|
+
return
|
|
328
|
+
this.history = next
|
|
329
|
+
this.show(true)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** Draw the pane the history points at. */
|
|
333
|
+
private show(report: boolean): void {
|
|
334
|
+
const panes = this.panes()
|
|
335
|
+
const id = settingsHistoryCurrent(this.history)
|
|
336
|
+
const info = findSettingsPane(panes, id)
|
|
337
|
+
if (!info)
|
|
338
|
+
return
|
|
339
|
+
|
|
340
|
+
for (const pane of this.paneNodes()) {
|
|
341
|
+
this.toggle(pane, this.attribute(pane, PANE) === id)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// A sub-pane keeps the item of the pane it was reached from selected.
|
|
345
|
+
const selected = settingsSidebarPane(panes, id)
|
|
346
|
+
for (const item of this.itemNodes()) {
|
|
347
|
+
const on = this.attribute(item, ITEM) === selected
|
|
348
|
+
// The item's label, tile and badge read the same state for their colours.
|
|
349
|
+
for (const node of [item, ...this.tree.descendants(item)])
|
|
350
|
+
this.tree.setAttribute(node, 'aria-selected', on ? 'true' : 'false')
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const changed = this.current !== id
|
|
354
|
+
this.current = id
|
|
355
|
+
this.report()
|
|
356
|
+
if (changed) {
|
|
357
|
+
this.store()
|
|
358
|
+
if (report)
|
|
359
|
+
this.callbacks.onPane?.({ id, title: info.title, parent: settingsParentPane(panes, id) })
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
private report(): void {
|
|
364
|
+
const panes = this.panes()
|
|
365
|
+
const info = findSettingsPane(panes, this.current)
|
|
366
|
+
const searching = this.query.trim() !== ''
|
|
367
|
+
this.callbacks.onState?.({
|
|
368
|
+
pane: this.current,
|
|
369
|
+
title: info?.title ?? '',
|
|
370
|
+
canGoBack: settingsCanGoBack(this.history, panes),
|
|
371
|
+
canGoForward: settingsCanGoForward(this.history),
|
|
372
|
+
query: this.query,
|
|
373
|
+
searching,
|
|
374
|
+
results: this.hits.map(({ hit }, position) => ({ position, label: hit.label, pane: hit.paneTitle })),
|
|
375
|
+
empty: searching && this.shownItems === 0 && this.hits.length === 0,
|
|
376
|
+
})
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// -------------------------------------------------------------------------
|
|
380
|
+
// Search
|
|
381
|
+
// -------------------------------------------------------------------------
|
|
382
|
+
|
|
383
|
+
private shownItems = 0
|
|
384
|
+
|
|
385
|
+
private applySearch(report: boolean): void {
|
|
386
|
+
const rows = this.rowNodes()
|
|
387
|
+
const index: SettingsRowInfo[] = rows.map(row => ({
|
|
388
|
+
id: this.attribute(row, 'data-setting'),
|
|
389
|
+
label: this.attribute(row, 'data-label').trim(),
|
|
390
|
+
keywords: this.attribute(row, 'data-keywords'),
|
|
391
|
+
pane: this.paneOf(row),
|
|
392
|
+
}))
|
|
393
|
+
const found = searchSettings(this.query, { panes: this.panes(), items: this.items(), rows: index })
|
|
394
|
+
const searching = found.query !== ''
|
|
395
|
+
|
|
396
|
+
const shown = new Set<N>()
|
|
397
|
+
for (const item of this.itemNodes()) {
|
|
398
|
+
const on = !searching || found.panes.includes(this.attribute(item, ITEM))
|
|
399
|
+
if (on)
|
|
400
|
+
shown.add(item)
|
|
401
|
+
this.toggle(item, on)
|
|
402
|
+
}
|
|
403
|
+
this.shownItems = searching ? found.panes.length : shown.size
|
|
404
|
+
|
|
405
|
+
// A group whose every item is hidden would leave its gap behind, and the
|
|
406
|
+
// gap sits above every group but the first one showing.
|
|
407
|
+
let first = true
|
|
408
|
+
for (const group of this.all('data-settings-group')) {
|
|
409
|
+
const on = this.all(ITEM, group).some(item => shown.has(item))
|
|
410
|
+
this.toggle(group, on)
|
|
411
|
+
for (const gap of this.all('data-settings-group-gap', group))
|
|
412
|
+
this.toggle(gap, on && !first)
|
|
413
|
+
if (on)
|
|
414
|
+
first = false
|
|
415
|
+
}
|
|
416
|
+
// The account is not a search result.
|
|
417
|
+
for (const account of this.all('data-settings-account'))
|
|
418
|
+
this.toggle(account, !searching)
|
|
419
|
+
|
|
420
|
+
this.hits = found.settings.map(hit => ({ hit, row: rows[hit.row]! }))
|
|
421
|
+
this.report()
|
|
422
|
+
|
|
423
|
+
if (report) {
|
|
424
|
+
this.callbacks.onSearch?.({
|
|
425
|
+
query: this.query.trim(),
|
|
426
|
+
panes: found.panes,
|
|
427
|
+
settings: found.settings.map(hit => ({ id: hit.id, label: hit.label, pane: hit.pane })),
|
|
428
|
+
})
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// -------------------------------------------------------------------------
|
|
433
|
+
// Finding a row
|
|
434
|
+
// -------------------------------------------------------------------------
|
|
435
|
+
|
|
436
|
+
private reveal(row: N): void {
|
|
437
|
+
this.clearHighlight()
|
|
438
|
+
this.tree.setAttribute(row, 'data-highlight', '')
|
|
439
|
+
const wash = this.options.wash?.()
|
|
440
|
+
if (wash)
|
|
441
|
+
this.tree.setStyle(row, { backgroundColor: wash })
|
|
442
|
+
this.highlighted = row
|
|
443
|
+
this.highlightTimer = setTimeout(() => this.clearHighlight(), SETTINGS_HIGHLIGHT_MS)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
private clearHighlight(): void {
|
|
447
|
+
if (this.highlightTimer)
|
|
448
|
+
clearTimeout(this.highlightTimer)
|
|
449
|
+
this.highlightTimer = null
|
|
450
|
+
if (this.highlighted) {
|
|
451
|
+
this.tree.removeAttribute(this.highlighted, 'data-highlight')
|
|
452
|
+
this.tree.setStyle(this.highlighted, { backgroundColor: 'transparent' })
|
|
453
|
+
}
|
|
454
|
+
this.highlighted = null
|
|
455
|
+
}
|
|
456
|
+
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The Settings window without its stx templates.
|
|
3
3
|
*
|
|
4
|
-
* `@stacksjs/components/settings`. What the window decides (`./core`, no DOM)
|
|
5
|
-
*
|
|
4
|
+
* `@stacksjs/components/settings`. What the window decides (`./core`, no DOM),
|
|
5
|
+
* the view that applies it to any markup following the DOM contract, the
|
|
6
|
+
* same window over a native host's tree of views, and what an update row says.
|
|
6
7
|
*/
|
|
7
8
|
export * from './core'
|
|
9
|
+
export * from './native'
|
|
10
|
+
export * from './update'
|
|
8
11
|
export * from './view'
|