@narumitw/pi-btw 0.61.1 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -5
- package/dist/index.ts +1067 -557
- package/dist/index.ts.map +4 -4
- package/docs/core-alignment.md +277 -0
- package/docs/native-appearance.md +857 -0
- package/docs/workflows.md +34 -13
- package/package.json +4 -4
- package/src/btw.ts +152 -108
- package/src/conversation-chrome.ts +93 -0
- package/src/fullscreen-renderer.ts +329 -0
- package/src/fullscreen-ui.ts +109 -309
- package/src/keybindings.ts +101 -23
- package/src/menu.ts +42 -27
- package/src/session-lifetime.ts +66 -0
- package/src/transcript-pager.ts +366 -164
- package/src/workspace-layout.ts +96 -36
package/src/fullscreen-ui.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
1
|
import {
|
|
3
2
|
copyToClipboard as copyToHostClipboard,
|
|
4
3
|
type ExtensionCommandContext,
|
|
5
4
|
type KeybindingsManager,
|
|
6
5
|
type Theme,
|
|
7
6
|
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { type Component, type OverlayHandle, ScrollView, type TUI, truncateToWidth } from "@earendil-works/pi-tui";
|
|
8
8
|
import {
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
type
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
9
|
+
type BtwFullscreenTui,
|
|
10
|
+
createBtwFullscreenTui,
|
|
11
|
+
hasManualSelectionCopyApi,
|
|
12
|
+
openUrlInBrowser,
|
|
13
|
+
} from "./fullscreen-renderer.js";
|
|
14
|
+
import {
|
|
15
|
+
type BtwKeybindingOverrides,
|
|
16
|
+
BtwPasteGuard,
|
|
17
|
+
getBtwShortcuts,
|
|
18
|
+
resolveBtwShortcuts,
|
|
19
|
+
setBtwHostInputBlockers,
|
|
20
|
+
setBtwKeybindings,
|
|
21
|
+
setBtwShortcuts,
|
|
22
|
+
} from "./keybindings.js";
|
|
23
23
|
import type { BtwMainThreadUpdateSubscription } from "./main-thread-updates.js";
|
|
24
24
|
import type { BtwLayout } from "./settings.js";
|
|
25
|
-
import {
|
|
25
|
+
import { sanitizeSingleLine } from "./text.js";
|
|
26
26
|
import { type BtwFullscreenLayoutComponent, BtwMainThreadInput, BtwSplitPane } from "./workspace-layout.js";
|
|
27
27
|
|
|
28
28
|
type BtwCustomOptions = Parameters<ExtensionCommandContext["ui"]["custom"]>[1];
|
|
@@ -33,20 +33,13 @@ type BtwCustomFactory<T> = (
|
|
|
33
33
|
done: (result: T) => void,
|
|
34
34
|
) => (Component & { dispose?(): void }) | Promise<Component & { dispose?(): void }>;
|
|
35
35
|
|
|
36
|
-
type BtwFullscreenTui = TUI & {
|
|
37
|
-
flash?: (message: string, durationMs?: number) => void;
|
|
38
|
-
setLayoutRoot(component: Component | undefined): void;
|
|
39
|
-
hasFocusedOverlay?(): boolean;
|
|
40
|
-
addInputListenerBeforeAll?(listener: TuiInputListener): () => void;
|
|
41
|
-
addInputListenerBeforeViewport?(listener: TuiInputListener): () => void;
|
|
42
|
-
setViewportTarget?(scrollView: ScrollView | undefined): void;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
36
|
type FocusInspectableTui = TUI & {
|
|
46
37
|
getFocusedComponent?(): Component | null;
|
|
47
38
|
};
|
|
48
39
|
|
|
49
40
|
export interface BtwFullscreenOptions {
|
|
41
|
+
signal?: AbortSignal;
|
|
42
|
+
canRestoreParent?(): boolean;
|
|
50
43
|
keybindings?: BtwKeybindingOverrides;
|
|
51
44
|
copyOnSelect?: boolean;
|
|
52
45
|
layout?: BtwLayout;
|
|
@@ -115,6 +108,10 @@ export async function runBtwFullscreen<T>(
|
|
|
115
108
|
run,
|
|
116
109
|
(value) => {
|
|
117
110
|
try {
|
|
111
|
+
if (options.canRestoreParent?.() === false) {
|
|
112
|
+
done(value);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
118
115
|
liveEditorText = ctx.ui.getEditorText();
|
|
119
116
|
restoreEditor = true;
|
|
120
117
|
} catch {
|
|
@@ -132,7 +129,7 @@ export async function runBtwFullscreen<T>(
|
|
|
132
129
|
onHandle: (handle) => host?.setParentOverlay(handle),
|
|
133
130
|
},
|
|
134
131
|
);
|
|
135
|
-
if (restoreEditor) {
|
|
132
|
+
if (restoreEditor && options.canRestoreParent?.() !== false) {
|
|
136
133
|
try {
|
|
137
134
|
if (ctx.ui.getEditorText() !== liveEditorText) ctx.ui.setEditorText(liveEditorText);
|
|
138
135
|
} catch {
|
|
@@ -143,281 +140,6 @@ export async function runBtwFullscreen<T>(
|
|
|
143
140
|
return outcome.value;
|
|
144
141
|
}
|
|
145
142
|
|
|
146
|
-
type BtwInputListeners = {
|
|
147
|
-
beforeAll: Set<TuiInputListener>;
|
|
148
|
-
beforeViewport: Set<TuiInputListener>;
|
|
149
|
-
regular: Set<TuiInputListener>;
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const btwInputListeners = new WeakMap<BtwTuiAltScreen, BtwInputListeners>();
|
|
153
|
-
|
|
154
|
-
function dispatchBtwInput(listeners: BtwInputListeners, data: string): TuiInputListenerResult {
|
|
155
|
-
let current = data;
|
|
156
|
-
for (const group of [listeners.beforeAll, listeners.beforeViewport, listeners.regular]) {
|
|
157
|
-
for (const listener of group) {
|
|
158
|
-
const result = listener(current);
|
|
159
|
-
if (result?.consume) return result;
|
|
160
|
-
if (result?.data !== undefined) current = result.data;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return current === data ? undefined : { data: current };
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
class BtwTuiAltScreen extends TuiAltScreen {
|
|
167
|
-
hasFocusedOverlay(): boolean {
|
|
168
|
-
return this.isOverlayFocused();
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
setViewportTarget(scrollView: ScrollView | undefined): void {
|
|
172
|
-
if (!scrollView) return;
|
|
173
|
-
const layout = Reflect.get(this, "currentLayout") as { primaryScrollView?: ScrollView } | undefined;
|
|
174
|
-
if (layout) layout.primaryScrollView = scrollView;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
override addInputListener(listener: TuiInputListener): () => void {
|
|
178
|
-
let listeners = btwInputListeners.get(this);
|
|
179
|
-
if (!listeners) {
|
|
180
|
-
const registeredListeners: BtwInputListeners = {
|
|
181
|
-
beforeAll: new Set(),
|
|
182
|
-
beforeViewport: new Set(),
|
|
183
|
-
regular: new Set(),
|
|
184
|
-
};
|
|
185
|
-
btwInputListeners.set(this, registeredListeners);
|
|
186
|
-
super.addInputListener((data) => dispatchBtwInput(registeredListeners, data));
|
|
187
|
-
listeners = registeredListeners;
|
|
188
|
-
}
|
|
189
|
-
listeners.regular.add(listener);
|
|
190
|
-
return () => listeners.regular.delete(listener);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
addInputListenerBeforeAll(listener: TuiInputListener): () => void {
|
|
194
|
-
const listeners = btwInputListeners.get(this);
|
|
195
|
-
if (!listeners) return super.addInputListener(listener);
|
|
196
|
-
listeners.beforeAll.add(listener);
|
|
197
|
-
return () => listeners.beforeAll.delete(listener);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
addInputListenerBeforeViewport(listener: TuiInputListener): () => void {
|
|
201
|
-
const listeners = btwInputListeners.get(this);
|
|
202
|
-
if (!listeners) return super.addInputListener(listener);
|
|
203
|
-
listeners.beforeViewport.add(listener);
|
|
204
|
-
return () => listeners.beforeViewport.delete(listener);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
override removeInputListener(listener: TuiInputListener): void {
|
|
208
|
-
const listeners = btwInputListeners.get(this);
|
|
209
|
-
if (!listeners) {
|
|
210
|
-
super.removeInputListener(listener);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
listeners.beforeAll.delete(listener);
|
|
214
|
-
listeners.beforeViewport.delete(listener);
|
|
215
|
-
listeners.regular.delete(listener);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const BRACKETED_PASTE_START = "\u001b[200~";
|
|
220
|
-
const BRACKETED_PASTE_END = "\u001b[201~";
|
|
221
|
-
|
|
222
|
-
// TuiAltScreen evaluates these actions before bottom, so shared keys cannot jump to latest.
|
|
223
|
-
const ALT_SCREEN_ACTIONS_BEFORE_BOTTOM = [
|
|
224
|
-
"tui.altScreen.search",
|
|
225
|
-
"tui.altScreen.searchNext",
|
|
226
|
-
"tui.altScreen.searchPrevious",
|
|
227
|
-
"tui.altScreen.searchClose",
|
|
228
|
-
"tui.altScreen.pageUp",
|
|
229
|
-
"tui.altScreen.pageDown",
|
|
230
|
-
"tui.altScreen.halfPageUp",
|
|
231
|
-
"tui.altScreen.halfPageDown",
|
|
232
|
-
"tui.altScreen.lineUp",
|
|
233
|
-
"tui.altScreen.lineDown",
|
|
234
|
-
"tui.altScreen.previousPrompt",
|
|
235
|
-
"tui.altScreen.nextPrompt",
|
|
236
|
-
"tui.altScreen.top",
|
|
237
|
-
] as const;
|
|
238
|
-
const KEY_MODIFIER_ORDER = ["shift", "ctrl", "alt", "super"] as const;
|
|
239
|
-
const MATCHABLE_SPECIAL_KEYS = new Set([
|
|
240
|
-
"space",
|
|
241
|
-
"tab",
|
|
242
|
-
"enter",
|
|
243
|
-
"backspace",
|
|
244
|
-
"delete",
|
|
245
|
-
"insert",
|
|
246
|
-
"home",
|
|
247
|
-
"end",
|
|
248
|
-
"pageup",
|
|
249
|
-
"pagedown",
|
|
250
|
-
"up",
|
|
251
|
-
"down",
|
|
252
|
-
"left",
|
|
253
|
-
"right",
|
|
254
|
-
]);
|
|
255
|
-
const MATCHABLE_SYMBOL_KEYS = new Set("`-=[]\\;',./!@#$%^&*()_+|~{}:<>?");
|
|
256
|
-
|
|
257
|
-
function normalizedKeyId(key: string): string {
|
|
258
|
-
const parts = key.toLowerCase().split("+");
|
|
259
|
-
const base = parts.at(-1);
|
|
260
|
-
if (!base) return "";
|
|
261
|
-
const normalizedBase = base === "esc" ? "escape" : base === "return" ? "enter" : base;
|
|
262
|
-
const modifiers = KEY_MODIFIER_ORDER.filter((modifier) => parts.includes(modifier));
|
|
263
|
-
return [...modifiers, normalizedBase].join("+");
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
function formatEffectiveKeyLabel(key: string): string {
|
|
267
|
-
const parts = key.split("+");
|
|
268
|
-
const base = parts.at(-1);
|
|
269
|
-
if (base === "pageup") parts[parts.length - 1] = "pageUp";
|
|
270
|
-
if (base === "pagedown") parts[parts.length - 1] = "pageDown";
|
|
271
|
-
return formatKeyLabel(parts.join("+"));
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function canMatchKeyInput(key: string): boolean {
|
|
275
|
-
const parts = key.split("+");
|
|
276
|
-
const base = parts.at(-1) ?? "";
|
|
277
|
-
const modifiers = parts.slice(0, -1);
|
|
278
|
-
if (base === "escape") return modifiers.length === 0;
|
|
279
|
-
if (base === "clear") {
|
|
280
|
-
return modifiers.length === 0 || (modifiers.length === 1 && (modifiers[0] === "shift" || modifiers[0] === "ctrl"));
|
|
281
|
-
}
|
|
282
|
-
if (/^f(?:[1-9]|1[0-2])$/u.test(base)) return modifiers.length === 0;
|
|
283
|
-
return (
|
|
284
|
-
MATCHABLE_SPECIAL_KEYS.has(base) ||
|
|
285
|
-
(base.length === 1 && (/^[a-z0-9]$/u.test(base) || MATCHABLE_SYMBOL_KEYS.has(base)))
|
|
286
|
-
);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function rawCtrlInput(base: string): string | undefined {
|
|
290
|
-
if (base.length !== 1) return undefined;
|
|
291
|
-
const rawBase = base === "-" ? "_" : base;
|
|
292
|
-
if (!"abcdefghijklmnopqrstuvwxyz[\\]_".includes(rawBase)) return undefined;
|
|
293
|
-
return String.fromCharCode(rawBase.charCodeAt(0) & 0x1f);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
function legacyRawInput(key: string): string | undefined {
|
|
297
|
-
const parts = key.split("+");
|
|
298
|
-
const base = parts.at(-1) ?? "";
|
|
299
|
-
if (parts.length === 2 && parts[0] === "ctrl") return rawCtrlInput(base);
|
|
300
|
-
if (isKittyProtocolActive()) return undefined;
|
|
301
|
-
if (parts.length === 2 && parts[0] === "alt" && base.length === 1) return `\u001b${base}`;
|
|
302
|
-
if (parts.length === 3 && parts[0] === "ctrl" && parts[1] === "alt") {
|
|
303
|
-
const input = rawCtrlInput(base);
|
|
304
|
-
return input ? `\u001b${input}` : undefined;
|
|
305
|
-
}
|
|
306
|
-
return undefined;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
// Mirror matchesKey(), using parseKey() to canonicalize IDs that share legacy raw input.
|
|
310
|
-
function keyInputIdentity(key: string): string {
|
|
311
|
-
const identity = normalizedKeyId(key);
|
|
312
|
-
const input = legacyRawInput(identity);
|
|
313
|
-
return input ? normalizedKeyId(parseKey(input) ?? identity) : identity;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function hasManualSelectionCopyApi(): boolean {
|
|
317
|
-
return (
|
|
318
|
-
typeof TuiAltScreen.prototype.hasActiveSelection === "function" &&
|
|
319
|
-
typeof TuiAltScreen.prototype.copyActiveSelectionToClipboard === "function"
|
|
320
|
-
);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
function createBtwFullscreenTui(
|
|
324
|
-
parent: TUI,
|
|
325
|
-
theme: Theme,
|
|
326
|
-
keybindings: KeybindingsManager,
|
|
327
|
-
copyOnSelect: boolean,
|
|
328
|
-
manualSelectionCopySupported: boolean,
|
|
329
|
-
openUrl: (url: string) => void,
|
|
330
|
-
copyToClipboard: (text: string) => Promise<void>,
|
|
331
|
-
): BtwFullscreenTui {
|
|
332
|
-
if (!copyOnSelect && !manualSelectionCopySupported) {
|
|
333
|
-
throw new Error(
|
|
334
|
-
"Manual fullscreen selection copying is unavailable in this Pi version; update Pi or enable automatic selection copying.",
|
|
335
|
-
);
|
|
336
|
-
}
|
|
337
|
-
const styleSearchMatch = (text: string) => theme.bg("searchMatchBg", theme.fg("searchMatchText", text));
|
|
338
|
-
const fullscreen = new BtwTuiAltScreen(parent.terminal, parent.getShowHardwareCursor(), undefined, {
|
|
339
|
-
mouse: true,
|
|
340
|
-
copyOnSelect,
|
|
341
|
-
searchMatchStyle: (text) => theme.underline(styleSearchMatch(text)),
|
|
342
|
-
scrollToEndIndicator: () => {
|
|
343
|
-
const unavailableKeyIdentities = new Set<string>([keyInputIdentity(Key.ctrl("c"))]);
|
|
344
|
-
for (const action of ALT_SCREEN_ACTIONS_BEFORE_BOTTOM) {
|
|
345
|
-
for (const actionKey of keybindings.getKeys(action)) {
|
|
346
|
-
unavailableKeyIdentities.add(keyInputIdentity(String(actionKey)));
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
if (!copyOnSelect) {
|
|
350
|
-
for (const copyKey of keybindings.getKeys("app.message.copy")) {
|
|
351
|
-
unavailableKeyIdentities.add(keyInputIdentity(String(copyKey)));
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
const key = keybindings
|
|
355
|
-
.getKeys("tui.altScreen.bottom")
|
|
356
|
-
.map((candidate) => keyInputIdentity(String(candidate)))
|
|
357
|
-
.find(
|
|
358
|
-
(identity) =>
|
|
359
|
-
identity &&
|
|
360
|
-
canMatchKeyInput(identity) &&
|
|
361
|
-
!unavailableKeyIdentities.has(identity) &&
|
|
362
|
-
formatEffectiveKeyLabel(identity),
|
|
363
|
-
);
|
|
364
|
-
const label = theme.fg("text", " ↓ Jump to latest message");
|
|
365
|
-
const shortcut = key ? theme.fg("muted", ` · ${formatEffectiveKeyLabel(key)}`) : "";
|
|
366
|
-
return theme.bg("selectedBg", `${label}${shortcut} `);
|
|
367
|
-
},
|
|
368
|
-
searchCurrentMatchStyle: (text) => theme.bold(theme.inverse(styleSearchMatch(text))),
|
|
369
|
-
openUrl,
|
|
370
|
-
copySelection: async (text) => {
|
|
371
|
-
try {
|
|
372
|
-
await copyToClipboard(text);
|
|
373
|
-
return true;
|
|
374
|
-
} catch {
|
|
375
|
-
return false;
|
|
376
|
-
}
|
|
377
|
-
},
|
|
378
|
-
});
|
|
379
|
-
if (!copyOnSelect) {
|
|
380
|
-
let isInBracketedPaste = false;
|
|
381
|
-
fullscreen.addInputListenerBeforeViewport((data) => {
|
|
382
|
-
const wasInBracketedPaste = isInBracketedPaste;
|
|
383
|
-
const startsBracketedPaste = data.includes(BRACKETED_PASTE_START);
|
|
384
|
-
if (startsBracketedPaste) isInBracketedPaste = true;
|
|
385
|
-
if (isInBracketedPaste && data.includes(BRACKETED_PASTE_END)) {
|
|
386
|
-
isInBracketedPaste = false;
|
|
387
|
-
}
|
|
388
|
-
if (
|
|
389
|
-
wasInBracketedPaste ||
|
|
390
|
-
startsBracketedPaste ||
|
|
391
|
-
fullscreen.hasFocusedOverlay() ||
|
|
392
|
-
isKeyRelease(data) ||
|
|
393
|
-
!keybindings.matches(data, "app.message.copy")
|
|
394
|
-
) {
|
|
395
|
-
return undefined;
|
|
396
|
-
}
|
|
397
|
-
if (!fullscreen.hasActiveSelection()) {
|
|
398
|
-
fullscreen.flash("No selection to copy");
|
|
399
|
-
return { consume: true };
|
|
400
|
-
}
|
|
401
|
-
void fullscreen.copyActiveSelectionToClipboard().catch(() => fullscreen.flash("Copy failed"));
|
|
402
|
-
return { consume: true };
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
return fullscreen;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
// Pi does not export its browser opener, so mirror its shell-free launcher for this isolated TUI.
|
|
409
|
-
function openUrlInBrowser(target: string): void {
|
|
410
|
-
const [command, args] =
|
|
411
|
-
process.platform === "darwin"
|
|
412
|
-
? ["open", [target]]
|
|
413
|
-
: process.platform === "win32"
|
|
414
|
-
? ["rundll32", ["url.dll,FileProtocolHandler", target]]
|
|
415
|
-
: ["xdg-open", [target]];
|
|
416
|
-
spawn(command, args, { stdio: "ignore", detached: true })
|
|
417
|
-
.on("error", () => {})
|
|
418
|
-
.unref();
|
|
419
|
-
}
|
|
420
|
-
|
|
421
143
|
class BtwFullscreenHost<T> implements Component {
|
|
422
144
|
private fullscreen: BtwFullscreenTui | undefined;
|
|
423
145
|
private parentOverlay: OverlayHandle | undefined;
|
|
@@ -481,16 +203,24 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
481
203
|
if (this.disposed || this.finished) return;
|
|
482
204
|
this.disposed = true;
|
|
483
205
|
this.lifetimeController.abort();
|
|
484
|
-
|
|
206
|
+
try {
|
|
207
|
+
this.cancelActiveCustom?.();
|
|
208
|
+
} finally {
|
|
209
|
+
this.queueParentRestore();
|
|
210
|
+
}
|
|
485
211
|
}
|
|
486
212
|
|
|
487
213
|
private async start(): Promise<void> {
|
|
488
214
|
if (this.started || this.finished) return;
|
|
489
215
|
this.started = true;
|
|
490
|
-
this.watchUpstreamCancellation();
|
|
491
216
|
let outcome: FullscreenOutcome<T>;
|
|
492
217
|
try {
|
|
218
|
+
this.watchUpstreamCancellation();
|
|
493
219
|
if (this.disposed) throw new FullscreenUiDisposedError();
|
|
220
|
+
if ((this.options.layout ?? "fullscreen") !== "fullscreen") {
|
|
221
|
+
getParentFullscreenLayout(this.parent);
|
|
222
|
+
getParentFullscreenScrollView(this.parent);
|
|
223
|
+
}
|
|
494
224
|
this.parent.stop({ preserveScreen: true });
|
|
495
225
|
this.parentStopped = true;
|
|
496
226
|
if (this.disposed) throw new FullscreenUiDisposedError();
|
|
@@ -504,6 +234,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
504
234
|
this.options.copyOnSelect ?? true,
|
|
505
235
|
);
|
|
506
236
|
setBtwShortcuts(this.fullscreen, shortcuts);
|
|
237
|
+
setBtwKeybindings(this.fullscreen, this.keybindings);
|
|
238
|
+
setBtwHostInputBlockers(
|
|
239
|
+
this.fullscreen,
|
|
240
|
+
this.options.copyOnSelect === false ? this.keybindings.getKeys("app.message.copy") : [],
|
|
241
|
+
);
|
|
507
242
|
// Negotiate before warning when possible: the first dispatched user input uses
|
|
508
243
|
// the current mode. Recheck each input, including later mode transitions.
|
|
509
244
|
let previousWarnings: readonly string[] = [];
|
|
@@ -541,7 +276,7 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
541
276
|
}
|
|
542
277
|
return { consume: true };
|
|
543
278
|
});
|
|
544
|
-
const value = await this.
|
|
279
|
+
const value = await this.runUntilCancelled();
|
|
545
280
|
await this.waitForSidePaneWrites();
|
|
546
281
|
outcome = { kind: "completed", value };
|
|
547
282
|
} catch (error) {
|
|
@@ -561,8 +296,35 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
561
296
|
this.done(outcome);
|
|
562
297
|
}
|
|
563
298
|
|
|
299
|
+
private async runUntilCancelled(): Promise<T> {
|
|
300
|
+
const signal = this.lifetimeController.signal;
|
|
301
|
+
let removeAbort: (() => void) | undefined;
|
|
302
|
+
let cancellation: ReturnType<typeof setImmediate> | undefined;
|
|
303
|
+
const cancelled = new Promise<never>((_resolve, reject) => {
|
|
304
|
+
// Let synchronous root cancellation and its promise continuations settle
|
|
305
|
+
// normally; abandon only a continuation that ignores the owned signal.
|
|
306
|
+
const onAbort = () => {
|
|
307
|
+
cancellation ??= setImmediate(() => reject(new FullscreenUiDisposedError()));
|
|
308
|
+
};
|
|
309
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
310
|
+
removeAbort = () => signal.removeEventListener("abort", onAbort);
|
|
311
|
+
if (signal.aborted) onAbort();
|
|
312
|
+
});
|
|
313
|
+
try {
|
|
314
|
+
// Observe late failures even when cancellation settles the owning UI first.
|
|
315
|
+
if (signal.aborted) throw new FullscreenUiDisposedError();
|
|
316
|
+
return await Promise.race([this.run(this.createContext()), cancelled]);
|
|
317
|
+
} finally {
|
|
318
|
+
removeAbort?.();
|
|
319
|
+
if (cancellation) clearImmediate(cancellation);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
564
323
|
private watchUpstreamCancellation(): void {
|
|
565
|
-
const
|
|
324
|
+
const signals = [this.ctx.signal, this.options.signal].filter(
|
|
325
|
+
(signal): signal is AbortSignal => signal !== undefined,
|
|
326
|
+
);
|
|
327
|
+
const signal = signals.length ? AbortSignal.any(signals) : undefined;
|
|
566
328
|
if (!signal) return;
|
|
567
329
|
const onAbort = () => this.dispose();
|
|
568
330
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
@@ -609,7 +371,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
609
371
|
} catch (error) {
|
|
610
372
|
this.cleanupError ??= error;
|
|
611
373
|
}
|
|
612
|
-
|
|
374
|
+
try {
|
|
375
|
+
this.mainThreadInput.dispose();
|
|
376
|
+
} catch (error) {
|
|
377
|
+
this.cleanupError ??= error;
|
|
378
|
+
}
|
|
613
379
|
const removeUpstreamAbortListener = this.removeUpstreamAbortListener;
|
|
614
380
|
this.removeUpstreamAbortListener = undefined;
|
|
615
381
|
try {
|
|
@@ -642,6 +408,7 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
642
408
|
}
|
|
643
409
|
try {
|
|
644
410
|
this.parentRestoreAttempted = true;
|
|
411
|
+
if (this.options.canRestoreParent?.() === false) return;
|
|
645
412
|
this.parent.start();
|
|
646
413
|
this.parent.renderNow(false);
|
|
647
414
|
} catch (error) {
|
|
@@ -658,13 +425,26 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
658
425
|
}
|
|
659
426
|
if (property === "notify") {
|
|
660
427
|
return (message: string, level?: Parameters<ExtensionCommandContext["ui"]["notify"]>[1]) => {
|
|
428
|
+
if (this.disposed || this.finished) return;
|
|
661
429
|
target.notify(message, level);
|
|
662
430
|
const display = sanitizeSingleLine(message);
|
|
663
431
|
if (display) this.fullscreen?.flash?.(display);
|
|
664
432
|
};
|
|
665
433
|
}
|
|
666
434
|
const value = Reflect.get(target, property, target) as unknown;
|
|
667
|
-
return typeof value === "function"
|
|
435
|
+
return typeof value === "function"
|
|
436
|
+
? (...args: unknown[]) => {
|
|
437
|
+
if (
|
|
438
|
+
this.disposed ||
|
|
439
|
+
this.finished ||
|
|
440
|
+
this.lifetimeController.signal.aborted ||
|
|
441
|
+
this.options.canRestoreParent?.() === false
|
|
442
|
+
) {
|
|
443
|
+
throw new FullscreenUiDisposedError();
|
|
444
|
+
}
|
|
445
|
+
return Reflect.apply(value, target, args);
|
|
446
|
+
}
|
|
447
|
+
: value;
|
|
668
448
|
},
|
|
669
449
|
});
|
|
670
450
|
const signal = this.ctx.signal
|
|
@@ -720,7 +500,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
720
500
|
} catch (error) {
|
|
721
501
|
cleanupError = error;
|
|
722
502
|
}
|
|
723
|
-
|
|
503
|
+
try {
|
|
504
|
+
splitPane?.dispose();
|
|
505
|
+
} catch (error) {
|
|
506
|
+
cleanupError ??= error;
|
|
507
|
+
}
|
|
724
508
|
splitPane = undefined;
|
|
725
509
|
try {
|
|
726
510
|
if (overlay) overlay.hide();
|
|
@@ -844,9 +628,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
844
628
|
sideLayout,
|
|
845
629
|
mainThread: this.parent,
|
|
846
630
|
mainLayout: getParentFullscreenLayout(this.parent),
|
|
631
|
+
mainScrollView: getParentFullscreenScrollView(this.parent),
|
|
847
632
|
mainInput: this.mainThreadInput,
|
|
848
633
|
sideScrollView: isFullscreenLayoutComponent(component) ? component.getPrimaryScrollView?.() : undefined,
|
|
849
634
|
layout: workspaceLayout,
|
|
635
|
+
shortcuts: getBtwShortcuts(fullscreen),
|
|
850
636
|
theme: this.theme,
|
|
851
637
|
terminalColumns: () => fullscreen.terminal.columns,
|
|
852
638
|
terminalRows: () => fullscreen.terminal.rows,
|
|
@@ -937,7 +723,21 @@ function getFocusedComponent(tui: TUI): Component | null {
|
|
|
937
723
|
function getParentFullscreenLayout(tui: TUI): Component | undefined {
|
|
938
724
|
if (tui.mode !== "fullscreen") return undefined;
|
|
939
725
|
const layoutRoot = Reflect.get(tui, "layoutRoot") as unknown;
|
|
940
|
-
|
|
726
|
+
if (!isComponent(layoutRoot)) {
|
|
727
|
+
throw new Error("Pi BTW cannot compose this fullscreen layout; use the fullscreen BTW layout or update Pi.");
|
|
728
|
+
}
|
|
729
|
+
return layoutRoot;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
// Use the viewport chosen by Pi's actual layout pass. A children walk cannot
|
|
733
|
+
// reproduce native visibility, zero-width allocation, or nested scroll ordering.
|
|
734
|
+
function getParentFullscreenScrollView(tui: TUI): ScrollView | undefined {
|
|
735
|
+
if (tui.mode !== "fullscreen") return undefined;
|
|
736
|
+
const layout = Reflect.get(tui, "currentLayout") as { primaryScrollView?: unknown } | undefined;
|
|
737
|
+
if (!layout || !(layout.primaryScrollView instanceof ScrollView)) {
|
|
738
|
+
throw new Error("Pi BTW cannot identify this fullscreen viewport; use the fullscreen BTW layout or update Pi.");
|
|
739
|
+
}
|
|
740
|
+
return layout.primaryScrollView;
|
|
941
741
|
}
|
|
942
742
|
|
|
943
743
|
function isComponent(value: unknown): value is Component {
|