@narumitw/pi-btw 0.63.4 → 0.63.6
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/CHANGELOG.md +14 -0
- package/README.md +1 -0
- package/dist/chunks/bring-to-main-VMICX6GU.ts +450 -0
- package/dist/chunks/bring-to-main-VMICX6GU.ts.map +7 -0
- package/dist/chunks/chunk-6GO5M6UJ.ts +142 -0
- package/dist/chunks/chunk-6GO5M6UJ.ts.map +7 -0
- package/dist/chunks/chunk-E4LBFYRH.ts +452 -0
- package/dist/chunks/chunk-E4LBFYRH.ts.map +7 -0
- package/dist/chunks/chunk-FWW662MW.ts +329 -0
- package/dist/chunks/chunk-FWW662MW.ts.map +7 -0
- package/dist/chunks/chunk-TPDBHPLD.ts +614 -0
- package/dist/chunks/chunk-TPDBHPLD.ts.map +7 -0
- package/dist/chunks/fullscreen-ui-4MJ3HOFJ.ts +1400 -0
- package/dist/chunks/fullscreen-ui-4MJ3HOFJ.ts.map +7 -0
- package/dist/chunks/import-main-updates-XFSIED52.ts +79 -0
- package/dist/chunks/import-main-updates-XFSIED52.ts.map +7 -0
- package/dist/chunks/main-tree-picker-YEHPIYZP.ts +317 -0
- package/dist/chunks/main-tree-picker-YEHPIYZP.ts.map +7 -0
- package/dist/chunks/menu-SYMLQYWC.ts +15 -0
- package/dist/chunks/menu-SYMLQYWC.ts.map +7 -0
- package/dist/chunks/transcript-pager-D7XAJC65.ts +824 -0
- package/dist/chunks/transcript-pager-D7XAJC65.ts.map +7 -0
- package/dist/index.ts +111 -4497
- package/dist/index.ts.map +4 -4
- package/package.json +2 -2
- package/src/btw.ts +55 -30
- package/src/main-tree-picker.ts +32 -40
|
@@ -0,0 +1,1400 @@
|
|
|
1
|
+
// @generated by scripts/build-runtime.mjs; do not edit.
|
|
2
|
+
// @ts-nocheck -- generated JavaScript uses a .ts extension for Pi's Jiti loader.
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_BTW_SIDE_PANE_RATIO,
|
|
5
|
+
MAX_BTW_SIDE_PANE_RATIO,
|
|
6
|
+
MIN_BTW_SIDE_PANE_RATIO
|
|
7
|
+
} from "./chunk-E4LBFYRH.ts";
|
|
8
|
+
import {
|
|
9
|
+
BtwPasteGuard,
|
|
10
|
+
formatKeyLabel,
|
|
11
|
+
getBtwShortcuts,
|
|
12
|
+
resolveBtwShortcuts,
|
|
13
|
+
sanitizeSingleLine,
|
|
14
|
+
setBtwHostInputBlockers,
|
|
15
|
+
setBtwKeybindings,
|
|
16
|
+
setBtwShortcuts
|
|
17
|
+
} from "./chunk-FWW662MW.ts";
|
|
18
|
+
|
|
19
|
+
// src/fullscreen-ui.ts
|
|
20
|
+
import {
|
|
21
|
+
copyToClipboard as copyToHostClipboard
|
|
22
|
+
} from "@earendil-works/pi-coding-agent";
|
|
23
|
+
import { ScrollView as ScrollView2, truncateToWidth as truncateToWidth2 } from "@earendil-works/pi-tui";
|
|
24
|
+
|
|
25
|
+
// src/fullscreen-renderer.ts
|
|
26
|
+
import { spawn } from "node:child_process";
|
|
27
|
+
import {
|
|
28
|
+
isKeyRelease,
|
|
29
|
+
isKittyProtocolActive,
|
|
30
|
+
Key,
|
|
31
|
+
parseKey,
|
|
32
|
+
TuiAltScreen
|
|
33
|
+
} from "@earendil-works/pi-tui";
|
|
34
|
+
var btwInputListeners = /* @__PURE__ */ new WeakMap();
|
|
35
|
+
function dispatchBtwInput(listeners, data) {
|
|
36
|
+
let current = data;
|
|
37
|
+
for (const group of [listeners.beforeAll, listeners.beforeViewport, listeners.regular]) {
|
|
38
|
+
for (const listener of group) {
|
|
39
|
+
const result = listener(current);
|
|
40
|
+
if (result?.consume) return result;
|
|
41
|
+
if (result?.data !== void 0) current = result.data;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return current === data ? void 0 : { data: current };
|
|
45
|
+
}
|
|
46
|
+
var BtwTuiAltScreen = class extends TuiAltScreen {
|
|
47
|
+
terminalActive = false;
|
|
48
|
+
beforeTerminalStart() {
|
|
49
|
+
super.beforeTerminalStart();
|
|
50
|
+
this.terminalActive = true;
|
|
51
|
+
}
|
|
52
|
+
beforeTerminalStop(options) {
|
|
53
|
+
this.terminalActive = false;
|
|
54
|
+
super.beforeTerminalStop(options);
|
|
55
|
+
}
|
|
56
|
+
flash(message, durationMs) {
|
|
57
|
+
if (this.terminalActive) super.flash(message, durationMs);
|
|
58
|
+
}
|
|
59
|
+
hasFocusedOverlay() {
|
|
60
|
+
return this.isOverlayFocused();
|
|
61
|
+
}
|
|
62
|
+
setViewportTarget(scrollView) {
|
|
63
|
+
const layout = Reflect.get(this, "currentLayout");
|
|
64
|
+
if (layout === void 0) return;
|
|
65
|
+
if (typeof layout !== "object" || layout === null || !Reflect.set(layout, "primaryScrollView", scrollView)) {
|
|
66
|
+
throw new Error("Pi BTW cannot route this fullscreen layout; update Pi.");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
addInputListener(listener) {
|
|
70
|
+
let listeners = btwInputListeners.get(this);
|
|
71
|
+
if (!listeners) {
|
|
72
|
+
const registeredListeners = {
|
|
73
|
+
beforeAll: /* @__PURE__ */ new Set(),
|
|
74
|
+
beforeViewport: /* @__PURE__ */ new Set(),
|
|
75
|
+
regular: /* @__PURE__ */ new Set()
|
|
76
|
+
};
|
|
77
|
+
btwInputListeners.set(this, registeredListeners);
|
|
78
|
+
super.addInputListener((data) => dispatchBtwInput(registeredListeners, data));
|
|
79
|
+
listeners = registeredListeners;
|
|
80
|
+
}
|
|
81
|
+
listeners.regular.add(listener);
|
|
82
|
+
return () => listeners.regular.delete(listener);
|
|
83
|
+
}
|
|
84
|
+
addInputListenerBeforeAll(listener) {
|
|
85
|
+
const listeners = btwInputListeners.get(this);
|
|
86
|
+
if (!listeners) return super.addInputListener(listener);
|
|
87
|
+
listeners.beforeAll.add(listener);
|
|
88
|
+
return () => listeners.beforeAll.delete(listener);
|
|
89
|
+
}
|
|
90
|
+
addInputListenerBeforeViewport(listener) {
|
|
91
|
+
const listeners = btwInputListeners.get(this);
|
|
92
|
+
if (!listeners) return super.addInputListener(listener);
|
|
93
|
+
listeners.beforeViewport.add(listener);
|
|
94
|
+
return () => listeners.beforeViewport.delete(listener);
|
|
95
|
+
}
|
|
96
|
+
removeInputListener(listener) {
|
|
97
|
+
const listeners = btwInputListeners.get(this);
|
|
98
|
+
if (!listeners) {
|
|
99
|
+
super.removeInputListener(listener);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
listeners.beforeAll.delete(listener);
|
|
103
|
+
listeners.beforeViewport.delete(listener);
|
|
104
|
+
listeners.regular.delete(listener);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
var BRACKETED_PASTE_START = "\x1B[200~";
|
|
108
|
+
var BRACKETED_PASTE_END = "\x1B[201~";
|
|
109
|
+
var ALT_SCREEN_ACTIONS_BEFORE_BOTTOM = [
|
|
110
|
+
"tui.altScreen.search",
|
|
111
|
+
"tui.altScreen.searchNext",
|
|
112
|
+
"tui.altScreen.searchPrevious",
|
|
113
|
+
"tui.altScreen.searchClose",
|
|
114
|
+
"tui.altScreen.pageUp",
|
|
115
|
+
"tui.altScreen.pageDown",
|
|
116
|
+
"tui.altScreen.halfPageUp",
|
|
117
|
+
"tui.altScreen.halfPageDown",
|
|
118
|
+
"tui.altScreen.lineUp",
|
|
119
|
+
"tui.altScreen.lineDown",
|
|
120
|
+
"tui.altScreen.previousPrompt",
|
|
121
|
+
"tui.altScreen.nextPrompt",
|
|
122
|
+
"tui.altScreen.top"
|
|
123
|
+
];
|
|
124
|
+
var KEY_MODIFIER_ORDER = ["shift", "ctrl", "alt", "super"];
|
|
125
|
+
var MATCHABLE_SPECIAL_KEYS = /* @__PURE__ */ new Set([
|
|
126
|
+
"space",
|
|
127
|
+
"tab",
|
|
128
|
+
"enter",
|
|
129
|
+
"backspace",
|
|
130
|
+
"delete",
|
|
131
|
+
"insert",
|
|
132
|
+
"home",
|
|
133
|
+
"end",
|
|
134
|
+
"pageup",
|
|
135
|
+
"pagedown",
|
|
136
|
+
"up",
|
|
137
|
+
"down",
|
|
138
|
+
"left",
|
|
139
|
+
"right"
|
|
140
|
+
]);
|
|
141
|
+
var MATCHABLE_SYMBOL_KEYS = new Set("`-=[]\\;',./!@#$%^&*()_+|~{}:<>?");
|
|
142
|
+
function normalizedKeyId(key) {
|
|
143
|
+
const parts = key.toLowerCase().split("+");
|
|
144
|
+
const base = parts.at(-1);
|
|
145
|
+
if (!base) return "";
|
|
146
|
+
const configuredModifiers = parts.slice(0, -1);
|
|
147
|
+
if (new Set(configuredModifiers).size !== configuredModifiers.length || configuredModifiers.some((modifier) => !KEY_MODIFIER_ORDER.includes(modifier)))
|
|
148
|
+
return "";
|
|
149
|
+
const normalizedBase = base === "esc" ? "escape" : base === "return" ? "enter" : base;
|
|
150
|
+
const modifiers = KEY_MODIFIER_ORDER.filter((modifier) => parts.includes(modifier));
|
|
151
|
+
return [...modifiers, normalizedBase].join("+");
|
|
152
|
+
}
|
|
153
|
+
function formatEffectiveKeyLabel(key) {
|
|
154
|
+
const parts = key.split("+");
|
|
155
|
+
const base = parts.at(-1);
|
|
156
|
+
if (base === "pageup") parts[parts.length - 1] = "pageUp";
|
|
157
|
+
if (base === "pagedown") parts[parts.length - 1] = "pageDown";
|
|
158
|
+
return formatKeyLabel(parts.join("+"));
|
|
159
|
+
}
|
|
160
|
+
function canMatchKeyInput(key) {
|
|
161
|
+
const parts = key.split("+");
|
|
162
|
+
const base = parts.at(-1) ?? "";
|
|
163
|
+
const modifiers = parts.slice(0, -1);
|
|
164
|
+
if (base === "escape") return modifiers.length === 0;
|
|
165
|
+
if (base === "clear") {
|
|
166
|
+
return modifiers.length === 0 || modifiers.length === 1 && (modifiers[0] === "shift" || modifiers[0] === "ctrl");
|
|
167
|
+
}
|
|
168
|
+
if (/^f(?:[1-9]|1[0-2])$/u.test(base)) return modifiers.length === 0;
|
|
169
|
+
return MATCHABLE_SPECIAL_KEYS.has(base) || base.length === 1 && (/^[a-z0-9]$/u.test(base) || MATCHABLE_SYMBOL_KEYS.has(base));
|
|
170
|
+
}
|
|
171
|
+
function rawCtrlInput(base) {
|
|
172
|
+
if (base.length !== 1) return void 0;
|
|
173
|
+
const rawBase = base === "-" ? "_" : base;
|
|
174
|
+
if (!"abcdefghijklmnopqrstuvwxyz[\\]_".includes(rawBase)) return void 0;
|
|
175
|
+
return String.fromCharCode(rawBase.charCodeAt(0) & 31);
|
|
176
|
+
}
|
|
177
|
+
function legacyRawInput(key) {
|
|
178
|
+
const parts = key.split("+");
|
|
179
|
+
const base = parts.at(-1) ?? "";
|
|
180
|
+
if (parts.length === 2 && parts[0] === "ctrl") return rawCtrlInput(base);
|
|
181
|
+
if (isKittyProtocolActive()) return void 0;
|
|
182
|
+
if (parts.length === 2 && parts[0] === "alt" && base.length === 1) return `\x1B${base}`;
|
|
183
|
+
if (parts.length === 3 && parts[0] === "ctrl" && parts[1] === "alt") {
|
|
184
|
+
const input = rawCtrlInput(base);
|
|
185
|
+
return input ? `\x1B${input}` : void 0;
|
|
186
|
+
}
|
|
187
|
+
return void 0;
|
|
188
|
+
}
|
|
189
|
+
function keyInputIdentity(key) {
|
|
190
|
+
const identity = normalizedKeyId(key);
|
|
191
|
+
const input = legacyRawInput(identity);
|
|
192
|
+
return input ? normalizedKeyId(parseKey(input) ?? identity) : identity;
|
|
193
|
+
}
|
|
194
|
+
function hasManualSelectionCopyApi() {
|
|
195
|
+
return typeof TuiAltScreen.prototype.hasActiveSelection === "function" && typeof TuiAltScreen.prototype.copyActiveSelectionToClipboard === "function";
|
|
196
|
+
}
|
|
197
|
+
function createBtwFullscreenTui(parent, theme, keybindings, copyOnSelect, manualSelectionCopySupported, openUrl, copyToClipboard) {
|
|
198
|
+
if (!copyOnSelect && !manualSelectionCopySupported) {
|
|
199
|
+
throw new Error(
|
|
200
|
+
"Manual fullscreen selection copying is unavailable in this Pi version; update Pi or enable automatic selection copying."
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
const styleSearchMatch = (text) => theme.bg("searchMatchBg", theme.fg("searchMatchText", text));
|
|
204
|
+
const fullscreen = new BtwTuiAltScreen(parent.terminal, parent.getShowHardwareCursor(), void 0, {
|
|
205
|
+
mouse: true,
|
|
206
|
+
wheelScrollLines: "auto",
|
|
207
|
+
copyOnSelect,
|
|
208
|
+
searchMatchStyle: (text) => theme.underline(styleSearchMatch(text)),
|
|
209
|
+
scrollToEndIndicator: () => {
|
|
210
|
+
const unavailableKeyIdentities = /* @__PURE__ */ new Set([keyInputIdentity(Key.ctrl("c"))]);
|
|
211
|
+
for (const action of ALT_SCREEN_ACTIONS_BEFORE_BOTTOM) {
|
|
212
|
+
for (const actionKey of keybindings.getKeys(action)) {
|
|
213
|
+
unavailableKeyIdentities.add(keyInputIdentity(String(actionKey)));
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (!copyOnSelect) {
|
|
217
|
+
for (const copyKey of keybindings.getKeys("app.message.copy")) {
|
|
218
|
+
unavailableKeyIdentities.add(keyInputIdentity(String(copyKey)));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const key = keybindings.getKeys("tui.altScreen.bottom").map((candidate) => keyInputIdentity(String(candidate))).find(
|
|
222
|
+
(identity) => identity && canMatchKeyInput(identity) && !unavailableKeyIdentities.has(identity) && formatEffectiveKeyLabel(identity)
|
|
223
|
+
);
|
|
224
|
+
const label = theme.fg("text", " \u2193 Jump to latest message");
|
|
225
|
+
const shortcut = key ? theme.fg("muted", ` \xB7 ${formatEffectiveKeyLabel(key)}`) : "";
|
|
226
|
+
return theme.bg("selectedBg", `${label}${shortcut} `);
|
|
227
|
+
},
|
|
228
|
+
searchCurrentMatchStyle: (text) => theme.bold(theme.inverse(styleSearchMatch(text))),
|
|
229
|
+
searchNavigationButtonStyle: (text, hovered) => hovered ? theme.underline(text) : text,
|
|
230
|
+
openUrl,
|
|
231
|
+
copySelection: async (text) => {
|
|
232
|
+
try {
|
|
233
|
+
await copyToClipboard(text);
|
|
234
|
+
return true;
|
|
235
|
+
} catch (error) {
|
|
236
|
+
return sanitizeSingleLine(error instanceof Error ? error.message : String(error)) || "Copy failed";
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
if (!copyOnSelect) {
|
|
241
|
+
let isInBracketedPaste = false;
|
|
242
|
+
fullscreen.addInputListenerBeforeViewport((data) => {
|
|
243
|
+
const wasInBracketedPaste = isInBracketedPaste;
|
|
244
|
+
const startsBracketedPaste = data.includes(BRACKETED_PASTE_START);
|
|
245
|
+
if (startsBracketedPaste) isInBracketedPaste = true;
|
|
246
|
+
if (isInBracketedPaste && data.includes(BRACKETED_PASTE_END)) {
|
|
247
|
+
isInBracketedPaste = false;
|
|
248
|
+
}
|
|
249
|
+
if (wasInBracketedPaste || startsBracketedPaste || fullscreen.hasFocusedOverlay() || isKeyRelease(data) || !keybindings.matches(data, "app.message.copy")) {
|
|
250
|
+
return void 0;
|
|
251
|
+
}
|
|
252
|
+
if (!fullscreen.hasActiveSelection()) {
|
|
253
|
+
fullscreen.flash("No selection to copy");
|
|
254
|
+
return { consume: true };
|
|
255
|
+
}
|
|
256
|
+
void fullscreen.copyActiveSelectionToClipboard().catch(() => fullscreen.flash("Copy failed"));
|
|
257
|
+
return { consume: true };
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
return fullscreen;
|
|
261
|
+
}
|
|
262
|
+
function openUrlInBrowser(target) {
|
|
263
|
+
const [command, args] = process.platform === "darwin" ? ["open", [target]] : process.platform === "win32" ? ["rundll32", ["url.dll,FileProtocolHandler", target]] : ["xdg-open", [target]];
|
|
264
|
+
spawn(command, args, { stdio: "ignore", detached: true }).on("error", () => {
|
|
265
|
+
}).unref();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/workspace-layout.ts
|
|
269
|
+
import {
|
|
270
|
+
HStack,
|
|
271
|
+
isFocusable,
|
|
272
|
+
isKeyRelease as isKeyRelease2,
|
|
273
|
+
ScrollView,
|
|
274
|
+
truncateToWidth,
|
|
275
|
+
visibleWidth
|
|
276
|
+
} from "@earendil-works/pi-tui";
|
|
277
|
+
var MIN_BTW_SPLIT_COLUMNS = 80;
|
|
278
|
+
var PANE_DIVIDER_COLUMNS = 1;
|
|
279
|
+
var SGR_MOUSE_PRESS_PATTERN = new RegExp("^\\u001b\\[<(\\d+);(\\d+);\\d+M$");
|
|
280
|
+
var BtwMainThreadInput = class {
|
|
281
|
+
constructor(initialTarget, resolveTarget, requestRender) {
|
|
282
|
+
this.resolveTarget = resolveTarget;
|
|
283
|
+
this.requestRender = requestRender;
|
|
284
|
+
this.target = hasInput(initialTarget) ? initialTarget : void 0;
|
|
285
|
+
}
|
|
286
|
+
resolveTarget;
|
|
287
|
+
requestRender;
|
|
288
|
+
target;
|
|
289
|
+
_focused = false;
|
|
290
|
+
disposed = false;
|
|
291
|
+
get focused() {
|
|
292
|
+
return this._focused;
|
|
293
|
+
}
|
|
294
|
+
set focused(value) {
|
|
295
|
+
this._focused = value;
|
|
296
|
+
if (this.target && isFocusable(this.target)) this.target.focused = value;
|
|
297
|
+
}
|
|
298
|
+
get wantsKeyRelease() {
|
|
299
|
+
return this.target?.wantsKeyRelease ?? false;
|
|
300
|
+
}
|
|
301
|
+
render() {
|
|
302
|
+
return [];
|
|
303
|
+
}
|
|
304
|
+
handleInput(data) {
|
|
305
|
+
if (this.disposed) return;
|
|
306
|
+
this.refreshTarget();
|
|
307
|
+
this.target?.handleInput?.(data);
|
|
308
|
+
this.refreshTarget();
|
|
309
|
+
this.requestRender();
|
|
310
|
+
}
|
|
311
|
+
refreshTarget() {
|
|
312
|
+
if (this.disposed) return;
|
|
313
|
+
const resolved = this.resolveTarget();
|
|
314
|
+
const next = hasInput(resolved) ? resolved : void 0;
|
|
315
|
+
if (next === this.target) return;
|
|
316
|
+
if (this._focused && this.target && isFocusable(this.target)) this.target.focused = false;
|
|
317
|
+
this.target = next;
|
|
318
|
+
if (this._focused && next && isFocusable(next)) next.focused = true;
|
|
319
|
+
}
|
|
320
|
+
invalidate() {
|
|
321
|
+
}
|
|
322
|
+
dispose() {
|
|
323
|
+
if (this.disposed) return;
|
|
324
|
+
this.disposed = true;
|
|
325
|
+
const target = this.target;
|
|
326
|
+
const wasFocused = this._focused;
|
|
327
|
+
this.target = void 0;
|
|
328
|
+
this._focused = false;
|
|
329
|
+
if (wasFocused && target && isFocusable(target)) target.focused = false;
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
var BtwSplitPane = class {
|
|
333
|
+
constructor(options) {
|
|
334
|
+
this.options = options;
|
|
335
|
+
this.sidePaneRatio = clampSidePaneRatio(options.sidePaneRatio ?? DEFAULT_BTW_SIDE_PANE_RATIO);
|
|
336
|
+
this.persistedSidePaneRatio = this.sidePaneRatio;
|
|
337
|
+
this.mainPane = new MainThreadPane(
|
|
338
|
+
options.mainThread,
|
|
339
|
+
options.mainLayout,
|
|
340
|
+
options.mainScrollView,
|
|
341
|
+
options.theme,
|
|
342
|
+
options.terminalRows
|
|
343
|
+
);
|
|
344
|
+
const side = options.sideScrollView ? options.sideLayout : new ScrollView(options.sideLayout, { primary: true });
|
|
345
|
+
this.viewportRouter = new PaneViewportRouter(
|
|
346
|
+
options.sideScrollView ?? side,
|
|
347
|
+
this.mainPane.getPrimaryScrollView(),
|
|
348
|
+
options.setViewportTarget
|
|
349
|
+
);
|
|
350
|
+
this.viewportRouter.activate("side");
|
|
351
|
+
this.updateFocusLabel();
|
|
352
|
+
const separator = {
|
|
353
|
+
render: (width) => Array.from({ length: Math.max(1, options.terminalRows()) }, () => truncateToWidth(this.renderDivider(), width)),
|
|
354
|
+
handleMouse: (event) => this.handleDividerMouse(event),
|
|
355
|
+
invalidate() {
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
const main = this.mainPane.getLayout();
|
|
359
|
+
this.layoutRoot = options.layout === "left-pane" ? new ResponsivePaneRow(
|
|
360
|
+
side,
|
|
361
|
+
separator,
|
|
362
|
+
main,
|
|
363
|
+
0,
|
|
364
|
+
() => this.sidePaneRatio,
|
|
365
|
+
(width) => this.handleViewportWidth(width)
|
|
366
|
+
) : new ResponsivePaneRow(
|
|
367
|
+
main,
|
|
368
|
+
separator,
|
|
369
|
+
side,
|
|
370
|
+
2,
|
|
371
|
+
() => this.sidePaneRatio,
|
|
372
|
+
(width) => this.handleViewportWidth(width)
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
options;
|
|
376
|
+
pasteGuard = new BtwPasteGuard();
|
|
377
|
+
mainPane;
|
|
378
|
+
layoutRoot;
|
|
379
|
+
viewportRouter;
|
|
380
|
+
activePane = "side";
|
|
381
|
+
sidePaneRatio;
|
|
382
|
+
persistedSidePaneRatio;
|
|
383
|
+
dividerDragStartRatio;
|
|
384
|
+
focusGeneration = 0;
|
|
385
|
+
ratioSaveGeneration = 0;
|
|
386
|
+
confirmedRatioSaveGeneration = 0;
|
|
387
|
+
failedRatioSaveGenerationDuringDrag;
|
|
388
|
+
disposed = false;
|
|
389
|
+
getFullscreenLayout() {
|
|
390
|
+
return this.layoutRoot;
|
|
391
|
+
}
|
|
392
|
+
handleTerminalInput(data) {
|
|
393
|
+
if (this.disposed) return false;
|
|
394
|
+
const pasted = this.pasteGuard.consume(data);
|
|
395
|
+
if (this.options.hasFocusedOverlay()) return false;
|
|
396
|
+
const width = this.terminalColumns();
|
|
397
|
+
if (width < MIN_BTW_SPLIT_COLUMNS && this.activePane !== "side") this.activatePane("side");
|
|
398
|
+
if (pasted) {
|
|
399
|
+
this.forwardPastedInput(data);
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
if (width < MIN_BTW_SPLIT_COLUMNS || isKeyRelease2(data)) return false;
|
|
403
|
+
if (this.options.shortcuts?.matches(data, "switchPane")) {
|
|
404
|
+
this.focusGeneration += 1;
|
|
405
|
+
this.cancelDividerDrag();
|
|
406
|
+
this.activatePane(this.activePane === "side" ? "main" : "side");
|
|
407
|
+
return true;
|
|
408
|
+
}
|
|
409
|
+
this.updateFocusLabel();
|
|
410
|
+
const pane = paneForMouseClick(data, width, this.options.layout, this.sidePaneRatio);
|
|
411
|
+
if (pane) this.queuePaneFocus(pane);
|
|
412
|
+
return false;
|
|
413
|
+
}
|
|
414
|
+
render(width) {
|
|
415
|
+
if (width <= 0) return [];
|
|
416
|
+
const safeWidth = Math.max(1, width);
|
|
417
|
+
this.handleViewportWidth(safeWidth);
|
|
418
|
+
if (safeWidth < MIN_BTW_SPLIT_COLUMNS) {
|
|
419
|
+
return this.options.sideComponent.render(safeWidth).map((line) => truncateToWidth(line, safeWidth));
|
|
420
|
+
}
|
|
421
|
+
const { sideWidth, mainWidth } = paneWidths(safeWidth, this.options.layout, this.sidePaneRatio);
|
|
422
|
+
const sideLines = this.options.sideComponent.render(sideWidth);
|
|
423
|
+
const mainLines = this.mainPane.render(mainWidth);
|
|
424
|
+
const rows = Math.max(1, this.options.terminalRows());
|
|
425
|
+
const separator = this.renderDivider();
|
|
426
|
+
const lines = [];
|
|
427
|
+
for (let index = 0; index < rows; index += 1) {
|
|
428
|
+
const sideLine = padLine(sideLines[index] ?? "", sideWidth);
|
|
429
|
+
const mainLine = padLine(mainLines[index] ?? "", mainWidth);
|
|
430
|
+
lines.push(
|
|
431
|
+
this.options.layout === "left-pane" ? `${sideLine}${separator}${mainLine}` : `${mainLine}${separator}${sideLine}`
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
return lines.map((line) => truncateToWidth(line, safeWidth));
|
|
435
|
+
}
|
|
436
|
+
invalidate() {
|
|
437
|
+
this.layoutRoot.invalidate();
|
|
438
|
+
}
|
|
439
|
+
dispose() {
|
|
440
|
+
if (this.disposed) return;
|
|
441
|
+
this.disposed = true;
|
|
442
|
+
this.focusGeneration += 1;
|
|
443
|
+
this.dividerDragStartRatio = void 0;
|
|
444
|
+
this.failedRatioSaveGenerationDuringDrag = void 0;
|
|
445
|
+
let error;
|
|
446
|
+
for (const cleanup of [
|
|
447
|
+
() => this.viewportRouter.dispose(),
|
|
448
|
+
() => this.mainPane.dispose(),
|
|
449
|
+
() => this.setFocusLabel(void 0)
|
|
450
|
+
]) {
|
|
451
|
+
try {
|
|
452
|
+
cleanup();
|
|
453
|
+
} catch (failure) {
|
|
454
|
+
error ??= failure;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (error !== void 0) throw error;
|
|
458
|
+
}
|
|
459
|
+
updateFocusLabel() {
|
|
460
|
+
const shortcuts = this.options.shortcuts;
|
|
461
|
+
this.setFocusLabel(shortcuts?.keys.switchPane.length ? shortcuts.label("switchPane") : void 0);
|
|
462
|
+
}
|
|
463
|
+
setFocusLabel(shortcutLabel) {
|
|
464
|
+
const component = this.options.sideComponent;
|
|
465
|
+
component.setWorkspaceFocus?.(this.activePane, shortcutLabel);
|
|
466
|
+
}
|
|
467
|
+
renderDivider() {
|
|
468
|
+
return this.options.theme.fg("borderMuted", "\u2502");
|
|
469
|
+
}
|
|
470
|
+
handleDividerMouse(event) {
|
|
471
|
+
if (this.disposed) return void 0;
|
|
472
|
+
if (this.options.hasFocusedOverlay()) {
|
|
473
|
+
return { handled: true, render: this.cancelDividerDrag() };
|
|
474
|
+
}
|
|
475
|
+
if (event.type === "press" && event.button === "left") {
|
|
476
|
+
if (this.terminalColumns() < MIN_BTW_SPLIT_COLUMNS) return void 0;
|
|
477
|
+
this.focusGeneration += 1;
|
|
478
|
+
this.dividerDragStartRatio = this.sidePaneRatio;
|
|
479
|
+
return { handled: true, capture: true, render: false };
|
|
480
|
+
}
|
|
481
|
+
if (this.dividerDragStartRatio === void 0) return void 0;
|
|
482
|
+
if (event.type === "drag") {
|
|
483
|
+
const changed = this.updateSidePaneRatio(event.screenX);
|
|
484
|
+
return { handled: true, render: changed };
|
|
485
|
+
}
|
|
486
|
+
if (event.type === "release") {
|
|
487
|
+
const changed = this.updateSidePaneRatio(event.screenX);
|
|
488
|
+
const startRatio = this.dividerDragStartRatio;
|
|
489
|
+
const failedDuringDrag = this.failedRatioSaveGenerationDuringDrag === this.ratioSaveGeneration;
|
|
490
|
+
this.dividerDragStartRatio = void 0;
|
|
491
|
+
this.failedRatioSaveGenerationDuringDrag = void 0;
|
|
492
|
+
if (failedDuringDrag && this.sidePaneRatio === this.persistedSidePaneRatio) {
|
|
493
|
+
return { handled: true, render: changed };
|
|
494
|
+
}
|
|
495
|
+
if (this.sidePaneRatio !== startRatio) {
|
|
496
|
+
this.persistCurrentSidePaneRatio();
|
|
497
|
+
return { handled: true, render: changed };
|
|
498
|
+
}
|
|
499
|
+
const restored = failedDuringDrag && this.restorePersistedSidePaneRatio();
|
|
500
|
+
return { handled: true, render: changed || restored };
|
|
501
|
+
}
|
|
502
|
+
return { handled: true, render: false };
|
|
503
|
+
}
|
|
504
|
+
updateSidePaneRatio(dividerColumn) {
|
|
505
|
+
const width = this.terminalColumns();
|
|
506
|
+
if (width < MIN_BTW_SPLIT_COLUMNS) return false;
|
|
507
|
+
const contentWidth = width - PANE_DIVIDER_COLUMNS;
|
|
508
|
+
const leftWidth = clamp(Math.floor(dividerColumn), 1, contentWidth - 1);
|
|
509
|
+
const sideWidth = this.options.layout === "left-pane" ? leftWidth : contentWidth - leftWidth;
|
|
510
|
+
const ratio = clampSidePaneRatio(roundPaneRatio(sideWidth / contentWidth));
|
|
511
|
+
if (ratio === this.sidePaneRatio) return false;
|
|
512
|
+
this.sidePaneRatio = ratio;
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
cancelDividerDrag() {
|
|
516
|
+
const startRatio = this.dividerDragStartRatio;
|
|
517
|
+
if (startRatio === void 0) return false;
|
|
518
|
+
const failedDuringDrag = this.failedRatioSaveGenerationDuringDrag === this.ratioSaveGeneration;
|
|
519
|
+
this.dividerDragStartRatio = void 0;
|
|
520
|
+
this.failedRatioSaveGenerationDuringDrag = void 0;
|
|
521
|
+
return this.setSidePaneRatio(failedDuringDrag ? this.persistedSidePaneRatio : startRatio);
|
|
522
|
+
}
|
|
523
|
+
persistCurrentSidePaneRatio() {
|
|
524
|
+
const persist = this.options.persistSidePaneRatio;
|
|
525
|
+
const ratio = this.sidePaneRatio;
|
|
526
|
+
if (!persist) {
|
|
527
|
+
this.persistedSidePaneRatio = ratio;
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
this.failedRatioSaveGenerationDuringDrag = void 0;
|
|
531
|
+
const saveGeneration = ++this.ratioSaveGeneration;
|
|
532
|
+
void Promise.resolve().then(() => persist(ratio)).then(() => {
|
|
533
|
+
if (this.disposed || saveGeneration <= this.confirmedRatioSaveGeneration) return;
|
|
534
|
+
this.confirmedRatioSaveGeneration = saveGeneration;
|
|
535
|
+
this.persistedSidePaneRatio = ratio;
|
|
536
|
+
}).catch(() => {
|
|
537
|
+
if (this.disposed || saveGeneration !== this.ratioSaveGeneration) return;
|
|
538
|
+
const dragActive = this.dividerDragStartRatio !== void 0;
|
|
539
|
+
const changed = this.restorePersistedSidePaneRatio();
|
|
540
|
+
if (dragActive) this.failedRatioSaveGenerationDuringDrag = saveGeneration;
|
|
541
|
+
if (changed) this.options.requestRender();
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
restorePersistedSidePaneRatio() {
|
|
545
|
+
return this.setSidePaneRatio(this.persistedSidePaneRatio);
|
|
546
|
+
}
|
|
547
|
+
setSidePaneRatio(ratio) {
|
|
548
|
+
if (ratio === this.sidePaneRatio) return false;
|
|
549
|
+
this.sidePaneRatio = ratio;
|
|
550
|
+
this.layoutRoot.invalidate();
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
queuePaneFocus(pane) {
|
|
554
|
+
const generation = ++this.focusGeneration;
|
|
555
|
+
queueMicrotask(() => {
|
|
556
|
+
if (this.disposed || generation !== this.focusGeneration || this.options.hasFocusedOverlay()) return;
|
|
557
|
+
this.activatePane(this.terminalColumns() < MIN_BTW_SPLIT_COLUMNS ? "side" : pane);
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
terminalColumns() {
|
|
561
|
+
return Math.max(1, Math.floor(this.options.terminalColumns()));
|
|
562
|
+
}
|
|
563
|
+
handleViewportWidth(width) {
|
|
564
|
+
if (this.disposed || width >= MIN_BTW_SPLIT_COLUMNS || this.activePane === "side" || this.options.hasFocusedOverlay()) {
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
this.activatePane("side");
|
|
568
|
+
}
|
|
569
|
+
activatePane(pane) {
|
|
570
|
+
if (this.disposed) return;
|
|
571
|
+
this.activePane = pane;
|
|
572
|
+
this.updateFocusLabel();
|
|
573
|
+
this.viewportRouter.activate(pane);
|
|
574
|
+
this.options.setFocus(pane === "side" ? this.options.sideComponent : this.options.mainInput);
|
|
575
|
+
this.options.requestRender();
|
|
576
|
+
}
|
|
577
|
+
forwardPastedInput(data) {
|
|
578
|
+
const target = this.activePane === "side" ? this.options.sideComponent : this.options.mainInput;
|
|
579
|
+
target.handleInput?.(data);
|
|
580
|
+
this.options.requestRender();
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
var ResponsivePaneRow = class extends HStack {
|
|
584
|
+
constructor(left, separator, right, sideIndex, sidePaneRatio, onViewportWidth) {
|
|
585
|
+
super([
|
|
586
|
+
{ component: left, basis: 1, grow: 0, shrink: 0, minSize: 1 },
|
|
587
|
+
{
|
|
588
|
+
component: separator,
|
|
589
|
+
basis: PANE_DIVIDER_COLUMNS,
|
|
590
|
+
grow: 0,
|
|
591
|
+
shrink: 0,
|
|
592
|
+
minSize: PANE_DIVIDER_COLUMNS
|
|
593
|
+
},
|
|
594
|
+
{ component: right, basis: 1, grow: 0, shrink: 0, minSize: 1 }
|
|
595
|
+
]);
|
|
596
|
+
this.sideIndex = sideIndex;
|
|
597
|
+
this.sidePaneRatio = sidePaneRatio;
|
|
598
|
+
this.onViewportWidth = onViewportWidth;
|
|
599
|
+
for (const [index, entry] of this.entries.entries()) {
|
|
600
|
+
entry.visible = (viewport) => {
|
|
601
|
+
if (index === 0) this.resize(viewport.width);
|
|
602
|
+
return index === this.sideIndex || viewport.width >= MIN_BTW_SPLIT_COLUMNS;
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
sideIndex;
|
|
607
|
+
sidePaneRatio;
|
|
608
|
+
onViewportWidth;
|
|
609
|
+
resize(width) {
|
|
610
|
+
const safeWidth = Math.max(1, Math.floor(width));
|
|
611
|
+
this.onViewportWidth(safeWidth);
|
|
612
|
+
if (safeWidth < MIN_BTW_SPLIT_COLUMNS) {
|
|
613
|
+
for (const [index, entry] of this.entries.entries()) {
|
|
614
|
+
entry.basis = index === this.sideIndex ? safeWidth : 1;
|
|
615
|
+
}
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
const layout = this.sideIndex === 0 ? "left-pane" : "right-pane";
|
|
619
|
+
const { leftWidth, rightWidth } = paneWidths(safeWidth, layout, this.sidePaneRatio());
|
|
620
|
+
const left = this.entries[0];
|
|
621
|
+
const separator = this.entries[1];
|
|
622
|
+
const right = this.entries[2];
|
|
623
|
+
if (left) left.basis = leftWidth;
|
|
624
|
+
if (separator) separator.basis = PANE_DIVIDER_COLUMNS;
|
|
625
|
+
if (right) right.basis = rightWidth;
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
var MainThreadPane = class {
|
|
629
|
+
constructor(mainThread, mainLayout, mainScrollView, theme, terminalRows) {
|
|
630
|
+
this.terminalRows = terminalRows;
|
|
631
|
+
this.borrowedScroll = mainLayout !== void 0;
|
|
632
|
+
this.body = {
|
|
633
|
+
render: (width) => mainThread.render(Math.max(1, width)).map((line) => truncateToWidth(line, Math.max(1, width))),
|
|
634
|
+
invalidate() {
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
if (mainLayout) {
|
|
638
|
+
this.layout = mainLayout;
|
|
639
|
+
if (!mainScrollView) throw new Error("The native main pane has no compatible viewport.");
|
|
640
|
+
this.scroll = mainScrollView;
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
this.scroll = new ScrollView(this.body, {
|
|
644
|
+
follow: "end",
|
|
645
|
+
scrollbar: "auto",
|
|
646
|
+
scrollbarTrackStyle: (text) => theme.fg("borderMuted", text),
|
|
647
|
+
scrollbarThumbStyle: (text) => theme.fg("muted", text)
|
|
648
|
+
});
|
|
649
|
+
this.layout = this.scroll;
|
|
650
|
+
}
|
|
651
|
+
terminalRows;
|
|
652
|
+
borrowedScroll;
|
|
653
|
+
body;
|
|
654
|
+
layout;
|
|
655
|
+
scroll;
|
|
656
|
+
getLayout() {
|
|
657
|
+
return this.layout;
|
|
658
|
+
}
|
|
659
|
+
dispose() {
|
|
660
|
+
if (this.borrowedScroll) return;
|
|
661
|
+
this.scroll?.setScrollbarActive(false);
|
|
662
|
+
this.scroll?.setScrollbar("hidden");
|
|
663
|
+
}
|
|
664
|
+
getPrimaryScrollView() {
|
|
665
|
+
return this.scroll;
|
|
666
|
+
}
|
|
667
|
+
render(width) {
|
|
668
|
+
if (width <= 0) return [];
|
|
669
|
+
const safeWidth = Math.max(1, width);
|
|
670
|
+
const rows = Math.max(1, this.terminalRows());
|
|
671
|
+
const visible = this.body.render(safeWidth).slice(-rows);
|
|
672
|
+
return [...Array.from({ length: Math.max(0, rows - visible.length) }, () => ""), ...visible];
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
var PaneViewportRouter = class {
|
|
676
|
+
constructor(side, main, setViewportTarget) {
|
|
677
|
+
this.side = side;
|
|
678
|
+
this.main = main;
|
|
679
|
+
this.setViewportTarget = setViewportTarget;
|
|
680
|
+
for (const scrollView of [side, main]) {
|
|
681
|
+
if (!scrollView) continue;
|
|
682
|
+
const primary = Object.getOwnPropertyDescriptor(scrollView, "primary");
|
|
683
|
+
if (!primary?.writable || typeof primary.value !== "boolean") {
|
|
684
|
+
throw new Error("Pi BTW cannot route this viewport; update Pi.");
|
|
685
|
+
}
|
|
686
|
+
this.originalPrimary.set(scrollView, scrollView.primary);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
side;
|
|
690
|
+
main;
|
|
691
|
+
setViewportTarget;
|
|
692
|
+
originalPrimary = /* @__PURE__ */ new Map();
|
|
693
|
+
disposed = false;
|
|
694
|
+
activate(pane) {
|
|
695
|
+
if (this.disposed) return;
|
|
696
|
+
const target = pane === "side" ? this.side : this.main;
|
|
697
|
+
const previous = new Map([...this.originalPrimary.keys()].map((scrollView) => [scrollView, scrollView.primary]));
|
|
698
|
+
try {
|
|
699
|
+
for (const scrollView of this.originalPrimary.keys()) setScrollViewPrimary(scrollView, scrollView === target);
|
|
700
|
+
this.setViewportTarget(target);
|
|
701
|
+
} catch (error) {
|
|
702
|
+
for (const [scrollView, primary] of previous) setScrollViewPrimary(scrollView, primary);
|
|
703
|
+
throw error;
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
dispose() {
|
|
707
|
+
if (this.disposed) return;
|
|
708
|
+
this.disposed = true;
|
|
709
|
+
let error;
|
|
710
|
+
for (const [scrollView, primary] of this.originalPrimary) {
|
|
711
|
+
try {
|
|
712
|
+
setScrollViewPrimary(scrollView, primary);
|
|
713
|
+
} catch (failure) {
|
|
714
|
+
error ??= failure;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
this.originalPrimary.clear();
|
|
718
|
+
try {
|
|
719
|
+
this.setViewportTarget(void 0);
|
|
720
|
+
} catch (failure) {
|
|
721
|
+
error ??= failure;
|
|
722
|
+
}
|
|
723
|
+
if (error !== void 0) throw error;
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
function setScrollViewPrimary(scrollView, primary) {
|
|
727
|
+
if (!Reflect.set(scrollView, "primary", primary)) throw new Error("Pi BTW could not restore viewport ownership.");
|
|
728
|
+
}
|
|
729
|
+
function paneForMouseClick(data, terminalColumns, layout, sidePaneRatio) {
|
|
730
|
+
const match = SGR_MOUSE_PRESS_PATTERN.exec(data);
|
|
731
|
+
if (!match) return void 0;
|
|
732
|
+
const button = Number.parseInt(match[1] ?? "", 10);
|
|
733
|
+
if ((button & 32) !== 0 || (button & 64) !== 0 || (button & 3) !== 0) return void 0;
|
|
734
|
+
const column = Number.parseInt(match[2] ?? "", 10) - 1;
|
|
735
|
+
if (!Number.isFinite(column) || column < 0 || column >= terminalColumns) return void 0;
|
|
736
|
+
const { leftWidth } = paneWidths(terminalColumns, layout, sidePaneRatio);
|
|
737
|
+
if (column >= leftWidth && column < leftWidth + PANE_DIVIDER_COLUMNS) return void 0;
|
|
738
|
+
const clickedLeft = column < leftWidth;
|
|
739
|
+
if (layout === "left-pane") return clickedLeft ? "side" : "main";
|
|
740
|
+
return clickedLeft ? "main" : "side";
|
|
741
|
+
}
|
|
742
|
+
function paneWidths(width, layout, sidePaneRatio) {
|
|
743
|
+
const contentWidth = Math.max(2, width - PANE_DIVIDER_COLUMNS);
|
|
744
|
+
const sideWidth = clamp(
|
|
745
|
+
Math.round(contentWidth * clampSidePaneRatio(sidePaneRatio)),
|
|
746
|
+
1,
|
|
747
|
+
Math.max(1, contentWidth - 1)
|
|
748
|
+
);
|
|
749
|
+
const mainWidth = Math.max(1, contentWidth - sideWidth);
|
|
750
|
+
const leftWidth = layout === "left-pane" ? sideWidth : mainWidth;
|
|
751
|
+
const rightWidth = layout === "left-pane" ? mainWidth : sideWidth;
|
|
752
|
+
return { leftWidth, rightWidth, sideWidth, mainWidth };
|
|
753
|
+
}
|
|
754
|
+
function clampSidePaneRatio(ratio) {
|
|
755
|
+
return Number.isFinite(ratio) ? clamp(ratio, MIN_BTW_SIDE_PANE_RATIO, MAX_BTW_SIDE_PANE_RATIO) : DEFAULT_BTW_SIDE_PANE_RATIO;
|
|
756
|
+
}
|
|
757
|
+
function roundPaneRatio(ratio) {
|
|
758
|
+
return Math.round(ratio * 1e4) / 1e4;
|
|
759
|
+
}
|
|
760
|
+
function clamp(value, minimum, maximum) {
|
|
761
|
+
return Math.max(minimum, Math.min(maximum, value));
|
|
762
|
+
}
|
|
763
|
+
function hasInput(component) {
|
|
764
|
+
return component !== null && typeof component.handleInput === "function";
|
|
765
|
+
}
|
|
766
|
+
function padLine(line, width) {
|
|
767
|
+
const truncated = truncateToWidth(line, width);
|
|
768
|
+
return `${truncated}${" ".repeat(Math.max(0, width - visibleWidth(truncated)))}`;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// src/fullscreen-ui.ts
|
|
772
|
+
var FullscreenUiDisposedError = class extends Error {
|
|
773
|
+
constructor() {
|
|
774
|
+
super("The dedicated pi-btw UI was disposed.");
|
|
775
|
+
this.name = "FullscreenUiDisposedError";
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
async function runBtwFullscreen(ctx, run, options = {}, dependencies = {}) {
|
|
779
|
+
const { createFullscreenHandoff } = await import("@narumitw/pi-tui-kit/fullscreen-handoff");
|
|
780
|
+
if (ctx.signal?.aborted || options.signal?.aborted || options.canRestoreParent?.() === false) {
|
|
781
|
+
throw new FullscreenUiDisposedError();
|
|
782
|
+
}
|
|
783
|
+
const createTui = dependencies.createTui ?? ((parent, theme, keybindings, fullscreenOptions) => createBtwFullscreenTui(
|
|
784
|
+
parent,
|
|
785
|
+
theme,
|
|
786
|
+
keybindings,
|
|
787
|
+
fullscreenOptions.copyOnSelect ?? true,
|
|
788
|
+
dependencies.manualSelectionCopySupported ?? hasManualSelectionCopyApi(),
|
|
789
|
+
dependencies.openUrl ?? openUrlInBrowser,
|
|
790
|
+
dependencies.copyToClipboard ?? copyToHostClipboard
|
|
791
|
+
));
|
|
792
|
+
let liveEditorText = ctx.ui.getEditorText();
|
|
793
|
+
let restoreEditor = false;
|
|
794
|
+
let host;
|
|
795
|
+
const outcome = await ctx.ui.custom(
|
|
796
|
+
(parent, theme, keybindings, done) => {
|
|
797
|
+
host = new BtwFullscreenHost(
|
|
798
|
+
parent,
|
|
799
|
+
theme,
|
|
800
|
+
keybindings,
|
|
801
|
+
ctx,
|
|
802
|
+
run,
|
|
803
|
+
(value) => {
|
|
804
|
+
try {
|
|
805
|
+
if (options.canRestoreParent?.() === false) {
|
|
806
|
+
done(value);
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
liveEditorText = ctx.ui.getEditorText();
|
|
810
|
+
restoreEditor = true;
|
|
811
|
+
} catch {
|
|
812
|
+
}
|
|
813
|
+
done(value);
|
|
814
|
+
},
|
|
815
|
+
createTui,
|
|
816
|
+
options,
|
|
817
|
+
createFullscreenHandoff
|
|
818
|
+
);
|
|
819
|
+
return host;
|
|
820
|
+
},
|
|
821
|
+
{
|
|
822
|
+
overlay: true,
|
|
823
|
+
onHandle: (handle) => host?.setParentOverlay(handle)
|
|
824
|
+
}
|
|
825
|
+
);
|
|
826
|
+
if (restoreEditor && options.canRestoreParent?.() !== false) {
|
|
827
|
+
try {
|
|
828
|
+
if (ctx.ui.getEditorText() !== liveEditorText) ctx.ui.setEditorText(liveEditorText);
|
|
829
|
+
} catch {
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
if (outcome.kind === "failed") throw outcome.error;
|
|
833
|
+
return outcome.value;
|
|
834
|
+
}
|
|
835
|
+
var BtwFullscreenHost = class {
|
|
836
|
+
constructor(parent, theme, keybindings, ctx, run, done, createTui, options, createHandoff) {
|
|
837
|
+
this.parent = parent;
|
|
838
|
+
this.theme = theme;
|
|
839
|
+
this.keybindings = keybindings;
|
|
840
|
+
this.ctx = ctx;
|
|
841
|
+
this.run = run;
|
|
842
|
+
this.done = done;
|
|
843
|
+
this.createTui = createTui;
|
|
844
|
+
this.options = options;
|
|
845
|
+
const initialMainInput = getFocusedComponent(parent);
|
|
846
|
+
this.mainThreadInput = new BtwMainThreadInput(
|
|
847
|
+
initialMainInput,
|
|
848
|
+
() => {
|
|
849
|
+
const target = getFocusedComponent(parent);
|
|
850
|
+
return target === this ? initialMainInput : target;
|
|
851
|
+
},
|
|
852
|
+
() => this.fullscreen?.requestRender()
|
|
853
|
+
);
|
|
854
|
+
this.handoff = createHandoff(parent, {
|
|
855
|
+
isCurrent: () => !this.disposed && !this.finished && !this.lifetimeController.signal.aborted && this.options.canRestoreParent?.() !== false,
|
|
856
|
+
canRestoreParent: () => this.options.canRestoreParent?.() !== false
|
|
857
|
+
});
|
|
858
|
+
this.sidePaneRatio = options.sidePaneRatio;
|
|
859
|
+
queueMicrotask(() => void this.start());
|
|
860
|
+
}
|
|
861
|
+
parent;
|
|
862
|
+
theme;
|
|
863
|
+
keybindings;
|
|
864
|
+
ctx;
|
|
865
|
+
run;
|
|
866
|
+
done;
|
|
867
|
+
createTui;
|
|
868
|
+
options;
|
|
869
|
+
fullscreen;
|
|
870
|
+
handoff;
|
|
871
|
+
cancelActiveCustom;
|
|
872
|
+
hardCancelActiveCustom;
|
|
873
|
+
removeHardCancelListener;
|
|
874
|
+
removeUpstreamAbortListener;
|
|
875
|
+
started = false;
|
|
876
|
+
splitReady = false;
|
|
877
|
+
disposed = false;
|
|
878
|
+
finished = false;
|
|
879
|
+
parentRestorePromise;
|
|
880
|
+
cleanupErrors = [];
|
|
881
|
+
removeMainThreadUpdateListener;
|
|
882
|
+
mainThreadRefreshTimer;
|
|
883
|
+
mainThreadInput;
|
|
884
|
+
lifetimeController = new AbortController();
|
|
885
|
+
pendingSidePaneWrites = /* @__PURE__ */ new Set();
|
|
886
|
+
sidePaneRatio;
|
|
887
|
+
sidePaneWriteGeneration = 0;
|
|
888
|
+
confirmedSidePaneWriteGeneration = 0;
|
|
889
|
+
setParentOverlay(overlay) {
|
|
890
|
+
this.handoff.setOverlayHandle(overlay);
|
|
891
|
+
}
|
|
892
|
+
render(width) {
|
|
893
|
+
return [truncateToWidth2(this.theme.fg("muted", "Opening btw side thread\u2026"), width)];
|
|
894
|
+
}
|
|
895
|
+
invalidate() {
|
|
896
|
+
}
|
|
897
|
+
dispose() {
|
|
898
|
+
if (this.disposed || this.finished) return;
|
|
899
|
+
this.disposed = true;
|
|
900
|
+
this.lifetimeController.abort();
|
|
901
|
+
try {
|
|
902
|
+
this.cancelActiveCustom?.();
|
|
903
|
+
} finally {
|
|
904
|
+
this.queueParentRestore();
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
async start() {
|
|
908
|
+
if (this.started || this.finished) return;
|
|
909
|
+
this.started = true;
|
|
910
|
+
let outcome;
|
|
911
|
+
try {
|
|
912
|
+
this.watchUpstreamCancellation();
|
|
913
|
+
if (this.disposed) throw new FullscreenUiDisposedError();
|
|
914
|
+
if ((this.options.layout ?? "fullscreen") !== "fullscreen") {
|
|
915
|
+
getParentFullscreenLayout(this.parent);
|
|
916
|
+
getParentFullscreenScrollView(this.parent);
|
|
917
|
+
}
|
|
918
|
+
this.fullscreen = await this.handoff.enter(
|
|
919
|
+
() => this.createTui(this.parent, this.theme, this.keybindings, this.options)
|
|
920
|
+
);
|
|
921
|
+
if (!this.fullscreen || this.disposed || this.finished || this.options.canRestoreParent?.() === false) {
|
|
922
|
+
throw new FullscreenUiDisposedError();
|
|
923
|
+
}
|
|
924
|
+
this.watchMainThreadUpdates();
|
|
925
|
+
const shortcuts = resolveBtwShortcuts(
|
|
926
|
+
this.options.keybindings,
|
|
927
|
+
this.keybindings,
|
|
928
|
+
this.options.copyOnSelect ?? true
|
|
929
|
+
);
|
|
930
|
+
setBtwShortcuts(this.fullscreen, shortcuts);
|
|
931
|
+
setBtwKeybindings(this.fullscreen, this.keybindings);
|
|
932
|
+
setBtwHostInputBlockers(
|
|
933
|
+
this.fullscreen,
|
|
934
|
+
this.options.copyOnSelect === false ? this.keybindings.getKeys("app.message.copy") : []
|
|
935
|
+
);
|
|
936
|
+
let previousWarnings = [];
|
|
937
|
+
const reportWarnings = () => {
|
|
938
|
+
const warnings = shortcuts.warnings;
|
|
939
|
+
for (const warning of warnings) {
|
|
940
|
+
if (previousWarnings.includes(warning)) continue;
|
|
941
|
+
try {
|
|
942
|
+
this.ctx.ui.notify(`Pi BTW: ${warning}`, "warning");
|
|
943
|
+
} catch {
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
previousWarnings = warnings;
|
|
947
|
+
};
|
|
948
|
+
const pasteGuard = new BtwPasteGuard();
|
|
949
|
+
const addHardCancelListener = this.fullscreen.addInputListenerBeforeAll?.bind(this.fullscreen) ?? this.fullscreen.addInputListenerBeforeViewport?.bind(this.fullscreen) ?? this.fullscreen.addInputListener.bind(this.fullscreen);
|
|
950
|
+
this.removeHardCancelListener = addHardCancelListener((data) => {
|
|
951
|
+
reportWarnings();
|
|
952
|
+
if (pasteGuard.consume(data) || !shortcuts.matches(data, "exit")) return void 0;
|
|
953
|
+
this.disposed = true;
|
|
954
|
+
this.lifetimeController.abort();
|
|
955
|
+
try {
|
|
956
|
+
this.hardCancelActiveCustom?.();
|
|
957
|
+
} finally {
|
|
958
|
+
this.queueParentRestore();
|
|
959
|
+
}
|
|
960
|
+
return { consume: true };
|
|
961
|
+
});
|
|
962
|
+
const value = await this.runUntilCancelled();
|
|
963
|
+
await this.waitForSidePaneWrites();
|
|
964
|
+
outcome = { kind: "completed", value };
|
|
965
|
+
} catch (error) {
|
|
966
|
+
await this.waitForSidePaneWrites();
|
|
967
|
+
outcome = { kind: "failed", error };
|
|
968
|
+
}
|
|
969
|
+
try {
|
|
970
|
+
this.cancelActiveCustom?.();
|
|
971
|
+
} catch (error) {
|
|
972
|
+
this.cleanupErrors.push(error);
|
|
973
|
+
}
|
|
974
|
+
this.queueParentRestore();
|
|
975
|
+
await this.parentRestorePromise;
|
|
976
|
+
if (this.cleanupErrors.length) {
|
|
977
|
+
outcome = {
|
|
978
|
+
kind: "failed",
|
|
979
|
+
error: combineErrors(
|
|
980
|
+
outcome.kind === "failed" ? [outcome.error, ...this.cleanupErrors] : this.cleanupErrors,
|
|
981
|
+
"Pi BTW flow and terminal cleanup failed"
|
|
982
|
+
)
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
this.finished = true;
|
|
986
|
+
this.done(outcome);
|
|
987
|
+
}
|
|
988
|
+
async runUntilCancelled() {
|
|
989
|
+
const signal = this.lifetimeController.signal;
|
|
990
|
+
let removeAbort;
|
|
991
|
+
let cancellation;
|
|
992
|
+
const cancelled = new Promise((_resolve, reject) => {
|
|
993
|
+
const onAbort = () => {
|
|
994
|
+
cancellation ??= setImmediate(() => reject(new FullscreenUiDisposedError()));
|
|
995
|
+
};
|
|
996
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
997
|
+
removeAbort = () => signal.removeEventListener("abort", onAbort);
|
|
998
|
+
if (signal.aborted) onAbort();
|
|
999
|
+
});
|
|
1000
|
+
try {
|
|
1001
|
+
if (signal.aborted) throw new FullscreenUiDisposedError();
|
|
1002
|
+
return await Promise.race([this.run(this.createContext()), cancelled]);
|
|
1003
|
+
} finally {
|
|
1004
|
+
removeAbort?.();
|
|
1005
|
+
if (cancellation) clearImmediate(cancellation);
|
|
1006
|
+
}
|
|
1007
|
+
}
|
|
1008
|
+
watchUpstreamCancellation() {
|
|
1009
|
+
const signals = [this.ctx.signal, this.options.signal].filter(
|
|
1010
|
+
(signal2) => signal2 !== void 0
|
|
1011
|
+
);
|
|
1012
|
+
const signal = signals.length ? AbortSignal.any(signals) : void 0;
|
|
1013
|
+
if (!signal) return;
|
|
1014
|
+
const onAbort = () => this.dispose();
|
|
1015
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
1016
|
+
this.removeUpstreamAbortListener = () => signal.removeEventListener("abort", onAbort);
|
|
1017
|
+
if (signal.aborted) onAbort();
|
|
1018
|
+
}
|
|
1019
|
+
queueParentRestore() {
|
|
1020
|
+
if (this.parentRestorePromise) return;
|
|
1021
|
+
const restoration = this.handoff.restore().catch((error) => {
|
|
1022
|
+
this.cleanupErrors.push(error);
|
|
1023
|
+
});
|
|
1024
|
+
this.parentRestorePromise = Promise.resolve().then(async () => {
|
|
1025
|
+
this.restoreParent();
|
|
1026
|
+
await restoration;
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
watchMainThreadUpdates() {
|
|
1030
|
+
if ((this.options.layout ?? "fullscreen") === "fullscreen") return;
|
|
1031
|
+
this.removeMainThreadUpdateListener = this.options.subscribeMainThreadUpdates?.(() => {
|
|
1032
|
+
this.mainThreadInput.refreshTarget();
|
|
1033
|
+
if (this.mainThreadRefreshTimer || this.disposed || this.finished) return;
|
|
1034
|
+
this.mainThreadRefreshTimer = setTimeout(() => {
|
|
1035
|
+
this.mainThreadRefreshTimer = void 0;
|
|
1036
|
+
if (!this.disposed && !this.finished) this.fullscreen?.requestRender();
|
|
1037
|
+
}, 0);
|
|
1038
|
+
this.mainThreadRefreshTimer.unref();
|
|
1039
|
+
});
|
|
1040
|
+
}
|
|
1041
|
+
restoreParent() {
|
|
1042
|
+
if (this.mainThreadRefreshTimer) {
|
|
1043
|
+
clearTimeout(this.mainThreadRefreshTimer);
|
|
1044
|
+
this.mainThreadRefreshTimer = void 0;
|
|
1045
|
+
}
|
|
1046
|
+
const removeMainThreadUpdateListener = this.removeMainThreadUpdateListener;
|
|
1047
|
+
this.removeMainThreadUpdateListener = void 0;
|
|
1048
|
+
try {
|
|
1049
|
+
removeMainThreadUpdateListener?.();
|
|
1050
|
+
} catch (error) {
|
|
1051
|
+
this.cleanupErrors.push(error);
|
|
1052
|
+
}
|
|
1053
|
+
try {
|
|
1054
|
+
this.mainThreadInput.dispose();
|
|
1055
|
+
} catch (error) {
|
|
1056
|
+
this.cleanupErrors.push(error);
|
|
1057
|
+
}
|
|
1058
|
+
const removeUpstreamAbortListener = this.removeUpstreamAbortListener;
|
|
1059
|
+
this.removeUpstreamAbortListener = void 0;
|
|
1060
|
+
try {
|
|
1061
|
+
removeUpstreamAbortListener?.();
|
|
1062
|
+
} catch (error) {
|
|
1063
|
+
this.cleanupErrors.push(error);
|
|
1064
|
+
}
|
|
1065
|
+
const removeHardCancelListener = this.removeHardCancelListener;
|
|
1066
|
+
this.removeHardCancelListener = void 0;
|
|
1067
|
+
try {
|
|
1068
|
+
removeHardCancelListener?.();
|
|
1069
|
+
} catch (error) {
|
|
1070
|
+
this.cleanupErrors.push(error);
|
|
1071
|
+
}
|
|
1072
|
+
}
|
|
1073
|
+
createContext() {
|
|
1074
|
+
const ui = new Proxy(this.ctx.ui, {
|
|
1075
|
+
get: (target, property) => {
|
|
1076
|
+
if (property === "custom") {
|
|
1077
|
+
return (factory, options) => this.showCustom(factory, options);
|
|
1078
|
+
}
|
|
1079
|
+
if (property === "notify") {
|
|
1080
|
+
return (message, level) => {
|
|
1081
|
+
if (this.disposed || this.finished) return;
|
|
1082
|
+
target.notify(message, level);
|
|
1083
|
+
const display = sanitizeSingleLine(message);
|
|
1084
|
+
if (display) this.fullscreen?.flash?.(display);
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
const value = Reflect.get(target, property, target);
|
|
1088
|
+
return typeof value === "function" ? (...args) => {
|
|
1089
|
+
if (this.disposed || this.finished || this.lifetimeController.signal.aborted || this.options.canRestoreParent?.() === false) {
|
|
1090
|
+
throw new FullscreenUiDisposedError();
|
|
1091
|
+
}
|
|
1092
|
+
return Reflect.apply(value, target, args);
|
|
1093
|
+
} : value;
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
const signal = this.ctx.signal ? AbortSignal.any([this.ctx.signal, this.lifetimeController.signal]) : this.lifetimeController.signal;
|
|
1097
|
+
return new Proxy(this.ctx, {
|
|
1098
|
+
get: (target, property) => {
|
|
1099
|
+
if (property === "ui") return ui;
|
|
1100
|
+
if (property === "signal") return signal;
|
|
1101
|
+
return Reflect.get(target, property, target);
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
showCustom(factory, options) {
|
|
1106
|
+
const fullscreen = this.fullscreen;
|
|
1107
|
+
if (!fullscreen || this.disposed || this.finished) {
|
|
1108
|
+
return Promise.reject(new FullscreenUiDisposedError());
|
|
1109
|
+
}
|
|
1110
|
+
if (this.cancelActiveCustom) {
|
|
1111
|
+
return Promise.reject(new Error("pi-btw attempted to open overlapping custom UI."));
|
|
1112
|
+
}
|
|
1113
|
+
return new Promise((resolve, reject) => {
|
|
1114
|
+
let component;
|
|
1115
|
+
let overlay;
|
|
1116
|
+
let splitPane;
|
|
1117
|
+
let removePaneFocusListener;
|
|
1118
|
+
let mounted = false;
|
|
1119
|
+
let layoutMounted = false;
|
|
1120
|
+
let factorySettled = false;
|
|
1121
|
+
let closed = false;
|
|
1122
|
+
let promiseSettled = false;
|
|
1123
|
+
let componentDisposed = false;
|
|
1124
|
+
let pendingValue;
|
|
1125
|
+
let hasPendingValue = false;
|
|
1126
|
+
const disposeComponent = () => {
|
|
1127
|
+
if (!component || componentDisposed) return;
|
|
1128
|
+
componentDisposed = true;
|
|
1129
|
+
try {
|
|
1130
|
+
component.dispose?.();
|
|
1131
|
+
} catch (error) {
|
|
1132
|
+
this.cleanupErrors.push(error);
|
|
1133
|
+
}
|
|
1134
|
+
};
|
|
1135
|
+
const unmount = () => {
|
|
1136
|
+
const cleanupErrors = [];
|
|
1137
|
+
const removeFocusListener = removePaneFocusListener;
|
|
1138
|
+
removePaneFocusListener = void 0;
|
|
1139
|
+
try {
|
|
1140
|
+
removeFocusListener?.();
|
|
1141
|
+
} catch (error) {
|
|
1142
|
+
cleanupErrors.push(error);
|
|
1143
|
+
}
|
|
1144
|
+
try {
|
|
1145
|
+
splitPane?.dispose();
|
|
1146
|
+
} catch (error) {
|
|
1147
|
+
cleanupErrors.push(error);
|
|
1148
|
+
}
|
|
1149
|
+
splitPane = void 0;
|
|
1150
|
+
try {
|
|
1151
|
+
if (overlay) overlay.hide();
|
|
1152
|
+
else if (mounted && layoutMounted) fullscreen.setLayoutRoot(void 0);
|
|
1153
|
+
else if (mounted && component) fullscreen.removeChild(component);
|
|
1154
|
+
} catch (error) {
|
|
1155
|
+
cleanupErrors.push(error);
|
|
1156
|
+
}
|
|
1157
|
+
if (overlay || mounted) {
|
|
1158
|
+
try {
|
|
1159
|
+
fullscreen.setFocus(null);
|
|
1160
|
+
fullscreen.requestRender();
|
|
1161
|
+
} catch (error) {
|
|
1162
|
+
cleanupErrors.push(error);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
disposeComponent();
|
|
1166
|
+
if (cleanupErrors.length) throw combineErrors(cleanupErrors, "Pi BTW custom UI cleanup failed");
|
|
1167
|
+
};
|
|
1168
|
+
const complete = () => {
|
|
1169
|
+
if (promiseSettled || !hasPendingValue) return;
|
|
1170
|
+
promiseSettled = true;
|
|
1171
|
+
this.cancelActiveCustom = void 0;
|
|
1172
|
+
this.hardCancelActiveCustom = void 0;
|
|
1173
|
+
if (!factorySettled) {
|
|
1174
|
+
resolve(pendingValue);
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
try {
|
|
1178
|
+
unmount();
|
|
1179
|
+
resolve(pendingValue);
|
|
1180
|
+
} catch (error) {
|
|
1181
|
+
reject(error);
|
|
1182
|
+
}
|
|
1183
|
+
};
|
|
1184
|
+
const close = (value) => {
|
|
1185
|
+
if (closed || promiseSettled) return;
|
|
1186
|
+
closed = true;
|
|
1187
|
+
pendingValue = value;
|
|
1188
|
+
hasPendingValue = true;
|
|
1189
|
+
complete();
|
|
1190
|
+
};
|
|
1191
|
+
const fail = (error) => {
|
|
1192
|
+
if (promiseSettled) return;
|
|
1193
|
+
closed = true;
|
|
1194
|
+
promiseSettled = true;
|
|
1195
|
+
this.cancelActiveCustom = void 0;
|
|
1196
|
+
this.hardCancelActiveCustom = void 0;
|
|
1197
|
+
try {
|
|
1198
|
+
unmount();
|
|
1199
|
+
reject(error);
|
|
1200
|
+
} catch (cleanupError) {
|
|
1201
|
+
reject(combineErrors([error, cleanupError], "Pi BTW custom UI and cleanup failed"));
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
this.cancelActiveCustom = () => {
|
|
1205
|
+
if (promiseSettled) return;
|
|
1206
|
+
disposeComponent();
|
|
1207
|
+
if (!promiseSettled) fail(new FullscreenUiDisposedError());
|
|
1208
|
+
};
|
|
1209
|
+
this.hardCancelActiveCustom = () => {
|
|
1210
|
+
if (promiseSettled) return;
|
|
1211
|
+
try {
|
|
1212
|
+
component?.handleInput?.("");
|
|
1213
|
+
} catch (error) {
|
|
1214
|
+
fail(error);
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
this.cancelActiveCustom?.();
|
|
1218
|
+
};
|
|
1219
|
+
let created;
|
|
1220
|
+
try {
|
|
1221
|
+
created = factory(fullscreen, this.theme, this.keybindings, close);
|
|
1222
|
+
} catch (error) {
|
|
1223
|
+
factorySettled = true;
|
|
1224
|
+
fail(error);
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
Promise.resolve(created).then(async (value) => {
|
|
1228
|
+
component = value;
|
|
1229
|
+
factorySettled = true;
|
|
1230
|
+
if (promiseSettled) {
|
|
1231
|
+
disposeComponent();
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
if (closed) {
|
|
1235
|
+
complete();
|
|
1236
|
+
return;
|
|
1237
|
+
}
|
|
1238
|
+
const workspaceLayout = this.options.layout ?? "fullscreen";
|
|
1239
|
+
if (!options?.overlay && workspaceLayout !== "fullscreen") {
|
|
1240
|
+
await this.waitForSidePaneWrites();
|
|
1241
|
+
if (promiseSettled) {
|
|
1242
|
+
disposeComponent();
|
|
1243
|
+
return;
|
|
1244
|
+
}
|
|
1245
|
+
if (closed) {
|
|
1246
|
+
complete();
|
|
1247
|
+
return;
|
|
1248
|
+
}
|
|
1249
|
+
if (this.disposed || this.finished || this.fullscreen !== fullscreen) {
|
|
1250
|
+
fail(new FullscreenUiDisposedError());
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
if (options?.overlay) {
|
|
1255
|
+
const overlayOptions = typeof options.overlayOptions === "function" ? options.overlayOptions() : options.overlayOptions;
|
|
1256
|
+
overlay = fullscreen.showOverlay(component, overlayOptions);
|
|
1257
|
+
options.onHandle?.(overlay);
|
|
1258
|
+
} else {
|
|
1259
|
+
fullscreen.clear();
|
|
1260
|
+
mounted = true;
|
|
1261
|
+
if (workspaceLayout !== "fullscreen") {
|
|
1262
|
+
layoutMounted = true;
|
|
1263
|
+
const sideLayout = isFullscreenLayoutComponent(component) ? component.getFullscreenLayout() : component;
|
|
1264
|
+
splitPane = new BtwSplitPane({
|
|
1265
|
+
sideComponent: component,
|
|
1266
|
+
sideLayout,
|
|
1267
|
+
mainThread: this.parent,
|
|
1268
|
+
mainLayout: getParentFullscreenLayout(this.parent),
|
|
1269
|
+
mainScrollView: getParentFullscreenScrollView(this.parent),
|
|
1270
|
+
mainInput: this.mainThreadInput,
|
|
1271
|
+
sideScrollView: isFullscreenLayoutComponent(component) ? component.getPrimaryScrollView?.() : void 0,
|
|
1272
|
+
layout: workspaceLayout,
|
|
1273
|
+
shortcuts: getBtwShortcuts(fullscreen),
|
|
1274
|
+
theme: this.theme,
|
|
1275
|
+
terminalColumns: () => fullscreen.terminal.columns,
|
|
1276
|
+
terminalRows: () => fullscreen.terminal.rows,
|
|
1277
|
+
hasFocusedOverlay: () => fullscreen.hasFocusedOverlay?.() ?? false,
|
|
1278
|
+
setFocus: (target) => fullscreen.setFocus(target),
|
|
1279
|
+
setViewportTarget: (target) => fullscreen.setViewportTarget?.(target),
|
|
1280
|
+
requestRender: () => fullscreen.requestRender(),
|
|
1281
|
+
sidePaneRatio: this.sidePaneRatio,
|
|
1282
|
+
...this.options.persistSidePaneRatio ? { persistSidePaneRatio: (ratio) => this.persistSidePaneRatio(ratio) } : {}
|
|
1283
|
+
});
|
|
1284
|
+
const addPaneFocusListener = fullscreen.addInputListenerBeforeViewport?.bind(fullscreen) ?? fullscreen.addInputListener.bind(fullscreen);
|
|
1285
|
+
removePaneFocusListener = addPaneFocusListener((data) => {
|
|
1286
|
+
const consumed = splitPane?.handleTerminalInput(data);
|
|
1287
|
+
return consumed ? { consume: true } : void 0;
|
|
1288
|
+
});
|
|
1289
|
+
fullscreen.setLayoutRoot(splitPane.getFullscreenLayout());
|
|
1290
|
+
} else if (isFullscreenLayoutComponent(component)) {
|
|
1291
|
+
layoutMounted = true;
|
|
1292
|
+
fullscreen.setLayoutRoot(component.getFullscreenLayout());
|
|
1293
|
+
} else {
|
|
1294
|
+
fullscreen.addChild(component);
|
|
1295
|
+
}
|
|
1296
|
+
fullscreen.setFocus(component);
|
|
1297
|
+
fullscreen.requestRender();
|
|
1298
|
+
if (splitPane && !this.splitReady && !this.disposed && !this.finished && !this.lifetimeController.signal.aborted) {
|
|
1299
|
+
this.splitReady = true;
|
|
1300
|
+
this.options.onSplitReady?.();
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
}).catch(fail);
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
persistSidePaneRatio(ratio) {
|
|
1307
|
+
const persist = this.options.persistSidePaneRatio;
|
|
1308
|
+
if (!persist) {
|
|
1309
|
+
this.sidePaneRatio = ratio;
|
|
1310
|
+
return Promise.resolve();
|
|
1311
|
+
}
|
|
1312
|
+
const writeGeneration = ++this.sidePaneWriteGeneration;
|
|
1313
|
+
let task;
|
|
1314
|
+
task = Promise.resolve().then(() => persist(ratio, this.lifetimeController.signal)).then(() => {
|
|
1315
|
+
if (this.lifetimeController.signal.aborted || writeGeneration <= this.confirmedSidePaneWriteGeneration) {
|
|
1316
|
+
return;
|
|
1317
|
+
}
|
|
1318
|
+
this.confirmedSidePaneWriteGeneration = writeGeneration;
|
|
1319
|
+
this.sidePaneRatio = ratio;
|
|
1320
|
+
}).catch((error) => {
|
|
1321
|
+
if (!this.lifetimeController.signal.aborted) {
|
|
1322
|
+
const message = sanitizeSingleLine(
|
|
1323
|
+
`Pi BTW pane width was not saved; the previous value remains active: ${formatError(error)}`
|
|
1324
|
+
);
|
|
1325
|
+
try {
|
|
1326
|
+
this.ctx.ui.notify(message, "error");
|
|
1327
|
+
} catch {
|
|
1328
|
+
}
|
|
1329
|
+
this.fullscreen?.flash?.(message);
|
|
1330
|
+
}
|
|
1331
|
+
throw error;
|
|
1332
|
+
}).finally(() => this.pendingSidePaneWrites.delete(task));
|
|
1333
|
+
this.pendingSidePaneWrites.add(task);
|
|
1334
|
+
return task;
|
|
1335
|
+
}
|
|
1336
|
+
async waitForSidePaneWrites() {
|
|
1337
|
+
while (this.pendingSidePaneWrites.size > 0) {
|
|
1338
|
+
await Promise.allSettled([...this.pendingSidePaneWrites]);
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
function combineErrors(errors, message) {
|
|
1343
|
+
const causes = [];
|
|
1344
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1345
|
+
const remember = (error) => {
|
|
1346
|
+
if (seen.has(error)) return;
|
|
1347
|
+
seen.add(error);
|
|
1348
|
+
if (error instanceof AggregateError) error.errors.forEach(remember);
|
|
1349
|
+
};
|
|
1350
|
+
const overlaps = (error, visited = /* @__PURE__ */ new Set()) => {
|
|
1351
|
+
if (seen.has(error)) return true;
|
|
1352
|
+
if (visited.has(error)) return false;
|
|
1353
|
+
visited.add(error);
|
|
1354
|
+
return error instanceof AggregateError && error.errors.some((cause) => overlaps(cause, visited));
|
|
1355
|
+
};
|
|
1356
|
+
const append = (error) => {
|
|
1357
|
+
if (seen.has(error)) return;
|
|
1358
|
+
if (error instanceof AggregateError && overlaps(error)) {
|
|
1359
|
+
seen.add(error);
|
|
1360
|
+
error.errors.forEach(append);
|
|
1361
|
+
return;
|
|
1362
|
+
}
|
|
1363
|
+
causes.push(error);
|
|
1364
|
+
remember(error);
|
|
1365
|
+
};
|
|
1366
|
+
errors.forEach(append);
|
|
1367
|
+
return causes.length === 1 ? causes[0] : new AggregateError(causes, message);
|
|
1368
|
+
}
|
|
1369
|
+
function formatError(error) {
|
|
1370
|
+
return error instanceof Error ? error.message : String(error);
|
|
1371
|
+
}
|
|
1372
|
+
function getFocusedComponent(tui) {
|
|
1373
|
+
return tui.getFocusedComponent?.() ?? null;
|
|
1374
|
+
}
|
|
1375
|
+
function getParentFullscreenLayout(tui) {
|
|
1376
|
+
if (tui.mode !== "fullscreen") return void 0;
|
|
1377
|
+
const layoutRoot = Reflect.get(tui, "layoutRoot");
|
|
1378
|
+
if (!isComponent(layoutRoot)) {
|
|
1379
|
+
throw new Error("Pi BTW cannot compose this fullscreen layout; use the fullscreen BTW layout or update Pi.");
|
|
1380
|
+
}
|
|
1381
|
+
return layoutRoot;
|
|
1382
|
+
}
|
|
1383
|
+
function getParentFullscreenScrollView(tui) {
|
|
1384
|
+
if (tui.mode !== "fullscreen") return void 0;
|
|
1385
|
+
const layout = Reflect.get(tui, "currentLayout");
|
|
1386
|
+
if (!layout || !(layout.primaryScrollView instanceof ScrollView2)) {
|
|
1387
|
+
throw new Error("Pi BTW cannot identify this fullscreen viewport; use the fullscreen BTW layout or update Pi.");
|
|
1388
|
+
}
|
|
1389
|
+
return layout.primaryScrollView;
|
|
1390
|
+
}
|
|
1391
|
+
function isComponent(value) {
|
|
1392
|
+
return typeof value === "object" && value !== null && "render" in value && typeof value.render === "function" && "invalidate" in value && typeof value.invalidate === "function";
|
|
1393
|
+
}
|
|
1394
|
+
function isFullscreenLayoutComponent(component) {
|
|
1395
|
+
return "getFullscreenLayout" in component && typeof component.getFullscreenLayout === "function";
|
|
1396
|
+
}
|
|
1397
|
+
export {
|
|
1398
|
+
runBtwFullscreen
|
|
1399
|
+
};
|
|
1400
|
+
//# sourceMappingURL=fullscreen-ui-4MJ3HOFJ.ts.map
|