-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathgui-settings.ts
215 lines (178 loc) · 6.48 KB
/
gui-settings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { app } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
import { IGuiSettingsState, SYSTEM_PREFERRED_LOCALE_KEY } from '../shared/gui-settings-state';
import log from '../shared/logging';
const settingsSchema: Record<keyof IGuiSettingsState, string> = {
preferredLocale: 'string',
autoConnect: 'boolean',
enableSystemNotifications: 'boolean',
monochromaticIcon: 'boolean',
startMinimized: 'boolean',
unpinnedWindow: 'boolean',
browsedForSplitTunnelingApplications: 'Array<string>',
changelogDisplayedForVersion: 'string',
updateDismissedForVersion: 'string',
animateMap: 'boolean',
};
const defaultSettings: IGuiSettingsState = {
preferredLocale: SYSTEM_PREFERRED_LOCALE_KEY,
autoConnect: false,
enableSystemNotifications: true,
monochromaticIcon: false,
startMinimized: false,
unpinnedWindow: process.platform !== 'win32' && process.platform !== 'darwin',
browsedForSplitTunnelingApplications: [],
changelogDisplayedForVersion: '',
updateDismissedForVersion: '',
animateMap: true,
};
export default class GuiSettings {
public onChange?: (newState: IGuiSettingsState, oldState: IGuiSettingsState) => void;
private stateValue: IGuiSettingsState = { ...defaultSettings };
get state(): IGuiSettingsState {
return this.stateValue;
}
set preferredLocale(newValue: string) {
this.changeStateAndNotify({ ...this.stateValue, preferredLocale: newValue });
}
get preferredLocale(): string {
return this.stateValue.preferredLocale;
}
set enableSystemNotifications(newValue: boolean) {
this.changeStateAndNotify({ ...this.stateValue, enableSystemNotifications: newValue });
}
get enableSystemNotifications(): boolean {
return this.stateValue.enableSystemNotifications;
}
set autoConnect(newValue: boolean) {
this.changeStateAndNotify({ ...this.stateValue, autoConnect: newValue });
}
get autoConnect(): boolean {
return this.stateValue.autoConnect;
}
set monochromaticIcon(newValue: boolean) {
this.changeStateAndNotify({ ...this.stateValue, monochromaticIcon: newValue });
}
get monochromaticIcon(): boolean {
return this.stateValue.monochromaticIcon;
}
set startMinimized(newValue: boolean) {
this.changeStateAndNotify({ ...this.stateValue, startMinimized: newValue });
}
get startMinimized(): boolean {
return this.stateValue.startMinimized;
}
set unpinnedWindow(newValue: boolean) {
this.changeStateAndNotify({ ...this.stateValue, unpinnedWindow: newValue });
}
get unpinnedWindow(): boolean {
return this.stateValue.unpinnedWindow;
}
public addBrowsedForSplitTunnelingApplications(newApp: string) {
this.changeStateAndNotify({
...this.stateValue,
browsedForSplitTunnelingApplications: [...this.browsedForSplitTunnelingApplications, newApp],
});
}
public deleteBrowsedForSplitTunnelingApplications(path: string) {
this.changeStateAndNotify({
...this.stateValue,
browsedForSplitTunnelingApplications: this.browsedForSplitTunnelingApplications.filter(
(application) => application !== path,
),
});
}
get browsedForSplitTunnelingApplications(): Array<string> {
return this.stateValue.browsedForSplitTunnelingApplications;
}
set changelogDisplayedForVersion(newValue: string | undefined) {
this.changeStateAndNotify({ ...this.stateValue, changelogDisplayedForVersion: newValue ?? '' });
}
get changelogDisplayedForVersion(): string | undefined {
return this.stateValue.changelogDisplayedForVersion === ''
? undefined
: this.stateValue.changelogDisplayedForVersion;
}
set updateDismissedForVersion(newValue: string | undefined) {
this.changeStateAndNotify({ ...this.stateValue, updateDismissedForVersion: newValue ?? '' });
}
get updateDismissedForVersion(): string | undefined {
return this.stateValue.updateDismissedForVersion === ''
? undefined
: this.stateValue.updateDismissedForVersion;
}
set animateMap(newValue: boolean) {
this.changeStateAndNotify({ ...this.stateValue, animateMap: newValue });
}
get animateMap(): boolean {
return this.stateValue.animateMap;
}
public load() {
try {
const settingsFile = this.filePath();
const contents = fs.readFileSync(settingsFile, 'utf8');
const rawJson = JSON.parse(contents);
this.stateValue = {
...defaultSettings,
...this.validateSettings(rawJson),
};
} catch (e) {
const error = e as Error & { code?: string };
// Read settings if the file exists, otherwise write the default settings to it.
if (error.code === 'ENOENT') {
log.verbose('Creating gui-settings file and writing the default settings to it');
this.store();
} else {
log.error(`Failed to read GUI settings file: ${error}`);
}
}
}
public store() {
try {
const settingsFile = this.filePath();
fs.writeFileSync(settingsFile, JSON.stringify(this.stateValue));
} catch (error) {
log.error(`Failed to write GUI settings file: ${error}`);
}
}
private validateSettings(settings: Record<string, unknown>) {
Object.entries(settingsSchema).forEach(([key, expectedType]) => {
if (key in settings) {
if (/^Array<.*>/.test(expectedType)) {
const value = settings[key];
if (!Array.isArray(value)) {
throw new Error(`Expected ${key} to be array but wasn't`);
} else {
const expectedInnerType = expectedType.replace(/^Array</, '').replace(/>$/, '');
const innerTypes: string[] = value.map((value) => typeof value);
if (
innerTypes.length > 0 &&
(innerTypes.some((value) => value !== innerTypes[0]) ||
innerTypes[0] !== expectedInnerType)
) {
throw new Error(`Expected ${key} to contain ${expectedInnerType}s`);
}
}
} else {
const actualType = typeof settings[key];
if (actualType !== expectedType) {
throw new Error(`Expected ${key} to be of type ${expectedType} but was ${actualType}`);
}
}
}
});
return settings as Partial<IGuiSettingsState>;
}
private filePath() {
return path.join(app.getPath('userData'), 'gui_settings.json');
}
private changeStateAndNotify(newState: IGuiSettingsState) {
const oldState = this.stateValue;
this.stateValue = newState;
this.store();
if (this.onChange) {
this.onChange({ ...newState }, oldState);
}
}
}