@@ -3,7 +3,7 @@ import { app, shell, dialog, ipcMain } from 'electron'
3
3
import is from 'electron-is'
4
4
import { readFile , unlink } from 'fs'
5
5
import { extname , basename } from 'path'
6
- import { isEmpty } from 'lodash'
6
+ import { isEmpty , isEqual } from 'lodash'
7
7
8
8
import {
9
9
APP_RUN_MODE ,
@@ -42,11 +42,9 @@ export default class Application extends EventEmitter {
42
42
}
43
43
44
44
init ( ) {
45
- this . configManager = this . initConfigManager ( )
45
+ this . initConfigManager ( )
46
46
47
- this . locale = this . configManager . getLocale ( )
48
- this . localeManager = setupLocaleManager ( this . locale )
49
- this . i18n = this . localeManager . getI18n ( )
47
+ this . initLocaleManager ( )
50
48
51
49
this . setupApplicationMenu ( )
52
50
@@ -66,9 +64,9 @@ export default class Application extends EventEmitter {
66
64
67
65
this . initDockManager ( )
68
66
69
- this . autoLaunchManager = new AutoLaunchManager ( )
67
+ this . initAutoLaunchManager ( )
70
68
71
- this . energyManager = new EnergyManager ( )
69
+ this . initEnergyManager ( )
72
70
73
71
this . initUpdaterManager ( )
74
72
@@ -87,7 +85,7 @@ export default class Application extends EventEmitter {
87
85
88
86
initConfigManager ( ) {
89
87
this . configListeners = { }
90
- return new ConfigManager ( )
88
+ this . configManager = new ConfigManager ( )
91
89
}
92
90
93
91
offConfigListeners ( ) {
@@ -101,6 +99,12 @@ export default class Application extends EventEmitter {
101
99
this . configListeners = { }
102
100
}
103
101
102
+ initLocaleManager ( ) {
103
+ this . locale = this . configManager . getLocale ( )
104
+ this . localeManager = setupLocaleManager ( this . locale )
105
+ this . i18n = this . localeManager . getI18n ( )
106
+ }
107
+
104
108
setupApplicationMenu ( ) {
105
109
this . menuManager = new MenuManager ( )
106
110
this . menuManager . setup ( this . locale )
@@ -162,11 +166,20 @@ export default class Application extends EventEmitter {
162
166
} )
163
167
}
164
168
169
+ initAutoLaunchManager ( ) {
170
+ this . autoLaunchManager = new AutoLaunchManager ( )
171
+ }
172
+
173
+ initEnergyManager ( ) {
174
+ this . energyManager = new EnergyManager ( )
175
+ }
176
+
165
177
initTrayManager ( ) {
166
178
this . trayManager = new TrayManager ( {
167
179
theme : this . configManager . getUserConfig ( 'tray-theme' ) ,
168
180
systemTheme : this . themeManager . getSystemTheme ( ) ,
169
- speedometer : this . configManager . getUserConfig ( 'tray-speedometer' )
181
+ speedometer : this . configManager . getUserConfig ( 'tray-speedometer' ) ,
182
+ runMode : this . configManager . getUserConfig ( 'run-mode' )
170
183
} )
171
184
172
185
this . watchTraySpeedometerEnabledChange ( )
@@ -191,8 +204,8 @@ export default class Application extends EventEmitter {
191
204
watchTraySpeedometerEnabledChange ( ) {
192
205
const { userConfig } = this . configManager
193
206
const key = 'tray-speedometer'
194
- this . configListeners [ key ] = userConfig . onDidChange ( 'tray-speedometer' , async ( newValue , oldValue ) => {
195
- logger . info ( ' [Motrix] detected tray speedometer value change event:' , newValue , oldValue )
207
+ this . configListeners [ key ] = userConfig . onDidChange ( key , async ( newValue , oldValue ) => {
208
+ logger . info ( ` [Motrix] detected ${ key } value change event:` , newValue , oldValue )
196
209
this . trayManager . handleSpeedometerEnableChange ( newValue )
197
210
} )
198
211
}
@@ -203,6 +216,55 @@ export default class Application extends EventEmitter {
203
216
} )
204
217
}
205
218
219
+ watchOpenAtLoginChange ( ) {
220
+ const { userConfig } = this . configManager
221
+ const key = 'open-at-login'
222
+ this . configListeners [ key ] = userConfig . onDidChange ( key , async ( newValue , oldValue ) => {
223
+ logger . info ( `[Motrix] detected ${ key } value change event:` , newValue , oldValue )
224
+ if ( is . linux ( ) ) {
225
+ return
226
+ }
227
+
228
+ if ( newValue ) {
229
+ this . autoLaunchManager . enable ( )
230
+ } else {
231
+ this . autoLaunchManager . disable ( )
232
+ }
233
+ } )
234
+ }
235
+
236
+ watchProtocolsChange ( ) {
237
+ const { userConfig } = this . configManager
238
+ const key = 'protocols'
239
+ this . configListeners [ key ] = userConfig . onDidChange ( key , async ( newValue , oldValue ) => {
240
+ logger . info ( `[Motrix] detected ${ key } value change event:` , newValue , oldValue )
241
+
242
+ if ( ! newValue || isEqual ( newValue , oldValue ) ) {
243
+ return
244
+ }
245
+
246
+ logger . info ( '[Motrix] setup protocols client:' , newValue )
247
+ this . protocolManager . setup ( newValue )
248
+ } )
249
+ }
250
+
251
+ watchRunModeChange ( ) {
252
+ const { userConfig } = this . configManager
253
+ const key = 'run-mode'
254
+ this . configListeners [ key ] = userConfig . onDidChange ( key , async ( newValue , oldValue ) => {
255
+ logger . info ( `[Motrix] detected ${ key } value change event:` , newValue , oldValue )
256
+ this . trayManager . handleRunModeChange ( newValue )
257
+
258
+ if ( newValue !== APP_RUN_MODE . TRAY ) {
259
+ this . dockManager . show ( )
260
+ } else {
261
+ this . dockManager . hide ( )
262
+ // Hiding the dock icon will trigger the entire app to hide.
263
+ this . show ( )
264
+ }
265
+ } )
266
+ }
267
+
206
268
initUPnPManager ( ) {
207
269
this . upnp = new UPnPManager ( )
208
270
@@ -365,7 +427,7 @@ export default class Application extends EventEmitter {
365
427
366
428
this . windowManager . on ( 'leave-full-screen' , ( window ) => {
367
429
const mode = this . configManager . getUserConfig ( 'run-mode' )
368
- if ( mode !== APP_RUN_MODE . STANDARD ) {
430
+ if ( mode === APP_RUN_MODE . TRAY ) {
369
431
this . dockManager . hide ( )
370
432
}
371
433
} )
@@ -393,6 +455,7 @@ export default class Application extends EventEmitter {
393
455
this . isReady = true
394
456
this . emit ( 'ready' )
395
457
} )
458
+
396
459
if ( is . macOS ( ) ) {
397
460
this . touchBarManager . setup ( page , win )
398
461
}
@@ -621,18 +684,6 @@ export default class Application extends EventEmitter {
621
684
this . quit ( )
622
685
} )
623
686
624
- this . on ( 'application:open-at-login' , ( openAtLogin ) => {
625
- if ( is . linux ( ) ) {
626
- return
627
- }
628
-
629
- if ( openAtLogin ) {
630
- this . autoLaunchManager . enable ( )
631
- } else {
632
- this . autoLaunchManager . disable ( )
633
- }
634
- } )
635
-
636
687
this . on ( 'application:show' , ( { page } ) => {
637
688
this . show ( page )
638
689
} )
@@ -769,6 +820,10 @@ export default class Application extends EventEmitter {
769
820
this . configManager . userConfig . onDidAnyChange ( ( ) => this . handleConfigChange ( 'user' ) )
770
821
this . configManager . systemConfig . onDidAnyChange ( ( ) => this . handleConfigChange ( 'system' ) )
771
822
823
+ this . watchOpenAtLoginChange ( )
824
+ this . watchProtocolsChange ( )
825
+ this . watchRunModeChange ( )
826
+
772
827
this . on ( 'download-status-change' , ( downloading ) => {
773
828
this . trayManager . handleDownloadStatusChange ( downloading )
774
829
if ( downloading ) {
0 commit comments