-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreload.js
More file actions
110 lines (107 loc) · 5.14 KB
/
preload.js
File metadata and controls
110 lines (107 loc) · 5.14 KB
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
const { contextBridge, ipcRenderer } = require('electron');
const { shell } = require('electron');
function decodeBase64ToUint8Array(b64) {
const buf = Buffer.from(b64, 'base64');
return new Uint8Array(buf);
}
contextBridge.exposeInMainWorld('api', {
tcp: {
open: (host, port, options) => ipcRenderer.invoke('tcp:open', { host, port, options }),
write: (id, data, mode, append, encoding) => ipcRenderer.invoke('tcp:write', { id, data, mode, append, encoding }),
close: (id) => ipcRenderer.invoke('tcp:close', { id }),
onData: (cb) => ipcRenderer.on('tcp:data', (_e, p) => cb({ id: p.id, bytes: decodeBase64ToUint8Array(p.base64), ts: p.ts })),
onEvent: (cb) => ipcRenderer.on('tcp:event', (_e, p) => cb(p)),
},
tcpShare: {
start: (id, port) => ipcRenderer.invoke('tcpShare:start', { id, port }),
stop: (id) => ipcRenderer.invoke('tcpShare:stop', { id }),
status:(id) => ipcRenderer.invoke('tcpShare:status',{ id })
},
window: {
setFullscreen: (flag) => ipcRenderer.send('window:set-fullscreen', { flag }),
toggleFullscreen: () => ipcRenderer.send('window:toggle-fullscreen'),
focus: () => ipcRenderer.send('window:focus'),
setOscilloscopeTop: (flag) => ipcRenderer.send('window:set-oscilloscope-top', { flag }),
},
serial: {
list: () => ipcRenderer.invoke('serial:list'),
open: (path, options) => ipcRenderer.invoke('serial:open', { path, options }),
close: (id) => ipcRenderer.invoke('serial:close', { id }),
write: (id, data, mode = 'text', append = 'none', encoding = 'utf-8') =>
ipcRenderer.invoke('serial:write', { id, data, mode, append, encoding }),
onData: (cb) => {
ipcRenderer.on('serial:data', (_e, payload) => {
cb({ id: payload.id, bytes: decodeBase64ToUint8Array(payload.base64), ts: payload.ts });
});
},
onEvent: (cb) => {
ipcRenderer.on('serial:event', (_e, payload) => cb(payload));
}
},
panel: {
popout: (id, title, historyStr, alwaysOnTop, isOpen, viewMode, optionsStr) =>
ipcRenderer.invoke('panel:popout', { id, title, historyStr, alwaysOnTop, isOpen, viewMode, optionsStr }),
onFocusFromPopout: (cb) => ipcRenderer.on('panel:focus', (_e, payload) => cb(payload)),
onDockRequest: (cb) => ipcRenderer.on('panel:dock', (_e, payload) => cb(payload)),
onHideRequest: (cb) => ipcRenderer.on('panel:hide', (_e, payload) => cb(payload)),
requestHide: (id) => ipcRenderer.send('panel:request-hide', { id }),
onLoadContent: (cb) => ipcRenderer.on('panel:loadContent', (_e, payload) => cb(payload)),
requestDock: (id, html) => ipcRenderer.send('panel:request-dock', { id, html }),
getPanelPortIdFromArgs: () => {
const arg = (process.argv || []).find(a => a.startsWith('--panelPortId='));
if (!arg) return null;
return decodeURIComponent(arg.split('=')[1]);
},
saveLog: (name, content) => ipcRenderer.invoke('panel:saveLog', { name, content }),
sendEcho: (id, text, hex) => ipcRenderer.send('panel:echo', { id, text, hex }),
onEcho: (cb) => ipcRenderer.on('panel:echo', (_e, payload) => cb(payload))
},
config: {
load: () => ipcRenderer.invoke('config:load'),
save: (panels) => ipcRenderer.send('config:save', panels),
export: (localStorageData) => ipcRenderer.invoke('config:export', localStorageData),
import: () => ipcRenderer.invoke('config:import'),
applyImport: (data) => ipcRenderer.invoke('config:applyImport', data)
},
commands: {
load: () => ipcRenderer.invoke('commands:load'),
save: (cmds) => ipcRenderer.send('commands:save', cmds)
},
file: {
readHex: (filePath) => ipcRenderer.invoke('file:readHex', filePath)
},
logger: {
append: (path, text) => ipcRenderer.invoke('logger:append', path, text),
pickFile: () => ipcRenderer.invoke('logger:pickFile')
},
shell: {
openExternal: (url) => shell.openExternal(url)
},
scripts: {
dir: () => ipcRenderer.invoke('scripts:dir'),
list: () => ipcRenderer.invoke('scripts:list'),
read: (name) => ipcRenderer.invoke('scripts:read', name),
write: (name, content) => ipcRenderer.invoke('scripts:write', { name, content }),
delete: (name) => ipcRenderer.invoke('scripts:delete', name),
run: (code, ctx) => ipcRenderer.invoke('scripts:run', { code, ctx }),
stop: (runId) => ipcRenderer.invoke('scripts:stop', { runId }),
onEnded: (cb) => ipcRenderer.on('scripts:ended', (_e, payload) => cb(payload))
},
app: {
getVersion: () => ipcRenderer.invoke('app:version'),
checkUpdate: () => ipcRenderer.send('app:checkUpdate'),
onUpdateProgress: (cb) => ipcRenderer.on('update:progress', (_e, p) => cb(p)),
onUpdateError: (cb) => ipcRenderer.on('update:error', (_e, p) => cb(p)),
cancelUpdate: () => ipcRenderer.send('update:cancel'),
restart: () => ipcRenderer.invoke('app:restart')
},
changelog: {
open: () => ipcRenderer.send('changelog:open'),
request: () => ipcRenderer.send('changelog:request'),
onLoad: (cb) => ipcRenderer.on('changelog:content', (_e, text) => cb(text))
},
theme: {
set: (dark) => ipcRenderer.send('theme:set', { dark }),
onApply: (cb) => ipcRenderer.on('theme:apply', (_e, payload) => cb(payload))
}
});