pi-stereo-view 0.1.17 → 0.1.19

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/src/layout.ts CHANGED
@@ -10,6 +10,8 @@ import { NestedCalls } from "./nested-calls.ts";
10
10
  import type { Preferences } from "./preferences.ts";
11
11
  import type { TranscriptTimestamps } from "./timestamps.ts";
12
12
  import { PANEL_SEARCH_ROWS, PanelSearch, PanelSearchShortcut, type SearchHit } from "./search.ts";
13
+ import { SplitSurface } from "./surface.ts";
14
+ import { sameValues } from "./render-cache.ts";
13
15
 
14
16
  type NativeTui = TUI & {
15
17
  layoutRoot?: Container;
@@ -21,6 +23,23 @@ const MIN_ACTIVITY = 40;
21
23
  const MIN_ROWS = 4;
22
24
  const TOOLBAR_BUTTON_WIDTH = 6;
23
25
 
26
+ /**
27
+ * A control that renders again only when one of its inputs changes. Every spinner frame lays out
28
+ * all toolbar and panel controls; restyling unchanged labels would cost more than the panels themselves.
29
+ */
30
+ function memoized(inputs: (width: number) => unknown[], render: (width: number) => string[]): Component {
31
+ let last: { inputs: unknown[]; lines: string[] } | undefined;
32
+ return {
33
+ render: width => {
34
+ const current = inputs(width);
35
+ if (last && sameValues(last.inputs, current)) return last.lines;
36
+ last = { inputs: current, lines: render(width) };
37
+ return last.lines;
38
+ },
39
+ invalidate: () => { last = undefined; },
40
+ };
41
+ }
42
+
24
43
  class PanelScrollView extends ScrollView {
25
44
  constructor(component: Component, options: ScrollViewOptions, private readonly interact: () => void, active: () => boolean) {
26
45
  super(component, options);
@@ -95,6 +114,8 @@ class ButtonStack extends VStack {
95
114
  return entry;
96
115
  });
97
116
  if (this.fill && !row.includes(entries[0])) spaced.push({ component: this.fill, basis: 0, grow: 1 });
117
+ // Keep the last label off the panel's right edge, where the scrollbar sits.
118
+ if (this.fill) spaced.push({ component: this.fill, basis: 1, shrink: 0 });
98
119
  this.addChild(new HStack(spaced), { basis: 1, shrink: 0 });
99
120
  }
100
121
  row = []; occupied = 0;
@@ -102,14 +123,16 @@ class ButtonStack extends VStack {
102
123
  for (const [index, entry] of entries.entries()) {
103
124
  const control = this.controls.find(candidate => candidate.index === index);
104
125
  const size = control ? textWidth(index) : this.titleLength ?? 0;
105
- if (row.length && occupied + 1 + size > width) flush();
126
+ if (row.length && occupied + 1 + size > width - (this.fill ? 1 : 0)) flush();
106
127
  occupied += (row.length ? 1 : 0) + size;
107
128
  row.push(entry);
108
129
  }
109
130
  flush();
110
131
  }
111
132
  }
112
- return super.render(width);
133
+ // Pi and the split surface lay out these rows themselves and read render only for the height.
134
+ // Rendering the rows here would composite every button into a full-width row on every frame.
135
+ return Array<string>(this.children.length).fill("");
113
136
  }
114
137
  }
115
138
  class TranscriptColumns extends HStack {
@@ -135,11 +158,17 @@ class TranscriptRows extends VStack {
135
158
  class TranscriptRoot extends VStack {
136
159
  private readonly owned: Component[];
137
160
 
138
- constructor(children: StackChild[]) {
161
+ constructor(children: StackChild[], private readonly unhandled: (event: TuiMouseEvent) => void) {
139
162
  super(children);
140
163
  this.owned = [...this.children];
141
164
  }
142
165
 
166
+ override handleMouse(event: TuiMouseEvent): ReturnType<Container["handleMouse"]> {
167
+ // Pi offers the root only events no component handled, such as pointer moves outside the split panels.
168
+ this.unhandled(event);
169
+ return undefined;
170
+ }
171
+
143
172
  unchanged(): boolean {
144
173
  return this.children.length === this.owned.length && this.children.every((child, index) => child === this.owned[index]);
145
174
  }
@@ -192,6 +221,7 @@ export class TranscriptLayout {
192
221
  private activity?: ScrollView;
193
222
  private chat?: ScrollView;
194
223
  private documents?: Record<Pane, NativeDocument>;
224
+ private surface?: SplitSurface;
195
225
  private disposeDocuments?: () => void;
196
226
  private activePane: Pane = "chat";
197
227
  private readonly searchShortcut = new PanelSearchShortcut();
@@ -223,12 +253,10 @@ export class TranscriptLayout {
223
253
  ) {}
224
254
 
225
255
  private button(label: () => string, action: () => void, enabled = (): boolean => true, background?: () => Color): Component {
226
- const button = new MouseRegion({
227
- render: width => new Text(this.theme().style((this.preferences.buttonStyle === "icons" ? "[ " + label() + " ]" : "[" + label() + "]").padEnd(width), {
256
+ const button = new MouseRegion(memoized(width => [width, this.theme(), this.preferences.buttonStyle, label(), enabled(), background?.()],
257
+ width => new Text(this.theme().style((this.preferences.buttonStyle === "icons" ? "[ " + label() + " ]" : "[" + label() + "]").padEnd(width), {
228
258
  fg: enabled() ? "accent" : "dim", bg: background?.(),
229
- }), 0, 0).render(width),
230
- invalidate() {},
231
- }, event => {
259
+ }), 0, 0).render(width)), event => {
232
260
  if (event.button !== "left") return;
233
261
  if (event.type === "press") return { capture: true };
234
262
  if (event.type === "click" && enabled() && event.x >= 0 && event.x < event.width && event.y === 0) {
@@ -318,7 +346,10 @@ export class TranscriptLayout {
318
346
  const index = new NativeContentIndex(document, this.nested);
319
347
  const activityDocument = new NativeDocument(document, "activity", this.timestamps, this.theme, timestampGutter, showTimestamps,
320
348
  () => this.activate("activity"), false, index, this.copy, showCopyButtons, () => this.preferences.compactTools, this.getTools,
321
- () => { if (this.activity) this.activity.scrollTo(this.activity.scrollTop, { disableFollow: true }); }, () => this.preferences.buttonStyle);
349
+ () => { if (this.activity) this.activity.scrollTo(this.activity.scrollTop, { disableFollow: true }); }, () => this.preferences.buttonStyle, {
350
+ enabled: () => this.preferences.streamTools, delay: () => this.preferences.streamDelay * 1000,
351
+ changed: () => this.host?.requestRender(),
352
+ });
322
353
  const chatDocument = new NativeDocument(document, "chat", this.timestamps, this.theme, timestampGutter, showTimestamps,
323
354
  () => this.activate("chat"), false, index, this.copy, showCopyButtons, undefined, undefined, undefined, () => this.preferences.buttonStyle);
324
355
  this.documents = { activity: activityDocument, chat: chatDocument };
@@ -337,6 +368,11 @@ export class TranscriptLayout {
337
368
  this.activity = new PanelScrollView(activityDocument, scrollOptions, () => this.activate("activity"),
338
369
  () => this.searchPane() === "activity");
339
370
  this.chat = new PanelScrollView(chatDocument, scrollOptions, () => this.activate("chat"), () => this.searchPane() === "chat");
371
+ // Each document renders only the history near its panel's viewport from the previous layout.
372
+ for (const [document, view] of [[activityDocument, this.activity], [chatDocument, this.chat]] as const) {
373
+ document.setViewport(() => view.viewportHeight ?
374
+ { top: view.scrollTop, height: view.viewportHeight, following: view.isFollowingEnd } : undefined, () => this.host?.requestRender());
375
+ }
340
376
  let historyHeight = 0;
341
377
  let chatHeader: Component;
342
378
  let activityHeader: Component;
@@ -425,7 +461,7 @@ export class TranscriptLayout {
425
461
  () => this.preferences.buttonStyle === "icons" ? (this.preferences.layout === "columns" ? "↔" : "↕") : "Swap", () => this.swap(),
426
462
  () => this.preferences.mode === "split" && !this.preferences.zoom && !this.fallback && !this.failed,
427
463
  ), basis: TOOLBAR_BUTTON_WIDTH + 1, minSize: TOOLBAR_BUTTON_WIDTH, shrink: 1 },
428
- { component: this.button(() => this.preferences.buttonStyle === "icons" ? "≡" : "Menu", this.openSettings), basis: TOOLBAR_BUTTON_WIDTH, shrink: 0 },
464
+ { component: this.button(() => this.preferences.buttonStyle === "icons" ? "≡" : "Settings", this.openSettings), basis: TOOLBAR_BUTTON_WIDTH, shrink: 0 },
429
465
  ], [
430
466
  { index: 0, icon: 6 },
431
467
  { index: 1, icon: 6 },
@@ -434,25 +470,25 @@ export class TranscriptLayout {
434
470
  ]);
435
471
  const pane = (title: string, target: Pane, view: ScrollView): Component => {
436
472
  const hue = target === "chat" ? rgbColor(104, 158, 115) : rgbColor(107, 152, 190);
473
+ let colors: { inputs: unknown[]; palette: { fill: Color; text: Color; border: Color } } | undefined;
474
+ // Mixed colors keep their identity while the theme is unchanged, so memoized controls can compare them.
437
475
  const palette = () => {
438
476
  const theme = this.theme();
439
- return {
477
+ const inputs = [theme, theme.colors.customMessageBg, theme.colors.text, theme.colors.border];
478
+ if (!colors || !sameValues(colors.inputs, inputs)) colors = { inputs, palette: {
440
479
  fill: mixColors(theme.colors.customMessageBg, hue, 0.15, "srgb"),
441
480
  text: mixColors(theme.colors.text, hue, 0.25),
442
481
  border: mixColors(theme.colors.border, hue, 0.3),
443
- };
444
- };
445
- const line: Component = {
446
- render: width => [this.theme().style("─".repeat(width), { fg: palette().border })],
447
- invalidate() {},
482
+ } };
483
+ return colors.palette;
448
484
  };
485
+ const line = memoized(width => [width, this.theme(), palette()],
486
+ width => [this.theme().style("─".repeat(width), { fg: palette().border })]);
449
487
  const label = new ButtonStack([
450
- { component: new MouseRegion({
451
- render: width => new Text(this.theme().style(truncateToWidth(width < title.length + 3 ? title : " " + (this.searchPane() === target ? "● " : " ") + title, width, "").padEnd(width), {
488
+ { component: new MouseRegion(memoized(width => [width, this.theme(), palette(), this.searchPane() === target],
489
+ width => new Text(this.theme().style(truncateToWidth(width < title.length + 3 ? title : " " + (this.searchPane() === target ? "● " : " ") + title, width, "").padEnd(width), {
452
490
  fg: this.searchPane() === target ? "accent" : palette().text, bg: palette().fill,
453
- }), 0, 0).render(width),
454
- invalidate() {},
455
- }, event => {
491
+ }), 0, 0).render(width)), event => {
456
492
  if (event.button === "left" && (event.type === "press" || event.type === "click")) {
457
493
  this.activate(target);
458
494
  return { handled: true, render: true };
@@ -481,10 +517,8 @@ export class TranscriptLayout {
481
517
  ] : [
482
518
  { index: 1, icon: 6 },
483
519
  { index: 2, icon: 5 },
484
- ], title.length, {
485
- render: width => [this.theme().style(" ".repeat(width), { bg: palette().fill })],
486
- invalidate() {},
487
- });
520
+ ], title.length, memoized(width => [width, this.theme(), palette()],
521
+ width => [this.theme().style(" ".repeat(width), { bg: palette().fill })]));
488
522
  headers.push(label);
489
523
  const header = new VStack([
490
524
  { component: line, basis: 1, shrink: 0, visible: () =>
@@ -502,12 +536,10 @@ export class TranscriptLayout {
502
536
  };
503
537
  const activityPane = pane("Activity", "activity", this.activity);
504
538
  const chatPane = pane("Conversation", "chat", this.chat);
505
- const divider = (axis: "columns" | "rows"): Component => new MouseRegion({
506
- render: width => axis === "columns" ?
539
+ const divider = (axis: "columns" | "rows"): Component => new MouseRegion(memoized(width => [width, host.terminal.rows, this.theme()],
540
+ width => axis === "columns" ?
507
541
  Array.from({ length: host.terminal.rows }, () => this.theme().fg("dim", "║")) :
508
- [this.theme().fg("dim", "═".repeat(width))],
509
- invalidate() {},
510
- }, event => {
542
+ [this.theme().fg("dim", "═".repeat(width))]), event => {
511
543
  if (event.button !== "left" && event.type !== "release") return;
512
544
  const position = axis === "columns" ? event.screenX : event.screenY;
513
545
  if (event.type === "press") {
@@ -559,32 +591,42 @@ export class TranscriptLayout {
559
591
  }
560
592
  };
561
593
  this.rebuild();
594
+ const surface = new SplitSurface(host, {
595
+ model: () => {
596
+ const zoom = this.preferences.zoom;
597
+ if (zoom) return { component: zoom === "chat" ? chatPane : activityPane, fill: false, minSize: MIN_ROWS };
598
+ return this.preferences.layout === "columns" ? { component: columns, fill: true, minSize: 1 } : { component: rows, fill: false, minSize: 1 };
599
+ },
600
+ height: () => historyHeight,
601
+ active: () => {
602
+ const pane = this.searchPane();
603
+ return pane === "chat" ? this.chat : pane === "activity" ? this.activity : undefined;
604
+ },
605
+ });
606
+ surface.setModels([columns, rows, activityPane, chatPane]);
607
+ this.surface = surface;
562
608
  const mounted = new TranscriptRoot([
563
609
  { component: toolbar, basis: "auto", shrink: 0, visible: () => {
564
610
  // Root visibility is evaluated once at the start of each native layout pass.
565
611
  pass = undefined;
566
612
  this.timestamps.beginFrame();
567
- activityDocument.beginFrame();
568
- chatDocument.beginFrame();
613
+ index.beginFrame();
614
+ activityDocument.beginFrame(true);
615
+ chatDocument.beginFrame(true);
569
616
  return true;
570
617
  } },
571
618
  { component: {
572
619
  render: width => [this.theme().style("─".repeat(width), { fg: this.theme().colors.border })],
573
620
  invalidate() {},
574
621
  }, basis: 1, shrink: 0, visible: viewport => !shown(viewport) },
575
- { component: columns, basis: 0, grow: 1, minSize: 1,
576
- visible: viewport => shown(viewport) && !this.preferences.zoom && this.preferences.layout === "columns" },
577
- { component: rows, basis: "auto", minSize: 1,
578
- visible: viewport => shown(viewport) && !this.preferences.zoom && this.preferences.layout === "rows" },
579
- { component: activityPane, basis: "auto", minSize: MIN_ROWS,
580
- visible: viewport => shown(viewport) && this.preferences.zoom === "activity" },
581
- { component: chatPane, basis: "auto", minSize: MIN_ROWS,
582
- visible: viewport => shown(viewport) && this.preferences.zoom === "chat" },
622
+ // The split paints as one full-width row block; the proxy gives Pi's keyboard scrolling the active panel.
623
+ { component: surface, basis: "auto", minSize: 1, visible: viewport => shown(viewport) },
624
+ { component: surface.proxy, basis: 0, shrink: 0, visible: viewport => shown(viewport) },
583
625
  { component: transcript, basis: 0, grow: 1, minSize: 1, visible: viewport => !shown(viewport) },
584
626
  ...(projectedDock ? [{ component: projectedDock, basis: "auto" as const, shrink: 0, minSize: 1,
585
627
  visible: () => projectSurface }] : []),
586
628
  { component: dock, basis: "auto", shrink: 0, minSize: 1, visible: () => !projectSurface },
587
- ]);
629
+ ], event => surface.unhandled(event));
588
630
  this.mounted = mounted;
589
631
  host.setLayoutRoot(mounted);
590
632
  host.requestRender();
@@ -744,6 +786,17 @@ export class TranscriptLayout {
744
786
  this.preferences.compactTools = compact;
745
787
  this.changed();
746
788
  }
789
+ /** Show the latest compact tool calls in full until they have been finished for the delay. */
790
+ setStreamTools(stream: boolean): void {
791
+ this.preferences.streamTools = stream;
792
+ this.changed();
793
+ }
794
+
795
+ /** Seconds a finished streamed call stays in full before collapsing. */
796
+ setStreamDelay(seconds: number): void {
797
+ this.preferences.streamDelay = Math.max(1, Math.min(300, Math.trunc(seconds)));
798
+ this.changed();
799
+ }
747
800
  private defaultWidth(width: number): number {
748
801
  return Math.max(MIN_CHAT, Math.floor((width - 1) * 0.2));
749
802
  }
@@ -830,6 +883,8 @@ export class TranscriptLayout {
830
883
  this.searchShortcut.dispose();
831
884
  this.disposeDocuments?.();
832
885
  this.disposeDocuments = undefined;
886
+ this.surface?.dispose();
887
+ this.surface = undefined;
833
888
  const host = this.host;
834
889
  this.activity?.setScrollbar("hidden");
835
890
  this.chat?.setScrollbar("hidden");