Skip to content

Commit f516846

Browse files
committed
Fit once during creation
This does not fix any bugs (that I know of) but we only need to fit once when the terminal is created, not every time we reconnect. Granted, currently we do not support reconnecting without refreshing anyway so it does not really matter, but this just seems more correct. Plus now we will not have to pass the fit addon around.
1 parent 1372bf8 commit f516846

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

site/src/pages/TerminalPage/TerminalPage.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ const TerminalPage: FC = () => {
5454
const [terminalState, setTerminalState] = useState<
5555
"connected" | "disconnected" | "initializing"
5656
>("initializing");
57-
const [fitAddon, setFitAddon] = useState<FitAddon | null>(null);
5857
const [searchParams] = useSearchParams();
5958
// The reconnection token is a unique token that identifies
6059
// a terminal session. It's generated by the client to reduce
@@ -125,7 +124,6 @@ const TerminalPage: FC = () => {
125124
terminal.loadAddon(new CanvasAddon());
126125
}
127126
const fitAddon = new FitAddon();
128-
setFitAddon(fitAddon);
129127
terminal.loadAddon(fitAddon);
130128
terminal.loadAddon(new Unicode11Addon());
131129
terminal.unicode.activeVersion = "11";
@@ -134,13 +132,21 @@ const TerminalPage: FC = () => {
134132
handleWebLinkRef.current(uri);
135133
}),
136134
);
137-
setTerminal(terminal);
135+
138136
terminal.open(xtermRef.current);
139-
const listener = () => {
140-
// This will trigger a resize event on the terminal.
141-
fitAddon.fit();
142-
};
137+
138+
// We have to fit twice here. It's unknown why, but the first fit will
139+
// overflow slightly in some scenarios. Applying a second fit resolves this.
140+
fitAddon.fit();
141+
fitAddon.fit();
142+
143+
// This will trigger a resize event on the terminal.
144+
const listener = () => fitAddon.fit();
143145
window.addEventListener("resize", listener);
146+
147+
// Terminal is correctly sized and is ready to be used.
148+
setTerminal(terminal);
149+
144150
return () => {
145151
window.removeEventListener("resize", listener);
146152
terminal.dispose();
@@ -165,16 +171,10 @@ const TerminalPage: FC = () => {
165171

166172
// Hook up the terminal through a web socket.
167173
useEffect(() => {
168-
if (!terminal || !fitAddon) {
174+
if (!terminal) {
169175
return;
170176
}
171177

172-
// We have to fit twice here. It's unknown why, but
173-
// the first fit will overflow slightly in some
174-
// scenarios. Applying a second fit resolves this.
175-
fitAddon.fit();
176-
fitAddon.fit();
177-
178178
// The terminal should be cleared on each reconnect
179179
// because all data is re-rendered from the backend.
180180
terminal.clear();
@@ -289,7 +289,6 @@ const TerminalPage: FC = () => {
289289
};
290290
}, [
291291
command,
292-
fitAddon,
293292
proxy.preferredPathAppURL,
294293
reconnectionToken,
295294
terminal,

0 commit comments

Comments
 (0)