-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
179 lines (157 loc) · 5.11 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
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
import { app, BrowserWindow, ipcMain, Tray, Menu } from 'electron'
import ActiveWindow from '@paymoapp/active-window'
import dayjs from 'dayjs'
import Store from 'electron-store'
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { start, stop, close, writeCommand, getConnect, getIsOledOn, getGpkRCVersion, getKBDList, deviceId } from './qmkrcd.js'
ActiveWindow.default.initialize()
let store
let mainWindow
let tray = null
const createWindow = () => {
mainWindow = new BrowserWindow({
width: 1280,
height: 1000,
icon: `${__dirname}/icons/icon-256x256.png`,
webPreferences: {
preload: __dirname + '/preload.js',
backgroundThrottling: false,
},
show: false,
})
mainWindow.loadURL(`file://${__dirname}/public/index.html`)
mainWindow.setMenu(null)
mainWindow.on('minimize', (event) => {
mainWindowHide(event, mainWindow)
})
mainWindow.on('close', (event) => {
if (!app.isQuiting) {
mainWindowHide(event, mainWindow)
return false
} else {
try {
app.quit()
} catch (e) {
console.log(e)
process.exit(1)
}
}
})
}
const doubleBoot = app.requestSingleInstanceLock()
if (!doubleBoot) app.quit()
app.on('ready', () => {
if (process.platform === 'darwin') app.dock.hide()
const icon = () => {
if(process.platform==='win32') return "icon-72x72.ico"
if(process.platform==='darwin') return "tray.png"
return "icon-72x72.png"
}
store = new Store()
tray = new Tray(`${__dirname}/icons/${icon()}`)
tray.setContextMenu(Menu.buildFromTemplate([
{
label: 'settings',
click: () => {
mainWindowShow(mainWindow)
}
}, {
type: 'separator'
}, {
label: 'Exit',
click: () => {
app.isQuiting = true
close()
mainWindow.close()
}
}
]))
tray.on('double-click', () => {
mainWindowShow(mainWindow)
})
createWindow()
//mainWindow.webContents.openDevTools()
})
const mainWindowShow = (mainWindow) => {
mainWindow.show()
mainWindow.focus()
mainWindow.webContents.send("mainWindowShow", true)
}
const mainWindowHide = (event, mainWindow) => {
event.preventDefault()
mainWindow.hide()
mainWindow.webContents.send("mainWindowShow", false)
}
app.on('activate', () => {
if (mainWindow === null) createWindow()
})
ipcMain.on("connectDevice", (e, data) => {
mainWindow.webContents.send("isConnectDevice", data)
})
ipcMain.on("changeActiveWindow", (e, data) => {
mainWindow.webContents.send("activeWindow", data)
})
ipcMain.on("setActiveWindow", async () => {
const getWindowName = async () => {
let activeWin
try {
activeWin = ActiveWindow.default.getActiveWindow();
} catch (e) {}
return Promise.resolve(activeWin ? activeWin.application : "")
}
mainWindow.webContents.send("getActiveWindow", await getWindowName())
})
ipcMain.on("onWindowShow", async () => {
mainWindow.webContents.send("windowShow", true)
})
const sleep = async (msec) => new Promise(resolve => setTimeout(resolve, msec))
const commandId = (device) => getGpkRCVersion(device) === 0 ? {
oledWrite: 23,
switchLayer: 34,
setOledState: 36,
gpkRCVersion: 117
} : {
oledWrite: 103,
switchLayer: 114,
setOledState: 116,
gpkRCVersion: 117
}
ipcMain.handle('deviceId', async (event, device) => await deviceId(device))
ipcMain.handle('start', async (event,device) => {
await start(device, process.platform)
await writeCommand(device, {id: commandId(device).gpkRCVersion })
})
ipcMain.handle('stop', async (event, device) => {
await stop(device)
})
ipcMain.handle('getKBDList', (event) => getKBDList())
ipcMain.handle('connect', (event, id) => getConnect(id))
ipcMain.handle('switchLayer', async (event, obj) => {
await writeCommand(obj.device, {id: commandId(obj.device).switchLayer, data: [obj.n]})
})
ipcMain.handle('isOledOn', (event) => getIsOledOn())
ipcMain.handle('setOledState', async (event, device) => {
await writeCommand(device, {id: commandId(device).setOledState})
await sleep(300)
})
ipcMain.handle('oledWrite', async (event, obj) => {
await writeCommand(obj.device, {id: commandId(obj.device).oledWrite, data: obj.now})
})
ipcMain.handle('sleep', async (event, msec) => {
await sleep(msec)
})
ipcMain.handle('now', async (event) => await dayjs().format('YYYY/MM/DD ddd HH:mm '))
ipcMain.handle('exitsStore', async (event) => !!store)
ipcMain.handle('setStoreDevice', async (event, device) => {
if(store && device) {
await store.set('devices', device)
}
})
ipcMain.handle('getStoreAllDevices', async (event) => {
if(store) return await store.get('devices')
})
ipcMain.handle('getStorePath', async (event) => store.path)
ipcMain.handle("getAppVersion", async () => app.getVersion())