-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
89 lines (65 loc) · 2.22 KB
/
index.ts
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
import createModal from '../modal.js';
export const isSupported = {
// mobile: () => {
// return commoners.target === 'android'
// },
web: async () => 'serial' in navigator // Ensure serial feature is available
}
export const desktop = {
ready: function () {
this.CALLBACKS = {}
},
load: function ( win ) {
const { __id } = win
const { session } = win.webContents
this.on(`${__id}:select`, (_, port) => this.CALLBACKS[__id]?.(port));
session.on('serial-port-added', (_, port) => this.send(`${__id}:added`, port))
session.on('serial-port-removed', (_, port) => this.send(`${__id}:removed`, port))
session.on('select-serial-port', (event, portList, webContents, callback) => {
const window = this.electron.BrowserWindow.fromWebContents(webContents);
if (__id !== window.__id) return // Skip if the attached window did not trigger the request
this.send(`${__id}:request`, portList);
event.preventDefault()
this.CALLBACKS[__id] = (port) => {
this.CALLBACKS[__id] = null // Ensures this is only called once
callback(port)
}
})
session.setPermissionCheckHandler((webContents, permission, requestingOrigin, details) => true)
session.setDevicePermissionHandler((details) => true)
}
}
export function load() {
const { DESKTOP } = commoners
if (!DESKTOP) return
const { __id } = DESKTOP
const added = (callback) => this.on(`${__id}:added`, (_, port) => callback(port))
const removed = (callback) => this.on(`${__id}:removed`, (_, port) => callback(port))
const select = (port) => this.send(`${__id}:select`, port)
const onRequest =(callback) => this.on(`${__id}:request`, (_, value) => callback(value))
const modal = createModal({
headerText: 'Available Serial Ports',
added,
removed,
mapDeviceToInfo: (o) => {
return {
name: o.displayName ?? o.portName,
info: o.displayName ? o.portName : '',
id: o.portId
}
},
onClose: (port) => select(port)
})
onRequest((devices) => {
modal.update(devices)
modal.showModal()
}) // Open on each request
document.body.append(modal)
return {
modal,
added,
removed,
select,
onRequest
}
}