Skip to content

Commit 9f1dfb3

Browse files
Update main serial font size based on zoom (#965)
1 parent f293f6d commit 9f1dfb3

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/serial/SerialArea.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface SerialAreaProps extends BoxProps {
1616
expandDirection: "up" | "down";
1717
onSizeChange: (size: "compact" | "open") => void;
1818
showSyncStatus: boolean;
19-
terminalFontSizePt?: number;
19+
terminalFontSizePt: number;
2020
hideExpandTextOnTraceback?: boolean;
2121
showHintsAndTips?: boolean;
2222
tabOutRef: HTMLElement;

src/serial/XTerm.tsx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66
import { Box, BoxProps } from "@chakra-ui/layout";
77
import { useToken } from "@chakra-ui/react";
8-
import React, { useEffect, useRef } from "react";
8+
import React, { useEffect, useMemo, useRef } from "react";
99
import { Terminal } from "xterm";
1010
import { FitAddon } from "xterm-addon-fit";
1111
import "xterm/css/xterm.css";
1212
import useActionFeedback from "../common/use-action-feedback";
1313
import useIsUnmounted from "../common/use-is-unmounted";
14-
import { backgroundColorTerm, defaultCodeFontSizePt } from "../deployment/misc";
14+
import { backgroundColorTerm } from "../deployment/misc";
1515
import { EVENT_SERIAL_DATA, EVENT_SERIAL_RESET } from "../device/device";
1616
import { parseTraceLine, useDevice } from "../device/device-hooks";
1717
import { useSelection } from "../workbench/use-selection";
@@ -21,20 +21,16 @@ import "./xterm-custom.css";
2121
import customKeyEventHandler from "./xterm-keyboard";
2222

2323
interface XTermProps extends BoxProps {
24-
fontSizePt?: number;
2524
tabOutRef: HTMLElement;
25+
fontSizePt: number;
2626
}
2727

2828
/**
2929
* xterm.js-based terminal.
3030
*/
31-
const XTerm = ({
32-
fontSizePt = defaultCodeFontSizePt,
33-
tabOutRef,
34-
...props
35-
}: XTermProps) => {
31+
const XTerm = ({ fontSizePt, tabOutRef, ...props }: XTermProps) => {
3632
const ref = useRef<HTMLDivElement>(null);
37-
useManagedTermimal(ref, fontSizePt, tabOutRef);
33+
useManagedTermimal(ref, tabOutRef, fontSizePt);
3834
return <Box {...props} ref={ref} backgroundColor={backgroundColorTerm} />;
3935
};
4036

@@ -48,16 +44,18 @@ const ptToPixelRatio = 96 / 72;
4844
*/
4945
const useManagedTermimal = (
5046
ref: React.RefObject<HTMLDivElement>,
51-
fontSizePt: number,
52-
tabOutRef: HTMLElement
47+
tabOutRef: HTMLElement,
48+
fontSizePt: number
5349
): void => {
5450
const parent = ref.current;
5551
const actionFeedback = useActionFeedback();
5652
const codeFontFamily = useToken("fonts", "code");
5753
const device = useDevice();
5854
const isUnmounted = useIsUnmounted();
5955
const [, setSelection] = useSelection();
56+
const fitAddon = useMemo(() => new FitAddon(), []);
6057
const currentTerminalRef = useCurrentTerminalRef();
58+
const initialFontSizeRef = useRef<number>(fontSizePt);
6159

6260
useEffect(() => {
6361
if (!parent) {
@@ -68,7 +66,7 @@ const useManagedTermimal = (
6866
}
6967
const terminal = new Terminal({
7068
fontFamily: codeFontFamily,
71-
fontSize: fontSizePt * ptToPixelRatio,
69+
fontSize: ptToPixelRatio * initialFontSizeRef.current,
7270
letterSpacing: 1.1,
7371
screenReaderMode: true,
7472
theme: {
@@ -93,7 +91,6 @@ const useManagedTermimal = (
9391
{}
9492
)
9593
);
96-
const fitAddon = new FitAddon();
9794
terminal.loadAddon(fitAddon);
9895
terminal.attachCustomKeyEventHandler((e) =>
9996
customKeyEventHandler(e, tabOutRef)
@@ -193,9 +190,22 @@ const useManagedTermimal = (
193190
isUnmounted,
194191
parent,
195192
setSelection,
196-
fontSizePt,
193+
fitAddon,
194+
initialFontSizeRef,
197195
tabOutRef,
198196
]);
197+
198+
useEffect(() => {
199+
currentTerminalRef.current?.setOption(
200+
"fontSize",
201+
fontSizePt * ptToPixelRatio
202+
);
203+
try {
204+
fitAddon.fit();
205+
} catch (e) {
206+
// It throws if you resize it when not visible but it does no harm.
207+
}
208+
}, [currentTerminalRef, fitAddon, fontSizePt]);
199209
};
200210

201211
export default XTerm;

src/workbench/Workbench.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { MAIN_FILE } from "../fs/fs";
3030
import { useProject } from "../project/project-hooks";
3131
import ProjectActionBar from "../project/ProjectActionBar";
3232
import SerialArea from "../serial/SerialArea";
33+
import { useSettings } from "../settings/settings";
3334
import Simulator from "../simulator/Simulator";
3435
import Overlay from "./connect-dialogs/Overlay";
3536
import SideBar from "./SideBar";
@@ -237,6 +238,7 @@ const Editor = ({ editor }: EditorProps) => {
237238
const [serialStateWhenOpen, setSerialStateWhenOpen] =
238239
useState<SizedMode>("compact");
239240
const serialSizedMode = connected ? serialStateWhenOpen : "collapsed";
241+
const [{ fontSize: settingsFontSizePt }] = useSettings();
240242
const ref = useRef<HTMLButtonElement>(null);
241243
return (
242244
<Flex
@@ -266,6 +268,7 @@ const Editor = ({ editor }: EditorProps) => {
266268
showSyncStatus={true}
267269
expandDirection="up"
268270
tabOutRef={ref.current!}
271+
terminalFontSizePt={settingsFontSizePt}
269272
/>
270273
</SplitViewSized>
271274
</SplitView>

0 commit comments

Comments
 (0)