generated from acrosman/electron-sf-base
-
Notifications
You must be signed in to change notification settings - Fork 7
/
preferences.js
138 lines (121 loc) · 4.07 KB
/
preferences.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
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
const path = require('path');
const { app, BrowserWindow, Menu } = require('electron'); // eslint-disable-line
const fs = require('fs-extra');
const appPath = app.getAppPath();
const settingsPath = path.join(app.getPath('userData'), 'preferences.json');
// A list of menu item IDs to disable when preference window is open.
const nonPrefWindowItems = [
'find-menu-item',
];
let prefWindow = null;
let mainWindow = null;
const setMainWindow = (win) => {
mainWindow = win;
};
const getCurrentPreferences = () => {
// Ensure we have the settings file created.
fs.ensureFileSync(settingsPath);
const preferences = {
theme: 'Cyborg',
indexes: {
externalIds: true,
lookups: true,
picklists: true,
},
picklists: {
type: 'enum',
unrestricted: true,
ensureBlanks: true,
},
lookups: {
type: 'char(18)',
},
defaults: {
attemptSFValues: false,
textEmptyString: false,
checkboxDefaultFalse: true,
suppressReadOnly: false,
suppressAudit: false,
},
};
// Load any existing values.
let settingsData = {};
try {
settingsData = JSON.parse(fs.readFileSync(settingsPath));
} catch (err) {
// Catch and release, we'll just use the defaults from there.
}
// Merge in settings that in the file an we know how to use.
const values = Object.getOwnPropertyNames(preferences);
for (let i = 0; i < values.length; i += 1) {
if (Object.prototype.hasOwnProperty.call(settingsData, values[i])) {
preferences[values[i]] = settingsData[values[i]];
}
}
return preferences;
};
const loadPreferences = () => {
// Get our defaults.
const preferences = getCurrentPreferences();
prefWindow.webContents.send('preferences_data', preferences);
};
const savePreferences = (event, settingData) => {
const preferences = getCurrentPreferences();
// Merge in settings that in the file an we know how to use.
const values = Object.getOwnPropertyNames(preferences);
for (let i = 0; i < values.length; i += 1) {
if (Object.prototype.hasOwnProperty.call(settingData, values[i])) {
preferences[values[i]] = settingData[values[i]];
}
}
fs.writeFileSync(settingsPath, JSON.stringify(preferences));
};
const closePreferences = () => {
if (prefWindow) {
prefWindow.close();
}
if (mainWindow) {
mainWindow.webContents.send('current_preferences', getCurrentPreferences());
}
// Enable menu items that don't work in this context:
const appMenu = Menu.getApplicationMenu();
nonPrefWindowItems.forEach((element) => {
appMenu.getMenuItemById(element).enabled = true;
});
};
const openPreferences = () => {
const htmlPath = `file://${appPath}/app/preferences.html`;
if (!prefWindow || prefWindow.isDestroyed()) {
prefWindow = new BrowserWindow({
width: 550,
height: 730,
resizable: false,
frame: false,
webPreferences: {
contextIsolation: true, // Enabling contextIsolation to protect against prototype pollution.
disableBlinkFeatures: 'Auxclick', // See: https://github.com/doyensec/electronegativity/wiki/AUXCLICK_JS_CHECK
enableRemoteModule: false, // Turn off remote to avoid temptation.
nodeIntegration: false, // Disable nodeIntegration for security.
nodeIntegrationInWorker: false,
nodeIntegrationInSubFrames: false,
worldSafeExecuteJavaScript: true, // https://github.com/electron/electron/pull/24712
preload: path.join(appPath, 'app/preferencesPreload.js'),
},
});
}
// Disable menu items that don't work in this context:
const appMenu = Menu.getApplicationMenu();
nonPrefWindowItems.forEach((element) => {
appMenu.getMenuItemById(element).enabled = false;
});
// Display the window.
prefWindow.loadURL(htmlPath);
prefWindow.setMenuBarVisibility(false);
prefWindow.show();
};
exports.setMainWindow = setMainWindow;
exports.getCurrentPreferences = getCurrentPreferences;
exports.openPreferences = openPreferences;
exports.loadPreferences = loadPreferences;
exports.savePreferences = savePreferences;
exports.closePreferences = closePreferences;