@narumitw/pi-btw 0.61.2 → 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 -560
- 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 -315
- package/src/keybindings.ts +100 -22
- 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,287 +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 configuredModifiers = parts.slice(0, -1);
|
|
262
|
-
if (
|
|
263
|
-
new Set(configuredModifiers).size !== configuredModifiers.length ||
|
|
264
|
-
configuredModifiers.some((modifier) => !KEY_MODIFIER_ORDER.includes(modifier as never))
|
|
265
|
-
)
|
|
266
|
-
return "";
|
|
267
|
-
const normalizedBase = base === "esc" ? "escape" : base === "return" ? "enter" : base;
|
|
268
|
-
const modifiers = KEY_MODIFIER_ORDER.filter((modifier) => parts.includes(modifier));
|
|
269
|
-
return [...modifiers, normalizedBase].join("+");
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function formatEffectiveKeyLabel(key: string): string {
|
|
273
|
-
const parts = key.split("+");
|
|
274
|
-
const base = parts.at(-1);
|
|
275
|
-
if (base === "pageup") parts[parts.length - 1] = "pageUp";
|
|
276
|
-
if (base === "pagedown") parts[parts.length - 1] = "pageDown";
|
|
277
|
-
return formatKeyLabel(parts.join("+"));
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
function canMatchKeyInput(key: string): boolean {
|
|
281
|
-
const parts = key.split("+");
|
|
282
|
-
const base = parts.at(-1) ?? "";
|
|
283
|
-
const modifiers = parts.slice(0, -1);
|
|
284
|
-
if (base === "escape") return modifiers.length === 0;
|
|
285
|
-
if (base === "clear") {
|
|
286
|
-
return modifiers.length === 0 || (modifiers.length === 1 && (modifiers[0] === "shift" || modifiers[0] === "ctrl"));
|
|
287
|
-
}
|
|
288
|
-
if (/^f(?:[1-9]|1[0-2])$/u.test(base)) return modifiers.length === 0;
|
|
289
|
-
return (
|
|
290
|
-
MATCHABLE_SPECIAL_KEYS.has(base) ||
|
|
291
|
-
(base.length === 1 && (/^[a-z0-9]$/u.test(base) || MATCHABLE_SYMBOL_KEYS.has(base)))
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function rawCtrlInput(base: string): string | undefined {
|
|
296
|
-
if (base.length !== 1) return undefined;
|
|
297
|
-
const rawBase = base === "-" ? "_" : base;
|
|
298
|
-
if (!"abcdefghijklmnopqrstuvwxyz[\\]_".includes(rawBase)) return undefined;
|
|
299
|
-
return String.fromCharCode(rawBase.charCodeAt(0) & 0x1f);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
function legacyRawInput(key: string): string | undefined {
|
|
303
|
-
const parts = key.split("+");
|
|
304
|
-
const base = parts.at(-1) ?? "";
|
|
305
|
-
if (parts.length === 2 && parts[0] === "ctrl") return rawCtrlInput(base);
|
|
306
|
-
if (isKittyProtocolActive()) return undefined;
|
|
307
|
-
if (parts.length === 2 && parts[0] === "alt" && base.length === 1) return `\u001b${base}`;
|
|
308
|
-
if (parts.length === 3 && parts[0] === "ctrl" && parts[1] === "alt") {
|
|
309
|
-
const input = rawCtrlInput(base);
|
|
310
|
-
return input ? `\u001b${input}` : undefined;
|
|
311
|
-
}
|
|
312
|
-
return undefined;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// Mirror matchesKey(), using parseKey() to canonicalize IDs that share legacy raw input.
|
|
316
|
-
function keyInputIdentity(key: string): string {
|
|
317
|
-
const identity = normalizedKeyId(key);
|
|
318
|
-
const input = legacyRawInput(identity);
|
|
319
|
-
return input ? normalizedKeyId(parseKey(input) ?? identity) : identity;
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
function hasManualSelectionCopyApi(): boolean {
|
|
323
|
-
return (
|
|
324
|
-
typeof TuiAltScreen.prototype.hasActiveSelection === "function" &&
|
|
325
|
-
typeof TuiAltScreen.prototype.copyActiveSelectionToClipboard === "function"
|
|
326
|
-
);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
function createBtwFullscreenTui(
|
|
330
|
-
parent: TUI,
|
|
331
|
-
theme: Theme,
|
|
332
|
-
keybindings: KeybindingsManager,
|
|
333
|
-
copyOnSelect: boolean,
|
|
334
|
-
manualSelectionCopySupported: boolean,
|
|
335
|
-
openUrl: (url: string) => void,
|
|
336
|
-
copyToClipboard: (text: string) => Promise<void>,
|
|
337
|
-
): BtwFullscreenTui {
|
|
338
|
-
if (!copyOnSelect && !manualSelectionCopySupported) {
|
|
339
|
-
throw new Error(
|
|
340
|
-
"Manual fullscreen selection copying is unavailable in this Pi version; update Pi or enable automatic selection copying.",
|
|
341
|
-
);
|
|
342
|
-
}
|
|
343
|
-
const styleSearchMatch = (text: string) => theme.bg("searchMatchBg", theme.fg("searchMatchText", text));
|
|
344
|
-
const fullscreen = new BtwTuiAltScreen(parent.terminal, parent.getShowHardwareCursor(), undefined, {
|
|
345
|
-
mouse: true,
|
|
346
|
-
copyOnSelect,
|
|
347
|
-
searchMatchStyle: (text) => theme.underline(styleSearchMatch(text)),
|
|
348
|
-
scrollToEndIndicator: () => {
|
|
349
|
-
const unavailableKeyIdentities = new Set<string>([keyInputIdentity(Key.ctrl("c"))]);
|
|
350
|
-
for (const action of ALT_SCREEN_ACTIONS_BEFORE_BOTTOM) {
|
|
351
|
-
for (const actionKey of keybindings.getKeys(action)) {
|
|
352
|
-
unavailableKeyIdentities.add(keyInputIdentity(String(actionKey)));
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
if (!copyOnSelect) {
|
|
356
|
-
for (const copyKey of keybindings.getKeys("app.message.copy")) {
|
|
357
|
-
unavailableKeyIdentities.add(keyInputIdentity(String(copyKey)));
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
const key = keybindings
|
|
361
|
-
.getKeys("tui.altScreen.bottom")
|
|
362
|
-
.map((candidate) => keyInputIdentity(String(candidate)))
|
|
363
|
-
.find(
|
|
364
|
-
(identity) =>
|
|
365
|
-
identity &&
|
|
366
|
-
canMatchKeyInput(identity) &&
|
|
367
|
-
!unavailableKeyIdentities.has(identity) &&
|
|
368
|
-
formatEffectiveKeyLabel(identity),
|
|
369
|
-
);
|
|
370
|
-
const label = theme.fg("text", " ↓ Jump to latest message");
|
|
371
|
-
const shortcut = key ? theme.fg("muted", ` · ${formatEffectiveKeyLabel(key)}`) : "";
|
|
372
|
-
return theme.bg("selectedBg", `${label}${shortcut} `);
|
|
373
|
-
},
|
|
374
|
-
searchCurrentMatchStyle: (text) => theme.bold(theme.inverse(styleSearchMatch(text))),
|
|
375
|
-
openUrl,
|
|
376
|
-
copySelection: async (text) => {
|
|
377
|
-
try {
|
|
378
|
-
await copyToClipboard(text);
|
|
379
|
-
return true;
|
|
380
|
-
} catch {
|
|
381
|
-
return false;
|
|
382
|
-
}
|
|
383
|
-
},
|
|
384
|
-
});
|
|
385
|
-
if (!copyOnSelect) {
|
|
386
|
-
let isInBracketedPaste = false;
|
|
387
|
-
fullscreen.addInputListenerBeforeViewport((data) => {
|
|
388
|
-
const wasInBracketedPaste = isInBracketedPaste;
|
|
389
|
-
const startsBracketedPaste = data.includes(BRACKETED_PASTE_START);
|
|
390
|
-
if (startsBracketedPaste) isInBracketedPaste = true;
|
|
391
|
-
if (isInBracketedPaste && data.includes(BRACKETED_PASTE_END)) {
|
|
392
|
-
isInBracketedPaste = false;
|
|
393
|
-
}
|
|
394
|
-
if (
|
|
395
|
-
wasInBracketedPaste ||
|
|
396
|
-
startsBracketedPaste ||
|
|
397
|
-
fullscreen.hasFocusedOverlay() ||
|
|
398
|
-
isKeyRelease(data) ||
|
|
399
|
-
!keybindings.matches(data, "app.message.copy")
|
|
400
|
-
) {
|
|
401
|
-
return undefined;
|
|
402
|
-
}
|
|
403
|
-
if (!fullscreen.hasActiveSelection()) {
|
|
404
|
-
fullscreen.flash("No selection to copy");
|
|
405
|
-
return { consume: true };
|
|
406
|
-
}
|
|
407
|
-
void fullscreen.copyActiveSelectionToClipboard().catch(() => fullscreen.flash("Copy failed"));
|
|
408
|
-
return { consume: true };
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
return fullscreen;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
// Pi does not export its browser opener, so mirror its shell-free launcher for this isolated TUI.
|
|
415
|
-
function openUrlInBrowser(target: string): void {
|
|
416
|
-
const [command, args] =
|
|
417
|
-
process.platform === "darwin"
|
|
418
|
-
? ["open", [target]]
|
|
419
|
-
: process.platform === "win32"
|
|
420
|
-
? ["rundll32", ["url.dll,FileProtocolHandler", target]]
|
|
421
|
-
: ["xdg-open", [target]];
|
|
422
|
-
spawn(command, args, { stdio: "ignore", detached: true })
|
|
423
|
-
.on("error", () => {})
|
|
424
|
-
.unref();
|
|
425
|
-
}
|
|
426
|
-
|
|
427
143
|
class BtwFullscreenHost<T> implements Component {
|
|
428
144
|
private fullscreen: BtwFullscreenTui | undefined;
|
|
429
145
|
private parentOverlay: OverlayHandle | undefined;
|
|
@@ -487,16 +203,24 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
487
203
|
if (this.disposed || this.finished) return;
|
|
488
204
|
this.disposed = true;
|
|
489
205
|
this.lifetimeController.abort();
|
|
490
|
-
|
|
206
|
+
try {
|
|
207
|
+
this.cancelActiveCustom?.();
|
|
208
|
+
} finally {
|
|
209
|
+
this.queueParentRestore();
|
|
210
|
+
}
|
|
491
211
|
}
|
|
492
212
|
|
|
493
213
|
private async start(): Promise<void> {
|
|
494
214
|
if (this.started || this.finished) return;
|
|
495
215
|
this.started = true;
|
|
496
|
-
this.watchUpstreamCancellation();
|
|
497
216
|
let outcome: FullscreenOutcome<T>;
|
|
498
217
|
try {
|
|
218
|
+
this.watchUpstreamCancellation();
|
|
499
219
|
if (this.disposed) throw new FullscreenUiDisposedError();
|
|
220
|
+
if ((this.options.layout ?? "fullscreen") !== "fullscreen") {
|
|
221
|
+
getParentFullscreenLayout(this.parent);
|
|
222
|
+
getParentFullscreenScrollView(this.parent);
|
|
223
|
+
}
|
|
500
224
|
this.parent.stop({ preserveScreen: true });
|
|
501
225
|
this.parentStopped = true;
|
|
502
226
|
if (this.disposed) throw new FullscreenUiDisposedError();
|
|
@@ -510,6 +234,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
510
234
|
this.options.copyOnSelect ?? true,
|
|
511
235
|
);
|
|
512
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
|
+
);
|
|
513
242
|
// Negotiate before warning when possible: the first dispatched user input uses
|
|
514
243
|
// the current mode. Recheck each input, including later mode transitions.
|
|
515
244
|
let previousWarnings: readonly string[] = [];
|
|
@@ -547,7 +276,7 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
547
276
|
}
|
|
548
277
|
return { consume: true };
|
|
549
278
|
});
|
|
550
|
-
const value = await this.
|
|
279
|
+
const value = await this.runUntilCancelled();
|
|
551
280
|
await this.waitForSidePaneWrites();
|
|
552
281
|
outcome = { kind: "completed", value };
|
|
553
282
|
} catch (error) {
|
|
@@ -567,8 +296,35 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
567
296
|
this.done(outcome);
|
|
568
297
|
}
|
|
569
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
|
+
|
|
570
323
|
private watchUpstreamCancellation(): void {
|
|
571
|
-
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;
|
|
572
328
|
if (!signal) return;
|
|
573
329
|
const onAbort = () => this.dispose();
|
|
574
330
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
@@ -615,7 +371,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
615
371
|
} catch (error) {
|
|
616
372
|
this.cleanupError ??= error;
|
|
617
373
|
}
|
|
618
|
-
|
|
374
|
+
try {
|
|
375
|
+
this.mainThreadInput.dispose();
|
|
376
|
+
} catch (error) {
|
|
377
|
+
this.cleanupError ??= error;
|
|
378
|
+
}
|
|
619
379
|
const removeUpstreamAbortListener = this.removeUpstreamAbortListener;
|
|
620
380
|
this.removeUpstreamAbortListener = undefined;
|
|
621
381
|
try {
|
|
@@ -648,6 +408,7 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
648
408
|
}
|
|
649
409
|
try {
|
|
650
410
|
this.parentRestoreAttempted = true;
|
|
411
|
+
if (this.options.canRestoreParent?.() === false) return;
|
|
651
412
|
this.parent.start();
|
|
652
413
|
this.parent.renderNow(false);
|
|
653
414
|
} catch (error) {
|
|
@@ -664,13 +425,26 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
664
425
|
}
|
|
665
426
|
if (property === "notify") {
|
|
666
427
|
return (message: string, level?: Parameters<ExtensionCommandContext["ui"]["notify"]>[1]) => {
|
|
428
|
+
if (this.disposed || this.finished) return;
|
|
667
429
|
target.notify(message, level);
|
|
668
430
|
const display = sanitizeSingleLine(message);
|
|
669
431
|
if (display) this.fullscreen?.flash?.(display);
|
|
670
432
|
};
|
|
671
433
|
}
|
|
672
434
|
const value = Reflect.get(target, property, target) as unknown;
|
|
673
|
-
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;
|
|
674
448
|
},
|
|
675
449
|
});
|
|
676
450
|
const signal = this.ctx.signal
|
|
@@ -726,7 +500,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
726
500
|
} catch (error) {
|
|
727
501
|
cleanupError = error;
|
|
728
502
|
}
|
|
729
|
-
|
|
503
|
+
try {
|
|
504
|
+
splitPane?.dispose();
|
|
505
|
+
} catch (error) {
|
|
506
|
+
cleanupError ??= error;
|
|
507
|
+
}
|
|
730
508
|
splitPane = undefined;
|
|
731
509
|
try {
|
|
732
510
|
if (overlay) overlay.hide();
|
|
@@ -850,9 +628,11 @@ class BtwFullscreenHost<T> implements Component {
|
|
|
850
628
|
sideLayout,
|
|
851
629
|
mainThread: this.parent,
|
|
852
630
|
mainLayout: getParentFullscreenLayout(this.parent),
|
|
631
|
+
mainScrollView: getParentFullscreenScrollView(this.parent),
|
|
853
632
|
mainInput: this.mainThreadInput,
|
|
854
633
|
sideScrollView: isFullscreenLayoutComponent(component) ? component.getPrimaryScrollView?.() : undefined,
|
|
855
634
|
layout: workspaceLayout,
|
|
635
|
+
shortcuts: getBtwShortcuts(fullscreen),
|
|
856
636
|
theme: this.theme,
|
|
857
637
|
terminalColumns: () => fullscreen.terminal.columns,
|
|
858
638
|
terminalRows: () => fullscreen.terminal.rows,
|
|
@@ -943,7 +723,21 @@ function getFocusedComponent(tui: TUI): Component | null {
|
|
|
943
723
|
function getParentFullscreenLayout(tui: TUI): Component | undefined {
|
|
944
724
|
if (tui.mode !== "fullscreen") return undefined;
|
|
945
725
|
const layoutRoot = Reflect.get(tui, "layoutRoot") as unknown;
|
|
946
|
-
|
|
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;
|
|
947
741
|
}
|
|
948
742
|
|
|
949
743
|
function isComponent(value: unknown): value is Component {
|