pi-odd-worktree 3.15.0 → 3.16.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 (127) hide show
  1. package/README.md +48 -25
  2. package/extensions/odd-worktree-tools.ts +64 -211
  3. package/lib/shell-api/api.ts +523 -0
  4. package/lib/shell-api/arm.ts +83 -0
  5. package/lib/shell-api/bus.ts +1389 -0
  6. package/lib/shell-api/capture.ts +1340 -0
  7. package/lib/shell-api/card-style-line.ts +112 -0
  8. package/lib/shell-api/card-style-rows.ts +390 -0
  9. package/lib/shell-api/card-style-transform.ts +862 -0
  10. package/lib/shell-api/card-style-wiring.ts +477 -0
  11. package/lib/shell-api/card-styles.ts +153 -0
  12. package/lib/shell-api/config.ts +71 -0
  13. package/lib/shell-api/extension.ts +247 -0
  14. package/lib/shell-api/hook.ts +322 -0
  15. package/lib/shell-api/index.ts +88 -0
  16. package/lib/shell-api/message-design-adoption.ts +549 -0
  17. package/lib/shell-api/message-design-rows.ts +313 -0
  18. package/lib/shell-api/message-design-wiring.ts +369 -0
  19. package/lib/shell-api/message-designs.ts +746 -0
  20. package/lib/shell-api/notices.ts +137 -0
  21. package/lib/shell-api/plugin-lines.ts +877 -0
  22. package/lib/shell-api/presence.ts +258 -0
  23. package/lib/shell-api/repaint.ts +351 -0
  24. package/lib/shell-api/row-labels.ts +146 -0
  25. package/lib/shell-api/row-names.ts +273 -0
  26. package/lib/shell-api/row-origin.ts +144 -0
  27. package/lib/shell-api/rows.ts +591 -0
  28. package/lib/shell-api/section-panel.ts +378 -0
  29. package/lib/shell-api/section-pointer.ts +164 -0
  30. package/lib/shell-api/sections-preview.ts +275 -0
  31. package/lib/shell-api/sections-rows.ts +133 -0
  32. package/lib/shell-api/sidebar-render.ts +705 -0
  33. package/lib/shell-api/sidebar.ts +1126 -0
  34. package/lib/shell-api/store.ts +279 -0
  35. package/lib/shell-api/tool-design-adoption.ts +263 -0
  36. package/lib/shell-api/tool-design-rows.ts +307 -0
  37. package/lib/shell-api/tool-design-wiring.ts +481 -0
  38. package/lib/shell-api/tool-designs.ts +1411 -0
  39. package/lib/shell-api/transform-pipeline.ts +351 -0
  40. package/lib/shell-api/types.ts +803 -0
  41. package/lib/shell-api/visibility.ts +154 -0
  42. package/lib/user-prompt-card.js +48 -2
  43. package/package.json +3 -2
  44. package/tools/files-bridge.test.js +81 -66
  45. package/tools/fixtures/load-extension.js +40 -18
  46. package/tools/issues-command.test.js +4 -7
  47. package/tools/issues-section.test.js +71 -81
  48. package/tools/odd-rail-wiring.test.js +16 -10
  49. package/tools/package-surface.test.js +42 -13
  50. package/tools/prs-command.test.js +11 -6
  51. package/tools/shell-api/api.test.ts +484 -0
  52. package/tools/shell-api/apply-on-move.test.ts +529 -0
  53. package/tools/shell-api/arm.test.ts +220 -0
  54. package/tools/shell-api/bridge-ready.test.ts +309 -0
  55. package/tools/shell-api/bus.test.ts +194 -0
  56. package/tools/shell-api/button-rows.test.ts +435 -0
  57. package/tools/shell-api/capture.test.ts +583 -0
  58. package/tools/shell-api/card-style-rows.test.ts +493 -0
  59. package/tools/shell-api/card-style-transform.test.ts +379 -0
  60. package/tools/shell-api/card-style-wiring.test.ts +258 -0
  61. package/tools/shell-api/card-styles-e2e.test.ts +502 -0
  62. package/tools/shell-api/card-styles.test.ts +324 -0
  63. package/tools/shell-api/card-transcript-path.test.ts +178 -0
  64. package/tools/shell-api/compatibility-regression.test.mjs +64 -0
  65. package/tools/shell-api/config.test.ts +93 -0
  66. package/tools/shell-api/docs-coverage.test.ts +82 -0
  67. package/tools/shell-api/e2e.test.ts +562 -0
  68. package/tools/shell-api/example-bash-identity.test.ts +372 -0
  69. package/tools/shell-api/example-bash-matrix.test.ts +856 -0
  70. package/tools/shell-api/example-designs.test.ts +737 -0
  71. package/tools/shell-api/example-neon-transform.test.ts +283 -0
  72. package/tools/shell-api/example-plain-style.test.ts +1225 -0
  73. package/tools/shell-api/extension.test.ts +157 -0
  74. package/tools/shell-api/fake-events.ts +58 -0
  75. package/tools/shell-api/fake-host.ts +211 -0
  76. package/tools/shell-api/fake-sidebar.ts +516 -0
  77. package/tools/shell-api/fake-transcript.ts +486 -0
  78. package/tools/shell-api/frame-telemetry.test.ts +317 -0
  79. package/tools/shell-api/hook.test.ts +196 -0
  80. package/tools/shell-api/manifest.test.ts +47 -0
  81. package/tools/shell-api/message-design-adoption.test.ts +611 -0
  82. package/tools/shell-api/message-design-follows.test.ts +361 -0
  83. package/tools/shell-api/message-design-pi-release.test.mjs +157 -0
  84. package/tools/shell-api/message-design-regressions.test.mjs +162 -0
  85. package/tools/shell-api/message-design-rows.test.ts +266 -0
  86. package/tools/shell-api/message-design-wiring.test.ts +362 -0
  87. package/tools/shell-api/message-designs.test.ts +339 -0
  88. package/tools/shell-api/new-category.test.ts +1101 -0
  89. package/tools/shell-api/notices.test.ts +292 -0
  90. package/tools/shell-api/plugin-integration-lines.test.ts +827 -0
  91. package/tools/shell-api/presence.test.ts +396 -0
  92. package/tools/shell-api/rail-part-hits.test.ts +281 -0
  93. package/tools/shell-api/rail-surfaces.test.ts +402 -0
  94. package/tools/shell-api/rail-width.test.ts +406 -0
  95. package/tools/shell-api/regression.test.ts +434 -0
  96. package/tools/shell-api/repaint-api.test.ts +200 -0
  97. package/tools/shell-api/reserved-categories.test.ts +250 -0
  98. package/tools/shell-api/row-labels.test.ts +261 -0
  99. package/tools/shell-api/row-names.test.ts +407 -0
  100. package/tools/shell-api/row-origin.test.ts +395 -0
  101. package/tools/shell-api/row-placement-regression.test.ts +797 -0
  102. package/tools/shell-api/row-placement.test.ts +410 -0
  103. package/tools/shell-api/section-interaction.test.ts +364 -0
  104. package/tools/shell-api/section-panel.test.ts +430 -0
  105. package/tools/shell-api/section-placement-rail.test.ts +390 -0
  106. package/tools/shell-api/section-placement.test.ts +386 -0
  107. package/tools/shell-api/sections-preview.test.ts +312 -0
  108. package/tools/shell-api/sections-rows.test.ts +293 -0
  109. package/tools/shell-api/sidebar-backend.test.ts +477 -0
  110. package/tools/shell-api/sidebar-e2e.test.ts +317 -0
  111. package/tools/shell-api/sidebar-hover.test.ts +276 -0
  112. package/tools/shell-api/sidebar-independent.test.ts +291 -0
  113. package/tools/shell-api/sidebar-render.test.ts +281 -0
  114. package/tools/shell-api/sidebar-sections.test.ts +247 -0
  115. package/tools/shell-api/store.test.ts +192 -0
  116. package/tools/shell-api/surface-repaint-cost.test.ts +520 -0
  117. package/tools/shell-api/tool-design-card-style.test.ts +308 -0
  118. package/tools/shell-api/tool-design-chain.test.ts +471 -0
  119. package/tools/shell-api/tool-design-cost.test.ts +591 -0
  120. package/tools/shell-api/tool-design-rows.test.ts +627 -0
  121. package/tools/shell-api/tool-design-scopes-audit.test.ts +212 -0
  122. package/tools/shell-api/tool-designs.test.ts +2412 -0
  123. package/tools/shell-api/transform-pipeline.test.ts +376 -0
  124. package/tools/shell-api/transforms-bus.test.ts +233 -0
  125. package/tools/shell-api/verifier-width-regressions.test.ts +147 -0
  126. package/tools/shell-api-rpc.test.js +189 -0
  127. package/tools/user-prompt-card.test.js +80 -10
@@ -0,0 +1,275 @@
1
+ /**
2
+ * The shared `Sections` preview (S1, S2, S4).
3
+ *
4
+ * The host draws ONE preview for every `Sections` row: a title naming that row (`Sections · rdd
5
+ * shown`) and a sample listing the sections active under its hypothetical (`[changes] [rdd]
6
+ * [agents] [todo] [usageCost]`), computed from its own `visual.visibility`
7
+ * (`extensions/gentle-shell.ts:3522-3524`). That list is closed to a bridge section: the host
8
+ * validates `visual-customization.json` against its own key list, so a key the bridge added cannot
9
+ * live there (`./visibility.ts`). Left alone, a section registered through `registerSection`
10
+ * therefore never appeared in the shared list — the bridge's rows drew a picture of the card
11
+ * instead, which is the defect this module fixes.
12
+ *
13
+ * Two halves, one list:
14
+ *
15
+ * - `patchSharedSections` replaces each host `Sections` row in the live rows array with a shallow
16
+ * copy whose `preview()` calls the host's own function UNCHANGED and appends the bridge's
17
+ * currently visible section labels to its sample. It records what it replaced, so the early
18
+ * capture path can undo it exactly like the `Card style` splice (`undoSharedSections`).
19
+ * - `sharedSectionsPreview` is what a bridge toggle row returns: the host's title shape and the
20
+ * same merged list, with THIS row's hypothetical applied to its own section (a `shown` row lists
21
+ * it, a `hidden` row does not) on top of the persisted state of the rest.
22
+ *
23
+ * The base list is the host's, honestly borrowed (L6): the ORIGINAL `preview()` of a host row whose
24
+ * `current()` is `true` renders the persisted state, so its sample IS the base. Nothing here reads
25
+ * or reinterprets `visual.visibility`, and a wrapper is never asked for a base — that would append
26
+ * the bridge's labels twice. With no host `Sections` row in the array the base is the host's own
27
+ * empty case, `[no optional sections]`.
28
+ *
29
+ * Nothing here may throw into the menu: a host preview that throws, a row that cannot be copied and
30
+ * a frozen array all leave the host's own drawing in place and lose only the bridge's labels.
31
+ *
32
+ * This module is internal: `src/index.ts` does not re-export it.
33
+ */
34
+
35
+ import { snapshotSections } from "./bus.ts";
36
+ import { sectionRowLabels } from "./row-labels.ts";
37
+ import { isRecord } from "./rows.ts";
38
+ import { SECTION_TOGGLE_ROW, SECTIONS_CATEGORY } from "./sections-rows.ts";
39
+ import type { CustomizeRow, RowPreview } from "./types.ts";
40
+ import { getSectionVisibility, isSectionVisible } from "./visibility.ts";
41
+
42
+ /** Marks a host row this module already wrapped; the value is the host row itself. */
43
+ const PATCHED_ROW = Symbol.for("gentle-shell-bridge.sections-preview.patched.v1");
44
+
45
+ /** The same words WITHOUT the brackets, as they sit inside the host's list. */
46
+ const EMPTY_LIST_TEXT = "no optional sections";
47
+
48
+ /** The host's own empty list, drawn when no optional section is active. */
49
+ export const NO_OPTIONAL_SECTIONS = `[${EMPTY_LIST_TEXT}]`;
50
+
51
+ /** One replaced host row: where it was, what it was, and the wrapper now standing in its place. */
52
+ export interface PatchedSectionRow {
53
+ /** The element's index in the live rows array at the moment it was replaced. */
54
+ index: number;
55
+ /** The host's own row object, untouched. */
56
+ original: CustomizeRow;
57
+ /** The shallow copy that is now in the array. */
58
+ wrapper: CustomizeRow;
59
+ }
60
+
61
+ /** What one build's patch replaced, so the early rollback can put all of it back. */
62
+ export interface SharedSectionsPatch {
63
+ /** The live rows array the host holds. */
64
+ array: CustomizeRow[];
65
+ rows: readonly PatchedSectionRow[];
66
+ }
67
+
68
+ /**
69
+ * The patch the last `patchSharedSections` call made, and the only place a bridge row's preview
70
+ * finds the host's base list. Process-global is not needed: both capture paths call the patch from
71
+ * this module's own copy, in the same process, on every build.
72
+ */
73
+ let lastPatch: SharedSectionsPatch | undefined;
74
+
75
+ /**
76
+ * Replace every host `Sections` row in `array` with a shallow copy whose preview appends the
77
+ * bridge's currently visible section labels.
78
+ *
79
+ * Idempotent per build: a row already carrying this module's marker is skipped, so a second call
80
+ * cannot wrap a wrapper (and therefore cannot append twice). Never throws — a row that cannot be
81
+ * copied, and an array that refuses the assignment, stay exactly as the host made them.
82
+ *
83
+ * Returns `undefined` when the array held no host `Sections` row to replace.
84
+ */
85
+ export function patchSharedSections(array: CustomizeRow[]): SharedSectionsPatch | undefined {
86
+ const patched: PatchedSectionRow[] = [];
87
+ let length = 0;
88
+ try {
89
+ length = array.length;
90
+ } catch {
91
+ return undefined;
92
+ }
93
+ for (let index = 0; index < length; index++) {
94
+ try {
95
+ const original = array[index];
96
+ if (!isHostSectionsRow(original)) continue;
97
+ const wrapper: CustomizeRow = { ...original, preview: () => mergedHostPreview(original) };
98
+ array[index] = wrapper;
99
+ Object.defineProperty(wrapper, PATCHED_ROW, {
100
+ value: original,
101
+ enumerable: false,
102
+ configurable: true,
103
+ });
104
+ patched.push({ index, original, wrapper });
105
+ } catch {
106
+ // A row we cannot copy (or an array that refuses the write) stays as the host made it.
107
+ }
108
+ }
109
+ if (patched.length === 0) return undefined;
110
+ const patch: SharedSectionsPatch = { array, rows: patched };
111
+ lastPatch = patch;
112
+ return patch;
113
+ }
114
+
115
+ /**
116
+ * Put each original host row back by index, only while the element there still is this patch's
117
+ * wrapper. The host may have moved elements between the patch and the rollback; an element it moved
118
+ * is left alone rather than guessed at.
119
+ */
120
+ export function undoSharedSections(patch: SharedSectionsPatch | undefined): void {
121
+ if (patch === undefined) return;
122
+ for (const row of patch.rows) {
123
+ try {
124
+ if (patch.array[row.index] === row.wrapper) patch.array[row.index] = row.original;
125
+ } catch {
126
+ // frozen, sealed or moved: nothing more can be taken back from this array
127
+ }
128
+ }
129
+ if (lastPatch === patch) lastPatch = undefined;
130
+ }
131
+
132
+ /** Test-only seam; deliberately not re-exported from `src/index.ts`. */
133
+ export function resetSharedSectionsPreviewForTests(): void {
134
+ lastPatch = undefined;
135
+ }
136
+
137
+ /**
138
+ * What a bridge `Sections` toggle row returns: the host's title shape, and the shared list with
139
+ * this row's own hypothetical (`shown`) applied to `sectionId` instead of its persisted value.
140
+ */
141
+ export function sharedSectionsPreview(
142
+ label: string,
143
+ shown: boolean,
144
+ sectionId: string,
145
+ ): RowPreview {
146
+ return {
147
+ title: `Sections · ${label} ${shown ? "shown" : "hidden"}`,
148
+ sample: sharedSample(sectionId, shown),
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Append the bridge's labels to the host's own list.
154
+ *
155
+ * The host's shape is `[a] [b]`, with its empty case written literally as `[no optional sections]`;
156
+ * rebuilding it as `[a] [b] [issues]` keeps the host's own text. A sample that is not that bracket
157
+ * shape gets the labels appended as `[...]` and is never dropped. No labels means the sample is
158
+ * returned byte for byte, so the bridge adds nothing when it has nothing to add.
159
+ */
160
+ export function mergeSectionLabels(sample: string, labels: readonly string[]): string {
161
+ if (labels.length === 0) return sample;
162
+ const additions = labels.map((label) => `[${label}]`);
163
+ const match = BRACKET_LIST.exec(sample);
164
+ if (match === null) return `${sample} ${additions.join(" ")}`;
165
+ const inner = match[1];
166
+ const entries =
167
+ inner === EMPTY_LIST_TEXT || inner.trim() === "" ? [] : inner.split("] [");
168
+ return [...entries.map((entry) => `[${entry}]`), ...additions].join(" ");
169
+ }
170
+
171
+ /** The host's whole sample shape: brackets around the list, nothing before or after. */
172
+ const BRACKET_LIST = /^\[([\s\S]*)\]$/u;
173
+
174
+ /** One host row, copied with its sample extended. Its own preview is called, never reimplemented. */
175
+ function mergedHostPreview(row: CustomizeRow): RowPreview {
176
+ const host = readHostPreview(row);
177
+ const labels = safeSectionLabels();
178
+ if (host === undefined) {
179
+ // The host's preview threw or answered nonsense: its title is unknown, but the bridge's list is
180
+ // still previewable, so the menu keeps working instead of breaking on one row.
181
+ return { title: SECTIONS_CATEGORY, sample: mergeSectionLabels(NO_OPTIONAL_SECTIONS, labels) };
182
+ }
183
+ return { title: host.title, sample: mergeSectionLabels(host.sample, labels) };
184
+ }
185
+
186
+ /**
187
+ * The base list, borrowed from the host: the original preview of a row whose `current()` is `true`
188
+ * renders exactly the persisted state. Fallbacks, in order: the first host `Sections` row's original
189
+ * sample, then the host's empty case.
190
+ */
191
+ function baseSample(): string {
192
+ const patch = lastPatch;
193
+ if (patch === undefined) return NO_OPTIONAL_SECTIONS;
194
+ for (const row of patch.rows) {
195
+ if (!isCurrentChoice(row.original)) continue;
196
+ const preview = readHostPreview(row.original);
197
+ if (preview !== undefined) return preview.sample;
198
+ }
199
+ for (const row of patch.rows) {
200
+ const preview = readHostPreview(row.original);
201
+ if (preview !== undefined) return preview.sample;
202
+ }
203
+ return NO_OPTIONAL_SECTIONS;
204
+ }
205
+
206
+ function sharedSample(sectionId: string, shown: boolean): string {
207
+ try {
208
+ return mergeSectionLabels(baseSample(), sectionLabels({ id: sectionId, shown }));
209
+ } catch {
210
+ // The host renderer must never meet a throw from a bridge preview.
211
+ return NO_OPTIONAL_SECTIONS;
212
+ }
213
+ }
214
+
215
+ /** The shown bridge sections' labels, in the order the rows read them. */
216
+ function sectionLabels(focused?: { id: string; shown: boolean }): string[] {
217
+ const visibility = getSectionVisibility();
218
+ const labels: string[] = [];
219
+ for (const entry of sectionRowLabels(snapshotSections())) {
220
+ const shown =
221
+ focused !== undefined && entry.section.id === focused.id
222
+ ? focused.shown
223
+ : isSectionVisible(visibility, entry.section);
224
+ if (shown) labels.push(entry.label);
225
+ }
226
+ return labels;
227
+ }
228
+
229
+ function safeSectionLabels(): string[] {
230
+ try {
231
+ return sectionLabels();
232
+ } catch {
233
+ return [];
234
+ }
235
+ }
236
+
237
+ /** Run the host's own preview exactly as the host would, and reject anything that is not its shape. */
238
+ function readHostPreview(row: CustomizeRow): RowPreview | undefined {
239
+ const preview = row.preview;
240
+ if (typeof preview !== "function") return undefined;
241
+ try {
242
+ const shown: unknown = preview.call(row);
243
+ if (isRecord(shown) && typeof shown.title === "string" && typeof shown.sample === "string") {
244
+ return { title: shown.title, sample: shown.sample };
245
+ }
246
+ } catch {
247
+ // A host preview that throws loses its own sample and nothing else.
248
+ }
249
+ return undefined;
250
+ }
251
+
252
+ /** Whether this host row's hypothetical is the persisted state, read live like the host does. */
253
+ function isCurrentChoice(row: CustomizeRow): boolean {
254
+ const choice = row.choice;
255
+ const current = choice?.current;
256
+ if (choice === undefined || typeof current !== "function") return false;
257
+ try {
258
+ return current.call(choice) === true;
259
+ } catch {
260
+ return false;
261
+ }
262
+ }
263
+
264
+ /**
265
+ * A host `Sections` row: the host's category, a callable preview and a named setting — and not one
266
+ * the bridge itself created (its own toggle rows share the category and both fields).
267
+ */
268
+ function isHostSectionsRow(value: CustomizeRow): boolean {
269
+ if (value === null || typeof value !== "object") return false;
270
+ if (Object.hasOwn(value, PATCHED_ROW)) return false;
271
+ if (Object.hasOwn(value, SECTION_TOGGLE_ROW)) return false;
272
+ if (value.category !== SECTIONS_CATEGORY) return false;
273
+ if (typeof value.preview !== "function") return false;
274
+ return typeof value.choice?.setting === "string";
275
+ }
@@ -0,0 +1,133 @@
1
+ /**
2
+ * The automatic `Sections` toggle rows (S5, S6): the half of the sidebar feature a user actually
3
+ * clicks.
4
+ *
5
+ * A plugin that calls `registerSection` never writes a `setting`, a `value`, a `current()`, an
6
+ * `action()` or a `preview()`, and never names the `Sections` category — this module derives all of
7
+ * it. That is S1's promise: adding the section is the whole integration.
8
+ *
9
+ * ## The toggle
10
+ *
11
+ * Two consecutive rows share one `choice.setting` — the line's name, which the host prints — with
12
+ * values `shown` and `hidden`, so the host's renderer draws one line: `music ● shown ○ hidden`.
13
+ * The setting is the row's LABEL, derived from the title (or the author's `rowLabel`): an id in
14
+ * that field would put `Section: bash-card.status` in front of the user (N1, N2, `./row-labels.ts`).
15
+ * The section id stays identity — replacement, the disposer, the visibility store — and never
16
+ * reaches a row. `current()` reads the persisted value live, `action()` writes it before returning
17
+ * (the host repaints afterwards, and its repaint re-reads `current()`), and `preview()` draws the
18
+ * host's ONE shared `Sections` preview with the bridge's own sections in it (`./sections-preview.ts`)
19
+ * instead of a picture of the card: every `Sections` row previews the same list.
20
+ *
21
+ * ## Where the value lives
22
+ *
23
+ * In the bridge's own store, keyed by section id — see `./visibility.ts` for the document and for why
24
+ * the host's `visual-customization.json` cannot hold it. Visibility is bridge-owned state, not plugin
25
+ * state: the bridge added the toggle, so the plugin needs no store, no file and no setting name.
26
+ *
27
+ * Rows are built per menu build from the live registry, so a section registered later appears (and a
28
+ * disposed section's rows disappear) without re-registering this provider. Rows must not be cached
29
+ * anywhere: a cached list would freeze the sections that existed at the first build.
30
+ *
31
+ * This module is internal: `src/index.ts` does not re-export it.
32
+ */
33
+
34
+ import { registerBridgeRows, snapshotSections } from "./bus.ts";
35
+ import { refreshCardStyleSurfaces } from "./card-style-wiring.ts";
36
+ import { requestSurfaceRepaint } from "./repaint.ts";
37
+ import { sectionRowLabels } from "./row-labels.ts";
38
+ import { sharedSectionsPreview } from "./sections-preview.ts";
39
+ import type { CustomizeRow, SidebarSection } from "./types.ts";
40
+ import { getSectionVisibility, isSectionVisible, setSectionVisible } from "./visibility.ts";
41
+
42
+ /** The internal provider's id; it is the bridge's own registration, never a plugin's. */
43
+ export const SECTION_ROWS_PROVIDER_ID = "bridge.sections";
44
+
45
+ /** The host category the rows join; it already exists and the bridge cannot create one. */
46
+ export const SECTIONS_CATEGORY = "Sections";
47
+
48
+ export const SECTION_SHOWN = "shown";
49
+ export const SECTION_HIDDEN = "hidden";
50
+
51
+ /** One of a toggle's two values. */
52
+ export type SectionValue = typeof SECTION_SHOWN | typeof SECTION_HIDDEN;
53
+
54
+ /**
55
+ * Marks a row this module created. `Sections` is the host's category and the bridge's own rows share
56
+ * it, so the shared-preview patch (`./sections-preview.ts`) needs a way to tell the two apart: a
57
+ * bridge row is skipped, because its preview is already the merged list.
58
+ */
59
+ export const SECTION_TOGGLE_ROW = Symbol.for("gentle-shell-bridge.sections-rows.toggle.v1");
60
+
61
+ function toggleRow(section: SidebarSection, label: string, value: SectionValue): CustomizeRow {
62
+ const shown = value === SECTION_SHOWN;
63
+ const row: CustomizeRow = {
64
+ category: SECTIONS_CATEGORY,
65
+ // The label names the line, so a user can tell which toggle controls what. It is the SAME string
66
+ // as `choice.setting`, because the host prints the setting as the line's name; the id is not a
67
+ // label and never appears here.
68
+ label: () => label,
69
+ choice: {
70
+ setting: label,
71
+ value,
72
+ // Move the cursor, press Enter to apply (A1, `docs/contract.md` §4): the host applies a choice on
73
+ // ←/→ only when this field is not `false`, and a bridge row that applied on the move made the
74
+ // user's focus land on a value they never confirmed. `lib/visual-customize-view.ts:565`.
75
+ applyOnMove: false,
76
+ // Live read: the bullet reflects what is on disk right now, not what was true at build time.
77
+ current: () => isSectionVisible(getSectionVisibility(), section) === shown,
78
+ },
79
+ // The host's shared preview, with this row's own hypothetical applied: the same list the host's
80
+ // own `Sections` rows draw, never a picture of the card.
81
+ preview: () => sharedSectionsPreview(label, shown, section.id),
82
+ // Persist FIRST and synchronously; the host's repaint runs after this returns and re-reads
83
+ // `current()`, so the bullet cannot land on a value that was never written. The store is keyed
84
+ // by the section id, never by the label.
85
+ //
86
+ // Then REPAINT the rail before returning (R3): writing the store alone changes nothing on screen
87
+ // until the host is told, which is why a bridge row used to lag behind the host's own rows by
88
+ // seconds. A press that would store the value already in force changes nothing and asks for no
89
+ // frame.
90
+ action: () => {
91
+ if (isSectionVisible(getSectionVisibility(), section) === shown) return;
92
+ setSectionVisible(section.id, shown);
93
+ refreshCardStyleSurfaces();
94
+ requestSurfaceRepaint({
95
+ cause: `section ${JSON.stringify(section.id)} → ${shown ? "shown" : "hidden"}`,
96
+ surfaces: ["rail"],
97
+ });
98
+ },
99
+ };
100
+ Object.defineProperty(row, SECTION_TOGGLE_ROW, {
101
+ value: true,
102
+ enumerable: false,
103
+ configurable: true,
104
+ });
105
+ return row;
106
+ }
107
+
108
+ /**
109
+ * Register the bridge's own rows provider and return its disposer. Safe to call more than once: the
110
+ * bus replaces a provider with the same `id` in place, so one entry (and one `Sections` line per
111
+ * section) always exists.
112
+ */
113
+ export function installSectionRows(): () => void {
114
+ return registerBridgeRows({
115
+ id: SECTION_ROWS_PROVIDER_ID,
116
+ category: SECTIONS_CATEGORY,
117
+ rows: () => {
118
+ const rows: CustomizeRow[] = [];
119
+ // Per build, from the live registry: the collision assignment depends on which sections exist
120
+ // right now, so it is recomputed here and reported by the diagnostics record through the same
121
+ // function — the menu and the report can never disagree.
122
+ for (const entry of sectionRowLabels(snapshotSections())) {
123
+ // Both values are emitted even while the section is hidden: that toggle is how a user
124
+ // brings it back.
125
+ rows.push(
126
+ toggleRow(entry.section, entry.label, SECTION_SHOWN),
127
+ toggleRow(entry.section, entry.label, SECTION_HIDDEN),
128
+ );
129
+ }
130
+ return rows;
131
+ },
132
+ });
133
+ }