windows95/src/main/menu.ts

260 lines
6.1 KiB
TypeScript
Raw Normal View History

2020-08-02 22:11:20 +02:00
import { app, shell, Menu, BrowserWindow, ipcMain } from "electron";
2019-08-21 09:48:49 +02:00
import { clearCaches } from "../cache";
import { IPC_COMMANDS } from "../constants";
import { isDevMode } from "../utils/devmode";
const LINKS = {
homepage: "https://www.twitter.com/felixrieseberg",
repo: "https://github.com/felixrieseberg/windows95",
credits: "https://github.com/felixrieseberg/windows95/blob/master/CREDITS.md",
2020-08-02 22:11:20 +02:00
help: "https://github.com/felixrieseberg/windows95/blob/master/HELP.md",
2019-08-21 09:48:49 +02:00
};
2019-08-21 11:26:56 +02:00
export async function setupMenu() {
await createMenu();
ipcMain.on(IPC_COMMANDS.MACHINE_STARTED, () =>
createMenu({ isRunning: true })
);
ipcMain.on(IPC_COMMANDS.MACHINE_STOPPED, () =>
createMenu({ isRunning: false })
);
}
2019-08-21 09:48:49 +02:00
function send(cmd: string) {
const windows = BrowserWindow.getAllWindows();
if (windows[0]) {
2019-08-21 11:26:56 +02:00
console.log(`Sending "${cmd}"`);
2019-08-21 09:48:49 +02:00
windows[0].webContents.send(cmd);
2019-08-21 11:26:56 +02:00
} else {
console.log(`Tried to send "${cmd}", but could not find window`);
2019-08-21 09:48:49 +02:00
}
}
2019-08-21 11:26:56 +02:00
async function createMenu({ isRunning } = { isRunning: false }) {
2019-08-21 09:48:49 +02:00
const template: Array<Electron.MenuItemConstructorOptions> = [
{
label: "View",
submenu: [
{
label: "Toggle Full Screen",
2020-08-02 22:11:20 +02:00
accelerator: (function () {
2019-08-21 09:48:49 +02:00
if (process.platform === "darwin") {
return "Ctrl+Command+F";
} else {
return "F11";
}
})(),
2020-08-02 22:11:20 +02:00
click: function (_item, focusedWindow) {
2019-08-21 09:48:49 +02:00
if (focusedWindow) {
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
2020-08-02 22:11:20 +02:00
},
2019-08-21 09:48:49 +02:00
},
{
label: "Toggle Developer Tools",
2020-08-02 22:11:20 +02:00
accelerator: (function () {
2019-08-21 09:48:49 +02:00
if (process.platform === "darwin") {
return "Alt+Command+I";
} else {
return "Ctrl+Shift+I";
}
})(),
2020-08-02 22:11:20 +02:00
click: function (_item, focusedWindow) {
2019-08-21 09:48:49 +02:00
if (focusedWindow) {
focusedWindow.webContents.toggleDevTools();
}
2020-08-02 22:11:20 +02:00
},
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
label: "Toggle Emulator Info",
2020-08-02 22:11:20 +02:00
click: () => send(IPC_COMMANDS.TOGGLE_INFO),
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
role: "reload",
},
],
2019-08-21 09:48:49 +02:00
},
{
role: "editMenu",
2020-08-02 22:11:20 +02:00
visible: isDevMode(),
2019-08-21 09:48:49 +02:00
},
{
label: "Window",
role: "window",
submenu: [
{
label: "Minimize",
accelerator: "CmdOrCtrl+M",
2020-08-02 22:11:20 +02:00
role: "minimize",
2019-08-21 09:48:49 +02:00
},
{
label: "Close",
accelerator: "CmdOrCtrl+W",
2020-08-02 22:11:20 +02:00
role: "close",
2019-08-21 19:43:21 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 19:43:21 +02:00
},
{
label: "Zoom in",
click: () => send(IPC_COMMANDS.ZOOM_IN),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 19:43:21 +02:00
},
{
label: "Zoom out",
click: () => send(IPC_COMMANDS.ZOOM_OUT),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 19:43:21 +02:00
},
{
label: "Reset zoom",
click: () => send(IPC_COMMANDS.ZOOM_RESET),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
},
],
2019-08-21 09:48:49 +02:00
},
{
label: "Machine",
submenu: [
{
label: "Send Ctrl+Alt+Del",
2019-08-21 11:26:56 +02:00
click: () => send(IPC_COMMANDS.MACHINE_CTRL_ALT_DEL),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 09:48:49 +02:00
},
2019-08-21 11:34:55 +02:00
{
label: "Send Alt+F4",
click: () => send(IPC_COMMANDS.MACHINE_ALT_F4),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 11:34:55 +02:00
},
{
label: "Send Alt+Enter",
click: () => send(IPC_COMMANDS.MACHINE_ALT_ENTER),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 11:34:55 +02:00
},
2019-08-24 16:58:21 +02:00
{
label: "Send Esc",
click: () => send(IPC_COMMANDS.MACHINE_ESC),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-24 16:58:21 +02:00
},
2019-08-21 10:37:04 +02:00
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 10:37:04 +02:00
},
2019-08-21 11:26:56 +02:00
isRunning
? {
label: "Stop",
2020-08-02 22:11:20 +02:00
click: () => send(IPC_COMMANDS.MACHINE_STOP),
2019-08-21 11:26:56 +02:00
}
: {
label: "Start",
2020-08-02 22:11:20 +02:00
click: () => send(IPC_COMMANDS.MACHINE_START),
2019-08-21 11:26:56 +02:00
},
2019-08-21 09:48:49 +02:00
{
label: "Restart",
2019-08-21 11:26:56 +02:00
click: () => send(IPC_COMMANDS.MACHINE_RESTART),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 09:48:49 +02:00
},
{
label: "Reset",
2019-08-21 11:26:56 +02:00
click: () => send(IPC_COMMANDS.MACHINE_RESET),
2020-08-02 22:11:20 +02:00
enabled: isRunning,
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
label: "Go to Disk Image",
2020-08-02 22:11:20 +02:00
click: () => send(IPC_COMMANDS.SHOW_DISK_IMAGE),
},
],
2019-08-21 09:48:49 +02:00
},
{
label: "Help",
role: "help",
submenu: [
{
label: "Author",
2020-08-02 22:11:20 +02:00
click: () => shell.openExternal(LINKS.homepage),
2019-08-21 09:48:49 +02:00
},
{
label: "windows95 on GitHub",
2020-08-02 22:11:20 +02:00
click: () => shell.openExternal(LINKS.repo),
2019-08-21 09:48:49 +02:00
},
{
label: "Help",
2020-08-02 22:11:20 +02:00
click: () => shell.openExternal(LINKS.help),
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
label: "Troubleshooting",
submenu: [
{
label: "Clear Cache and Restart",
async click() {
await clearCaches();
app.relaunch();
app.quit();
2020-08-02 22:11:20 +02:00
},
},
],
},
],
},
2019-08-21 09:48:49 +02:00
];
if (process.platform === "darwin") {
template.unshift({
label: "windows95",
submenu: [
{
2020-08-02 22:11:20 +02:00
role: "about",
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
role: "services",
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
label: "Hide windows95",
accelerator: "Command+H",
2020-08-02 22:11:20 +02:00
role: "hide",
2019-08-21 09:48:49 +02:00
},
{
label: "Hide Others",
accelerator: "Command+Shift+H",
2020-08-02 22:11:20 +02:00
role: "hideothers",
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
role: "unhide",
2019-08-21 09:48:49 +02:00
},
{
2020-08-02 22:11:20 +02:00
type: "separator",
2019-08-21 09:48:49 +02:00
},
{
label: "Quit",
accelerator: "Command+Q",
click() {
app.quit();
2020-08-02 22:11:20 +02:00
},
},
],
2019-08-21 09:48:49 +02:00
} as any);
}
Menu.setApplicationMenu(Menu.buildFromTemplate(template as any));
}