Skip to content

Commit 87ffd8a

Browse files
Add editor font size setting
1 parent 1f299fb commit 87ffd8a

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

js/common/settings.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SettingsDialog extends GenericModal {
2929
contentDiv.appendChild(label);
3030

3131
const control = await this._createControl(setting);
32-
control.value = settings[setting.key];
32+
control.value = settings?.[setting.key] ?? setting.default;
3333
contentDiv.appendChild(control);
3434
}
3535

@@ -40,7 +40,20 @@ class SettingsDialog extends GenericModal {
4040
let settings = {}
4141
for (const setting of this._settingsData) {
4242
const control = this._currentModal.querySelector(`#setting-${setting.key}`);
43-
settings[setting.key] = control.value;
43+
let value = control.value;
44+
if (setting.type === 'number') {
45+
value = Number.parseInt(value, 10);
46+
if (Number.isNaN(value)) {
47+
value = setting.default;
48+
}
49+
if (setting.min !== undefined) {
50+
value = Math.max(value, setting.min);
51+
}
52+
if (setting.max !== undefined) {
53+
value = Math.min(value, setting.max);
54+
}
55+
}
56+
settings[setting.key] = value;
4457
}
4558
this._returnValue(settings);
4659
}
@@ -56,6 +69,18 @@ class SettingsDialog extends GenericModal {
5669
option.textContent = optionValue.charAt(0).toUpperCase() + optionValue.slice(1);
5770
control.appendChild(option);
5871
}
72+
} else if (settingData.type === 'number') {
73+
control = document.createElement('input');
74+
control.type = 'number';
75+
if (settingData.min !== undefined) {
76+
control.min = settingData.min;
77+
}
78+
if (settingData.max !== undefined) {
79+
control.max = settingData.max;
80+
}
81+
if (settingData.step !== undefined) {
82+
control.step = settingData.step;
83+
}
5984
}
6085
control.id = `setting-${settingData.key}`;
6186

@@ -70,7 +95,8 @@ class Settings {
7095
constructor() {
7196
// This will hold the layout/save data for the settings
7297
this._settingsData = [
73-
{ key: 'theme', type: 'select', label: 'Editor Theme', icon: 'palette', options: ['dark', 'light'], default: 'dark' }
98+
{ key: 'theme', type: 'select', label: 'Editor Theme', icon: 'palette', options: ['dark', 'light'], default: 'dark' },
99+
{ key: 'editorFontSize', type: 'number', label: 'Font Size', icon: 'text-height', min: 8, max: 48, step: 1, default: 16 }
74100
];
75101
this._settings = {};
76102
this._loadSettings();
@@ -110,8 +136,9 @@ class Settings {
110136
}
111137

112138
async showDialog() {
113-
this._settings = await this._settingsDialog.open(this._settings);
114-
if (this._settings) {
139+
const updatedSettings = await this._settingsDialog.open(this._settings);
140+
if (updatedSettings) {
141+
this._settings = updatedSettings;
115142
this._saveSettings();
116143
return true;
117144
}

js/layout.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ function updatePageLayout(updateType) {
151151
}
152152
}
153153

154-
function refitTerminal() {
154+
export function refitTerminal() {
155155
// Custom function to replace the terminal refit function as it was a bit buggy
156156

157157
// Re-fitting the terminal requires a full re-layout of the DOM which can be tricky to time right.

js/script.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { Settings } from './common/settings.js';
2929
import { CONNTYPE } from './constants.js';
3030
import './layout.js'; // load for side effects only
3131
import { setupPlotterChart } from "./common/plotter.js";
32-
import { mainContent, showSerial } from './layout.js';
32+
import { mainContent, showSerial, refitTerminal } from './layout.js';
3333
import { registerPWA } from "./common/pwa.js";
3434

3535
registerPWA();
@@ -68,6 +68,10 @@ const messageDialog = new MessageModal("message");
6868
const connectionType = new ButtonValueDialog("connection-type");
6969
const settings = new Settings();
7070

71+
const DEFAULT_EDITOR_FONT_SIZE = 16;
72+
const MIN_EDITOR_FONT_SIZE = 8;
73+
const MAX_EDITOR_FONT_SIZE = 48;
74+
7175
// localStorage key used to remember the most recently chosen backend
7276
// ("web" | "ble" | "usb"). When the user clicks Connect after a
7377
// disconnect, we prefer the last backend over re-prompting for one.
@@ -97,6 +101,23 @@ function rememberLastBackend(workflowType) {
97101
}
98102

99103
const editorTheme = EditorView.theme({}, {dark: getCssVar('editor-theme-dark').trim() === '1'});
104+
const editorFontSizeCompartment = new Compartment();
105+
106+
function getEditorFontSize() {
107+
const fontSize = Number.parseInt(settings.getSetting('editorFontSize'), 10);
108+
if (Number.isNaN(fontSize)) {
109+
return DEFAULT_EDITOR_FONT_SIZE;
110+
}
111+
return Math.min(Math.max(fontSize, MIN_EDITOR_FONT_SIZE), MAX_EDITOR_FONT_SIZE);
112+
}
113+
114+
function editorFontSizeTheme() {
115+
return EditorView.theme({
116+
"&": {
117+
fontSize: `${getEditorFontSize()}px`,
118+
},
119+
});
120+
}
100121

101122
// Map file extensions to a CodeMirror 6 language extension factory.
102123
// Anything not in this map falls back to plain text (no language plugin).
@@ -655,6 +676,7 @@ const baseEditorExtensions = [
655676
keymap.of(hotkeyMap),
656677
indentUnit.of(" "),
657678
editorTheme,
679+
editorFontSizeCompartment.of(editorFontSizeTheme()),
658680
syntaxHighlighting(classHighlighter),
659681
EditorView.updateListener.of(onTextChange)
660682
];
@@ -939,6 +961,7 @@ function getCssVar(varName) {
939961

940962
async function setupXterm() {
941963
state.terminal = new Terminal({
964+
fontSize: getEditorFontSize(),
942965
theme: {
943966
background: getCssVar('background-color'),
944967
foreground: getCssVar('terminal-text-color'),
@@ -999,13 +1022,18 @@ function applySettings() {
9991022

10001023
// Apply to EditorView.theme dark parameter
10011024
editor.darkTheme = getCssVar('editor-theme-dark').trim() === '1';
1025+
editor.dispatch({
1026+
effects: editorFontSizeCompartment.reconfigure(editorFontSizeTheme()),
1027+
});
10021028

10031029
// Apply to xterm
10041030
state.terminal.options.theme = {
10051031
background: getCssVar('background-color'),
10061032
foreground: getCssVar('terminal-text-color'),
10071033
cursor: getCssVar('terminal-text-color'),
10081034
};
1035+
state.terminal.options.fontSize = getEditorFontSize();
1036+
refitTerminal();
10091037

10101038
debugMessageAnsi = null;
10111039

sass/layout/_layout.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@
155155
&.settings-dialog {
156156
#settings-content {
157157
padding-top: 10px;
158-
display: flex;
159-
justify-content: center;
158+
display: grid;
159+
grid-template-columns: max-content minmax(120px, 1fr);
160+
gap: 12px 20px;
160161
align-items: center;
161162
min-width: 300px;
162163
label {
163164
i {
164165
margin-right: 3px;
165166
}
166-
margin-right: 20px;
167167
white-space: nowrap;
168168
}
169169
input, select {

0 commit comments

Comments
 (0)