Skip to content

Add button to launch package installer #164

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 7 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 20 additions & 0 deletions backend/ipc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs')
const registerMenu = require('./menu.js')
const serial = require('./serial/serial.js').sharedInstance
const { shell } = require('electron');

const {
openFolderDialog,
Expand Down Expand Up @@ -138,6 +139,25 @@ module.exports = function registerIPCHandlers(win, ipcMain, app, dialog) {
registerMenu(win, state)
})

ipcMain.handle('launch-app', async (event, urlScheme) => {
// Launch an external app with a custom protocol
return new Promise((resolve, reject) => {
try {
shell.openExternal(urlScheme).then(() => {
resolve(true); // App opened successfully
}).catch(() => {
resolve(false); // App not installed
});
} catch (err) {
reject(err);
}
});
});

ipcMain.handle('open-url', async (event, url) => {
shell.openExternal(url);
});

win.on('close', (event) => {
console.log('BrowserWindow', 'close')
event.preventDefault()
Expand Down
17 changes: 16 additions & 1 deletion preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ console.log('preload')
const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')
const shortcuts = require('./backend/shortcuts.js').shortcuts.global
const { emit, platform } = require('process')
const { platform } = require('process')
const SerialBridge = require('./backend/serial/serial-bridge.js')

const Disk = {
Expand Down Expand Up @@ -85,6 +85,21 @@ const Window = {
getShortcuts: () => shortcuts
}

/**
* Launches an app using the provided URL scheme (e.g. myapp://). If the app is not installed, it will
* fallback to open the provided fallback URL.
* @param {string} url The URL scheme to use to launch the app
* @param {string} fallbackUrl The URL to open if the app is not installed
*/
async function launchApp(url, fallbackUrl) {
const success = await ipcRenderer.invoke('launch-app', url);

if (!success) {
await ipcRenderer.invoke('open-url', fallbackUrl); // Fallback to open a URL in the default browser
}
}

contextBridge.exposeInMainWorld('launchApp', launchApp)
contextBridge.exposeInMainWorld('BridgeSerial', SerialBridge)
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
contextBridge.exposeInMainWorld('BridgeWindow', Window)
33 changes: 33 additions & 0 deletions ui/arduino/media/install-package.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions ui/arduino/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ async function store(state, emitter) {
updateMenu()
})

emitter.on('launch-app', async (url, fallbackUrl) => {
window.launchApp(url, fallbackUrl)
})

// CONNECTION DIALOG
emitter.on('open-connection-dialog', async () => {
log('open-connection-dialog')
Expand Down
12 changes: 11 additions & 1 deletion ui/arduino/views/components/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ function Toolbar(state, emit) {
disabled: !_canSave,
onClick: () => emit('save')
})}

${!window.BridgeWindow.isLinux() ? html`<div class="separator"></div>` : ''}

${!window.BridgeWindow.isLinux() ? Button({
icon: 'install-package.svg',
label: `Add Package`,
onClick: () => {
if(state.isConnected) emit('disconnect') // Package installer requires exclusive access to the serial port
emit('launch-app', 'micropython-package-installer://', 'https://github.com/arduino/lab-micropython-package-installer/releases/latest')
}
}) : '' }
</div>

<div id="app-views">
Expand All @@ -88,7 +99,6 @@ function Toolbar(state, emit) {
square: true,
onClick: () => emit('change-view', 'file-manager')
})}

</div>
</div>
`
Expand Down