-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
165 lines (139 loc) · 4.53 KB
/
main.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
'use strict';
const { app, BrowserWindow, ipcMain, Menu } = require('electron/main');
const { onMidiEnabled, sendCCselectedOut, disableWebMidi, startWebMidi, getOutputDevices, midiOutputDevice, setActiveMidiOutputDevice, getActiveMidiOutputDevice } = require('./WebMidiIMP');
const path = require('node:path');
const log = require('electron-log/main');
const debuggerToolsEnabled = false;
log.initialize();
log.transports.console.level = 'debug';
const isMac = process.platform === 'darwin';
let midiDeviceSelectionWindow = null;
function createAppMenu () {
let macTemplate = [];
if (isMac) {
macTemplate = [
{
label: app.name,
submenu: [
{ role: 'quit' }
]
}
];
}
const template = [
{
label: 'File',
submenu: [
{ label: 'Midi Preferences', accelerator: 'CmdOrCtrl+M', click: () => createMidiDeviceSelectWindow(BrowserWindow.getFocusedWindow()) },
{ type: 'separator' },
{ label: 'Quit', accelerator: 'CmdOrCtrl+Q', click: () => app.quit() }
]
}
];
const menu = Menu.buildFromTemplate(macTemplate.concat(template));
Menu.setApplicationMenu(menu);
}
function createMainWindow () {
const win = new BrowserWindow({
width: 400,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'index-preload.js')
}
});
win.setTitle(`1CONTROL v${app.getVersion()}`);
win.webContents.session.setPermissionRequestHandler((webContents, permission, callback, details) => {
if (permission === 'midi' || permission === 'midiSysex') {
log.debug('Set Midi Permission true');
callback(true);
} else {
log.debug('Don\'t Set Midi Permission true');
callback(false);
}
});
win.webContents.session.setPermissionCheckHandler((webContents, permission, requestingOrigin) => {
if (permission === 'midi' || permission === 'midiSysex') {
return true;
}
return false;
});
if (debuggerToolsEnabled) { win.webContents.openDevTools(); }
win.loadFile('index.html');
return win;
}
function createMidiDeviceSelectWindow (parentWindow) {
const popup = new BrowserWindow({
parent: parentWindow,
modal: true,
show: false,
width: 300,
height: 100,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: true, // Required for dropdown interaction
preload: path.join(__dirname, 'device-popup-preload.js')
}
});
popup.setTitle(`1CONTROL v${app.getVersion()}`);
popup.loadFile('device-popup.html'); // Load the popup HTML file
if (debuggerToolsEnabled) { popup.webContents.openDevTools(); }
popup.once('ready-to-show', () => {
popup.show();
});
return popup;
}
app.whenReady().then(() => {
ipcMain.on('send-CC', (event, channel, cc, value) => {
sendCCselectedOut(channel, cc, value);
console.log(`Channel: ${channel}, CC: ${cc}, Value: ${value}`);
});
ipcMain.handle('open-DeviceSelection', () => {
const devices = getOutputDevices();
const deviceList = [];
devices.forEach((device) => {
const deviceDetails = {
name: device.name,
id: device.id
};
deviceList.push(deviceDetails);
log.debug(`ipcMain.handle(open-DeviceSelection) DeviceList: ${deviceList}`);
});
return deviceList;
});
ipcMain.on('set-MidiOutputId', (event, midiOutputId) => {
const devices = getOutputDevices();
log.debug(`ipcMain.on(set-MidiOutputId ) midiOutputDevice Before: ${midiOutputDevice}`);
devices.forEach((device) => {
log.debug(`${device.id} : ${midiOutputId}`);
if (device.id === midiOutputId) {
setActiveMidiOutputDevice(device);
log.debug(`ipcMain.on(set-MidiOutputId ) midiOutputDevice After: ${getActiveMidiOutputDevice().id}`);
}
});
});
ipcMain.on('close-DevicePopup', (event) => {
midiDeviceSelectionWindow.close();
midiDeviceSelectionWindow = null;
});
startWebMidi();
const mainWindow = createMainWindow();
midiDeviceSelectionWindow = createMidiDeviceSelectWindow(mainWindow);
log.info('Electron Window started');
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
const mainWindow = createMainWindow();
if (getActiveMidiOutputDevice() == null) {
midiDeviceSelectionWindow = createMidiDeviceSelectWindow(mainWindow);
}
} else if (getActiveMidiOutputDevice() == null) {
midiDeviceSelectionWindow = createMidiDeviceSelectWindow(BrowserWindow.getFocusedWindow());
}
});
app.on('window-all-closed', () => {
disableWebMidi();
if (!isMac) {
app.quit();
}
});
createAppMenu();