Skip to content

Commit a5b2cc0

Browse files
author
Alberto Iannaccone
committed
limit interface scale
1 parent 8783952 commit a5b2cc0

File tree

1 file changed

+116
-19
lines changed

1 file changed

+116
-19
lines changed

arduino-ide-extension/src/browser/contributions/edit-contributions.ts

Lines changed: 116 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import {
99
KeybindingRegistry,
1010
CommandRegistry,
1111
} from './contribution';
12-
import { ArduinoMenus } from '../menu/arduino-menus';
13-
import { nls } from '@theia/core/lib/common';
12+
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus';
13+
import { DisposableCollection, nls } from '@theia/core/lib/common';
1414
import type { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser';
1515
import type { StandaloneCodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor';
1616

17+
import { Settings } from '../dialogs/settings/settings';
18+
import { MainMenuManager } from '../../common/main-menu-manager';
19+
1720
// TODO: [macOS]: to remove `Start Dictation...` and `Emoji & Symbol` see this thread: https://github.com/electron/electron/issues/8283#issuecomment-269522072
1821
// Depends on https://github.com/eclipse-theia/theia/pull/7964
1922
@injectable()
@@ -24,6 +27,39 @@ export class EditContributions extends Contribution {
2427
@inject(ClipboardService)
2528
private readonly clipboardService: ClipboardService;
2629

30+
@inject(MenuModelRegistry)
31+
private readonly menuRegistry: MenuModelRegistry;
32+
33+
@inject(MainMenuManager)
34+
protected readonly mainMenuManager: MainMenuManager;
35+
36+
private readonly menuActionsDisposables = new DisposableCollection();
37+
private fontScalingEnabled: EditContributions.FontScalingEnabled = {
38+
increase: true,
39+
decrease: true,
40+
};
41+
42+
private checkInterfaceScaleMenuActions(settings: Settings): void {
43+
let newFontScalingEnabled: EditContributions.FontScalingEnabled = {
44+
increase: true,
45+
decrease: true,
46+
};
47+
if (settings.autoScaleInterface) {
48+
newFontScalingEnabled = {
49+
increase: settings.interfaceScale < EditContributions.ZoomLevel.MAX,
50+
decrease: settings.interfaceScale > EditContributions.ZoomLevel.MIN,
51+
};
52+
}
53+
const isChanged = Object.keys(newFontScalingEnabled).some(
54+
(key: keyof EditContributions.FontScalingEnabled) =>
55+
newFontScalingEnabled[key] !== this.fontScalingEnabled[key]
56+
);
57+
if (isChanged) {
58+
this.registerInterfaceScaleMenuActions(newFontScalingEnabled);
59+
}
60+
this.fontScalingEnabled = newFontScalingEnabled;
61+
}
62+
2763
override registerCommands(registry: CommandRegistry): void {
2864
registry.registerCommand(EditContributions.Commands.GO_TO_LINE, {
2965
execute: () => this.run('editor.action.gotoLine'),
@@ -59,7 +95,9 @@ export class EditContributions extends Contribution {
5995
}
6096
await this.settingsService.update(settings);
6197
await this.settingsService.save();
98+
this.checkInterfaceScaleMenuActions(settings);
6299
},
100+
isEnabled: () => this.fontScalingEnabled.increase,
63101
});
64102
registry.registerCommand(EditContributions.Commands.DECREASE_FONT_SIZE, {
65103
execute: async () => {
@@ -71,7 +109,9 @@ export class EditContributions extends Contribution {
71109
}
72110
await this.settingsService.update(settings);
73111
await this.settingsService.save();
112+
this.checkInterfaceScaleMenuActions(settings);
74113
},
114+
isEnabled: () => this.fontScalingEnabled.decrease,
75115
});
76116
/* Tools */ registry.registerCommand(
77117
EditContributions.Commands.AUTO_FORMAT,
@@ -147,23 +187,6 @@ ${value}
147187
order: '3',
148188
});
149189

150-
registry.registerMenuAction(ArduinoMenus.EDIT__FONT_CONTROL_GROUP, {
151-
commandId: EditContributions.Commands.INCREASE_FONT_SIZE.id,
152-
label: nls.localize(
153-
'arduino/editor/increaseFontSize',
154-
'Increase Font Size'
155-
),
156-
order: '0',
157-
});
158-
registry.registerMenuAction(ArduinoMenus.EDIT__FONT_CONTROL_GROUP, {
159-
commandId: EditContributions.Commands.DECREASE_FONT_SIZE.id,
160-
label: nls.localize(
161-
'arduino/editor/decreaseFontSize',
162-
'Decrease Font Size'
163-
),
164-
order: '1',
165-
});
166-
167190
registry.registerMenuAction(ArduinoMenus.EDIT__FIND_GROUP, {
168191
commandId: EditContributions.Commands.FIND.id,
169192
label: nls.localize('vscode/findController/startFindAction', 'Find'),
@@ -200,6 +223,70 @@ ${value}
200223
label: nls.localize('arduino/editor/autoFormat', 'Auto Format'), // XXX: The Java IDE uses `Use Selection For Find`.
201224
order: '0',
202225
});
226+
227+
this.registerInterfaceScaleMenuActions();
228+
}
229+
230+
private registerInterfaceScaleMenuActions(
231+
newFontScalingEnabled = this.fontScalingEnabled
232+
): void {
233+
this.menuActionsDisposables.dispose();
234+
const increaseFontSizeMenuAction = {
235+
commandId: EditContributions.Commands.INCREASE_FONT_SIZE.id,
236+
label: nls.localize(
237+
'arduino/editor/increaseFontSize',
238+
'Increase Font Size'
239+
),
240+
order: '0',
241+
};
242+
const decreaseFontSizeMenuAction = {
243+
commandId: EditContributions.Commands.DECREASE_FONT_SIZE.id,
244+
label: nls.localize(
245+
'arduino/editor/decreaseFontSize',
246+
'Decrease Font Size'
247+
),
248+
order: '1',
249+
};
250+
251+
if (newFontScalingEnabled.increase) {
252+
this.menuActionsDisposables.push(
253+
this.menuRegistry.registerMenuAction(
254+
ArduinoMenus.EDIT__FONT_CONTROL_GROUP,
255+
increaseFontSizeMenuAction
256+
)
257+
);
258+
} else {
259+
this.menuActionsDisposables.push(
260+
this.menuRegistry.registerMenuNode(
261+
ArduinoMenus.EDIT__FONT_CONTROL_GROUP,
262+
new PlaceholderMenuNode(
263+
ArduinoMenus.EDIT__FONT_CONTROL_GROUP,
264+
increaseFontSizeMenuAction.label,
265+
{ order: increaseFontSizeMenuAction.order }
266+
)
267+
)
268+
);
269+
}
270+
if (newFontScalingEnabled.decrease) {
271+
this.menuActionsDisposables.push(
272+
this.menuRegistry.registerMenuAction(
273+
ArduinoMenus.EDIT__FONT_CONTROL_GROUP,
274+
decreaseFontSizeMenuAction
275+
)
276+
);
277+
} else {
278+
this.menuActionsDisposables.push(
279+
this.menuRegistry.registerMenuNode(
280+
ArduinoMenus.EDIT__FONT_CONTROL_GROUP,
281+
new PlaceholderMenuNode(
282+
ArduinoMenus.EDIT__FONT_CONTROL_GROUP,
283+
decreaseFontSizeMenuAction.label,
284+
{ order: decreaseFontSizeMenuAction.order }
285+
)
286+
)
287+
);
288+
}
289+
this.mainMenuManager.update();
203290
}
204291

205292
override registerKeybindings(registry: KeybindingRegistry): void {
@@ -325,4 +412,14 @@ export namespace EditContributions {
325412
id: 'arduino-auto-format', // `Auto Format` should belong to `Tool`.
326413
};
327414
}
415+
416+
export namespace ZoomLevel {
417+
export const MIN = -8;
418+
export const MAX = 9;
419+
}
420+
421+
export interface FontScalingEnabled {
422+
increase: boolean;
423+
decrease: boolean;
424+
}
328425
}

0 commit comments

Comments
 (0)