phirepass-widgets 0.0.105 → 0.0.106

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.
@@ -240,6 +240,24 @@ function control_char(key) {
240
240
  return '\u007f';
241
241
  return null;
242
242
  }
243
+ /**
244
+ * How many frames to keep re-trying a fit before leaving it to the observer.
245
+ *
246
+ * Generous, because what is being waited on is a font load and a layout pass,
247
+ * not a network round trip — and cheap, because each attempt is one measurement
248
+ * that stops the moment it succeeds.
249
+ */
250
+ const FIT_MAX_ATTEMPTS = 60;
251
+ /**
252
+ * How much of the container the session may leave unfilled and still count as
253
+ * fitted.
254
+ *
255
+ * There is always a remainder: rows are whole, so the last partial row's worth
256
+ * of pixels belongs to nobody. One cell is normal and invisible; half a panel is
257
+ * the bug this guards. Sized above any realistic cell height so a correctly
258
+ * fitted terminal is never retried, and far below the gap a stale fit leaves.
259
+ */
260
+ const FIT_SLACK_PX = 48;
243
261
  /** The bar, left to right. `label` is what is drawn; `hint` is for a screen reader. */
244
262
  const KEY_BAR_KEYS = [
245
263
  { id: 'esc', label: 'esc', hint: 'Escape', data: '\u001b' },
@@ -269,6 +287,7 @@ const PhirepassTerminal = class {
269
287
  inputMode = protocol.InputMode.Default;
270
288
  resizeObserver;
271
289
  resizeDebounceHandle;
290
+ fitRetryHandle;
272
291
  session_id;
273
292
  usernameBuffer = "";
274
293
  passwordBuffer = "";
@@ -403,6 +422,10 @@ const PhirepassTerminal = class {
403
422
  clearTimeout(this.resizeDebounceHandle);
404
423
  this.resizeDebounceHandle = undefined;
405
424
  }
425
+ if (this.fitRetryHandle !== undefined) {
426
+ cancelAnimationFrame(this.fitRetryHandle);
427
+ this.fitRetryHandle = undefined;
428
+ }
406
429
  if (this.resizeObserver) {
407
430
  this.resizeObserver.disconnect();
408
431
  }
@@ -415,16 +438,76 @@ const PhirepassTerminal = class {
415
438
  is_terminal_open() {
416
439
  return Boolean(this.connected && this.containerEl && this.terminal.element);
417
440
  }
441
+ /**
442
+ * Size the session to its container, and **keep trying until it works**.
443
+ *
444
+ * The bug this replaces: a fit could be skipped — the renderer not ready
445
+ * yet, `connected` not yet set, `fit()` throwing on dimensions that do not
446
+ * exist for another frame — and nothing ever retried it. The only other
447
+ * thing that fits is the `ResizeObserver`, so a terminal opened into a
448
+ * container that never changes size again stayed at whatever number of rows
449
+ * it happened to have. On a panel the user does not resize, that is
450
+ * permanent: the session renders at a fraction of the height it was given
451
+ * and the dead space below it is not part of the terminal at all.
452
+ *
453
+ * Retrying on `requestAnimationFrame` rather than a timer because what is
454
+ * being waited for *is* a frame: xterm computes its cell metrics from a
455
+ * measured element, and that measurement is only meaningful once the
456
+ * browser has laid the container out and the font it will actually use has
457
+ * loaded. The attempt cap keeps a container that is genuinely zero-sized —
458
+ * a hidden tab, a collapsed panel — from spinning a frame loop forever;
459
+ * the observer will call this again when it gains a size.
460
+ */
461
+ schedule_fit() {
462
+ if (this.fitRetryHandle !== undefined) {
463
+ cancelAnimationFrame(this.fitRetryHandle);
464
+ this.fitRetryHandle = undefined;
465
+ }
466
+ let attempts = 0;
467
+ const attempt = () => {
468
+ this.fitRetryHandle = undefined;
469
+ if (this.fit_terminal_safely()) {
470
+ return;
471
+ }
472
+ attempts += 1;
473
+ if (attempts >= FIT_MAX_ATTEMPTS) {
474
+ console.warn('Giving up fitting the terminal; waiting for a resize instead');
475
+ return;
476
+ }
477
+ this.fitRetryHandle = requestAnimationFrame(attempt);
478
+ };
479
+ attempt();
480
+ }
481
+ /**
482
+ * One attempt. `true` when the session is now sized to its container.
483
+ *
484
+ * A container with no height yet counts as a failure rather than a success,
485
+ * which is what makes the retry above worth having: `fit()` on a zero-height
486
+ * parent does not throw, it computes one row and returns happily.
487
+ */
418
488
  fit_terminal_safely() {
419
489
  if (!this.fitAddon || !this.is_terminal_open()) {
420
- return;
490
+ return false;
491
+ }
492
+ const height = this.containerEl?.clientHeight ?? 0;
493
+ const width = this.containerEl?.clientWidth ?? 0;
494
+ if (height <= 0 || width <= 0) {
495
+ return false;
421
496
  }
422
497
  try {
423
498
  this.fitAddon.fit();
424
499
  }
425
500
  catch (err) {
426
501
  console.warn('Skipping terminal fit before renderer is ready:', err);
502
+ return false;
427
503
  }
504
+ // Fitted, but to the right thing? `proposeDimensions` can be answered
505
+ // from stale cell metrics — most often when a web font finishes loading
506
+ // after the first measurement — and the symptom is a session that fills
507
+ // half its panel. Compare what we got against what there was to fill.
508
+ const rows = this.terminal?.rows ?? 0;
509
+ const rendered = this.terminal.element?.clientHeight ?? 0;
510
+ return rows > 0 && rendered > 0 && height - rendered < FIT_SLACK_PX;
428
511
  }
429
512
  try_connect() {
430
513
  if (this.connected || !this.domReady || !this.runtimeReady) {
@@ -612,7 +695,7 @@ const PhirepassTerminal = class {
612
695
  handle_tunnel_opened(web) {
613
696
  this.session_id = web.sid;
614
697
  this.terminal.reset();
615
- this.fit_terminal_safely();
698
+ this.schedule_fit();
616
699
  this.send_ssh_terminal_resize();
617
700
  }
618
701
  handle_tunnel_data(web) {
@@ -633,14 +716,42 @@ const PhirepassTerminal = class {
633
716
  this.terminal.open(container);
634
717
  console.log('Terminal opened in container');
635
718
  this.connected = true;
636
- this.fit_terminal_safely();
719
+ // Scheduled, not attempted once: `open()` has only just handed the
720
+ // renderer its element, so the first measurement is routinely the
721
+ // one that is too early.
722
+ this.schedule_fit();
637
723
  this.terminal.focus();
638
724
  this.terminal.onData(this.handle_terminal_data.bind(this));
639
725
  this.channel.connect();
640
726
  this.setup_resize_observer();
727
+ this.refit_when_fonts_settle();
641
728
  console.log('Terminal connected and ready');
642
729
  }
643
730
  }
731
+ /**
732
+ * Fit again once the web fonts have actually loaded.
733
+ *
734
+ * xterm measures one character to derive its cell size, and if that
735
+ * measurement happens while the browser is still falling back — "Berkeley
736
+ * Mono" and "Fira Code" are both remote — every row is sized from the wrong
737
+ * font. The container does not change size when the real font arrives, so
738
+ * the `ResizeObserver` never fires and nothing else would ever correct it.
739
+ *
740
+ * Guarded because `document.fonts` is absent in some embedding contexts, and
741
+ * a missing font API is not a reason to fail to open a shell.
742
+ */
743
+ refit_when_fonts_settle() {
744
+ const fonts = document.fonts;
745
+ if (!fonts?.ready) {
746
+ return;
747
+ }
748
+ void fonts.ready.then(() => {
749
+ if (this.connected) {
750
+ this.schedule_fit();
751
+ this.send_ssh_terminal_resize();
752
+ }
753
+ });
754
+ }
644
755
  setup_resize_observer() {
645
756
  this.resizeObserver = new ResizeObserver(() => {
646
757
  console.log('Container resized, fitting terminal');
@@ -648,7 +759,7 @@ const PhirepassTerminal = class {
648
759
  clearTimeout(this.resizeDebounceHandle);
649
760
  }
650
761
  this.resizeDebounceHandle = setTimeout(() => {
651
- this.fit_terminal_safely();
762
+ this.schedule_fit();
652
763
  this.send_ssh_terminal_resize();
653
764
  }, 100);
654
765
  });
@@ -831,13 +942,13 @@ const PhirepassTerminal = class {
831
942
  return (index.h("button", { key: id, type: "button", class: { key: true, armed: !!armed }, "aria-label": hint, "aria-pressed": armed === undefined ? undefined : String(armed), onPointerDown: (event) => event.preventDefault(), onMouseDown: (event) => event.preventDefault(), onClick: onPress }, label));
832
943
  }
833
944
  render() {
834
- return (index.h(index.Host, { key: '60fd152dea1b043f2b0f4cd1cea72172aed394ad', class: { 'keys-on': this.keyBar === 'on', 'keys-off': this.keyBar === 'off' } }, index.h("div", { key: 'd0b1c920b13ce3aa264d4602ed2af57fed706abd', id: "ccc", ref: el => (this.containerEl = el) }), index.h("div", { key: '3013a54597fc7ae293408bbf7924270699b1e636', class: "keys", role: "toolbar", "aria-label": "Terminal keys" }, this.key_button('ctrl', 'ctrl', 'Control', () => {
945
+ return (index.h(index.Host, { key: 'a4e7018d85dc6ac27abfb46e199c1d2d947e57c6', class: { 'keys-on': this.keyBar === 'on', 'keys-off': this.keyBar === 'off' } }, index.h("div", { key: '0603c1fd65d7c70c0d48d45c4b85a7ad14c913bf', id: "ccc", ref: el => (this.containerEl = el) }), index.h("div", { key: '30bc8d4e31816e3c83d422133bbe9c1ced323141', class: "keys", role: "toolbar", "aria-label": "Terminal keys" }, this.key_button('ctrl', 'ctrl', 'Control', () => {
835
946
  this.ctrlArmed = !this.ctrlArmed;
836
947
  this.terminal?.focus();
837
948
  }, this.ctrlArmed), this.key_button('alt', 'alt', 'Alt', () => {
838
949
  this.altArmed = !this.altArmed;
839
950
  this.terminal?.focus();
840
- }, this.altArmed), KEY_BAR_KEYS.map(entry => this.key_button(entry.id, entry.label, entry.hint, () => this.press(entry.data))), index.h("span", { key: '636c553d673c3fd7e4f38278fbda7df5bcdcb06f', class: "spacer" }), this.key_button('left', '←', 'Left arrow', () => this.press(this.arrow('D'))), this.key_button('down', '↓', 'Down arrow', () => this.press(this.arrow('B'))), this.key_button('up', '↑', 'Up arrow', () => this.press(this.arrow('A'))), this.key_button('right', '→', 'Right arrow', () => this.press(this.arrow('C'))))));
951
+ }, this.altArmed), KEY_BAR_KEYS.map(entry => this.key_button(entry.id, entry.label, entry.hint, () => this.press(entry.data))), index.h("span", { key: '117e1718e8f4bc7451dd95222733ef3f841600db', class: "spacer" }), this.key_button('left', '←', 'Left arrow', () => this.press(this.arrow('D'))), this.key_button('down', '↓', 'Down arrow', () => this.press(this.arrow('B'))), this.key_button('up', '↑', 'Up arrow', () => this.press(this.arrow('A'))), this.key_button('right', '→', 'Right arrow', () => this.press(this.arrow('C'))))));
841
952
  }
842
953
  static get watchers() { return {
843
954
  "nodeId": [{
@@ -32,6 +32,24 @@ function control_char(key) {
32
32
  return '\u007f';
33
33
  return null;
34
34
  }
35
+ /**
36
+ * How many frames to keep re-trying a fit before leaving it to the observer.
37
+ *
38
+ * Generous, because what is being waited on is a font load and a layout pass,
39
+ * not a network round trip — and cheap, because each attempt is one measurement
40
+ * that stops the moment it succeeds.
41
+ */
42
+ const FIT_MAX_ATTEMPTS = 60;
43
+ /**
44
+ * How much of the container the session may leave unfilled and still count as
45
+ * fitted.
46
+ *
47
+ * There is always a remainder: rows are whole, so the last partial row's worth
48
+ * of pixels belongs to nobody. One cell is normal and invisible; half a panel is
49
+ * the bug this guards. Sized above any realistic cell height so a correctly
50
+ * fitted terminal is never retried, and far below the gap a stale fit leaves.
51
+ */
52
+ const FIT_SLACK_PX = 48;
35
53
  /** The bar, left to right. `label` is what is drawn; `hint` is for a screen reader. */
36
54
  const KEY_BAR_KEYS = [
37
55
  { id: 'esc', label: 'esc', hint: 'Escape', data: '\u001b' },
@@ -57,6 +75,7 @@ export class PhirepassTerminal {
57
75
  inputMode = InputMode.Default;
58
76
  resizeObserver;
59
77
  resizeDebounceHandle;
78
+ fitRetryHandle;
60
79
  session_id;
61
80
  usernameBuffer = "";
62
81
  passwordBuffer = "";
@@ -191,6 +210,10 @@ export class PhirepassTerminal {
191
210
  clearTimeout(this.resizeDebounceHandle);
192
211
  this.resizeDebounceHandle = undefined;
193
212
  }
213
+ if (this.fitRetryHandle !== undefined) {
214
+ cancelAnimationFrame(this.fitRetryHandle);
215
+ this.fitRetryHandle = undefined;
216
+ }
194
217
  if (this.resizeObserver) {
195
218
  this.resizeObserver.disconnect();
196
219
  }
@@ -203,16 +226,76 @@ export class PhirepassTerminal {
203
226
  is_terminal_open() {
204
227
  return Boolean(this.connected && this.containerEl && this.terminal.element);
205
228
  }
229
+ /**
230
+ * Size the session to its container, and **keep trying until it works**.
231
+ *
232
+ * The bug this replaces: a fit could be skipped — the renderer not ready
233
+ * yet, `connected` not yet set, `fit()` throwing on dimensions that do not
234
+ * exist for another frame — and nothing ever retried it. The only other
235
+ * thing that fits is the `ResizeObserver`, so a terminal opened into a
236
+ * container that never changes size again stayed at whatever number of rows
237
+ * it happened to have. On a panel the user does not resize, that is
238
+ * permanent: the session renders at a fraction of the height it was given
239
+ * and the dead space below it is not part of the terminal at all.
240
+ *
241
+ * Retrying on `requestAnimationFrame` rather than a timer because what is
242
+ * being waited for *is* a frame: xterm computes its cell metrics from a
243
+ * measured element, and that measurement is only meaningful once the
244
+ * browser has laid the container out and the font it will actually use has
245
+ * loaded. The attempt cap keeps a container that is genuinely zero-sized —
246
+ * a hidden tab, a collapsed panel — from spinning a frame loop forever;
247
+ * the observer will call this again when it gains a size.
248
+ */
249
+ schedule_fit() {
250
+ if (this.fitRetryHandle !== undefined) {
251
+ cancelAnimationFrame(this.fitRetryHandle);
252
+ this.fitRetryHandle = undefined;
253
+ }
254
+ let attempts = 0;
255
+ const attempt = () => {
256
+ this.fitRetryHandle = undefined;
257
+ if (this.fit_terminal_safely()) {
258
+ return;
259
+ }
260
+ attempts += 1;
261
+ if (attempts >= FIT_MAX_ATTEMPTS) {
262
+ console.warn('Giving up fitting the terminal; waiting for a resize instead');
263
+ return;
264
+ }
265
+ this.fitRetryHandle = requestAnimationFrame(attempt);
266
+ };
267
+ attempt();
268
+ }
269
+ /**
270
+ * One attempt. `true` when the session is now sized to its container.
271
+ *
272
+ * A container with no height yet counts as a failure rather than a success,
273
+ * which is what makes the retry above worth having: `fit()` on a zero-height
274
+ * parent does not throw, it computes one row and returns happily.
275
+ */
206
276
  fit_terminal_safely() {
207
277
  if (!this.fitAddon || !this.is_terminal_open()) {
208
- return;
278
+ return false;
279
+ }
280
+ const height = this.containerEl?.clientHeight ?? 0;
281
+ const width = this.containerEl?.clientWidth ?? 0;
282
+ if (height <= 0 || width <= 0) {
283
+ return false;
209
284
  }
210
285
  try {
211
286
  this.fitAddon.fit();
212
287
  }
213
288
  catch (err) {
214
289
  console.warn('Skipping terminal fit before renderer is ready:', err);
290
+ return false;
215
291
  }
292
+ // Fitted, but to the right thing? `proposeDimensions` can be answered
293
+ // from stale cell metrics — most often when a web font finishes loading
294
+ // after the first measurement — and the symptom is a session that fills
295
+ // half its panel. Compare what we got against what there was to fill.
296
+ const rows = this.terminal?.rows ?? 0;
297
+ const rendered = this.terminal.element?.clientHeight ?? 0;
298
+ return rows > 0 && rendered > 0 && height - rendered < FIT_SLACK_PX;
216
299
  }
217
300
  try_connect() {
218
301
  if (this.connected || !this.domReady || !this.runtimeReady) {
@@ -400,7 +483,7 @@ export class PhirepassTerminal {
400
483
  handle_tunnel_opened(web) {
401
484
  this.session_id = web.sid;
402
485
  this.terminal.reset();
403
- this.fit_terminal_safely();
486
+ this.schedule_fit();
404
487
  this.send_ssh_terminal_resize();
405
488
  }
406
489
  handle_tunnel_data(web) {
@@ -421,14 +504,42 @@ export class PhirepassTerminal {
421
504
  this.terminal.open(container);
422
505
  console.log('Terminal opened in container');
423
506
  this.connected = true;
424
- this.fit_terminal_safely();
507
+ // Scheduled, not attempted once: `open()` has only just handed the
508
+ // renderer its element, so the first measurement is routinely the
509
+ // one that is too early.
510
+ this.schedule_fit();
425
511
  this.terminal.focus();
426
512
  this.terminal.onData(this.handle_terminal_data.bind(this));
427
513
  this.channel.connect();
428
514
  this.setup_resize_observer();
515
+ this.refit_when_fonts_settle();
429
516
  console.log('Terminal connected and ready');
430
517
  }
431
518
  }
519
+ /**
520
+ * Fit again once the web fonts have actually loaded.
521
+ *
522
+ * xterm measures one character to derive its cell size, and if that
523
+ * measurement happens while the browser is still falling back — "Berkeley
524
+ * Mono" and "Fira Code" are both remote — every row is sized from the wrong
525
+ * font. The container does not change size when the real font arrives, so
526
+ * the `ResizeObserver` never fires and nothing else would ever correct it.
527
+ *
528
+ * Guarded because `document.fonts` is absent in some embedding contexts, and
529
+ * a missing font API is not a reason to fail to open a shell.
530
+ */
531
+ refit_when_fonts_settle() {
532
+ const fonts = document.fonts;
533
+ if (!fonts?.ready) {
534
+ return;
535
+ }
536
+ void fonts.ready.then(() => {
537
+ if (this.connected) {
538
+ this.schedule_fit();
539
+ this.send_ssh_terminal_resize();
540
+ }
541
+ });
542
+ }
432
543
  setup_resize_observer() {
433
544
  this.resizeObserver = new ResizeObserver(() => {
434
545
  console.log('Container resized, fitting terminal');
@@ -436,7 +547,7 @@ export class PhirepassTerminal {
436
547
  clearTimeout(this.resizeDebounceHandle);
437
548
  }
438
549
  this.resizeDebounceHandle = setTimeout(() => {
439
- this.fit_terminal_safely();
550
+ this.schedule_fit();
440
551
  this.send_ssh_terminal_resize();
441
552
  }, 100);
442
553
  });
@@ -619,13 +730,13 @@ export class PhirepassTerminal {
619
730
  return (h("button", { key: id, type: "button", class: { key: true, armed: !!armed }, "aria-label": hint, "aria-pressed": armed === undefined ? undefined : String(armed), onPointerDown: (event) => event.preventDefault(), onMouseDown: (event) => event.preventDefault(), onClick: onPress }, label));
620
731
  }
621
732
  render() {
622
- return (h(Host, { key: '60fd152dea1b043f2b0f4cd1cea72172aed394ad', class: { 'keys-on': this.keyBar === 'on', 'keys-off': this.keyBar === 'off' } }, h("div", { key: 'd0b1c920b13ce3aa264d4602ed2af57fed706abd', id: "ccc", ref: el => (this.containerEl = el) }), h("div", { key: '3013a54597fc7ae293408bbf7924270699b1e636', class: "keys", role: "toolbar", "aria-label": "Terminal keys" }, this.key_button('ctrl', 'ctrl', 'Control', () => {
733
+ return (h(Host, { key: 'a4e7018d85dc6ac27abfb46e199c1d2d947e57c6', class: { 'keys-on': this.keyBar === 'on', 'keys-off': this.keyBar === 'off' } }, h("div", { key: '0603c1fd65d7c70c0d48d45c4b85a7ad14c913bf', id: "ccc", ref: el => (this.containerEl = el) }), h("div", { key: '30bc8d4e31816e3c83d422133bbe9c1ced323141', class: "keys", role: "toolbar", "aria-label": "Terminal keys" }, this.key_button('ctrl', 'ctrl', 'Control', () => {
623
734
  this.ctrlArmed = !this.ctrlArmed;
624
735
  this.terminal?.focus();
625
736
  }, this.ctrlArmed), this.key_button('alt', 'alt', 'Alt', () => {
626
737
  this.altArmed = !this.altArmed;
627
738
  this.terminal?.focus();
628
- }, this.altArmed), KEY_BAR_KEYS.map(entry => this.key_button(entry.id, entry.label, entry.hint, () => this.press(entry.data))), h("span", { key: '636c553d673c3fd7e4f38278fbda7df5bcdcb06f', class: "spacer" }), this.key_button('left', '←', 'Left arrow', () => this.press(this.arrow('D'))), this.key_button('down', '↓', 'Down arrow', () => this.press(this.arrow('B'))), this.key_button('up', '↑', 'Up arrow', () => this.press(this.arrow('A'))), this.key_button('right', '→', 'Right arrow', () => this.press(this.arrow('C'))))));
739
+ }, this.altArmed), KEY_BAR_KEYS.map(entry => this.key_button(entry.id, entry.label, entry.hint, () => this.press(entry.data))), h("span", { key: '117e1718e8f4bc7451dd95222733ef3f841600db', class: "spacer" }), this.key_button('left', '←', 'Left arrow', () => this.press(this.arrow('D'))), this.key_button('down', '↓', 'Down arrow', () => this.press(this.arrow('B'))), this.key_button('up', '↑', 'Up arrow', () => this.press(this.arrow('A'))), this.key_button('right', '→', 'Right arrow', () => this.press(this.arrow('C'))))));
629
740
  }
630
741
  static get is() { return "phirepass-terminal"; }
631
742
  static get encapsulation() { return "shadow"; }