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.
@@ -238,6 +238,24 @@ function control_char(key) {
238
238
  return '\u007f';
239
239
  return null;
240
240
  }
241
+ /**
242
+ * How many frames to keep re-trying a fit before leaving it to the observer.
243
+ *
244
+ * Generous, because what is being waited on is a font load and a layout pass,
245
+ * not a network round trip — and cheap, because each attempt is one measurement
246
+ * that stops the moment it succeeds.
247
+ */
248
+ const FIT_MAX_ATTEMPTS = 60;
249
+ /**
250
+ * How much of the container the session may leave unfilled and still count as
251
+ * fitted.
252
+ *
253
+ * There is always a remainder: rows are whole, so the last partial row's worth
254
+ * of pixels belongs to nobody. One cell is normal and invisible; half a panel is
255
+ * the bug this guards. Sized above any realistic cell height so a correctly
256
+ * fitted terminal is never retried, and far below the gap a stale fit leaves.
257
+ */
258
+ const FIT_SLACK_PX = 48;
241
259
  /** The bar, left to right. `label` is what is drawn; `hint` is for a screen reader. */
242
260
  const KEY_BAR_KEYS = [
243
261
  { id: 'esc', label: 'esc', hint: 'Escape', data: '\u001b' },
@@ -267,6 +285,7 @@ const PhirepassTerminal = class {
267
285
  inputMode = InputMode.Default;
268
286
  resizeObserver;
269
287
  resizeDebounceHandle;
288
+ fitRetryHandle;
270
289
  session_id;
271
290
  usernameBuffer = "";
272
291
  passwordBuffer = "";
@@ -401,6 +420,10 @@ const PhirepassTerminal = class {
401
420
  clearTimeout(this.resizeDebounceHandle);
402
421
  this.resizeDebounceHandle = undefined;
403
422
  }
423
+ if (this.fitRetryHandle !== undefined) {
424
+ cancelAnimationFrame(this.fitRetryHandle);
425
+ this.fitRetryHandle = undefined;
426
+ }
404
427
  if (this.resizeObserver) {
405
428
  this.resizeObserver.disconnect();
406
429
  }
@@ -413,16 +436,76 @@ const PhirepassTerminal = class {
413
436
  is_terminal_open() {
414
437
  return Boolean(this.connected && this.containerEl && this.terminal.element);
415
438
  }
439
+ /**
440
+ * Size the session to its container, and **keep trying until it works**.
441
+ *
442
+ * The bug this replaces: a fit could be skipped — the renderer not ready
443
+ * yet, `connected` not yet set, `fit()` throwing on dimensions that do not
444
+ * exist for another frame — and nothing ever retried it. The only other
445
+ * thing that fits is the `ResizeObserver`, so a terminal opened into a
446
+ * container that never changes size again stayed at whatever number of rows
447
+ * it happened to have. On a panel the user does not resize, that is
448
+ * permanent: the session renders at a fraction of the height it was given
449
+ * and the dead space below it is not part of the terminal at all.
450
+ *
451
+ * Retrying on `requestAnimationFrame` rather than a timer because what is
452
+ * being waited for *is* a frame: xterm computes its cell metrics from a
453
+ * measured element, and that measurement is only meaningful once the
454
+ * browser has laid the container out and the font it will actually use has
455
+ * loaded. The attempt cap keeps a container that is genuinely zero-sized —
456
+ * a hidden tab, a collapsed panel — from spinning a frame loop forever;
457
+ * the observer will call this again when it gains a size.
458
+ */
459
+ schedule_fit() {
460
+ if (this.fitRetryHandle !== undefined) {
461
+ cancelAnimationFrame(this.fitRetryHandle);
462
+ this.fitRetryHandle = undefined;
463
+ }
464
+ let attempts = 0;
465
+ const attempt = () => {
466
+ this.fitRetryHandle = undefined;
467
+ if (this.fit_terminal_safely()) {
468
+ return;
469
+ }
470
+ attempts += 1;
471
+ if (attempts >= FIT_MAX_ATTEMPTS) {
472
+ console.warn('Giving up fitting the terminal; waiting for a resize instead');
473
+ return;
474
+ }
475
+ this.fitRetryHandle = requestAnimationFrame(attempt);
476
+ };
477
+ attempt();
478
+ }
479
+ /**
480
+ * One attempt. `true` when the session is now sized to its container.
481
+ *
482
+ * A container with no height yet counts as a failure rather than a success,
483
+ * which is what makes the retry above worth having: `fit()` on a zero-height
484
+ * parent does not throw, it computes one row and returns happily.
485
+ */
416
486
  fit_terminal_safely() {
417
487
  if (!this.fitAddon || !this.is_terminal_open()) {
418
- return;
488
+ return false;
489
+ }
490
+ const height = this.containerEl?.clientHeight ?? 0;
491
+ const width = this.containerEl?.clientWidth ?? 0;
492
+ if (height <= 0 || width <= 0) {
493
+ return false;
419
494
  }
420
495
  try {
421
496
  this.fitAddon.fit();
422
497
  }
423
498
  catch (err) {
424
499
  console.warn('Skipping terminal fit before renderer is ready:', err);
500
+ return false;
425
501
  }
502
+ // Fitted, but to the right thing? `proposeDimensions` can be answered
503
+ // from stale cell metrics — most often when a web font finishes loading
504
+ // after the first measurement — and the symptom is a session that fills
505
+ // half its panel. Compare what we got against what there was to fill.
506
+ const rows = this.terminal?.rows ?? 0;
507
+ const rendered = this.terminal.element?.clientHeight ?? 0;
508
+ return rows > 0 && rendered > 0 && height - rendered < FIT_SLACK_PX;
426
509
  }
427
510
  try_connect() {
428
511
  if (this.connected || !this.domReady || !this.runtimeReady) {
@@ -610,7 +693,7 @@ const PhirepassTerminal = class {
610
693
  handle_tunnel_opened(web) {
611
694
  this.session_id = web.sid;
612
695
  this.terminal.reset();
613
- this.fit_terminal_safely();
696
+ this.schedule_fit();
614
697
  this.send_ssh_terminal_resize();
615
698
  }
616
699
  handle_tunnel_data(web) {
@@ -631,14 +714,42 @@ const PhirepassTerminal = class {
631
714
  this.terminal.open(container);
632
715
  console.log('Terminal opened in container');
633
716
  this.connected = true;
634
- this.fit_terminal_safely();
717
+ // Scheduled, not attempted once: `open()` has only just handed the
718
+ // renderer its element, so the first measurement is routinely the
719
+ // one that is too early.
720
+ this.schedule_fit();
635
721
  this.terminal.focus();
636
722
  this.terminal.onData(this.handle_terminal_data.bind(this));
637
723
  this.channel.connect();
638
724
  this.setup_resize_observer();
725
+ this.refit_when_fonts_settle();
639
726
  console.log('Terminal connected and ready');
640
727
  }
641
728
  }
729
+ /**
730
+ * Fit again once the web fonts have actually loaded.
731
+ *
732
+ * xterm measures one character to derive its cell size, and if that
733
+ * measurement happens while the browser is still falling back — "Berkeley
734
+ * Mono" and "Fira Code" are both remote — every row is sized from the wrong
735
+ * font. The container does not change size when the real font arrives, so
736
+ * the `ResizeObserver` never fires and nothing else would ever correct it.
737
+ *
738
+ * Guarded because `document.fonts` is absent in some embedding contexts, and
739
+ * a missing font API is not a reason to fail to open a shell.
740
+ */
741
+ refit_when_fonts_settle() {
742
+ const fonts = document.fonts;
743
+ if (!fonts?.ready) {
744
+ return;
745
+ }
746
+ void fonts.ready.then(() => {
747
+ if (this.connected) {
748
+ this.schedule_fit();
749
+ this.send_ssh_terminal_resize();
750
+ }
751
+ });
752
+ }
642
753
  setup_resize_observer() {
643
754
  this.resizeObserver = new ResizeObserver(() => {
644
755
  console.log('Container resized, fitting terminal');
@@ -646,7 +757,7 @@ const PhirepassTerminal = class {
646
757
  clearTimeout(this.resizeDebounceHandle);
647
758
  }
648
759
  this.resizeDebounceHandle = setTimeout(() => {
649
- this.fit_terminal_safely();
760
+ this.schedule_fit();
650
761
  this.send_ssh_terminal_resize();
651
762
  }, 100);
652
763
  });
@@ -829,13 +940,13 @@ const PhirepassTerminal = class {
829
940
  return (h$1("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));
830
941
  }
831
942
  render() {
832
- return (h$1(Host, { key: '60fd152dea1b043f2b0f4cd1cea72172aed394ad', class: { 'keys-on': this.keyBar === 'on', 'keys-off': this.keyBar === 'off' } }, h$1("div", { key: 'd0b1c920b13ce3aa264d4602ed2af57fed706abd', id: "ccc", ref: el => (this.containerEl = el) }), h$1("div", { key: '3013a54597fc7ae293408bbf7924270699b1e636', class: "keys", role: "toolbar", "aria-label": "Terminal keys" }, this.key_button('ctrl', 'ctrl', 'Control', () => {
943
+ return (h$1(Host, { key: 'a4e7018d85dc6ac27abfb46e199c1d2d947e57c6', class: { 'keys-on': this.keyBar === 'on', 'keys-off': this.keyBar === 'off' } }, h$1("div", { key: '0603c1fd65d7c70c0d48d45c4b85a7ad14c913bf', id: "ccc", ref: el => (this.containerEl = el) }), h$1("div", { key: '30bc8d4e31816e3c83d422133bbe9c1ced323141', class: "keys", role: "toolbar", "aria-label": "Terminal keys" }, this.key_button('ctrl', 'ctrl', 'Control', () => {
833
944
  this.ctrlArmed = !this.ctrlArmed;
834
945
  this.terminal?.focus();
835
946
  }, this.ctrlArmed), this.key_button('alt', 'alt', 'Alt', () => {
836
947
  this.altArmed = !this.altArmed;
837
948
  this.terminal?.focus();
838
- }, this.altArmed), KEY_BAR_KEYS.map(entry => this.key_button(entry.id, entry.label, entry.hint, () => this.press(entry.data))), h$1("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'))))));
949
+ }, this.altArmed), KEY_BAR_KEYS.map(entry => this.key_button(entry.id, entry.label, entry.hint, () => this.press(entry.data))), h$1("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'))))));
839
950
  }
840
951
  static get watchers() { return {
841
952
  "nodeId": [{