@@ -10,12 +10,17 @@ import {
10
10
CommandRegistry ,
11
11
} from './contribution' ;
12
12
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' ;
14
18
import type { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser' ;
15
19
import type { StandaloneCodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneCodeEditor' ;
16
20
17
21
import { Settings } from '../dialogs/settings/settings' ;
18
22
import { MainMenuManager } from '../../common/main-menu-manager' ;
23
+ import debounce = require( 'lodash.debounce' ) ;
19
24
20
25
// TODO: [macOS]: to remove `Start Dictation...` and `Emoji & Symbol` see this thread: https://github.com/electron/electron/issues/8283#issuecomment-269522072
21
26
// Depends on https://github.com/eclipse-theia/theia/pull/7964
@@ -39,25 +44,24 @@ export class EditContributions extends Contribution {
39
44
decrease : true ,
40
45
} ;
41
46
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 } ;
46
62
} ;
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 ) ) ;
61
65
}
62
66
63
67
override registerCommands ( registry : CommandRegistry ) : void {
@@ -86,31 +90,11 @@ export class EditContributions extends Contribution {
86
90
execute : ( ) => this . run ( 'editor.action.previousSelectionMatchFindAction' ) ,
87
91
} ) ;
88
92
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' ) ,
100
94
isEnabled : ( ) => this . fontScalingEnabled . increase ,
101
95
} ) ;
102
96
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' ) ,
114
98
isEnabled : ( ) => this . fontScalingEnabled . decrease ,
115
99
} ) ;
116
100
/* Tools */ registry . registerCommand (
@@ -371,6 +355,44 @@ ${value}
371
355
}
372
356
}
373
357
}
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
+ }
374
396
}
375
397
376
398
export namespace EditContributions {
@@ -422,4 +444,9 @@ export namespace EditContributions {
422
444
increase : boolean ;
423
445
decrease : boolean ;
424
446
}
447
+
448
+ export type ScaleSettings = Pick <
449
+ Settings ,
450
+ 'interfaceScale' | 'editorFontSize'
451
+ > ;
425
452
}
0 commit comments