Improve terminal resizing

This commit is contained in:
GitHub Copilot
2026-01-27 22:45:34 +00:00
parent 26489567cb
commit 6b1e17eda0
3 changed files with 29 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "textual-webterm" name = "textual-webterm"
version = "0.3.19" version = "0.3.20"
description = "Serve terminal sessions over the web" description = "Serve terminal sessions over the web"
authors = ["Will McGugan <will@textualize.io>"] authors = ["Will McGugan <will@textualize.io>"]
license = "MIT" license = "MIT"
File diff suppressed because one or more lines are too long
+27 -3
View File
@@ -54,6 +54,8 @@ class WebTerminal {
private fitAddon: FitAddon; private fitAddon: FitAddon;
private element: HTMLElement; private element: HTMLElement;
private wsUrl: string; private wsUrl: string;
private resizeObserver: ResizeObserver | null = null;
private resizeRaf = 0;
private reconnectAttempts = 0; private reconnectAttempts = 0;
private maxReconnectAttempts = 5; private maxReconnectAttempts = 5;
private reconnectDelay = 1000; private reconnectDelay = 1000;
@@ -115,9 +117,13 @@ class WebTerminal {
this.send(["resize", { width: cols, height: rows }]); this.send(["resize", { width: cols, height: rows }]);
}); });
// Fit to container and handle window resize // Fit to container and handle resize changes
this.fit(); this.scheduleFit();
window.addEventListener("resize", () => this.fit()); window.addEventListener("resize", () => this.scheduleFit());
if (window.ResizeObserver) {
this.resizeObserver = new ResizeObserver(() => this.scheduleFit());
this.resizeObserver.observe(container);
}
// Connect WebSocket // Connect WebSocket
this.connect(); this.connect();
@@ -132,6 +138,16 @@ class WebTerminal {
} }
} }
private scheduleFit(): void {
if (this.resizeRaf) {
return;
}
this.resizeRaf = window.requestAnimationFrame(() => {
this.resizeRaf = 0;
this.fit();
});
}
/** Connect to WebSocket server */ /** Connect to WebSocket server */
connect(): void { connect(): void {
if (this.socket?.readyState === WebSocket.OPEN) { if (this.socket?.readyState === WebSocket.OPEN) {
@@ -227,6 +243,14 @@ class WebTerminal {
/** Clean up resources */ /** Clean up resources */
dispose(): void { dispose(): void {
if (this.resizeObserver) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
if (this.resizeRaf) {
window.cancelAnimationFrame(this.resizeRaf);
this.resizeRaf = 0;
}
this.socket?.close(); this.socket?.close();
this.terminal.dispose(); this.terminal.dispose();
} }