-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
102 lines (86 loc) · 3.18 KB
/
index.js
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
/*
Copyright 2019 Marc Nuri San Felix
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const {WebContentsView} = require('electron');
const fs = require('fs');
const path = require('path');
const HOME_DIR = require('os').homedir();
const {CLOSE_BUTTON_BEHAVIORS} = require('../constants');
const {showDialog} = require('../base-window');
const APP_DIR = '.electronim';
const SETTINGS_FILE = 'settings.json';
const DEFAULT_SETTINGS = {
tabs: [],
useNativeSpellChecker: false,
enabledDictionaries: ['en'],
theme: 'system',
trayEnabled: false,
startMinimized: false,
closeButtonBehavior: CLOSE_BUTTON_BEHAVIORS.quit
};
const webPreferences = {
contextIsolation: false,
nativeWindowOpen: true,
nodeIntegration: false,
sandbox: true,
preload: path.resolve(__dirname, '..', '..', 'bundles', 'settings.preload.js'),
partition: 'persist:electronim'
};
const appDir = path.join(HOME_DIR, APP_DIR);
const settingsPath = path.join(appDir, SETTINGS_FILE);
/**
* Wrapper function to retrieve the current system's platform.
*
* @returns {NodeJS.Platform} the OS platform.
*/
const getPlatform = () => process.platform;
const containsTabId = tabs => tabId => tabs.map(({id}) => id).includes(tabId);
const ensureDefaultValues = settings => {
settings = Object.assign(DEFAULT_SETTINGS, settings);
let {activeTab} = settings;
const enabledTabs = settings.tabs.filter(({disabled}) => !disabled);
if (enabledTabs.length > 0 && !containsTabId(enabledTabs)(activeTab)) {
activeTab = enabledTabs[0].id;
}
return {...settings, activeTab};
};
const migrate = settings => {
// en-us dictionary key is now en
if (settings.enabledDictionaries.includes('en-US')) {
settings.enabledDictionaries = settings.enabledDictionaries
.filter(dictionary => dictionary !== 'en-US')
.filter(dictionary => dictionary !== 'en')
.concat('en');
}
return settings;
};
const initAppDir = () => fs.mkdirSync(appDir, {recursive: true});
const loadSettings = () => {
initAppDir();
let loadedSettings = {};
if (fs.existsSync(settingsPath)) {
loadedSettings = JSON.parse(fs.readFileSync(settingsPath));
}
return migrate(ensureDefaultValues(loadedSettings));
};
const writeSettings = settings => {
initAppDir();
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
};
const updateSettings = settings =>
writeSettings(ensureDefaultValues({...loadSettings(), ...settings}));
const openSettingsDialog = mainWindow => () => {
const settingsView = new WebContentsView({webPreferences});
settingsView.webContents.loadURL(`file://${__dirname}/index.html`);
showDialog(mainWindow, settingsView);
};
module.exports = {getPlatform, loadSettings, updateSettings, openSettingsDialog};