@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.
@@ -4,13 +4,14 @@ import {
4
4
  type Focusable,
5
5
  HStack,
6
6
  isFocusable,
7
+ isKeyRelease,
7
8
  ScrollView,
8
9
  type TuiMouseEvent,
9
10
  type TuiMouseEventResult,
10
11
  truncateToWidth,
11
12
  visibleWidth,
12
13
  } from "@earendil-works/pi-tui";
13
- import { BtwPasteGuard } from "./keybindings.js";
14
+ import { BtwPasteGuard, type BtwShortcuts } from "./keybindings.js";
14
15
  import {
15
16
  type BtwLayout,
16
17
  DEFAULT_BTW_SIDE_PANE_RATIO,
@@ -27,6 +28,7 @@ type BtwActivePane = "side" | "main";
27
28
  export interface BtwFullscreenLayoutComponent extends Component {
28
29
  getFullscreenLayout(): Component;
29
30
  getPrimaryScrollView?(): ScrollView;
31
+ setWorkspaceFocus?(pane: "side" | "main", shortcutLabel: string | undefined): void;
30
32
  }
31
33
 
32
34
  export class BtwMainThreadInput implements Component, Focusable {
@@ -69,11 +71,12 @@ export class BtwMainThreadInput implements Component, Focusable {
69
71
 
70
72
  refreshTarget(): void {
71
73
  if (this.disposed) return;
72
- const next = this.resolveTarget();
73
- if (!hasInput(next) || next === this.target) return;
74
+ const resolved = this.resolveTarget();
75
+ const next = hasInput(resolved) ? resolved : undefined;
76
+ if (next === this.target) return;
74
77
  if (this._focused && this.target && isFocusable(this.target)) this.target.focused = false;
75
78
  this.target = next;
76
- if (this._focused && isFocusable(next)) next.focused = true;
79
+ if (this._focused && next && isFocusable(next)) next.focused = true;
77
80
  }
78
81
 
79
82
  invalidate(): void {}
@@ -81,9 +84,11 @@ export class BtwMainThreadInput implements Component, Focusable {
81
84
  dispose(): void {
82
85
  if (this.disposed) return;
83
86
  this.disposed = true;
84
- if (this._focused && this.target && isFocusable(this.target)) this.target.focused = false;
87
+ const target = this.target;
88
+ const wasFocused = this._focused;
85
89
  this.target = undefined;
86
90
  this._focused = false;
91
+ if (wasFocused && target && isFocusable(target)) target.focused = false;
87
92
  }
88
93
  }
89
94
 
@@ -92,8 +97,10 @@ export interface BtwSplitPaneOptions {
92
97
  sideLayout: Component;
93
98
  mainThread: Component;
94
99
  mainLayout?: Component;
100
+ mainScrollView?: ScrollView;
95
101
  mainInput: Component;
96
102
  sideScrollView?: ScrollView;
103
+ shortcuts?: BtwShortcuts;
97
104
  layout: Exclude<BtwLayout, "fullscreen">;
98
105
  theme: Theme;
99
106
  terminalColumns(): number;
@@ -124,20 +131,27 @@ export class BtwSplitPane implements BtwFullscreenLayoutComponent {
124
131
  constructor(private readonly options: BtwSplitPaneOptions) {
125
132
  this.sidePaneRatio = clampSidePaneRatio(options.sidePaneRatio ?? DEFAULT_BTW_SIDE_PANE_RATIO);
126
133
  this.persistedSidePaneRatio = this.sidePaneRatio;
127
- this.mainPane = new MainThreadPane(options.mainThread, options.mainLayout, options.theme, options.terminalRows);
134
+ this.mainPane = new MainThreadPane(
135
+ options.mainThread,
136
+ options.mainLayout,
137
+ options.mainScrollView,
138
+ options.theme,
139
+ options.terminalRows,
140
+ );
141
+ const side = options.sideScrollView ? options.sideLayout : new ScrollView(options.sideLayout, { primary: true });
128
142
  this.viewportRouter = new PaneViewportRouter(
129
- options.sideScrollView ?? findPrimaryScrollView(options.sideLayout),
143
+ options.sideScrollView ?? (side as ScrollView),
130
144
  this.mainPane.getPrimaryScrollView(),
131
145
  options.setViewportTarget,
132
146
  );
133
147
  this.viewportRouter.activate("side");
148
+ this.updateFocusLabel();
134
149
  const separator: Component = {
135
150
  render: (width) =>
136
151
  Array.from({ length: Math.max(1, options.terminalRows()) }, () => truncateToWidth(this.renderDivider(), width)),
137
152
  handleMouse: (event) => this.handleDividerMouse(event),
138
153
  invalidate() {},
139
154
  };
140
- const side = options.sideLayout;
141
155
  const main = this.mainPane.getLayout();
142
156
  this.layoutRoot =
143
157
  options.layout === "left-pane"
@@ -173,7 +187,14 @@ export class BtwSplitPane implements BtwFullscreenLayoutComponent {
173
187
  this.forwardPastedInput(data);
174
188
  return true;
175
189
  }
176
- if (width < MIN_BTW_SPLIT_COLUMNS) return false;
190
+ if (width < MIN_BTW_SPLIT_COLUMNS || isKeyRelease(data)) return false;
191
+ if (this.options.shortcuts?.matches(data, "switchPane")) {
192
+ this.focusGeneration += 1;
193
+ this.cancelDividerDrag();
194
+ this.activatePane(this.activePane === "side" ? "main" : "side");
195
+ return true;
196
+ }
197
+ this.updateFocusLabel();
177
198
  const pane = paneForMouseClick(data, width, this.options.layout, this.sidePaneRatio);
178
199
  if (pane) this.queuePaneFocus(pane);
179
200
  return false;
@@ -215,7 +236,29 @@ export class BtwSplitPane implements BtwFullscreenLayoutComponent {
215
236
  this.focusGeneration += 1;
216
237
  this.dividerDragStartRatio = undefined;
217
238
  this.failedRatioSaveGenerationDuringDrag = undefined;
218
- this.viewportRouter.dispose();
239
+ let error: unknown;
240
+ for (const cleanup of [
241
+ () => this.viewportRouter.dispose(),
242
+ () => this.mainPane.dispose(),
243
+ () => this.setFocusLabel(undefined),
244
+ ]) {
245
+ try {
246
+ cleanup();
247
+ } catch (failure) {
248
+ error ??= failure;
249
+ }
250
+ }
251
+ if (error !== undefined) throw error;
252
+ }
253
+
254
+ private updateFocusLabel(): void {
255
+ const shortcuts = this.options.shortcuts;
256
+ this.setFocusLabel(shortcuts?.keys.switchPane.length ? shortcuts.label("switchPane") : undefined);
257
+ }
258
+
259
+ private setFocusLabel(shortcutLabel: string | undefined): void {
260
+ const component = this.options.sideComponent as BtwFullscreenLayoutComponent;
261
+ component.setWorkspaceFocus?.(this.activePane, shortcutLabel);
219
262
  }
220
263
 
221
264
  private renderDivider(): string {
@@ -290,7 +333,7 @@ export class BtwSplitPane implements BtwFullscreenLayoutComponent {
290
333
  void Promise.resolve()
291
334
  .then(() => persist(ratio))
292
335
  .then(() => {
293
- if (saveGeneration <= this.confirmedRatioSaveGeneration) return;
336
+ if (this.disposed || saveGeneration <= this.confirmedRatioSaveGeneration) return;
294
337
  this.confirmedRatioSaveGeneration = saveGeneration;
295
338
  this.persistedSidePaneRatio = ratio;
296
339
  })
@@ -341,6 +384,7 @@ export class BtwSplitPane implements BtwFullscreenLayoutComponent {
341
384
  private activatePane(pane: BtwActivePane): void {
342
385
  if (this.disposed) return;
343
386
  this.activePane = pane;
387
+ this.updateFocusLabel();
344
388
  this.viewportRouter.activate(pane);
345
389
  this.options.setFocus(pane === "side" ? this.options.sideComponent : this.options.mainInput);
346
390
  this.options.requestRender();
@@ -404,6 +448,7 @@ class ResponsivePaneRow extends HStack {
404
448
  }
405
449
 
406
450
  class MainThreadPane {
451
+ private readonly borrowedScroll: boolean;
407
452
  private readonly body: Component;
408
453
  private readonly layout: Component;
409
454
  private readonly scroll: ScrollView | undefined;
@@ -411,16 +456,19 @@ class MainThreadPane {
411
456
  constructor(
412
457
  mainThread: Component,
413
458
  mainLayout: Component | undefined,
459
+ mainScrollView: ScrollView | undefined,
414
460
  theme: Theme,
415
461
  private readonly terminalRows: () => number,
416
462
  ) {
463
+ this.borrowedScroll = mainLayout !== undefined;
417
464
  this.body = {
418
465
  render: (width) => mainThread.render(Math.max(1, width)).map((line) => truncateToWidth(line, Math.max(1, width))),
419
466
  invalidate() {},
420
467
  };
421
468
  if (mainLayout) {
422
469
  this.layout = mainLayout;
423
- this.scroll = findPrimaryScrollView(mainLayout);
470
+ if (!mainScrollView) throw new Error("The native main pane has no compatible viewport.");
471
+ this.scroll = mainScrollView;
424
472
  return;
425
473
  }
426
474
  this.scroll = new ScrollView(this.body, {
@@ -436,6 +484,12 @@ class MainThreadPane {
436
484
  return this.layout;
437
485
  }
438
486
 
487
+ dispose(): void {
488
+ if (this.borrowedScroll) return;
489
+ this.scroll?.setScrollbarActive(false);
490
+ this.scroll?.setScrollbar("hidden");
491
+ }
492
+
439
493
  getPrimaryScrollView(): ScrollView | undefined {
440
494
  return this.scroll;
441
495
  }
@@ -459,22 +513,46 @@ class PaneViewportRouter {
459
513
  private readonly setViewportTarget: (scrollView: ScrollView | undefined) => void,
460
514
  ) {
461
515
  for (const scrollView of [side, main]) {
462
- if (scrollView) this.originalPrimary.set(scrollView, scrollView.primary);
516
+ if (!scrollView) continue;
517
+ const primary = Object.getOwnPropertyDescriptor(scrollView, "primary");
518
+ if (!primary?.writable || typeof primary.value !== "boolean") {
519
+ throw new Error("Pi BTW cannot route this viewport; update Pi.");
520
+ }
521
+ this.originalPrimary.set(scrollView, scrollView.primary);
463
522
  }
464
523
  }
465
524
 
466
525
  activate(pane: BtwActivePane): void {
467
526
  if (this.disposed) return;
468
527
  const target = pane === "side" ? this.side : this.main;
469
- for (const scrollView of this.originalPrimary.keys()) setScrollViewPrimary(scrollView, scrollView === target);
470
- this.setViewportTarget(target);
528
+ const previous = new Map([...this.originalPrimary.keys()].map((scrollView) => [scrollView, scrollView.primary]));
529
+ try {
530
+ for (const scrollView of this.originalPrimary.keys()) setScrollViewPrimary(scrollView, scrollView === target);
531
+ this.setViewportTarget(target);
532
+ } catch (error) {
533
+ for (const [scrollView, primary] of previous) setScrollViewPrimary(scrollView, primary);
534
+ throw error;
535
+ }
471
536
  }
472
537
 
473
538
  dispose(): void {
474
539
  if (this.disposed) return;
475
540
  this.disposed = true;
476
- for (const [scrollView, primary] of this.originalPrimary) setScrollViewPrimary(scrollView, primary);
477
- this.setViewportTarget(undefined);
541
+ let error: unknown;
542
+ for (const [scrollView, primary] of this.originalPrimary) {
543
+ try {
544
+ setScrollViewPrimary(scrollView, primary);
545
+ } catch (failure) {
546
+ error ??= failure;
547
+ }
548
+ }
549
+ this.originalPrimary.clear();
550
+ try {
551
+ this.setViewportTarget(undefined);
552
+ } catch (failure) {
553
+ error ??= failure;
554
+ }
555
+ if (error !== undefined) throw error;
478
556
  }
479
557
  }
480
558
 
@@ -482,25 +560,7 @@ class PaneViewportRouter {
482
560
  // active-pane viewport router. Preserve and restore this runtime field so Pi's
483
561
  // native search, prompt navigation, and scrolling keep their standard behavior.
484
562
  function setScrollViewPrimary(scrollView: ScrollView, primary: boolean): void {
485
- Reflect.set(scrollView, "primary", primary);
486
- }
487
-
488
- function findPrimaryScrollView(root: Component): ScrollView | undefined {
489
- const visited = new Set<Component>();
490
- let fallback: ScrollView | undefined;
491
- let primary: ScrollView | undefined;
492
- const visit = (component: Component) => {
493
- if (visited.has(component)) return;
494
- visited.add(component);
495
- if (component instanceof ScrollView) {
496
- fallback ??= component;
497
- if (component.primary) primary = component;
498
- }
499
- if (!("children" in component) || !Array.isArray(component.children)) return;
500
- for (const child of component.children) visit(child);
501
- };
502
- visit(root);
503
- return primary ?? fallback;
563
+ if (!Reflect.set(scrollView, "primary", primary)) throw new Error("Pi BTW could not restore viewport ownership.");
504
564
  }
505
565
 
506
566
  function paneForMouseClick(