-
Notifications
You must be signed in to change notification settings - Fork 47
/
Settings.ts
86 lines (72 loc) · 2.1 KB
/
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
import storage = require('electron-json-storage');
export interface SettingsValues {
alwaysOnTop: boolean;
autoScroll: boolean;
hideLyrics: boolean;
theme: 'dark'|'light';
fontSize: 'eight-pt'|'ten-pt'|'twelve-pt'|'fourteen-pt'|'sixteen-pt';
refreshInterval: number;
closeToTray: boolean;
}
export const defaultSettings: SettingsValues = {
hideLyrics: false,
alwaysOnTop: false,
autoScroll: false,
theme: 'light',
fontSize: 'twelve-pt',
refreshInterval: 5000,
closeToTray: false
};
export class Settings {
protected raw: SettingsValues = <SettingsValues>{};
getRaw() {
return this.raw;
}
protected setDefaults(settings: SettingsValues) {
for (let k of Object.keys(defaultSettings)) {
settings[k] = defaultSettings[k];
}
}
load(ready) {
this.setDefaults(this.raw);
storage.get('settings', (err, savedSettings: SettingsValues) => {
if (err) savedSettings = <SettingsValues>{};
for (let attr in this.raw) {
if ((attr in savedSettings) === false) {
savedSettings[attr] = this.raw[attr];
}
}
this.raw = savedSettings;
ready();
});
}
save(newSettings, ready?) {
let oldSettings = {};
for (let attr in newSettings) {
oldSettings[attr] = this.raw[attr];
}
if (JSON.stringify(newSettings) === JSON.stringify(oldSettings)) {
console.log('no modifications')
} else {
console.log('modified settings!')
for (let attr in newSettings) {
this.raw[attr] = newSettings[attr];
}
this.persist();
}
ready && ready();
}
persist(ready?) {
storage.set('settings', this.raw, (err) => {
if (err) console.log('Err persisting settings', err);
ready && ready(err);
});
}
set(key, value) {
this.raw[key] = value;
this.persist();
}
get(key) {
return this.raw[key];
}
}