From ebe4b40e7e5415ca263a0f5624af505a89b67ec6 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Fri, 23 Sep 2022 15:24:28 +0200 Subject: [PATCH 1/9] limit interface scale --- .../contributions/edit-contributions.ts | 135 +++++++++++++++--- 1 file changed, 116 insertions(+), 19 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts index 7c75c7225..605b1aad3 100644 --- a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts +++ b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts @@ -9,11 +9,14 @@ import { KeybindingRegistry, CommandRegistry, } from './contribution'; -import { ArduinoMenus } from '../menu/arduino-menus'; -import { nls } from '@theia/core/lib/common'; +import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus'; +import { DisposableCollection, nls } from '@theia/core/lib/common'; import type { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser'; import type { StandaloneCodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor'; +import { Settings } from '../dialogs/settings/settings'; +import { MainMenuManager } from '../../common/main-menu-manager'; + // TODO: [macOS]: to remove `Start Dictation...` and `Emoji & Symbol` see this thread: https://github.com/electron/electron/issues/8283#issuecomment-269522072 // Depends on https://github.com/eclipse-theia/theia/pull/7964 @injectable() @@ -24,6 +27,39 @@ export class EditContributions extends Contribution { @inject(ClipboardService) private readonly clipboardService: ClipboardService; + @inject(MenuModelRegistry) + private readonly menuRegistry: MenuModelRegistry; + + @inject(MainMenuManager) + protected readonly mainMenuManager: MainMenuManager; + + private readonly menuActionsDisposables = new DisposableCollection(); + private fontScalingEnabled: EditContributions.FontScalingEnabled = { + increase: true, + decrease: true, + }; + + private checkInterfaceScaleMenuActions(settings: Settings): void { + let newFontScalingEnabled: EditContributions.FontScalingEnabled = { + increase: true, + decrease: true, + }; + if (settings.autoScaleInterface) { + newFontScalingEnabled = { + increase: settings.interfaceScale < EditContributions.ZoomLevel.MAX, + decrease: settings.interfaceScale > EditContributions.ZoomLevel.MIN, + }; + } + const isChanged = Object.keys(newFontScalingEnabled).some( + (key: keyof EditContributions.FontScalingEnabled) => + newFontScalingEnabled[key] !== this.fontScalingEnabled[key] + ); + if (isChanged) { + this.registerInterfaceScaleMenuActions(newFontScalingEnabled); + } + this.fontScalingEnabled = newFontScalingEnabled; + } + override registerCommands(registry: CommandRegistry): void { registry.registerCommand(EditContributions.Commands.GO_TO_LINE, { execute: () => this.run('editor.action.gotoLine'), @@ -59,7 +95,9 @@ export class EditContributions extends Contribution { } await this.settingsService.update(settings); await this.settingsService.save(); + this.checkInterfaceScaleMenuActions(settings); }, + isEnabled: () => this.fontScalingEnabled.increase, }); registry.registerCommand(EditContributions.Commands.DECREASE_FONT_SIZE, { execute: async () => { @@ -71,7 +109,9 @@ export class EditContributions extends Contribution { } await this.settingsService.update(settings); await this.settingsService.save(); + this.checkInterfaceScaleMenuActions(settings); }, + isEnabled: () => this.fontScalingEnabled.decrease, }); /* Tools */ registry.registerCommand( EditContributions.Commands.AUTO_FORMAT, @@ -147,23 +187,6 @@ ${value} order: '3', }); - registry.registerMenuAction(ArduinoMenus.EDIT__FONT_CONTROL_GROUP, { - commandId: EditContributions.Commands.INCREASE_FONT_SIZE.id, - label: nls.localize( - 'arduino/editor/increaseFontSize', - 'Increase Font Size' - ), - order: '0', - }); - registry.registerMenuAction(ArduinoMenus.EDIT__FONT_CONTROL_GROUP, { - commandId: EditContributions.Commands.DECREASE_FONT_SIZE.id, - label: nls.localize( - 'arduino/editor/decreaseFontSize', - 'Decrease Font Size' - ), - order: '1', - }); - registry.registerMenuAction(ArduinoMenus.EDIT__FIND_GROUP, { commandId: EditContributions.Commands.FIND.id, label: nls.localize('vscode/findController/startFindAction', 'Find'), @@ -200,6 +223,70 @@ ${value} label: nls.localize('arduino/editor/autoFormat', 'Auto Format'), // XXX: The Java IDE uses `Use Selection For Find`. order: '0', }); + + this.registerInterfaceScaleMenuActions(); + } + + private registerInterfaceScaleMenuActions( + newFontScalingEnabled = this.fontScalingEnabled + ): void { + this.menuActionsDisposables.dispose(); + const increaseFontSizeMenuAction = { + commandId: EditContributions.Commands.INCREASE_FONT_SIZE.id, + label: nls.localize( + 'arduino/editor/increaseFontSize', + 'Increase Font Size' + ), + order: '0', + }; + const decreaseFontSizeMenuAction = { + commandId: EditContributions.Commands.DECREASE_FONT_SIZE.id, + label: nls.localize( + 'arduino/editor/decreaseFontSize', + 'Decrease Font Size' + ), + order: '1', + }; + + if (newFontScalingEnabled.increase) { + this.menuActionsDisposables.push( + this.menuRegistry.registerMenuAction( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + increaseFontSizeMenuAction + ) + ); + } else { + this.menuActionsDisposables.push( + this.menuRegistry.registerMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + new PlaceholderMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + increaseFontSizeMenuAction.label, + { order: increaseFontSizeMenuAction.order } + ) + ) + ); + } + if (newFontScalingEnabled.decrease) { + this.menuActionsDisposables.push( + this.menuRegistry.registerMenuAction( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + decreaseFontSizeMenuAction + ) + ); + } else { + this.menuActionsDisposables.push( + this.menuRegistry.registerMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + new PlaceholderMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + decreaseFontSizeMenuAction.label, + { order: decreaseFontSizeMenuAction.order } + ) + ) + ); + } + this.mainMenuManager.update(); } override registerKeybindings(registry: KeybindingRegistry): void { @@ -325,4 +412,14 @@ export namespace EditContributions { id: 'arduino-auto-format', // `Auto Format` should belong to `Tool`. }; } + + export namespace ZoomLevel { + export const MIN = -8; + export const MAX = 9; + } + + export interface FontScalingEnabled { + increase: boolean; + decrease: boolean; + } } From bc0ea85a15e90b9d1fac921baedbd7f084a623f8 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Mon, 26 Sep 2022 15:31:13 +0200 Subject: [PATCH 2/9] debounce interface scale updates --- .../contributions/edit-contributions.ts | 109 +++++++++++------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts index 605b1aad3..abbcf1b03 100644 --- a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts +++ b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts @@ -10,12 +10,17 @@ import { CommandRegistry, } from './contribution'; import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus'; -import { DisposableCollection, nls } from '@theia/core/lib/common'; +import { + DisposableCollection, + MaybePromise, + nls, +} from '@theia/core/lib/common'; import type { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser'; import type { StandaloneCodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor'; import { Settings } from '../dialogs/settings/settings'; import { MainMenuManager } from '../../common/main-menu-manager'; +import debounce = require('lodash.debounce'); // TODO: [macOS]: to remove `Start Dictation...` and `Emoji & Symbol` see this thread: https://github.com/electron/electron/issues/8283#issuecomment-269522072 // Depends on https://github.com/eclipse-theia/theia/pull/7964 @@ -39,25 +44,24 @@ export class EditContributions extends Contribution { decrease: true, }; - private checkInterfaceScaleMenuActions(settings: Settings): void { - let newFontScalingEnabled: EditContributions.FontScalingEnabled = { - increase: true, - decrease: true, + private currentScale: EditContributions.ScaleSettings; + private currentSettings: Settings; + private updateSettingsDebounced = debounce( + async () => { + await this.settingsService.update(this.currentSettings); + await this.settingsService.save(); + }, + 100, + { maxWait: 200 } + ); + + override onStart(): MaybePromise { + const updateCurrent = (settings: Settings) => { + this.currentSettings = settings; + this.currentScale = { ...settings }; }; - if (settings.autoScaleInterface) { - newFontScalingEnabled = { - increase: settings.interfaceScale < EditContributions.ZoomLevel.MAX, - decrease: settings.interfaceScale > EditContributions.ZoomLevel.MIN, - }; - } - const isChanged = Object.keys(newFontScalingEnabled).some( - (key: keyof EditContributions.FontScalingEnabled) => - newFontScalingEnabled[key] !== this.fontScalingEnabled[key] - ); - if (isChanged) { - this.registerInterfaceScaleMenuActions(newFontScalingEnabled); - } - this.fontScalingEnabled = newFontScalingEnabled; + this.settingsService.onDidChange((settings) => updateCurrent(settings)); + this.settingsService.settings().then((settings) => updateCurrent(settings)); } override registerCommands(registry: CommandRegistry): void { @@ -86,31 +90,11 @@ export class EditContributions extends Contribution { execute: () => this.run('editor.action.previousSelectionMatchFindAction'), }); registry.registerCommand(EditContributions.Commands.INCREASE_FONT_SIZE, { - execute: async () => { - const settings = await this.settingsService.settings(); - if (settings.autoScaleInterface) { - settings.interfaceScale = settings.interfaceScale + 1; - } else { - settings.editorFontSize = settings.editorFontSize + 1; - } - await this.settingsService.update(settings); - await this.settingsService.save(); - this.checkInterfaceScaleMenuActions(settings); - }, + execute: () => this.updateFontSize('increase'), isEnabled: () => this.fontScalingEnabled.increase, }); registry.registerCommand(EditContributions.Commands.DECREASE_FONT_SIZE, { - execute: async () => { - const settings = await this.settingsService.settings(); - if (settings.autoScaleInterface) { - settings.interfaceScale = settings.interfaceScale - 1; - } else { - settings.editorFontSize = settings.editorFontSize - 1; - } - await this.settingsService.update(settings); - await this.settingsService.save(); - this.checkInterfaceScaleMenuActions(settings); - }, + execute: () => this.updateFontSize('decrease'), isEnabled: () => this.fontScalingEnabled.decrease, }); /* Tools */ registry.registerCommand( @@ -371,6 +355,44 @@ ${value} } } } + + private async updateFontSize(mode: 'increase' | 'decrease'): Promise { + if (this.currentSettings.autoScaleInterface) { + mode === 'increase' + ? this.currentScale.interfaceScale++ + : this.currentScale.interfaceScale--; + } else { + mode === 'increase' + ? this.currentScale.editorFontSize++ + : this.currentScale.editorFontSize++; + } + this.currentSettings = { + ...this.currentSettings, + editorFontSize: this.currentScale.editorFontSize, + interfaceScale: this.currentScale.interfaceScale, + }; + let newFontScalingEnabled: EditContributions.FontScalingEnabled = { + increase: true, + decrease: true, + }; + if (this.currentSettings.autoScaleInterface) { + newFontScalingEnabled = { + increase: + this.currentSettings.interfaceScale < EditContributions.ZoomLevel.MAX, + decrease: + this.currentSettings.interfaceScale > EditContributions.ZoomLevel.MIN, + }; + } + const isChanged = Object.keys(newFontScalingEnabled).some( + (key: keyof EditContributions.FontScalingEnabled) => + newFontScalingEnabled[key] !== this.fontScalingEnabled[key] + ); + if (isChanged) { + this.registerInterfaceScaleMenuActions(newFontScalingEnabled); + } + this.fontScalingEnabled = newFontScalingEnabled; + this.updateSettingsDebounced(); + } } export namespace EditContributions { @@ -422,4 +444,9 @@ export namespace EditContributions { increase: boolean; decrease: boolean; } + + export type ScaleSettings = Pick< + Settings, + 'interfaceScale' | 'editorFontSize' + >; } From dd53eacd4c378188ba4283f6aae36231cd2caa72 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Mon, 26 Sep 2022 16:45:37 +0200 Subject: [PATCH 3/9] limit font-size + refactor --- .../browser/arduino-ide-frontend-module.ts | 2 + .../contributions/edit-contributions.ts | 184 +------------- .../browser/contributions/interface-scale.ts | 233 ++++++++++++++++++ .../dialogs/settings/settings-component.tsx | 25 +- 4 files changed, 253 insertions(+), 191 deletions(-) create mode 100644 arduino-ide-extension/src/browser/contributions/interface-scale.ts diff --git a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts index afa5eb5ec..380c2c9ab 100644 --- a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts +++ b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts @@ -333,6 +333,7 @@ import { StartupTaskProvider } from '../electron-common/startup-task'; import { DeleteSketch } from './contributions/delete-sketch'; import { UserFields } from './contributions/user-fields'; import { UpdateIndexes } from './contributions/update-indexes'; +import { InterfaceScale } from './contributions/interface-scale'; const registerArduinoThemes = () => { const themes: MonacoThemeJson[] = [ @@ -746,6 +747,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => { Contribution.configure(bind, UserFields); Contribution.configure(bind, DeleteSketch); Contribution.configure(bind, UpdateIndexes); + Contribution.configure(bind, InterfaceScale); bindContributionProvider(bind, StartupTaskProvider); bind(StartupTaskProvider).toService(BoardsServiceProvider); // to reuse the boards config in another window diff --git a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts index abbcf1b03..cc1451ec3 100644 --- a/arduino-ide-extension/src/browser/contributions/edit-contributions.ts +++ b/arduino-ide-extension/src/browser/contributions/edit-contributions.ts @@ -9,19 +9,11 @@ import { KeybindingRegistry, CommandRegistry, } from './contribution'; -import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus'; -import { - DisposableCollection, - MaybePromise, - nls, -} from '@theia/core/lib/common'; +import { ArduinoMenus } from '../menu/arduino-menus'; +import { nls } from '@theia/core/lib/common'; import type { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser'; import type { StandaloneCodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor'; -import { Settings } from '../dialogs/settings/settings'; -import { MainMenuManager } from '../../common/main-menu-manager'; -import debounce = require('lodash.debounce'); - // TODO: [macOS]: to remove `Start Dictation...` and `Emoji & Symbol` see this thread: https://github.com/electron/electron/issues/8283#issuecomment-269522072 // Depends on https://github.com/eclipse-theia/theia/pull/7964 @injectable() @@ -32,38 +24,6 @@ export class EditContributions extends Contribution { @inject(ClipboardService) private readonly clipboardService: ClipboardService; - @inject(MenuModelRegistry) - private readonly menuRegistry: MenuModelRegistry; - - @inject(MainMenuManager) - protected readonly mainMenuManager: MainMenuManager; - - private readonly menuActionsDisposables = new DisposableCollection(); - private fontScalingEnabled: EditContributions.FontScalingEnabled = { - increase: true, - decrease: true, - }; - - private currentScale: EditContributions.ScaleSettings; - private currentSettings: Settings; - private updateSettingsDebounced = debounce( - async () => { - await this.settingsService.update(this.currentSettings); - await this.settingsService.save(); - }, - 100, - { maxWait: 200 } - ); - - override onStart(): MaybePromise { - const updateCurrent = (settings: Settings) => { - this.currentSettings = settings; - this.currentScale = { ...settings }; - }; - this.settingsService.onDidChange((settings) => updateCurrent(settings)); - this.settingsService.settings().then((settings) => updateCurrent(settings)); - } - override registerCommands(registry: CommandRegistry): void { registry.registerCommand(EditContributions.Commands.GO_TO_LINE, { execute: () => this.run('editor.action.gotoLine'), @@ -89,14 +49,6 @@ export class EditContributions extends Contribution { registry.registerCommand(EditContributions.Commands.USE_FOR_FIND, { execute: () => this.run('editor.action.previousSelectionMatchFindAction'), }); - registry.registerCommand(EditContributions.Commands.INCREASE_FONT_SIZE, { - execute: () => this.updateFontSize('increase'), - isEnabled: () => this.fontScalingEnabled.increase, - }); - registry.registerCommand(EditContributions.Commands.DECREASE_FONT_SIZE, { - execute: () => this.updateFontSize('decrease'), - isEnabled: () => this.fontScalingEnabled.decrease, - }); /* Tools */ registry.registerCommand( EditContributions.Commands.AUTO_FORMAT, { execute: () => this.run('editor.action.formatDocument') } @@ -207,70 +159,6 @@ ${value} label: nls.localize('arduino/editor/autoFormat', 'Auto Format'), // XXX: The Java IDE uses `Use Selection For Find`. order: '0', }); - - this.registerInterfaceScaleMenuActions(); - } - - private registerInterfaceScaleMenuActions( - newFontScalingEnabled = this.fontScalingEnabled - ): void { - this.menuActionsDisposables.dispose(); - const increaseFontSizeMenuAction = { - commandId: EditContributions.Commands.INCREASE_FONT_SIZE.id, - label: nls.localize( - 'arduino/editor/increaseFontSize', - 'Increase Font Size' - ), - order: '0', - }; - const decreaseFontSizeMenuAction = { - commandId: EditContributions.Commands.DECREASE_FONT_SIZE.id, - label: nls.localize( - 'arduino/editor/decreaseFontSize', - 'Decrease Font Size' - ), - order: '1', - }; - - if (newFontScalingEnabled.increase) { - this.menuActionsDisposables.push( - this.menuRegistry.registerMenuAction( - ArduinoMenus.EDIT__FONT_CONTROL_GROUP, - increaseFontSizeMenuAction - ) - ); - } else { - this.menuActionsDisposables.push( - this.menuRegistry.registerMenuNode( - ArduinoMenus.EDIT__FONT_CONTROL_GROUP, - new PlaceholderMenuNode( - ArduinoMenus.EDIT__FONT_CONTROL_GROUP, - increaseFontSizeMenuAction.label, - { order: increaseFontSizeMenuAction.order } - ) - ) - ); - } - if (newFontScalingEnabled.decrease) { - this.menuActionsDisposables.push( - this.menuRegistry.registerMenuAction( - ArduinoMenus.EDIT__FONT_CONTROL_GROUP, - decreaseFontSizeMenuAction - ) - ); - } else { - this.menuActionsDisposables.push( - this.menuRegistry.registerMenuNode( - ArduinoMenus.EDIT__FONT_CONTROL_GROUP, - new PlaceholderMenuNode( - ArduinoMenus.EDIT__FONT_CONTROL_GROUP, - decreaseFontSizeMenuAction.label, - { order: decreaseFontSizeMenuAction.order } - ) - ) - ); - } - this.mainMenuManager.update(); } override registerKeybindings(registry: KeybindingRegistry): void { @@ -291,15 +179,6 @@ ${value} when: 'editorFocus', }); - registry.registerKeybinding({ - command: EditContributions.Commands.INCREASE_FONT_SIZE.id, - keybinding: 'CtrlCmd+=', - }); - registry.registerKeybinding({ - command: EditContributions.Commands.DECREASE_FONT_SIZE.id, - keybinding: 'CtrlCmd+-', - }); - registry.registerKeybinding({ command: EditContributions.Commands.FIND.id, keybinding: 'CtrlCmd+F', @@ -355,44 +234,6 @@ ${value} } } } - - private async updateFontSize(mode: 'increase' | 'decrease'): Promise { - if (this.currentSettings.autoScaleInterface) { - mode === 'increase' - ? this.currentScale.interfaceScale++ - : this.currentScale.interfaceScale--; - } else { - mode === 'increase' - ? this.currentScale.editorFontSize++ - : this.currentScale.editorFontSize++; - } - this.currentSettings = { - ...this.currentSettings, - editorFontSize: this.currentScale.editorFontSize, - interfaceScale: this.currentScale.interfaceScale, - }; - let newFontScalingEnabled: EditContributions.FontScalingEnabled = { - increase: true, - decrease: true, - }; - if (this.currentSettings.autoScaleInterface) { - newFontScalingEnabled = { - increase: - this.currentSettings.interfaceScale < EditContributions.ZoomLevel.MAX, - decrease: - this.currentSettings.interfaceScale > EditContributions.ZoomLevel.MIN, - }; - } - const isChanged = Object.keys(newFontScalingEnabled).some( - (key: keyof EditContributions.FontScalingEnabled) => - newFontScalingEnabled[key] !== this.fontScalingEnabled[key] - ); - if (isChanged) { - this.registerInterfaceScaleMenuActions(newFontScalingEnabled); - } - this.fontScalingEnabled = newFontScalingEnabled; - this.updateSettingsDebounced(); - } } export namespace EditContributions { @@ -424,29 +265,8 @@ export namespace EditContributions { export const USE_FOR_FIND: Command = { id: 'arduino-for-find', }; - export const INCREASE_FONT_SIZE: Command = { - id: 'arduino-increase-font-size', - }; - export const DECREASE_FONT_SIZE: Command = { - id: 'arduino-decrease-font-size', - }; export const AUTO_FORMAT: Command = { id: 'arduino-auto-format', // `Auto Format` should belong to `Tool`. }; } - - export namespace ZoomLevel { - export const MIN = -8; - export const MAX = 9; - } - - export interface FontScalingEnabled { - increase: boolean; - decrease: boolean; - } - - export type ScaleSettings = Pick< - Settings, - 'interfaceScale' | 'editorFontSize' - >; } diff --git a/arduino-ide-extension/src/browser/contributions/interface-scale.ts b/arduino-ide-extension/src/browser/contributions/interface-scale.ts new file mode 100644 index 000000000..f648ddba4 --- /dev/null +++ b/arduino-ide-extension/src/browser/contributions/interface-scale.ts @@ -0,0 +1,233 @@ +import { inject, injectable } from '@theia/core/shared/inversify'; +import { + Contribution, + Command, + MenuModelRegistry, + KeybindingRegistry, +} from './contribution'; +import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus'; +import { + CommandRegistry, + DisposableCollection, + MaybePromise, + nls, +} from '@theia/core/lib/common'; + +import { Settings } from '../dialogs/settings/settings'; +import { MainMenuManager } from '../../common/main-menu-manager'; +import debounce = require('lodash.debounce'); + +@injectable() +export class InterfaceScale extends Contribution { + @inject(MenuModelRegistry) + private readonly menuRegistry: MenuModelRegistry; + + @inject(MainMenuManager) + protected readonly mainMenuManager: MainMenuManager; + + private readonly menuActionsDisposables = new DisposableCollection(); + private fontScalingEnabled: InterfaceScale.FontScalingEnabled = { + increase: true, + decrease: true, + }; + + private currentScale: InterfaceScale.ScaleSettings; + private currentSettings: Settings; + private updateSettingsDebounced = debounce( + async () => { + await this.settingsService.update(this.currentSettings); + await this.settingsService.save(); + }, + 100, + { maxWait: 200 } + ); + + override onStart(): MaybePromise { + const updateCurrent = (settings: Settings) => { + this.currentSettings = settings; + this.currentScale = { ...settings }; + }; + this.settingsService.onDidChange((settings) => updateCurrent(settings)); + this.settingsService.settings().then((settings) => updateCurrent(settings)); + } + + override registerCommands(registry: CommandRegistry): void { + registry.registerCommand(InterfaceScale.Commands.INCREASE_FONT_SIZE, { + execute: () => this.updateFontSize('increase'), + isEnabled: () => this.fontScalingEnabled.increase, + }); + registry.registerCommand(InterfaceScale.Commands.DECREASE_FONT_SIZE, { + execute: () => this.updateFontSize('decrease'), + isEnabled: () => this.fontScalingEnabled.decrease, + }); + } + + override registerMenus(registry: MenuModelRegistry): void { + this.menuActionsDisposables.dispose(); + const increaseFontSizeMenuAction = { + commandId: InterfaceScale.Commands.INCREASE_FONT_SIZE.id, + label: nls.localize( + 'arduino/editor/increaseFontSize', + 'Increase Font Size' + ), + order: '0', + }; + const decreaseFontSizeMenuAction = { + commandId: InterfaceScale.Commands.DECREASE_FONT_SIZE.id, + label: nls.localize( + 'arduino/editor/decreaseFontSize', + 'Decrease Font Size' + ), + order: '1', + }; + + if (this.fontScalingEnabled.increase) { + this.menuActionsDisposables.push( + registry.registerMenuAction( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + increaseFontSizeMenuAction + ) + ); + } else { + this.menuActionsDisposables.push( + registry.registerMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + new PlaceholderMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + increaseFontSizeMenuAction.label, + { order: increaseFontSizeMenuAction.order } + ) + ) + ); + } + if (this.fontScalingEnabled.decrease) { + this.menuActionsDisposables.push( + this.menuRegistry.registerMenuAction( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + decreaseFontSizeMenuAction + ) + ); + } else { + this.menuActionsDisposables.push( + this.menuRegistry.registerMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + new PlaceholderMenuNode( + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, + decreaseFontSizeMenuAction.label, + { order: decreaseFontSizeMenuAction.order } + ) + ) + ); + } + this.mainMenuManager.update(); + } + + private async updateFontSize(mode: 'increase' | 'decrease'): Promise { + if (this.currentSettings.autoScaleInterface) { + mode === 'increase' + ? (this.currentScale.interfaceScale += InterfaceScale.ZoomLevel.STEP) + : (this.currentScale.interfaceScale -= InterfaceScale.ZoomLevel.STEP); + } else { + mode === 'increase' + ? (this.currentScale.editorFontSize += InterfaceScale.FontSize.STEP) + : (this.currentScale.editorFontSize -= InterfaceScale.FontSize.STEP); + } + this.currentSettings = { + ...this.currentSettings, + editorFontSize: this.currentScale.editorFontSize, + interfaceScale: this.currentScale.interfaceScale, + }; + let newFontScalingEnabled: InterfaceScale.FontScalingEnabled = { + increase: true, + decrease: true, + }; + if (this.currentSettings.autoScaleInterface) { + newFontScalingEnabled = { + increase: + this.currentSettings.interfaceScale + InterfaceScale.ZoomLevel.STEP <= + InterfaceScale.ZoomLevel.MAX, + decrease: + this.currentSettings.interfaceScale - InterfaceScale.ZoomLevel.STEP >= + InterfaceScale.ZoomLevel.MIN, + }; + } else { + newFontScalingEnabled = { + increase: + this.currentSettings.editorFontSize + InterfaceScale.FontSize.STEP <= + InterfaceScale.FontSize.MAX, + decrease: + this.currentSettings.editorFontSize - InterfaceScale.FontSize.STEP >= + InterfaceScale.FontSize.MIN, + }; + } + const isChanged = Object.keys(newFontScalingEnabled).some( + (key: keyof InterfaceScale.FontScalingEnabled) => + newFontScalingEnabled[key] !== this.fontScalingEnabled[key] + ); + if (isChanged) { + this.registerMenus(this.menuRegistry); + this.fontScalingEnabled = newFontScalingEnabled; + } + this.fontScalingEnabled = newFontScalingEnabled; + this.updateSettingsDebounced(); + } + + override registerKeybindings(registry: KeybindingRegistry): void { + registry.registerKeybinding({ + command: InterfaceScale.Commands.INCREASE_FONT_SIZE.id, + keybinding: 'CtrlCmd+=', + }); + registry.registerKeybinding({ + command: InterfaceScale.Commands.DECREASE_FONT_SIZE.id, + keybinding: 'CtrlCmd+-', + }); + } +} + +export namespace InterfaceScale { + export namespace Commands { + export const INCREASE_FONT_SIZE: Command = { + id: 'arduino-increase-font-size', + }; + export const DECREASE_FONT_SIZE: Command = { + id: 'arduino-decrease-font-size', + }; + } + + export namespace ZoomLevel { + export const MIN = -8; + export const MAX = 9; + export const STEP = 1; + + export function toPercentage(scale: number): number { + return scale * 20 + 100; + } + export function fromPercentage(percentage: number): number { + return (percentage - 100) / 20; + } + export namespace Step { + export function toPercentage(step: number): number { + return step * 20; + } + export function fromPercentage(percentage: number): number { + return percentage / 20; + } + } + } + + export namespace FontSize { + export const MIN = 8; + export const MAX = 72; + export const STEP = 2; + } + + export interface FontScalingEnabled { + increase: boolean; + decrease: boolean; + } + + export type ScaleSettings = Pick< + Settings, + 'interfaceScale' | 'editorFontSize' + >; +} diff --git a/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx b/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx index dac80d61c..8464eb5e2 100644 --- a/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx +++ b/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx @@ -23,14 +23,22 @@ import { LanguageInfo, } from '@theia/core/lib/common/i18n/localization'; import SettingsStepInput from './settings-step-input'; +import { InterfaceScale } from '../../contributions/interface-scale'; + +const maxScale = InterfaceScale.ZoomLevel.toPercentage( + InterfaceScale.ZoomLevel.MAX +); +const minScale = InterfaceScale.ZoomLevel.toPercentage( + InterfaceScale.ZoomLevel.MIN +); +const scaleStep = InterfaceScale.ZoomLevel.Step.fromPercentage( + InterfaceScale.ZoomLevel.STEP +); + +const maxFontSize = InterfaceScale.FontSize.MAX; +const minFontSize = InterfaceScale.FontSize.MIN; +const fontSizeStep = InterfaceScale.FontSize.STEP; -const maxScale = 280; -const minScale = -60; -const scaleStep = 20; - -const maxFontSize = 72; -const minFontSize = 0; -const fontSizeStep = 2; export class SettingsComponent extends React.Component< SettingsComponent.Props, SettingsComponent.State @@ -554,8 +562,7 @@ export class SettingsComponent extends React.Component< }; private setInterfaceScale = (percentage: number) => { - const interfaceScale = (percentage - 100) / 20; - + const interfaceScale = InterfaceScale.ZoomLevel.fromPercentage(percentage); this.setState({ interfaceScale }); }; From ca22d782e0c74ad9bf029d9743c30374cad1a90c Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Tue, 27 Sep 2022 10:18:13 +0200 Subject: [PATCH 4/9] remove excessive settings duplicate --- .../browser/contributions/interface-scale.ts | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/interface-scale.ts b/arduino-ide-extension/src/browser/contributions/interface-scale.ts index f648ddba4..0491d72b1 100644 --- a/arduino-ide-extension/src/browser/contributions/interface-scale.ts +++ b/arduino-ide-extension/src/browser/contributions/interface-scale.ts @@ -31,7 +31,6 @@ export class InterfaceScale extends Contribution { decrease: true, }; - private currentScale: InterfaceScale.ScaleSettings; private currentSettings: Settings; private updateSettingsDebounced = debounce( async () => { @@ -45,7 +44,6 @@ export class InterfaceScale extends Contribution { override onStart(): MaybePromise { const updateCurrent = (settings: Settings) => { this.currentSettings = settings; - this.currentScale = { ...settings }; }; this.settingsService.onDidChange((settings) => updateCurrent(settings)); this.settingsService.settings().then((settings) => updateCurrent(settings)); @@ -125,18 +123,13 @@ export class InterfaceScale extends Contribution { private async updateFontSize(mode: 'increase' | 'decrease'): Promise { if (this.currentSettings.autoScaleInterface) { mode === 'increase' - ? (this.currentScale.interfaceScale += InterfaceScale.ZoomLevel.STEP) - : (this.currentScale.interfaceScale -= InterfaceScale.ZoomLevel.STEP); + ? (this.currentSettings.interfaceScale += InterfaceScale.ZoomLevel.STEP) + : (this.currentSettings.interfaceScale -= InterfaceScale.ZoomLevel.STEP); } else { mode === 'increase' - ? (this.currentScale.editorFontSize += InterfaceScale.FontSize.STEP) - : (this.currentScale.editorFontSize -= InterfaceScale.FontSize.STEP); + ? (this.currentSettings.editorFontSize += InterfaceScale.FontSize.STEP) + : (this.currentSettings.editorFontSize -= InterfaceScale.FontSize.STEP); } - this.currentSettings = { - ...this.currentSettings, - editorFontSize: this.currentScale.editorFontSize, - interfaceScale: this.currentScale.interfaceScale, - }; let newFontScalingEnabled: InterfaceScale.FontScalingEnabled = { increase: true, decrease: true, @@ -225,9 +218,4 @@ export namespace InterfaceScale { increase: boolean; decrease: boolean; } - - export type ScaleSettings = Pick< - Settings, - 'interfaceScale' | 'editorFontSize' - >; } From 4a64138c3e9c467346ca06d2052ed1c872eda270 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Mon, 3 Oct 2022 08:17:05 +0200 Subject: [PATCH 5/9] remove useless async --- .../src/browser/contributions/interface-scale.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/interface-scale.ts b/arduino-ide-extension/src/browser/contributions/interface-scale.ts index 0491d72b1..29f21ce9c 100644 --- a/arduino-ide-extension/src/browser/contributions/interface-scale.ts +++ b/arduino-ide-extension/src/browser/contributions/interface-scale.ts @@ -120,11 +120,12 @@ export class InterfaceScale extends Contribution { this.mainMenuManager.update(); } - private async updateFontSize(mode: 'increase' | 'decrease'): Promise { + private updateFontSize(mode: 'increase' | 'decrease'): void { if (this.currentSettings.autoScaleInterface) { mode === 'increase' ? (this.currentSettings.interfaceScale += InterfaceScale.ZoomLevel.STEP) - : (this.currentSettings.interfaceScale -= InterfaceScale.ZoomLevel.STEP); + : (this.currentSettings.interfaceScale -= + InterfaceScale.ZoomLevel.STEP); } else { mode === 'increase' ? (this.currentSettings.editorFontSize += InterfaceScale.FontSize.STEP) From 402ae13f13e5dfd181021aaf6f208a6b3d5d78da Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Mon, 3 Oct 2022 08:20:14 +0200 Subject: [PATCH 6/9] fix interface scale step --- .../src/browser/dialogs/settings/settings-component.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx b/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx index 8464eb5e2..bbc73b8c3 100644 --- a/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx +++ b/arduino-ide-extension/src/browser/dialogs/settings/settings-component.tsx @@ -31,7 +31,7 @@ const maxScale = InterfaceScale.ZoomLevel.toPercentage( const minScale = InterfaceScale.ZoomLevel.toPercentage( InterfaceScale.ZoomLevel.MIN ); -const scaleStep = InterfaceScale.ZoomLevel.Step.fromPercentage( +const scaleStep = InterfaceScale.ZoomLevel.Step.toPercentage( InterfaceScale.ZoomLevel.STEP ); From d0566ec3472cf2ef65931af229bf317d54a1eb23 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Mon, 3 Oct 2022 11:59:27 +0200 Subject: [PATCH 7/9] change mainMenuManager visibility to private --- .../src/browser/contributions/interface-scale.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/browser/contributions/interface-scale.ts b/arduino-ide-extension/src/browser/contributions/interface-scale.ts index 29f21ce9c..da9ba7eaf 100644 --- a/arduino-ide-extension/src/browser/contributions/interface-scale.ts +++ b/arduino-ide-extension/src/browser/contributions/interface-scale.ts @@ -23,7 +23,7 @@ export class InterfaceScale extends Contribution { private readonly menuRegistry: MenuModelRegistry; @inject(MainMenuManager) - protected readonly mainMenuManager: MainMenuManager; + private readonly mainMenuManager: MainMenuManager; private readonly menuActionsDisposables = new DisposableCollection(); private fontScalingEnabled: InterfaceScale.FontScalingEnabled = { From 26f99c64e98f276f1232d98b4c6dce66283e1c9b Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Tue, 4 Oct 2022 12:28:30 +0200 Subject: [PATCH 8/9] fix menu registration --- .../src/browser/contributions/interface-scale.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/interface-scale.ts b/arduino-ide-extension/src/browser/contributions/interface-scale.ts index da9ba7eaf..4c126f93c 100644 --- a/arduino-ide-extension/src/browser/contributions/interface-scale.ts +++ b/arduino-ide-extension/src/browser/contributions/interface-scale.ts @@ -159,10 +159,9 @@ export class InterfaceScale extends Contribution { newFontScalingEnabled[key] !== this.fontScalingEnabled[key] ); if (isChanged) { - this.registerMenus(this.menuRegistry); this.fontScalingEnabled = newFontScalingEnabled; + this.registerMenus(this.menuRegistry); } - this.fontScalingEnabled = newFontScalingEnabled; this.updateSettingsDebounced(); } From b118a1f3bbdd445b6cc5362d3974f5052f0847f8 Mon Sep 17 00:00:00 2001 From: Alberto Iannaccone Date: Tue, 4 Oct 2022 13:15:50 +0200 Subject: [PATCH 9/9] update menu actions when autoScaleInterface changes --- .../browser/contributions/interface-scale.ts | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/arduino-ide-extension/src/browser/contributions/interface-scale.ts b/arduino-ide-extension/src/browser/contributions/interface-scale.ts index 4c126f93c..eefd1ab6e 100644 --- a/arduino-ide-extension/src/browser/contributions/interface-scale.ts +++ b/arduino-ide-extension/src/browser/contributions/interface-scale.ts @@ -44,6 +44,7 @@ export class InterfaceScale extends Contribution { override onStart(): MaybePromise { const updateCurrent = (settings: Settings) => { this.currentSettings = settings; + this.updateFontScalingEnabled(); }; this.settingsService.onDidChange((settings) => updateCurrent(settings)); this.settingsService.settings().then((settings) => updateCurrent(settings)); @@ -120,23 +121,14 @@ export class InterfaceScale extends Contribution { this.mainMenuManager.update(); } - private updateFontSize(mode: 'increase' | 'decrease'): void { - if (this.currentSettings.autoScaleInterface) { - mode === 'increase' - ? (this.currentSettings.interfaceScale += InterfaceScale.ZoomLevel.STEP) - : (this.currentSettings.interfaceScale -= - InterfaceScale.ZoomLevel.STEP); - } else { - mode === 'increase' - ? (this.currentSettings.editorFontSize += InterfaceScale.FontSize.STEP) - : (this.currentSettings.editorFontSize -= InterfaceScale.FontSize.STEP); - } - let newFontScalingEnabled: InterfaceScale.FontScalingEnabled = { + private updateFontScalingEnabled(): void { + let fontScalingEnabled = { increase: true, decrease: true, }; + if (this.currentSettings.autoScaleInterface) { - newFontScalingEnabled = { + fontScalingEnabled = { increase: this.currentSettings.interfaceScale + InterfaceScale.ZoomLevel.STEP <= InterfaceScale.ZoomLevel.MAX, @@ -145,7 +137,7 @@ export class InterfaceScale extends Contribution { InterfaceScale.ZoomLevel.MIN, }; } else { - newFontScalingEnabled = { + fontScalingEnabled = { increase: this.currentSettings.editorFontSize + InterfaceScale.FontSize.STEP <= InterfaceScale.FontSize.MAX, @@ -154,14 +146,29 @@ export class InterfaceScale extends Contribution { InterfaceScale.FontSize.MIN, }; } - const isChanged = Object.keys(newFontScalingEnabled).some( + + const isChanged = Object.keys(fontScalingEnabled).some( (key: keyof InterfaceScale.FontScalingEnabled) => - newFontScalingEnabled[key] !== this.fontScalingEnabled[key] + fontScalingEnabled[key] !== this.fontScalingEnabled[key] ); if (isChanged) { - this.fontScalingEnabled = newFontScalingEnabled; + this.fontScalingEnabled = fontScalingEnabled; this.registerMenus(this.menuRegistry); } + } + + private updateFontSize(mode: 'increase' | 'decrease'): void { + if (this.currentSettings.autoScaleInterface) { + mode === 'increase' + ? (this.currentSettings.interfaceScale += InterfaceScale.ZoomLevel.STEP) + : (this.currentSettings.interfaceScale -= + InterfaceScale.ZoomLevel.STEP); + } else { + mode === 'increase' + ? (this.currentSettings.editorFontSize += InterfaceScale.FontSize.STEP) + : (this.currentSettings.editorFontSize -= InterfaceScale.FontSize.STEP); + } + this.updateFontScalingEnabled(); this.updateSettingsDebounced(); }