1
1
import { BrowserWindow , shell , screen , Menu , nativeTheme } from 'electron' ;
2
2
import { rendererAppName , rendererAppPort } from './constants' ;
3
3
import { environment } from '../environments/environment' ;
4
- import { join } from 'path' ;
4
+ import path , { join } from 'path' ;
5
5
import { format } from 'url' ;
6
6
import { getMenu } from './menu' ;
7
+ import * as fs from 'fs' ;
7
8
8
9
export default class App {
9
10
// Keep a global reference of the window object, if you don't, the window will
@@ -100,6 +101,7 @@ export default class App {
100
101
}
101
102
102
103
static setTheme ( source : 'system' | 'dark' | 'light' ) {
104
+ this . saveSetting ( 'theme' , source ) ;
103
105
nativeTheme . themeSource = source ;
104
106
}
105
107
@@ -122,6 +124,32 @@ export default class App {
122
124
}
123
125
}
124
126
127
+ private static saveSetting < T > ( key : string , value : T ) {
128
+ const userData = App . application . getPath ( 'userData' ) ;
129
+ const settingsPath = path . join ( userData , 'settings.json' ) ;
130
+
131
+ let settings = { } ;
132
+ if ( fs . existsSync ( settingsPath ) ) {
133
+ settings = JSON . parse ( fs . readFileSync ( settingsPath , 'utf8' ) ) ;
134
+ }
135
+
136
+ settings [ key ] = value ;
137
+
138
+ fs . writeFileSync ( settingsPath , JSON . stringify ( settings ) , 'utf8' ) ;
139
+ }
140
+
141
+ private static getSetting < T > ( key : string ) : T | undefined {
142
+ const userData = App . application . getPath ( 'userData' ) ;
143
+ const settingsPath = path . join ( userData , 'settings.json' ) ;
144
+
145
+ let settings = { } ;
146
+ if ( fs . existsSync ( settingsPath ) ) {
147
+ settings = JSON . parse ( fs . readFileSync ( settingsPath , 'utf8' ) ) ;
148
+ }
149
+
150
+ return settings [ key ] ;
151
+ }
152
+
125
153
static main ( app : Electron . App , browserWindow : typeof BrowserWindow ) {
126
154
// we pass the Electron.App object and the
127
155
// Electron.BrowserWindow into this function
@@ -131,6 +159,8 @@ export default class App {
131
159
App . BrowserWindow = browserWindow ;
132
160
App . application = app ;
133
161
162
+ nativeTheme . themeSource = App . getSetting ( 'theme' ) ?? 'system' ;
163
+
134
164
App . application . on ( 'window-all-closed' , App . onWindowAllClosed ) ; // Quit when all windows are closed.
135
165
App . application . on ( 'ready' , App . onReady ) ; // App is ready to load data
136
166
App . application . on ( 'activate' , App . onActivate ) ; // App is activated
0 commit comments