|
| 1 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 2 | +import { |
| 3 | + Contribution, |
| 4 | + Command, |
| 5 | + MenuModelRegistry, |
| 6 | + KeybindingRegistry, |
| 7 | +} from './contribution'; |
| 8 | +import { ArduinoMenus, PlaceholderMenuNode } from '../menu/arduino-menus'; |
| 9 | +import { |
| 10 | + CommandRegistry, |
| 11 | + DisposableCollection, |
| 12 | + MaybePromise, |
| 13 | + nls, |
| 14 | +} from '@theia/core/lib/common'; |
| 15 | + |
| 16 | +import { Settings } from '../dialogs/settings/settings'; |
| 17 | +import { MainMenuManager } from '../../common/main-menu-manager'; |
| 18 | +import debounce = require('lodash.debounce'); |
| 19 | + |
| 20 | +@injectable() |
| 21 | +export class InterfaceScale extends Contribution { |
| 22 | + @inject(MenuModelRegistry) |
| 23 | + private readonly menuRegistry: MenuModelRegistry; |
| 24 | + |
| 25 | + @inject(MainMenuManager) |
| 26 | + private readonly mainMenuManager: MainMenuManager; |
| 27 | + |
| 28 | + private readonly menuActionsDisposables = new DisposableCollection(); |
| 29 | + private fontScalingEnabled: InterfaceScale.FontScalingEnabled = { |
| 30 | + increase: true, |
| 31 | + decrease: true, |
| 32 | + }; |
| 33 | + |
| 34 | + private currentSettings: Settings; |
| 35 | + private updateSettingsDebounced = debounce( |
| 36 | + async () => { |
| 37 | + await this.settingsService.update(this.currentSettings); |
| 38 | + await this.settingsService.save(); |
| 39 | + }, |
| 40 | + 100, |
| 41 | + { maxWait: 200 } |
| 42 | + ); |
| 43 | + |
| 44 | + override onStart(): MaybePromise<void> { |
| 45 | + const updateCurrent = (settings: Settings) => { |
| 46 | + this.currentSettings = settings; |
| 47 | + this.updateFontScalingEnabled(); |
| 48 | + }; |
| 49 | + this.settingsService.onDidChange((settings) => updateCurrent(settings)); |
| 50 | + this.settingsService.settings().then((settings) => updateCurrent(settings)); |
| 51 | + } |
| 52 | + |
| 53 | + override registerCommands(registry: CommandRegistry): void { |
| 54 | + registry.registerCommand(InterfaceScale.Commands.INCREASE_FONT_SIZE, { |
| 55 | + execute: () => this.updateFontSize('increase'), |
| 56 | + isEnabled: () => this.fontScalingEnabled.increase, |
| 57 | + }); |
| 58 | + registry.registerCommand(InterfaceScale.Commands.DECREASE_FONT_SIZE, { |
| 59 | + execute: () => this.updateFontSize('decrease'), |
| 60 | + isEnabled: () => this.fontScalingEnabled.decrease, |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + override registerMenus(registry: MenuModelRegistry): void { |
| 65 | + this.menuActionsDisposables.dispose(); |
| 66 | + const increaseFontSizeMenuAction = { |
| 67 | + commandId: InterfaceScale.Commands.INCREASE_FONT_SIZE.id, |
| 68 | + label: nls.localize( |
| 69 | + 'arduino/editor/increaseFontSize', |
| 70 | + 'Increase Font Size' |
| 71 | + ), |
| 72 | + order: '0', |
| 73 | + }; |
| 74 | + const decreaseFontSizeMenuAction = { |
| 75 | + commandId: InterfaceScale.Commands.DECREASE_FONT_SIZE.id, |
| 76 | + label: nls.localize( |
| 77 | + 'arduino/editor/decreaseFontSize', |
| 78 | + 'Decrease Font Size' |
| 79 | + ), |
| 80 | + order: '1', |
| 81 | + }; |
| 82 | + |
| 83 | + if (this.fontScalingEnabled.increase) { |
| 84 | + this.menuActionsDisposables.push( |
| 85 | + registry.registerMenuAction( |
| 86 | + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, |
| 87 | + increaseFontSizeMenuAction |
| 88 | + ) |
| 89 | + ); |
| 90 | + } else { |
| 91 | + this.menuActionsDisposables.push( |
| 92 | + registry.registerMenuNode( |
| 93 | + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, |
| 94 | + new PlaceholderMenuNode( |
| 95 | + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, |
| 96 | + increaseFontSizeMenuAction.label, |
| 97 | + { order: increaseFontSizeMenuAction.order } |
| 98 | + ) |
| 99 | + ) |
| 100 | + ); |
| 101 | + } |
| 102 | + if (this.fontScalingEnabled.decrease) { |
| 103 | + this.menuActionsDisposables.push( |
| 104 | + this.menuRegistry.registerMenuAction( |
| 105 | + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, |
| 106 | + decreaseFontSizeMenuAction |
| 107 | + ) |
| 108 | + ); |
| 109 | + } else { |
| 110 | + this.menuActionsDisposables.push( |
| 111 | + this.menuRegistry.registerMenuNode( |
| 112 | + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, |
| 113 | + new PlaceholderMenuNode( |
| 114 | + ArduinoMenus.EDIT__FONT_CONTROL_GROUP, |
| 115 | + decreaseFontSizeMenuAction.label, |
| 116 | + { order: decreaseFontSizeMenuAction.order } |
| 117 | + ) |
| 118 | + ) |
| 119 | + ); |
| 120 | + } |
| 121 | + this.mainMenuManager.update(); |
| 122 | + } |
| 123 | + |
| 124 | + private updateFontScalingEnabled(): void { |
| 125 | + let fontScalingEnabled = { |
| 126 | + increase: true, |
| 127 | + decrease: true, |
| 128 | + }; |
| 129 | + |
| 130 | + if (this.currentSettings.autoScaleInterface) { |
| 131 | + fontScalingEnabled = { |
| 132 | + increase: |
| 133 | + this.currentSettings.interfaceScale + InterfaceScale.ZoomLevel.STEP <= |
| 134 | + InterfaceScale.ZoomLevel.MAX, |
| 135 | + decrease: |
| 136 | + this.currentSettings.interfaceScale - InterfaceScale.ZoomLevel.STEP >= |
| 137 | + InterfaceScale.ZoomLevel.MIN, |
| 138 | + }; |
| 139 | + } else { |
| 140 | + fontScalingEnabled = { |
| 141 | + increase: |
| 142 | + this.currentSettings.editorFontSize + InterfaceScale.FontSize.STEP <= |
| 143 | + InterfaceScale.FontSize.MAX, |
| 144 | + decrease: |
| 145 | + this.currentSettings.editorFontSize - InterfaceScale.FontSize.STEP >= |
| 146 | + InterfaceScale.FontSize.MIN, |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + const isChanged = Object.keys(fontScalingEnabled).some( |
| 151 | + (key: keyof InterfaceScale.FontScalingEnabled) => |
| 152 | + fontScalingEnabled[key] !== this.fontScalingEnabled[key] |
| 153 | + ); |
| 154 | + if (isChanged) { |
| 155 | + this.fontScalingEnabled = fontScalingEnabled; |
| 156 | + this.registerMenus(this.menuRegistry); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private updateFontSize(mode: 'increase' | 'decrease'): void { |
| 161 | + if (this.currentSettings.autoScaleInterface) { |
| 162 | + mode === 'increase' |
| 163 | + ? (this.currentSettings.interfaceScale += InterfaceScale.ZoomLevel.STEP) |
| 164 | + : (this.currentSettings.interfaceScale -= |
| 165 | + InterfaceScale.ZoomLevel.STEP); |
| 166 | + } else { |
| 167 | + mode === 'increase' |
| 168 | + ? (this.currentSettings.editorFontSize += InterfaceScale.FontSize.STEP) |
| 169 | + : (this.currentSettings.editorFontSize -= InterfaceScale.FontSize.STEP); |
| 170 | + } |
| 171 | + this.updateFontScalingEnabled(); |
| 172 | + this.updateSettingsDebounced(); |
| 173 | + } |
| 174 | + |
| 175 | + override registerKeybindings(registry: KeybindingRegistry): void { |
| 176 | + registry.registerKeybinding({ |
| 177 | + command: InterfaceScale.Commands.INCREASE_FONT_SIZE.id, |
| 178 | + keybinding: 'CtrlCmd+=', |
| 179 | + }); |
| 180 | + registry.registerKeybinding({ |
| 181 | + command: InterfaceScale.Commands.DECREASE_FONT_SIZE.id, |
| 182 | + keybinding: 'CtrlCmd+-', |
| 183 | + }); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +export namespace InterfaceScale { |
| 188 | + export namespace Commands { |
| 189 | + export const INCREASE_FONT_SIZE: Command = { |
| 190 | + id: 'arduino-increase-font-size', |
| 191 | + }; |
| 192 | + export const DECREASE_FONT_SIZE: Command = { |
| 193 | + id: 'arduino-decrease-font-size', |
| 194 | + }; |
| 195 | + } |
| 196 | + |
| 197 | + export namespace ZoomLevel { |
| 198 | + export const MIN = -8; |
| 199 | + export const MAX = 9; |
| 200 | + export const STEP = 1; |
| 201 | + |
| 202 | + export function toPercentage(scale: number): number { |
| 203 | + return scale * 20 + 100; |
| 204 | + } |
| 205 | + export function fromPercentage(percentage: number): number { |
| 206 | + return (percentage - 100) / 20; |
| 207 | + } |
| 208 | + export namespace Step { |
| 209 | + export function toPercentage(step: number): number { |
| 210 | + return step * 20; |
| 211 | + } |
| 212 | + export function fromPercentage(percentage: number): number { |
| 213 | + return percentage / 20; |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + export namespace FontSize { |
| 219 | + export const MIN = 8; |
| 220 | + export const MAX = 72; |
| 221 | + export const STEP = 2; |
| 222 | + } |
| 223 | + |
| 224 | + export interface FontScalingEnabled { |
| 225 | + increase: boolean; |
| 226 | + decrease: boolean; |
| 227 | + } |
| 228 | +} |
0 commit comments