Skip to content

Feature/shortcuts #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e159d5f
Implemented keyboard shortcuts.
ubidefeo Dec 8, 2024
bfcc2c6
Renamed method to onKeyboardShortcut.
ubidefeo Dec 8, 2024
c6d8d91
Removed whitespace.
ubidefeo Dec 8, 2024
45a582a
Removed whitespace.
ubidefeo Dec 8, 2024
4c648a3
Removed leftover test code from main.js.
ubidefeo Dec 8, 2024
ed7f839
Removed unnecessary shortcutsContext member.
ubidefeo Dec 8, 2024
9a490c2
Removed accelerator shortcuts from Menu items for Reload and Dev Tools.
ubidefeo Dec 8, 2024
177b7d1
Implemented Board menu item.
ubidefeo Dec 9, 2024
c6ec180
Menu cleanup.
ubidefeo Dec 9, 2024
e9c8a2e
Merge branch 'fix/hide-navigation-dots' into feature/shortcuts
ubidefeo Dec 9, 2024
1fb588e
Added ESC key listener. Enabled it on connection dialog.
ubidefeo Dec 9, 2024
861b658
Added Clear Terminal shortcut Meta+k.
ubidefeo Dec 9, 2024
afadd3f
Testing disconnect before manual reload.
ubidefeo Dec 10, 2024
1560f07
Merge branch 'development' into feature/shortcuts
ubidefeo Dec 10, 2024
7c6cdde
Merge branch 'development' into feature/shortcuts
ubidefeo Dec 10, 2024
9a58c11
Merge branch 'development' into feature/shortcuts
ubidefeo Dec 10, 2024
88fe9a2
Implemented ALT option to run code selection.
ubidefeo Dec 11, 2024
9908d4a
Refactored menu and global shortcuts into constants file.
ubidefeo Dec 12, 2024
0fa99b1
Changed 'selection' to 'onlySelected'.
ubidefeo Dec 12, 2024
530dbc6
Moved registerMenu to file scope.
ubidefeo Dec 12, 2024
a019c99
Only save if openFile (tab) has changes.
ubidefeo Dec 13, 2024
b051120
Added view switch, replaced editor icon, updated shortcuts.
ubidefeo Dec 13, 2024
bd3cc3b
Moved 'Clear Terminal' to 'View'
ubidefeo Dec 13, 2024
9231661
Removed comments with dev notes.
ubidefeo Dec 13, 2024
9759ed7
Replaced Win/Linux Ctrl+Alt+R ignored shortcut with Ctrl+Alt+S
ubidefeo Dec 13, 2024
6432226
Amended shortcut handling in store.js
ubidefeo Dec 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions backend/ipc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const fs = require('fs')
const registerMenu = require('./menu.js')

const {
openFolderDialog,
listFolder,
Expand Down Expand Up @@ -129,9 +131,18 @@ module.exports = function registerIPCHandlers(win, ipcMain, app, dialog) {
return response != opt.cancelId
})

ipcMain.handle('update-menu-state', (event, state) => {
registerMenu(win, state)
})

win.on('close', (event) => {
console.log('BrowserWindow', 'close')
event.preventDefault()
win.webContents.send('check-before-close')
})

// handle disconnection before reload
ipcMain.handle('prepare-reload', async (event) => {
return win.webContents.send('before-reload')
})
Comment on lines +143 to +147
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go when we merge #158

}
86 changes: 78 additions & 8 deletions backend/menu.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
const { app, Menu } = require('electron')
const path = require('path')
const openAboutWindow = require('about-window').default
const shortcuts = require('./shortcuts.js')
const { type } = require('os')

module.exports = function registerMenu(win) {
module.exports = function registerMenu(win, state = {}) {
const isMac = process.platform === 'darwin'
const template = [
...(isMac ? [{
label: app.name,
submenu: [
{ role: 'about'},
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hide', accelerator: 'CmdOrCtrl+Shift+H' },
{ role: 'hideOthers' },
{ role: 'unhide' },
{ type: 'separator' },
Expand All @@ -35,7 +36,6 @@ module.exports = function registerMenu(win) {
{ role: 'copy' },
{ role: 'paste' },
...(isMac ? [
{ role: 'pasteAndMatchStyle' },
{ role: 'selectAll' },
{ type: 'separator' },
{
Expand All @@ -51,11 +51,66 @@ module.exports = function registerMenu(win) {
])
]
},
{
label: 'Board',
submenu: [
{
label: 'Connect',
accelerator: shortcuts.menu.CONNECT,
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.CONNECT)
},
{
label: 'Disconnect',
accelerator: shortcuts.menu.DISCONNECT,
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.DISCONNECT)
},
{ type: 'separator' },
{
label: 'Run',
accelerator: shortcuts.menu.RUN,
enabled: state.isConnected && state.view === 'editor',
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.RUN)
},
{
label: 'Run selection',
accelerator: isMac ? shortcuts.menu.RUN_SELECTION : shortcuts.menu.RUN_SELECTION_WL,
enabled: state.isConnected && state.view === 'editor',
click: () => win.webContents.send('shortcut-cmd', (isMac ? shortcuts.global.RUN_SELECTION : shortcuts.global.RUN_SELECTION_WL))
},
{
label: 'Stop',
accelerator: shortcuts.menu.STOP,
enabled: state.isConnected && state.view === 'editor',
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.STOP)
},
{
label: 'Reset',
accelerator: shortcuts.menu.RESET,
enabled: state.isConnected && state.view === 'editor',
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.RESET)
},
{ type: 'separator' }
]
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'toggleDevTools' },
{
label: 'Editor',
accelerator: shortcuts.menu.EDITOR_VIEW,
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.EDITOR_VIEW,)
},
{
label: 'Files',
accelerator: shortcuts.menu.FILES_VIEW,
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.FILES_VIEW)
},
{
label: 'Clear terminal',
accelerator: shortcuts.menu.CLEAR_TERMINAL,
enabled: state.isConnected && state.view === 'editor',
click: () => win.webContents.send('shortcut-cmd', shortcuts.global.CLEAR_TERMINAL)
},
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
Expand All @@ -67,6 +122,22 @@ module.exports = function registerMenu(win) {
{
label: 'Window',
submenu: [
{
label: 'Reload',
accelerator: '',
click: async () => {
try {
win.webContents.send('cleanup-before-reload')
setTimeout(() => {
win.reload()
}, 500)
} catch(e) {
console.error('Reload from menu failed:', e)
}
}
},
{ role: 'toggleDevTools'},
{ type: 'separator' },
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac ? [
Expand All @@ -75,7 +146,7 @@ module.exports = function registerMenu(win) {
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }

])
]
},
Expand All @@ -102,7 +173,6 @@ module.exports = function registerMenu(win) {
openAboutWindow({
icon_path: path.resolve(__dirname, '../ui/arduino/media/about_image.png'),
css_path: path.resolve(__dirname, '../ui/arduino/views/about.css'),
// about_page_dir: path.resolve(__dirname, '../ui/arduino/views/'),
copyright: '© Arduino SA 2022',
package_json_dir: path.resolve(__dirname, '..'),
bug_report_url: "https://github.com/arduino/lab-micropython-editor/issues",
Expand Down
29 changes: 29 additions & 0 deletions backend/shortcuts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
global: {
CONNECT: 'CommandOrControl+Shift+C',
DISCONNECT: 'CommandOrControl+Shift+D',
SAVE: 'CommandOrControl+S',
RUN: 'CommandOrControl+R',
RUN_SELECTION: 'CommandOrControl+Alt+R',
RUN_SELECTION_WL: 'CommandOrControl+Alt+S',
STOP: 'CommandOrControl+H',
RESET: 'CommandOrControl+Shift+R',
CLEAR_TERMINAL: 'CommandOrControl+L',
EDITOR_VIEW: 'CommandOrControl+Alt+1',
FILES_VIEW: 'CommandOrControl+Alt+2',
ESC: 'Escape'
},
menu: {
CONNECT: 'CmdOrCtrl+Shift+C',
DISCONNECT: 'CmdOrCtrl+Shift+D',
SAVE: 'CmdOrCtrl+S',
RUN: 'CmdOrCtrl+R',
RUN_SELECTION: 'CmdOrCtrl+Alt+R',
RUN_SELECTION_WL: 'CmdOrCtrl+Alt+S',
STOP: 'CmdOrCtrl+H',
RESET: 'CmdOrCtrl+Shift+R',
CLEAR_TERMINAL: 'CmdOrCtrl+L',
EDITOR_VIEW: 'CmdOrCtrl+Alt+1',
FILES_VIEW: 'CmdOrCtrl+Alt+2'
}
}
54 changes: 51 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { app, BrowserWindow, ipcMain, dialog } = require('electron')
const { app, BrowserWindow, ipcMain, dialog, globalShortcut } = require('electron')
const path = require('path')
const fs = require('fs')
const shortcuts = require('./backend/shortcuts.js').global

const registerIPCHandlers = require('./backend/ipc.js')
const registerMenu = require('./backend/menu.js')
Expand Down Expand Up @@ -49,12 +50,59 @@ function createWindow () {
win.show()
})

win.webContents.on('before-reload', async (event) => {
// Prevent the default reload behavior
event.preventDefault()

try {
// Tell renderer to do cleanup
win.webContents.send('cleanup-before-reload')

// Wait for cleanup then reload
setTimeout(() => {
// This will trigger a page reload, but won't trigger 'before-reload' again
win.reload()
}, 500)
} catch(e) {
console.error('Reload preparation failed:', e)
}
})
Comment on lines +53 to +69
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this when merging #158

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll have to do this in your PR :)


const initialMenuState = {
isConnected: false,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous if isConnected is not set it has a falsy value

Suggested change
isConnected: false,

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually at first menu registration you want to hide Run, Stop etc based on an initial state, forced to be false

view: 'editor'
}

registerIPCHandlers(win, ipcMain, app, dialog)
registerMenu(win)
registerMenu(win, initialMenuState)

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
}

app.on('ready', createWindow)
function shortcutAction(key) {
win.webContents.send('shortcut-cmd', key);
}

// Shortcuts
function registerShortcuts() {
Object.entries(shortcuts).forEach(([command, shortcut]) => {
globalShortcut.register(shortcut, () => {
shortcutAction(shortcut)
});
})
}

app.on('ready', () => {
createWindow()
registerShortcuts()

win.on('focus', () => {
registerShortcuts()
})
win.on('blur', () => {
globalShortcut.unregisterAll()
})

})
33 changes: 30 additions & 3 deletions preload.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
console.log('preload')
const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')

const shortcuts = require('./backend/shortcuts.js').global
const MicroPython = require('micropython.js')
const { emit, platform } = require('process')

const board = new MicroPython()
board.chunk_size = 192
board.chunk_sleep = 200
Expand Down Expand Up @@ -155,12 +157,37 @@ const Window = {
setWindowSize: (minWidth, minHeight) => {
ipcRenderer.invoke('set-window-size', minWidth, minHeight)
},
onKeyboardShortcut: (callback, key) => {
ipcRenderer.on('shortcut-cmd', (event, k) => {
callback(k);
})
},

onBeforeReload: (callback) => {
ipcRenderer.on('cleanup-before-reload', async () => {
try {
await callback()
} catch(e) {
console.error('Cleanup before reload failed:', e)
}
})
},
Comment on lines +166 to +174
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to go when merging #158

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sebromero , could you take it out when you rebase #158 after this is merged?


beforeClose: (callback) => ipcRenderer.on('check-before-close', callback),
confirmClose: () => ipcRenderer.invoke('confirm-close'),
isPackaged: () => ipcRenderer.invoke('is-packaged'),
openDialog: (opt) => ipcRenderer.invoke('open-dialog', opt)
}
openDialog: (opt) => ipcRenderer.invoke('open-dialog', opt),

getOS: () => platform,
isWindows: () => platform === 'win32',
isMac: () => platform === 'darwin',
isLinux: () => platform === 'linux',

updateMenuState: (state) => {
return ipcRenderer.invoke('update-menu-state', state)
},
getShortcuts: () => shortcuts
}

contextBridge.exposeInMainWorld('BridgeSerial', Serial)
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
Expand Down
2 changes: 0 additions & 2 deletions ui/arduino/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ window.addEventListener('load', () => {
app.use(store);
app.route('*', App)
app.mount('#app')

app.emitter.on('DOMContentLoaded', () => {
if (app.state.diskNavigationRoot) {
app.emitter.emit('refresh-files')
}
})

})
3 changes: 3 additions & 0 deletions ui/arduino/media/code.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy