Skip to content

Commit e159d5f

Browse files
committed
Implemented keyboard shortcuts.
Signed-off-by: ubi de feo <[email protected]>
1 parent 9f56d75 commit e159d5f

File tree

6 files changed

+131
-11
lines changed

6 files changed

+131
-11
lines changed

backend/ipc.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,15 @@ module.exports = function registerIPCHandlers(win, ipcMain, app, dialog) {
129129
return response != opt.cancelId
130130
})
131131

132+
ipcMain.handle('reload', (event, message = 'ciao, cane') => {
133+
console.log('reload?')
134+
win.webContents.send('reload', message)
135+
})
136+
132137
win.on('close', (event) => {
133138
console.log('BrowserWindow', 'close')
134139
event.preventDefault()
135140
win.webContents.send('check-before-close')
136141
})
142+
137143
}

index.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
1+
const { app, BrowserWindow, ipcMain, dialog, globalShortcut } = require('electron')
22
const path = require('path')
33
const fs = require('fs')
44

@@ -57,4 +57,48 @@ function createWindow () {
5757
})
5858
}
5959

60-
app.on('ready', createWindow)
60+
function shortcutAction(key) {
61+
win.webContents.send('shortcut-cmd', key);
62+
}
63+
64+
// Shortcuts
65+
function registerShortcuts() {
66+
globalShortcut.register('CommandOrControl+R', () => {
67+
console.log('Running Program')
68+
shortcutAction('r')
69+
})
70+
globalShortcut.register('CommandOrControl+H', () => {
71+
console.log('Stopping Program (Halt)')
72+
shortcutAction('h')
73+
})
74+
globalShortcut.register('CommandOrControl+S', () => {
75+
console.log('Saving File')
76+
shortcutAction('s')
77+
})
78+
79+
globalShortcut.register('CommandOrControl+Shift+R', () => {
80+
console.log('Resetting Board')
81+
shortcutAction('R')
82+
})
83+
globalShortcut.register('CommandOrControl+Shift+C', () => {
84+
console.log('Connect to Board')
85+
shortcutAction('C')
86+
})
87+
globalShortcut.register('CommandOrControl+Shift+D', () => {
88+
console.log('Disconnect from Board')
89+
shortcutAction('D')
90+
})
91+
}
92+
93+
app.on('ready', () => {
94+
createWindow()
95+
registerShortcuts()
96+
97+
win.on('focus', () => {
98+
registerShortcuts()
99+
})
100+
win.on('blur', () => {
101+
globalShortcut.unregisterAll()
102+
})
103+
104+
})

preload.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const { contextBridge, ipcRenderer } = require('electron')
33
const path = require('path')
44

55
const MicroPython = require('micropython.js')
6+
const { emit, platform } = require('process')
7+
// const { platform } = requireprocess.platform
68
const board = new MicroPython()
79
board.chunk_size = 192
810
board.chunk_sleep = 200
@@ -155,12 +157,31 @@ const Window = {
155157
setWindowSize: (minWidth, minHeight) => {
156158
ipcRenderer.invoke('set-window-size', minWidth, minHeight)
157159
},
160+
anyShortcut: (callback, key) => {
161+
ipcRenderer.on('shortcut-cmd', (event, k) => {
162+
// Get the active element
163+
const activeElement = document.activeElement;
164+
// Check if the active element is the terminal
165+
const isTerminalFocused = activeElement.classList.contains('xterm-helper-textarea');
166+
// Only trigger callback if terminal is not focused AND we're in editor view
167+
if (!isTerminalFocused) {
168+
console.log('shortcut-cmd-r executed')
169+
callback(k);
170+
}
171+
})
172+
},
173+
174+
158175
beforeClose: (callback) => ipcRenderer.on('check-before-close', callback),
159176
confirmClose: () => ipcRenderer.invoke('confirm-close'),
160177
isPackaged: () => ipcRenderer.invoke('is-packaged'),
161-
openDialog: (opt) => ipcRenderer.invoke('open-dialog', opt)
162-
}
178+
openDialog: (opt) => ipcRenderer.invoke('open-dialog', opt),
163179

180+
getOS: () => platform,
181+
isWindows: () => platform === 'win32',
182+
isMac: () => platform === 'darwin',
183+
isLinux: () => platform === 'linux'
184+
}
164185

165186
contextBridge.exposeInMainWorld('BridgeSerial', Serial)
166187
contextBridge.exposeInMainWorld('BridgeDisk', Disk)

ui/arduino/main.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ window.addEventListener('load', () => {
4646
app.use(store);
4747
app.route('*', App)
4848
app.mount('#app')
49-
5049
app.emitter.on('DOMContentLoaded', () => {
5150
if (app.state.diskNavigationRoot) {
5251
app.emitter.emit('refresh-files')
5352
}
5453
})
54+
app.emitter.on('', () => {
55+
if (app.state.diskNavigationRoot) {
56+
app.emitter.emit('refresh-files')
57+
}
58+
})
5559

5660
})

ui/arduino/store.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async function confirm(msg, cancelMsg, confirmMsg) {
2424
async function store(state, emitter) {
2525
win.setWindowSize(720, 640)
2626

27+
state.platform = window.BridgeWindow.getOS()
2728
state.view = 'editor'
2829
state.diskNavigationPath = '/'
2930
state.diskNavigationRoot = getDiskNavigationRootFromStorage()
@@ -57,6 +58,8 @@ async function store(state, emitter) {
5758

5859
state.isTerminalBound = false
5960

61+
state.shortcutsContext = 'editor'
62+
6063
const newFile = createEmptyFile({
6164
parentFolder: null, // Null parent folder means not saved?
6265
source: 'disk'
@@ -1360,6 +1363,47 @@ async function store(state, emitter) {
13601363
await win.confirmClose()
13611364
})
13621365

1366+
// win.shortcutCmdR(() => {
1367+
// // Only run if we can execute
1368+
1369+
// })
1370+
1371+
win.anyShortcut((key) => {
1372+
if (key === 'C') {
1373+
emitter.emit('open-connection-dialog')
1374+
}
1375+
if (key === 'D') {
1376+
emitter.emit('disconnect')
1377+
}
1378+
if (key === 'R') {
1379+
if (state.view != 'editor') return
1380+
emitter.emit('reset')
1381+
}
1382+
if (key === 'r') {
1383+
if (state.view != 'editor') return
1384+
runCode()
1385+
}
1386+
if (key === 'h') {
1387+
if (state.view != 'editor') return
1388+
stopCode()
1389+
}
1390+
if (key === 's') {
1391+
if (state.view != 'editor') return
1392+
emitter.emit('save')
1393+
}
1394+
1395+
})
1396+
1397+
function runCode() {
1398+
if (canExecute({ view: state.view, isConnected: state.isConnected })) {
1399+
emitter.emit('run')
1400+
}
1401+
}
1402+
function stopCode() {
1403+
if (canExecute({ view: state.view, isConnected: state.isConnected })) {
1404+
emitter.emit('stop')
1405+
}
1406+
}
13631407
function createFile(args) {
13641408
const {
13651409
source,

ui/arduino/views/components/toolbar.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ function Toolbar(state, emit) {
99
view: state.view,
1010
isConnected: state.isConnected
1111
})
12-
12+
const metaKeyString = state.platform === 'darwin' ? 'Cmd' : 'Ctrl'
13+
1314
return html`
1415
<div id="toolbar">
1516
${Button({
1617
icon: state.isConnected ? 'connect.svg' : 'disconnect.svg',
17-
tooltip: state.isConnected ? 'Disconnect' : 'Connect',
18+
tooltip: state.isConnected ? `Disconnect (${metaKeyString}+Shift+D)` : `Connect (${metaKeyString}+Shift+C)`,
1819
onClick: () => emit('open-connection-dialog'),
1920
active: state.isConnected
2021
})}
@@ -23,19 +24,19 @@ function Toolbar(state, emit) {
2324
2425
${Button({
2526
icon: 'run.svg',
26-
tooltip: 'Run',
27+
tooltip: `Run (${metaKeyString}+r)`,
2728
disabled: !_canExecute,
2829
onClick: () => emit('run')
2930
})}
3031
${Button({
3132
icon: 'stop.svg',
32-
tooltip: 'Stop',
33+
tooltip: `Stop (${metaKeyString}+h)`,
3334
disabled: !_canExecute,
3435
onClick: () => emit('stop')
3536
})}
3637
${Button({
3738
icon: 'reboot.svg',
38-
tooltip: 'Reset',
39+
tooltip: `Reset (${metaKeyString}+Shift+r)`,
3940
disabled: !_canExecute,
4041
onClick: () => emit('reset')
4142
})}
@@ -44,7 +45,7 @@ function Toolbar(state, emit) {
4445
4546
${Button({
4647
icon: 'save.svg',
47-
tooltip: 'Save',
48+
tooltip: `Save (${metaKeyString}+s)`,
4849
disabled: !_canSave,
4950
onClick: () => emit('save')
5051
})}

0 commit comments

Comments
 (0)