Skip to content

Commit

Permalink
Refactor menu template code
Browse files Browse the repository at this point in the history
  • Loading branch information
FLSoz committed Mar 29, 2022
1 parent 802c028 commit 42bf785
Showing 1 changed file with 32 additions and 90 deletions.
122 changes: 32 additions & 90 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ export default class MenuBuilder {
if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true') {
this.setupDevelopmentEnvironment();
}

const template = process.platform === 'darwin' ? this.buildDarwinTemplate() : this.buildDefaultTemplate();

const template = this.buildDefaultTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);

return menu;
}

setupDevelopmentEnvironment(): void {
this.mainWindow.webContents.on('context-menu', (_, props) => {
const { x, y } = props;

Menu.buildFromTemplate([
{
label: 'Inspect element',
Expand All @@ -41,8 +37,8 @@ export default class MenuBuilder {
});
}

buildDarwinTemplate(): MenuItemConstructorOptions[] {
const subMenuAbout: DarwinMenuItemConstructorOptions = {
buildDefaultTemplate(): MenuItemConstructorOptions[] {
const subMenuAboutDarwin: DarwinMenuItemConstructorOptions = {
label: 'TTSMM',
submenu: [
{
Expand Down Expand Up @@ -73,14 +69,29 @@ export default class MenuBuilder {
}
]
};
const subMenuEdit: DarwinMenuItemConstructorOptions = {
const subMenuEditDarwin: DarwinMenuItemConstructorOptions = {
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'Command+Z', selector: 'undo:' },
{ label: 'Redo', accelerator: 'Shift+Command+Z', selector: 'redo:' },
{ type: 'separator' }
]
};
const subMenuEdit: MenuItemConstructorOptions = {
label: '&Edit',
submenu: [
{
label: '&Undo',
accelerator: 'Ctrl+Z',
click: () => {}
},
{
label: '&Redo',
accelerator: 'Ctrl+Y',
click: () => {}
}
]
};
const subMenuView: MenuItemConstructorOptions = {
label: 'View',
submenu: [
Expand All @@ -107,7 +118,7 @@ export default class MenuBuilder {
}
]
};
const subMenuWindow: DarwinMenuItemConstructorOptions = {
const subMenuWindowDarwin: DarwinMenuItemConstructorOptions = {
label: 'Window',
submenu: [
{
Expand Down Expand Up @@ -149,87 +160,18 @@ export default class MenuBuilder {
}
]
};
return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp];
}

buildDefaultTemplate(): MenuItemConstructorOptions[] {
const templateDefault: MenuItemConstructorOptions[] = [
{
label: '&Edit',
submenu: [
{
label: '&Undo',
accelerator: 'Ctrl+Z',
click: () => {}
},
{
label: '&Redo',
accelerator: 'Ctrl+Y',
click: () => {}
}
]
},
{
label: '&View',
submenu: [
{
label: '&Reload',
accelerator: 'Ctrl+R',
click: () => {
this.mainWindow.webContents.reload();
}
},
{
label: 'Toggle &Full Screen',
accelerator: 'F11',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
}
},
{
label: 'Toggle &Developer Tools',
accelerator: 'F12',
click: () => {
this.mainWindow.webContents.toggleDevTools();
}
}
]
},
{
label: 'Check for Updates',
click: checkForUpdates
},
{
label: 'Help',
submenu: [
{
label: 'TerraTech Forums',
click() {
shell.openExternal('https://forum.terratechgame.com/index.php');
}
},
{
label: `TerraTech Discord`,
click() {
shell.openExternal('https://discord.com/invite/terratechgame');
}
},
{
label: 'Documentation',
click() {
shell.openExternal('https://github.com/FLSoz/terratech-steam-mod-loader/#readme');
}
},
{
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/FLSoz/terratech-steam-mod-loader/issues');
}
}
]
}
];
const subMenuUpdates: MenuItemConstructorOptions = {
label: '&Updates',
submenu: [
{
label: 'Check for updates',
click: checkForUpdates
}
]
};

return templateDefault;
return process.platform === 'darwin'
? [subMenuAboutDarwin, subMenuEditDarwin, subMenuView, subMenuWindowDarwin, subMenuUpdates, subMenuHelp]
: [subMenuEdit, subMenuView, subMenuUpdates, subMenuHelp];
}
}

0 comments on commit 42bf785

Please sign in to comment.