-
Notifications
You must be signed in to change notification settings - Fork 902
fix: initialize terminal with correct size #10369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,7 +54,6 @@ const TerminalPage: FC = () => { | |
const [terminalState, setTerminalState] = useState< | ||
"connected" | "disconnected" | "initializing" | ||
>("initializing"); | ||
const [fitAddon, setFitAddon] = useState<FitAddon | null>(null); | ||
const [searchParams] = useSearchParams(); | ||
// The reconnection token is a unique token that identifies | ||
// a terminal session. It's generated by the client to reduce | ||
|
@@ -125,7 +124,6 @@ const TerminalPage: FC = () => { | |
terminal.loadAddon(new CanvasAddon()); | ||
} | ||
const fitAddon = new FitAddon(); | ||
setFitAddon(fitAddon); | ||
terminal.loadAddon(fitAddon); | ||
terminal.loadAddon(new Unicode11Addon()); | ||
terminal.unicode.activeVersion = "11"; | ||
|
@@ -134,13 +132,21 @@ const TerminalPage: FC = () => { | |
handleWebLinkRef.current(uri); | ||
}), | ||
); | ||
setTerminal(terminal); | ||
|
||
terminal.open(xtermRef.current); | ||
const listener = () => { | ||
// This will trigger a resize event on the terminal. | ||
fitAddon.fit(); | ||
}; | ||
|
||
// We have to fit twice here. It's unknown why, but the first fit will | ||
// overflow slightly in some scenarios. Applying a second fit resolves this. | ||
fitAddon.fit(); | ||
fitAddon.fit(); | ||
|
||
// This will trigger a resize event on the terminal. | ||
const listener = () => fitAddon.fit(); | ||
window.addEventListener("resize", listener); | ||
|
||
// Terminal is correctly sized and is ready to be used. | ||
setTerminal(terminal); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is moving the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup exactly, I just felt it flowed better but that is probably rather subjective. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest part of me also wanted to move it just because I do not necessarily trust what React does behind the scenes hahaha, this way I think the intent is clearer at least even if it is functionally identical. |
||
|
||
return () => { | ||
window.removeEventListener("resize", listener); | ||
terminal.dispose(); | ||
|
@@ -165,16 +171,10 @@ const TerminalPage: FC = () => { | |
|
||
// Hook up the terminal through a web socket. | ||
useEffect(() => { | ||
if (!terminal || !fitAddon) { | ||
if (!terminal) { | ||
return; | ||
} | ||
|
||
// We have to fit twice here. It's unknown why, but | ||
// the first fit will overflow slightly in some | ||
// scenarios. Applying a second fit resolves this. | ||
fitAddon.fit(); | ||
fitAddon.fit(); | ||
|
||
// The terminal should be cleared on each reconnect | ||
// because all data is re-rendered from the backend. | ||
terminal.clear(); | ||
|
@@ -229,6 +229,8 @@ const TerminalPage: FC = () => { | |
reconnectionToken, | ||
workspaceAgent.id, | ||
command, | ||
terminal.rows, | ||
terminal.cols, | ||
) | ||
.then((url) => { | ||
if (disposed) { | ||
|
@@ -289,7 +291,6 @@ const TerminalPage: FC = () => { | |
}; | ||
}, [ | ||
command, | ||
fitAddon, | ||
proxy.preferredPathAppURL, | ||
reconnectionToken, | ||
terminal, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know much about xterm, but moving the calls does seem like a good change.
Just making sure for my own understanding – is the terminal expected to resize only when the browser/window viewport does?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, exactly. We set the size on page load, send the current size on connect, and we only need resize events on subsequent size changes of the viewport.