feat: More menu options

This commit is contained in:
Felix Rieseberg 2019-08-21 11:26:56 +02:00
parent dcc3e72bcf
commit c7f765df03
3 changed files with 64 additions and 22 deletions

View file

@ -12,10 +12,15 @@ export const CONSTANTS = {
export const IPC_COMMANDS = {
TOGGLE_INFO: 'TOGGLE_INFO',
SHOW_DISK_IMAGE: 'SHOW_DISK_IMAGE',
// Machine instructions
MACHINE_START: 'MACHINE_START',
MACHINE_RESTART: 'MACHINE_RESTART',
MACHINE_STOP: 'MACHINE_STOP',
MACHINE_RESET: 'MACHINE_RESET',
MACHINE_ALTF4: 'MACHINE_ALTF4',
MACHINE_CTRL_ALT_DEL: 'MACHINE_CTRL_ALT_DEL',
SHOW_DISK_IMAGE: 'SHOW_DISK_IMAGE'
// Machine events
MACHINE_STARTED: 'MACHINE_STARTED',
MACHINE_STOPPED: 'MACHINE_STOPPED'
}

View file

@ -1,4 +1,4 @@
import { app, shell, Menu, BrowserWindow } from "electron";
import { app, shell, Menu, BrowserWindow, ipcMain } from "electron";
import { clearCaches } from "../cache";
import { IPC_COMMANDS } from "../constants";
@ -11,15 +11,29 @@ const LINKS = {
help: "https://github.com/felixrieseberg/windows95/blob/master/HELP.md"
};
export async function setupMenu() {
await createMenu();
ipcMain.on(IPC_COMMANDS.MACHINE_STARTED, () =>
createMenu({ isRunning: true })
);
ipcMain.on(IPC_COMMANDS.MACHINE_STOPPED, () =>
createMenu({ isRunning: false })
);
}
function send(cmd: string) {
const windows = BrowserWindow.getAllWindows();
if (windows[0]) {
console.log(`Sending "${cmd}"`);
windows[0].webContents.send(cmd);
} else {
console.log(`Tried to send "${cmd}", but could not find window`);
}
}
export async function setupMenu() {
async function createMenu({ isRunning } = { isRunning: false }) {
const template: Array<Electron.MenuItemConstructorOptions> = [
{
label: "View",
@ -94,22 +108,30 @@ export async function setupMenu() {
submenu: [
{
label: "Send Ctrl+Alt+Del",
click: () => send(IPC_COMMANDS.MACHINE_CTRL_ALT_DEL)
click: () => send(IPC_COMMANDS.MACHINE_CTRL_ALT_DEL),
enabled: isRunning
},
{
type: "separator"
},
{
label: "Stop",
click: () => send(IPC_COMMANDS.MACHINE_STOP)
},
isRunning
? {
label: "Stop",
click: () => send(IPC_COMMANDS.MACHINE_STOP)
}
: {
label: "Start",
click: () => send(IPC_COMMANDS.MACHINE_START)
},
{
label: "Restart",
click: () => send(IPC_COMMANDS.MACHINE_RESTART)
click: () => send(IPC_COMMANDS.MACHINE_RESTART),
enabled: isRunning
},
{
label: "Reset",
click: () => send(IPC_COMMANDS.MACHINE_RESET)
click: () => send(IPC_COMMANDS.MACHINE_RESET),
enabled: isRunning
},
{
type: "separator"

View file

@ -29,6 +29,9 @@ export class Emulator extends React.Component<{}, EmulatorState> {
super(props);
this.startEmulator = this.startEmulator.bind(this);
this.stopEmulator = this.stopEmulator.bind(this);
this.restartEmulator = this.restartEmulator.bind(this);
this.resetEmulator = this.resetEmulator.bind(this);
this.state = {
isBootingFresh: false,
@ -116,15 +119,10 @@ export class Emulator extends React.Component<{}, EmulatorState> {
}
});
ipcRenderer.on(IPC_COMMANDS.MACHINE_STOP, () => {
this.stopEmulator();
});
ipcRenderer.on(IPC_COMMANDS.MACHINE_RESTART, () => {
if (this.state.emulator && this.state.isRunning) {
this.state.emulator.restart();
}
});
ipcRenderer.on(IPC_COMMANDS.MACHINE_STOP, this.stopEmulator);
ipcRenderer.on(IPC_COMMANDS.MACHINE_RESET, this.resetEmulator);
ipcRenderer.on(IPC_COMMANDS.MACHINE_START, this.startEmulator);
ipcRenderer.on(IPC_COMMANDS.MACHINE_RESTART, this.restartEmulator);
ipcRenderer.on(IPC_COMMANDS.TOGGLE_INFO, () => {
this.setState({ isInfoDisplayed: !this.state.isInfoDisplayed });
@ -240,7 +238,7 @@ export class Emulator extends React.Component<{}, EmulatorState> {
boot_order: 0x132
};
console.log(`Starting emulator with options`, options);
console.log(`🚜 Starting emulator with options`, options);
window["emulator"] = new V86Starter(options);
@ -250,6 +248,8 @@ export class Emulator extends React.Component<{}, EmulatorState> {
isRunning: true
});
ipcRenderer.send(IPC_COMMANDS.MACHINE_STARTED);
// Restore state. We can't do this right away
// and randomly chose 500ms as the appropriate
// wait time (lol)
@ -263,22 +263,37 @@ export class Emulator extends React.Component<{}, EmulatorState> {
}, 500);
}
/**
* Restart emulator
*/
private restartEmulator() {
if (this.state.emulator && this.state.isRunning) {
console.log(`🚜 Restarting emulator`);
this.state.emulator.restart();
} else {
console.log(`🚜 Restarting emulator failed: Emulator not running`);
}
}
/**
* Stop the emulator
*/
private async stopEmulator() {
const { emulator } = this.state;
const { emulator, isRunning } = this.state;
if (!emulator) {
if (!emulator || !isRunning) {
return;
}
console.log(`🚜 Stopping emulator`);
await this.saveState();
this.unlockMouse();
emulator.stop();
this.setState({ isRunning: false });
document.body.classList.add("paused");
ipcRenderer.send(IPC_COMMANDS.MACHINE_STOPPED);
}
/**