Skip to content

Commit 8edf62f

Browse files
author
Alberto Iannaccone
committed
debounce interface scale updates
1 parent a5b2cc0 commit 8edf62f

File tree

1 file changed

+68
-41
lines changed

1 file changed

+68
-41
lines changed

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

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ import {
1010
CommandRegistry,
1111
} from './contribution';
1212
import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus';
13-
import { DisposableCollection, nls } from '@theia/core/lib/common';
13+
import {
14+
DisposableCollection,
15+
MaybePromise,
16+
nls,
17+
} from '@theia/core/lib/common';
1418
import type { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser';
1519
import type { StandaloneCodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor';
1620

1721
import { Settings } from '../dialogs/settings/settings';
1822
import { MainMenuManager } from '../../common/main-menu-manager';
23+
import debounce = require('lodash.debounce');
1924

2025
// TODO: [macOS]: to remove `Start Dictation...` and `Emoji & Symbol` see this thread: https://github.com/electron/electron/issues/8283#issuecomment-269522072
2126
// Depends on https://github.com/eclipse-theia/theia/pull/7964
@@ -39,25 +44,24 @@ export class EditContributions extends Contribution {
3944
decrease: true,
4045
};
4146

42-
private checkInterfaceScaleMenuActions(settings: Settings): void {
43-
let newFontScalingEnabled: EditContributions.FontScalingEnabled = {
44-
increase: true,
45-
decrease: true,
47+
private currentScale: EditContributions.ScaleSettings;
48+
private currentSettings: Settings;
49+
private updateSettingsDebounced = debounce(
50+
async () => {
51+
await this.settingsService.update(this.currentSettings);
52+
await this.settingsService.save();
53+
},
54+
100,
55+
{ maxWait: 200 }
56+
);
57+
58+
override onStart(): MaybePromise<void> {
59+
const updateCurrent = (settings: Settings) => {
60+
this.currentSettings = settings;
61+
this.currentScale = { ...settings };
4662
};
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;
63+
this.settingsService.onDidChange((settings) => updateCurrent(settings));
64+
this.settingsService.settings().then((settings) => updateCurrent(settings));
6165
}
6266

6367
override registerCommands(registry: CommandRegistry): void {
@@ -86,31 +90,11 @@ export class EditContributions extends Contribution {
8690
execute: () => this.run('editor.action.previousSelectionMatchFindAction'),
8791
});
8892
registry.registerCommand(EditContributions.Commands.INCREASE_FONT_SIZE, {
89-
execute: async () => {
90-
const settings = await this.settingsService.settings();
91-
if (settings.autoScaleInterface) {
92-
settings.interfaceScale = settings.interfaceScale + 1;
93-
} else {
94-
settings.editorFontSize = settings.editorFontSize + 1;
95-
}
96-
await this.settingsService.update(settings);
97-
await this.settingsService.save();
98-
this.checkInterfaceScaleMenuActions(settings);
99-
},
93+
execute: () => this.updateFontSize('increase'),
10094
isEnabled: () => this.fontScalingEnabled.increase,
10195
});
10296
registry.registerCommand(EditContributions.Commands.DECREASE_FONT_SIZE, {
103-
execute: async () => {
104-
const settings = await this.settingsService.settings();
105-
if (settings.autoScaleInterface) {
106-
settings.interfaceScale = settings.interfaceScale - 1;
107-
} else {
108-
settings.editorFontSize = settings.editorFontSize - 1;
109-
}
110-
await this.settingsService.update(settings);
111-
await this.settingsService.save();
112-
this.checkInterfaceScaleMenuActions(settings);
113-
},
97+
execute: () => this.updateFontSize('decrease'),
11498
isEnabled: () => this.fontScalingEnabled.decrease,
11599
});
116100
/* Tools */ registry.registerCommand(
@@ -371,6 +355,44 @@ ${value}
371355
}
372356
}
373357
}
358+
359+
private async updateFontSize(mode: 'increase' | 'decrease'): Promise<void> {
360+
if (this.currentSettings.autoScaleInterface) {
361+
mode === 'increase'
362+
? this.currentScale.interfaceScale++
363+
: this.currentScale.interfaceScale--;
364+
} else {
365+
mode === 'increase'
366+
? this.currentScale.editorFontSize++
367+
: this.currentScale.editorFontSize++;
368+
}
369+
this.currentSettings = {
370+
...this.currentSettings,
371+
editorFontSize: this.currentScale.editorFontSize,
372+
interfaceScale: this.currentScale.interfaceScale,
373+
};
374+
let newFontScalingEnabled: EditContributions.FontScalingEnabled = {
375+
increase: true,
376+
decrease: true,
377+
};
378+
if (this.currentSettings.autoScaleInterface) {
379+
newFontScalingEnabled = {
380+
increase:
381+
this.currentSettings.interfaceScale < EditContributions.ZoomLevel.MAX,
382+
decrease:
383+
this.currentSettings.interfaceScale > EditContributions.ZoomLevel.MIN,
384+
};
385+
}
386+
const isChanged = Object.keys(newFontScalingEnabled).some(
387+
(key: keyof EditContributions.FontScalingEnabled) =>
388+
newFontScalingEnabled[key] !== this.fontScalingEnabled[key]
389+
);
390+
if (isChanged) {
391+
this.registerInterfaceScaleMenuActions(newFontScalingEnabled);
392+
}
393+
this.fontScalingEnabled = newFontScalingEnabled;
394+
this.updateSettingsDebounced();
395+
}
374396
}
375397

376398
export namespace EditContributions {
@@ -422,4 +444,9 @@ export namespace EditContributions {
422444
increase: boolean;
423445
decrease: boolean;
424446
}
447+
448+
export type ScaleSettings = Pick<
449+
Settings,
450+
'interfaceScale' | 'editorFontSize'
451+
>;
425452
}

0 commit comments

Comments
 (0)