Last active
January 25, 2022 00:30
-
-
Save kiliman/4b58e3f726fd796d50280ef1c855ba3a to your computer and use it in GitHub Desktop.
Remix Electron to add menus and navigate to routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @ts-check | |
const electron = require("electron"); | |
const { app, BrowserWindow, dialog, Menu } = electron; | |
const { startServer } = require("./server"); | |
let win; | |
let server; | |
async function main() { | |
await app.whenReady(); | |
server = await startServer(); | |
win = new BrowserWindow(); | |
await win.loadURL(server.url); | |
initMenu(); | |
} | |
app.on("window-all-closed", () => { | |
if (process.platform != "darwin") app.quit(); | |
}); | |
main().catch((error) => { | |
dialog.showErrorBox("Error", error.stack || error.message || String(error)); | |
}); | |
function navigate(route) { | |
const url = new URL(server.url); | |
win.loadURL(url.origin + route); | |
} | |
function initMenu() { | |
var application_menu = [ | |
{ | |
label: "menu1", | |
submenu: [ | |
{ | |
label: "Undo", | |
accelerator: "CmdOrCtrl+Z", | |
role: "undo", | |
}, | |
{ | |
label: "Open", | |
accelerator: "CmdOrCtrl+O", | |
click: () => { | |
electron.dialog.showOpenDialog({ | |
properties: ["openFile", "openDirectory", "multiSelections"], | |
}); | |
}, | |
}, | |
{ | |
label: "submenu1", | |
submenu: [ | |
{ | |
label: "item1", | |
accelerator: "CmdOrCtrl+A", | |
click: () => { | |
win.openDevTools(); | |
}, | |
}, | |
{ | |
label: "item2", | |
accelerator: "CmdOrCtrl+B", | |
click: () => { | |
win.closeDevTools(); | |
}, | |
}, | |
], | |
}, | |
], | |
}, | |
]; | |
if (process.platform == "darwin") { | |
const name = app.getName(); | |
application_menu.unshift({ | |
label: name, | |
submenu: [ | |
{ | |
label: "About " + name, | |
role: "about", | |
}, | |
{ | |
type: "separator", | |
}, | |
{ | |
label: "Preferences", | |
role: "preferences", | |
accelerator: "CmdOrCtrl+,", | |
click: () => { | |
navigate("/settings"); | |
}, | |
}, | |
{ | |
type: "separator", | |
}, | |
{ | |
label: "Services", | |
role: "services", | |
submenu: [], | |
}, | |
{ | |
type: "separator", | |
}, | |
{ | |
label: "Hide " + name, | |
accelerator: "Command+H", | |
role: "hide", | |
}, | |
{ | |
label: "Hide Others", | |
accelerator: "Command+Shift+H", | |
role: "hideothers", | |
}, | |
{ | |
label: "Show All", | |
role: "unhide", | |
}, | |
{ | |
type: "separator", | |
}, | |
{ | |
label: "Quit", | |
accelerator: "Command+Q", | |
click: () => { | |
app.quit(); | |
}, | |
}, | |
], | |
}); | |
} | |
let menu = Menu.buildFromTemplate(application_menu); | |
Menu.setApplicationMenu(menu); | |
// Emitted when the window is closed. | |
win.on("closed", function () { | |
// Dereference the window object, usually you would store windows | |
// in an array if your app supports multi windows, this is the time | |
// when you should delete the corresponding element. | |
win = null; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment