8000 Refactored menu and global shortcuts into constants file. · arduino/lab-micropython-editor@9908d4a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9908d4a

Browse files
committed
Refactored menu and global shortcuts into constants file.
Signed-off-by: ubi de feo <me@ubidefeo.com>
1 parent 88fe9a2 commit 9908d4a

File tree

6 files changed

+114
-63
lines changed

6 files changed

+114
-63
lines changed

backend/menu.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { app, Menu } = require('electron')
22
const path = require('path')
33
const openAboutWindow = require('about-window').default
4+
const shortcuts = require('./shortcuts.js')
45

56
module.exports = function registerMenu(win, state = {}) {
67
const isMac = process.platform === 'darwin'
@@ -54,32 +55,45 @@ module.exports = function registerMenu(win, state = {}) {
5455
submenu: [
5556
{
5657
label: 'Connect',
57-
accelerator: 'CmdOrCtrl+Shift+C',
58-
click: () => win.webContents.send('shortcut-cmd', 'C')
58+
accelerator: shortcuts.menu.CONNECT,
59+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.CONNECT)
5960
},
6061
{
6162
label: 'Disconnect',
62-
accelerator: 'CmdOrCtrl+Shift+D',
63-
click: () => win.webContents.send('shortcut-cmd', 'D')
63+
accelerator: shortcuts.menu.DISCONNECT,
64+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.DISCONNECT)
6465
},
65-
{ role: 'separator' },
66+
{ type: 'separator' },
6667
{
6768
label: 'Run',
68-
accelerator: 'CmdOrCtrl+R',
69+
accelerator: shortcuts.menu.RUN,
70+
enabled: state.isConnected && state.view === 'editor',
71+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.RUN)
72+
},
73+
{
74+
label: 'Run selection',
75+
accelerator: shortcuts.menu.RUN_SELECTION,
6976
enabled: state.isConnected && state.view === 'editor',
70-
click: () => win.webContents.send('shortcut-cmd', 'r')
77+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.RUN_SELECTION)
7178
},
7279
{
7380
label: 'Stop',
74-
accelerator: 'CmdOrCtrl+H',
81+
accelerator: shortcuts.menu.STOP,
7582
enabled: state.isConnected && state.view === 'editor',
76-
click: () => win.webContents.send('shortcut-cmd', 'h')
83+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.STOP)
7784
},
7885
{
7986
label: 'Reset',
80-
accelerator: 'CmdOrCtrl+Shift+R',
87+
accelerator: shortcuts.menu.RESET,
88+
enabled: state.isConnected && state.view === 'editor',
89+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.RESET)
90+
},
91+
{ type: 'separator' },
92+
{
93+
label: 'Clear terminal',
94+
accelerator: shortcuts.menu.CLEAR_TERMINAL,
8195
enabled: state.isConnected && state.view === 'editor',
82-
click: () => win.webContents.send('shortcut-cmd', 'R')
96+
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.CLEAR_TERMINAL)
8397
}
8498
]
8599
},
@@ -100,7 +114,7 @@ module.exports = function registerMenu(win, state = {}) {
100114
}
101115
}
102116
},
103-
{ role: 'toggleDevTools', accelerator: ''},
117+
{ role: 'toggleDevTools'},
104118
{ type: 'separator' },
105119
{ role: 'resetZoom' },
106120
{ role: 'zoomIn' },

backend/shortcuts.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
global: {
3+
CONNECT: 'CommandOrControl+Shift+C',
4+
DISCONNECT: 'CommandOrControl+Shift+D',
5+
SAVE: 'CommandOrControl+S',
6+
RUN: 'CommandOrControl+R',
7+
RUN_SELECTION: 'CommandOrControl+Alt+R',
8+
STOP: 'CommandOrControl+H',
9+
RESET: 'CommandOrControl+Shift+R',
10+
CLEAR_TERMINAL: 'CommandOrControl+L',
11+
ESC: 'Escape'
12+
},
13+
menu: {
14+
CONNECT: 'CmdOrCtrl+Shift+C',
15+
DISCONNECT: 'CmdOrCtrl+Shift+D',
16+
SAVE: 'CmdOrCtrl+S',
17+
RUN: 'CmdOrCtrl+R',
18+
RUN_SELECTION: 'CmdOrCtrl+Alt+R',
19+
STOP: 'CmdOrCtrl+H',
20+
RESET: 'CmdOrCtrl+Shift+R',
21+
CLEAR_TERMINAL: 'CmdOrCtrl+L',
22+
}
23+
}

index.js

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { app, BrowserWindow, ipcMain, dialog, globalShortcut } = require('electron')
22
const path = require('path')
33
const fs = require('fs')
4+
const shortcuts = require('./backend/shortcuts.js').global
45

56
const registerIPCHandlers = require('./backend/ipc.js')
67
const registerMenu = require('./backend/menu.js')
@@ -86,47 +87,58 @@ function shortcutAction(key) {
8687

8788
// Shortcuts
8889
function registerShortcuts() {
89-
globalShortcut.register('CommandOrControl+R', () => {
90-
console.log('Running Program')
91-
shortcutAction('r')
92-
})
93-
globalShortcut.register('CommandOrControl+Alt+R', () => {
94-
console.log('Running Code Selection')
95-
shortcutAction('_r')
96-
})
97-
globalShortcut.register('CommandOrControl+H', () => {
98-
console.log('Stopping Program (Halt)')
99-
shortcutAction('h')
100-
})
101-
globalShortcut.register('CommandOrControl+S', () => {
102-
console.log('Saving File')
103-
shortcutAction('s')
90+
Object.entries(shortcuts).forEach(([command, shortcut]) => {
91+
globalShortcut.register(shortcut, () => {
92+
shortcutAction(shortcut)
93+
});
10494
})
95+
// shortcuts.forEach(element => {
96+
// globalShortcut.register(element, () => {
97+
98+
// shortcutAction(element)
99+
// });
100+
// });
101+
// globalShortcut.register(shortcuts.RUN, () => {
102+
// console.log('Running Program')
103+
// shortcutAction(shortcuts.RUN)
104+
// })
105+
// globalShortcut.register('CommandOrControl+Alt+R', () => {
106+
// console.log('Running Code Selection')
107+
// shortcutAction('meta_alt_r')
108+
// })
109+
// globalShortcut.register('CommandOrControl+H', () => {
110+
// console.log('Stopping Program (Halt)')
111+
// shortcutAction('meta_h')
112+
// })
113+
// globalShortcut.register('CommandOrControl+S', () => {
114+
// console.log('Saving File')
115+
// shortcutAction('meta_s')
116+
// })
105117

106-
globalShortcut.register('CommandOrControl+Shift+R', () => {
107-
console.log('Resetting Board')
108-
shortcutAction('R')
109-
})
110-
globalShortcut.register('CommandOrControl+Shift+C', () => {
111-
console.log('Connect to Board')
112-
shortcutAction('C')
113-
})
114-
globalShortcut.register('CommandOrControl+Shift+D', () => {
115-
console.log('Disconnect from Board')
116-
shortcutAction('D')
117-
}),
118-
globalShortcut.register('CommandOrControl+K', () => {
119-
console.log('Clear Terminal')
120-
shortcutAction('K')
121-
}),
122-
// Future: Toggle REPL Panel
123-
// globalShortcut.register('CommandOrControl+T', () => {
124-
// console.log('Toggle Terminal')
125-
// shortcutAction('T')
118+
// globalShortcut.register('CommandOrControl+Shift+R', () => {
119+
// console.log('Resetting Board')
120+
// shortcutAction('meta_shift_r')
121+
// })
122+
// globalShortcut.register(shortcuts.CONNECT, () => {
123+
// console.log('Connect to Board')
124+
// shortcutAction(shortcuts.CONNECT)
125+
// })
126+
// globalShortcut.register(shortcuts.DISCONNECT, () => {
127+
// console.log('Disconnect from Board')
128+
// shortcutAction(shortcuts.DISCONNECT)
126129
// }),
127-
globalShortcut.register('Escape', () => {
128-
shortcutAction('ESC')
129-
})
130+
// globalShortcut.register('CommandOrControl+K', () => {
131+
// console.log('Clear Terminal')
132+
// shortcutAction('K')
133+
// }),
134+
// // Future: Toggle REPL Panel
135+
// // globalShortcut.register('CommandOrControl+T', () => {
136+
// // console.log('Toggle Terminal')
137+
// // shortcutAction('T')
138+
// // }),
139+
// globalShortcut.register('Escape', () => {
140+
// shortcutAction('ESC')
141+
// })
130142
}
131143

132144
app.on('ready', () => {

preload.js

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
console.log('preload')
22
const { contextBridge, ipcRenderer } = require('electron')
33
const path = require('path')
4-
4+
const shortcuts = require('./backend/shortcuts.js').global
55
const MicroPython = require('micropython.js')
66
const { emit, platform } = require('process')
77
// const { platform } = requireprocess.platform
@@ -196,8 +196,8 @@ const Window = {
196196

197197
updateMenuState: (state) => {
198198
return ipcRenderer.invoke('update-menu-state', state)
199-
}
200-
199+
},
200+
getShortcuts: () => shortcuts
201201
}
202202

203203
contextBridge.exposeInMainWorld('BridgeSerial', Serial)

ui/arduino/store.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const serial = window.BridgeSerial
33
const disk = window.BridgeDisk
44
const win = window.BridgeWindow
55

6+
const shortcuts = window.BridgeWindow.getShortcuts()
7+
68
const newFileContent = `# This program was created in Arduino Lab for MicroPython
79
810
print('Hello, MicroPython!')
@@ -1407,17 +1409,17 @@ async function store(state, emitter) {
14071409
// })
14081410

14091411
win.onKeyboardShortcut((key) => {
1410-
if (key === 'C') {
1412+
if (key === shortcuts.CONNECT) {
14111413
emitter.emit('open-connection-dialog')
14121414
}
1413-
if (key === 'D') {
1415+
if (key === shortcuts.DISCONNECT) {
14141416
emitter.emit('disconnect')
14151417
}
1416-
if (key === 'R') {
1418+
if (key === shortcuts.RESET) {
14171419
if (state.view != 'editor') return
14181420
emitter.emit('reset')
14191421
}
1420-
if (key === 'K') {
1422+
if (key === shortcuts.CLEAR_TERMINAL) {
14211423
if (state.view != 'editor') return
14221424
emitter.emit('clear-terminal')
14231425
}
@@ -1426,23 +1428,23 @@ async function store(state, emitter) {
14261428
// if (state.view != 'editor') return
14271429
// emitter.emit('clear-terminal')
14281430
// }
1429-
if (key === 'r') {
1431+
if (key === shortcuts.RUN) {
14301432
if (state.view != 'editor') return
14311433
runCode()
14321434
}
1433-
if (key === '_r') {
1435+
if (key === shortcuts.RUN_SELECTION) {
14341436
if (state.view != 'editor') return
14351437
runCodeSelection()
14361438
}
1437-
if (key === 'h') {
1439+
if (key === shortcuts.STOP) {
14381440
if (state.view != 'editor') return
14391441
stopCode()
14401442
}
1441-
if (key === 's') {
1443+
if (key === shortcuts.SAVE) {
14421444
if (state.view != 'editor') return
14431445
emitter.emit('save')
14441446
}
1445-
if (key === 'ESC') {
1447+
if (key === shortcuts.ESC) {
14461448
if (state.isConnectionDialogOpen) {
14471449
emitter.emit('close-connection-dialog')
14481450
}

ui/arduino/views/components/repl-panel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function ReplOperations(state, emit) {
5050
Button({
5151
icon: 'delete.svg',
5252
size: 'small',
53-
tooltip: `Clean (${state.platform === 'darwin' ? 'Cmd' : 'Ctrl'}+k)`,
53+
tooltip: `Clean (${state.platform === 'darwin' ? 'Cmd' : 'Ctrl'}+L)`,
5454
onClick: () => emit('clear-terminal')
5555
})
5656
]

0 commit comments

Comments
 (0)
0