Skip to content

Commit 53133e6

Browse files
authored
Fix terminal font size bounds checking in mouse wheel zoom (microsoft#253010)
1 parent 60544f4 commit 53133e6

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

src/vs/workbench/contrib/terminalContrib/zoom/browser/terminal.zoom.contribution.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class TerminalMouseWheelZoomContribution extends Disposable implements ITerminal
5757
return this._configurationService.getValue(TerminalSettingId.FontSize);
5858
}
5959

60+
private _clampFontSize(fontSize: number): number {
61+
return clampTerminalFontSize(fontSize);
62+
}
63+
6064
private _setupMouseWheelZoomListener(raw: RawXtermTerminal) {
6165
// This is essentially a copy of what we do in the editor, just we modify font size directly
6266
// as there is no separate zoom level concept in the terminal
@@ -72,7 +76,8 @@ class TerminalMouseWheelZoomContribution extends Disposable implements ITerminal
7276
if (classifier.isPhysicalMouseWheel()) {
7377
if (this._hasMouseWheelZoomModifiers(browserEvent)) {
7478
const delta = browserEvent.deltaY > 0 ? -1 : 1;
75-
this._configurationService.updateValue(TerminalSettingId.FontSize, this._getConfigFontSize() + delta);
79+
const newFontSize = this._clampFontSize(this._getConfigFontSize() + delta);
80+
this._configurationService.updateValue(TerminalSettingId.FontSize, newFontSize);
7681
// EditorZoom.setZoomLevel(zoomLevel + delta);
7782
browserEvent.preventDefault();
7883
browserEvent.stopPropagation();
@@ -96,7 +101,8 @@ class TerminalMouseWheelZoomContribution extends Disposable implements ITerminal
96101
const deltaAbs = Math.ceil(Math.abs(gestureAccumulatedDelta / 5));
97102
const deltaDirection = gestureAccumulatedDelta > 0 ? -1 : 1;
98103
const delta = deltaAbs * deltaDirection;
99-
this._configurationService.updateValue(TerminalSettingId.FontSize, gestureStartFontSize + delta);
104+
const newFontSize = this._clampFontSize(gestureStartFontSize + delta);
105+
this._configurationService.updateValue(TerminalSettingId.FontSize, newFontSize);
100106
gestureAccumulatedDelta += browserEvent.deltaY;
101107
browserEvent.preventDefault();
102108
browserEvent.stopPropagation();
@@ -128,7 +134,8 @@ registerTerminalAction({
128134
const configurationService = accessor.get(IConfigurationService);
129135
const value = configurationService.getValue(TerminalSettingId.FontSize);
130136
if (isNumber(value)) {
131-
await configurationService.updateValue(TerminalSettingId.FontSize, value + 1);
137+
const newFontSize = clampTerminalFontSize(value + 1);
138+
await configurationService.updateValue(TerminalSettingId.FontSize, newFontSize);
132139
}
133140
}
134141
});
@@ -140,7 +147,8 @@ registerTerminalAction({
140147
const configurationService = accessor.get(IConfigurationService);
141148
const value = configurationService.getValue(TerminalSettingId.FontSize);
142149
if (isNumber(value)) {
143-
await configurationService.updateValue(TerminalSettingId.FontSize, value - 1);
150+
const newFontSize = clampTerminalFontSize(value - 1);
151+
await configurationService.updateValue(TerminalSettingId.FontSize, newFontSize);
144152
}
145153
}
146154
});
@@ -153,3 +161,7 @@ registerTerminalAction({
153161
await configurationService.updateValue(TerminalSettingId.FontSize, defaultTerminalFontSize);
154162
}
155163
});
164+
165+
export function clampTerminalFontSize(fontSize: number): number {
166+
return Math.max(6, Math.min(100, fontSize));
167+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { strictEqual } from 'assert';
7+
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../base/test/common/utils.js';
8+
import { clampTerminalFontSize } from '../../browser/terminal.zoom.contribution.js';
9+
10+
suite('Terminal Mouse Wheel Zoom', () => {
11+
ensureNoDisposablesAreLeakedInTestSuite();
12+
13+
test('clamps font size to minimum value when below bounds', () => {
14+
const result = clampTerminalFontSize(3 + (-2)); // 3 - 2 = 1, clamped to 6
15+
strictEqual(result, 6, 'Font size should be clamped to minimum value of 6');
16+
});
17+
18+
test('clamps font size to maximum value when above bounds', () => {
19+
const result = clampTerminalFontSize(99 + 5); // 99 + 5 = 104, clamped to 100
20+
strictEqual(result, 100, 'Font size should be clamped to maximum value of 100');
21+
});
22+
23+
test('preserves font size when within bounds', () => {
24+
const result = clampTerminalFontSize(12 + 3); // 12 + 3 = 15, within bounds
25+
strictEqual(result, 15, 'Font size should remain unchanged when within bounds');
26+
});
27+
28+
test('clamps font size when going below minimum', () => {
29+
const result = clampTerminalFontSize(6 + (-1)); // 6 - 1 = 5, clamped to 6
30+
strictEqual(result, 6, 'Font size should be clamped when going below minimum');
31+
});
32+
33+
test('clamps font size when going above maximum', () => {
34+
const result = clampTerminalFontSize(100 + 1); // 100 + 1 = 101, clamped to 100
35+
strictEqual(result, 100, 'Font size should be clamped when going above maximum');
36+
});
37+
});

0 commit comments

Comments
 (0)