fix: wait for fonts and retry fit until proper dimensions

- Wait for document.fonts.ready before fitting (ensures correct cell metrics)
- Retry fit every 100ms if rows <= 1 or cols <= 10 (layout still settling)
- Up to 30 retries (3 seconds) for layout to stabilize
- Remove redundant delayed fits, keep only header auto-hide fit at 4s
This commit is contained in:
Fusion
2026-05-24 15:25:47 +02:00
parent 38185b9659
commit fa17b13413
+34 -43
View File
@@ -212,52 +212,43 @@ export const TerminalComponent: React.FC<TerminalProps> = ({
// Connect WebSocket
ws = connectWebSocket();
// Fit after layout settles
requestAnimationFrame(() => {
// Robust fitting: wait for fonts and stable dimensions
const performFit = () => {
fitAddon.fit();
const { cols, rows } = term;
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "resize",
cols,
rows,
})
);
}
return { cols, rows };
};
const ensureProperFit = (attempt = 0) => {
const { cols, rows } = performFit();
// If dimensions are unreasonably small, retry (layout still settling)
if ((rows <= 1 || cols <= 10) && attempt < 30) {
setTimeout(() => ensureProperFit(attempt + 1), 100);
}
};
// Wait for fonts to load, then fit with retry logic
document.fonts.ready.then(() => {
requestAnimationFrame(() => {
fitAddon.fit();
const { cols, rows } = term;
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "resize",
cols,
rows,
})
);
}
// Force a second fit after layout fully settles
setTimeout(() => {
fitAddon.fit();
const { cols, rows } = term;
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "resize",
cols,
rows,
})
);
}
}, 500);
// Fit after mobile header auto-hides (3s delay + 0.3s transition)
setTimeout(() => {
fitAddon.fit();
const { cols, rows } = term;
if (ws.readyState === WebSocket.OPEN) {
ws.send(
JSON.stringify({
type: "resize",
cols,
rows,
})
);
}
}, 4000);
requestAnimationFrame(() => {
ensureProperFit();
});
});
});
// Fit after mobile header auto-hides (3s delay + 0.3s transition)
setTimeout(() => {
performFit();
}, 4000);
};
// Check if container already has dimensions