forked from BlankSourceCode/qmk-hid-display
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.js
154 lines (135 loc) · 4.27 KB
/
config.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// config window.
// used https://github.com/pbarbiero/basic-electron-react-boilerplate
// as a starting off point
'use strict';
const Store = require('electron-store');
const { app, BrowserWindow, ipcMain } = require('electron');
const ScreenManager = require('./screenmanager.js');
const { merge } = require('lodash');
const url = require('url');
const path = require('path');
class ConfigPage {
constructor(storePrefix, groups, parentConfig) {
this.prefix = storePrefix;
this.originalGroups = [
...(parentConfig ? parentConfig.originalGroups : []),
...(groups ? groups : [])];
// Prefixed
this.groups = this.originalGroups;
for (let group of this.groups) {
let prefixedParams = {};
for (const [key, value] of Object.entries(group.params)) {
prefixedParams[this.prefix + key] = value;
}
group.params = prefixedParams;
}
}
getDefaults() {
return Object.fromEntries(
Object.entries(this.groups.map(group => group.params)
.reduce((accum, params) => merge(accum, params), {}))
.filter(([key, param]) => param.default != null)
.map(([key, param]) => [key, param.default]));
}
}
let dev = false;
if (process.defaultApp || /[\\/]electron-prebuilt[\\/]/.test(process.execPath) || /[\\/]electron[\\/]/.test(process.execPath)) {
dev = true;
}
class Config {
constructor() {
this.screenManager = null;
this.keyboardConfigPage = new ConfigPage('keyboard-', [
{ params: {
name: { label: 'Name', default: 'SPIN' },
id: { label: 'ID', default: '0x61' },
page: { label: 'Page', default: '0xFF60' }
}}]);
}
init(screenManager) {
this.screenManager = screenManager;
this.store = new Store({defaults: this.getStoreSchema()});
ipcMain.handle('get-config', async (event, field) => {
return this.store.get(field);
});
ipcMain.on('set-config', (event, data) => {
this.store.set(data.key, data.value)
});
ipcMain.handle('get-pages', async (event) => {
return this.getPages();
});
ipcMain.handle('get-page-groups', async (event, page) => {
return this.getGroupsForPage(page);
});
}
getConfigPages() {
return [this.keyboardConfigPage,
...Object.values(this.screenManager.screens).map(
screen => screen.configPage)];
}
getStoreSchema() {
return merge({}, ...Object.values(this.getConfigPages()).map(
configPage => configPage.getDefaults()));
}
getPages() {
return [
'Keyboard',
...Object.values(this.screenManager.screens)
.filter(screen => screen.configPage.groups.length > 0)
.map(screen => screen.name)];
}
getGroupsForPage(page) {
if (page == 'Keyboard') {
return this.keyboardConfigPage.groups;
} else if (page in this.screenManager.screens) {
return this.screenManager.screens[page].configPage.groups;
}
return [];
}
createWindow() {
// Create the browser window.
this.configWindow = new BrowserWindow({
width: 1024, height: 768, show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
// and load the index.html of the app.
let indexPath;
if (dev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
host: 'localhost:8080',
pathname: 'index.html',
slashes: true
});
} else {
indexPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true
});
}
this.configWindow.loadURL(indexPath);
// Don't show until we are ready and loaded
this.configWindow.once('ready-to-show', () => {
this.configWindow.show();
if (dev) {
// Open the window and DevTools automatically if developing
this.configWindow.webContents.openDevTools();
}
});
// Emitted when the window is closed.
this.configWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
this.configWindow = null;
});
}
}
module.exports = {
Config: Config,
ConfigPage: ConfigPage
};