feishin/src/main/menu.ts

280 lines
9.9 KiB
TypeScript
Raw Normal View History

2022-12-19 15:59:14 -08:00
import { app, Menu, shell, BrowserWindow, MenuItemConstructorOptions } from 'electron';
interface DarwinMenuItemConstructorOptions extends MenuItemConstructorOptions {
2023-07-01 19:10:05 -07:00
selector?: string;
submenu?: DarwinMenuItemConstructorOptions[] | Menu;
2022-12-19 15:59:14 -08:00
}
export default class MenuBuilder {
2023-07-01 19:10:05 -07:00
mainWindow: BrowserWindow;
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
constructor(mainWindow: BrowserWindow) {
this.mainWindow = mainWindow;
2022-12-19 15:59:14 -08:00
}
2023-07-01 19:10:05 -07:00
buildMenu(): Menu {
if (process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true') {
this.setupDevelopmentEnvironment();
}
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
const template =
process.platform === 'darwin'
? this.buildDarwinTemplate()
: this.buildDefaultTemplate();
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
return menu;
}
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
setupDevelopmentEnvironment(): void {
this.mainWindow.webContents.on('context-menu', (_, props) => {
const { x, y } = props;
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
Menu.buildFromTemplate([
{
click: () => {
this.mainWindow.webContents.inspectElement(x, y);
},
label: 'Inspect element',
},
]).popup({ window: this.mainWindow });
});
}
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
buildDarwinTemplate(): MenuItemConstructorOptions[] {
const subMenuAbout: DarwinMenuItemConstructorOptions = {
label: 'Electron',
submenu: [
2022-12-19 15:59:14 -08:00
{
2023-07-01 19:10:05 -07:00
label: 'About ElectronReact',
selector: 'orderFrontStandardAboutPanel:',
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
{ type: 'separator' },
{ label: 'Services', submenu: [] },
{ type: 'separator' },
2022-12-19 15:59:14 -08:00
{
2023-07-01 19:10:05 -07:00
accelerator: 'Command+H',
label: 'Hide ElectronReact',
selector: 'hide:',
2022-12-19 15:59:14 -08:00
},
{
2023-07-01 19:10:05 -07:00
accelerator: 'Command+Shift+H',
label: 'Hide Others',
selector: 'hideOtherApplications:',
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
{ label: 'Show All', selector: 'unhideAllApplications:' },
{ type: 'separator' },
2022-12-19 15:59:14 -08:00
{
2023-07-01 19:10:05 -07:00
accelerator: 'Command+Q',
click: () => {
app.quit();
},
label: 'Quit',
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
],
};
const subMenuEdit: DarwinMenuItemConstructorOptions = {
label: 'Edit',
submenu: [
{ accelerator: 'Command+Z', label: 'Undo', selector: 'undo:' },
{ accelerator: 'Shift+Command+Z', label: 'Redo', selector: 'redo:' },
{ type: 'separator' },
{ accelerator: 'Command+X', label: 'Cut', selector: 'cut:' },
{ accelerator: 'Command+C', label: 'Copy', selector: 'copy:' },
{ accelerator: 'Command+V', label: 'Paste', selector: 'paste:' },
{
accelerator: 'Command+A',
label: 'Select All',
selector: 'selectAll:',
},
],
};
const subMenuViewDev: MenuItemConstructorOptions = {
label: 'View',
submenu: [
{
accelerator: 'Command+R',
click: () => {
this.mainWindow.webContents.reload();
},
label: 'Reload',
},
{
accelerator: 'Ctrl+Command+F',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
},
label: 'Toggle Full Screen',
},
{
accelerator: 'Alt+Command+I',
click: () => {
this.mainWindow.webContents.toggleDevTools();
},
label: 'Toggle Developer Tools',
},
],
};
const subMenuViewProd: MenuItemConstructorOptions = {
label: 'View',
submenu: [
{
accelerator: 'Ctrl+Command+F',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
},
label: 'Toggle Full Screen',
},
],
};
const subMenuWindow: DarwinMenuItemConstructorOptions = {
label: 'Window',
submenu: [
{
accelerator: 'Command+M',
label: 'Minimize',
selector: 'performMiniaturize:',
},
{ accelerator: 'Command+W', label: 'Close', selector: 'performClose:' },
{ type: 'separator' },
{ label: 'Bring All to Front', selector: 'arrangeInFront:' },
],
};
const subMenuHelp: MenuItemConstructorOptions = {
label: 'Help',
submenu: [
{
click() {
shell.openExternal('https://electronjs.org');
},
label: 'Learn More',
},
{
click() {
shell.openExternal(
'https://github.com/electron/electron/tree/main/docs#readme',
);
},
label: 'Documentation',
},
{
click() {
shell.openExternal('https://www.electronjs.org/community');
},
label: 'Community Discussions',
},
{
click() {
shell.openExternal('https://github.com/electron/electron/issues');
},
label: 'Search Issues',
},
],
};
const subMenuView =
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true'
? subMenuViewDev
: subMenuViewProd;
return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp];
}
buildDefaultTemplate() {
const templateDefault = [
{
label: '&File',
submenu: [
{
accelerator: 'Ctrl+O',
label: '&Open',
},
{
accelerator: 'Ctrl+W',
click: () => {
this.mainWindow.close();
},
label: '&Close',
},
],
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
{
label: '&View',
submenu:
process.env.NODE_ENV === 'development' || process.env.DEBUG_PROD === 'true'
? [
{
accelerator: 'Ctrl+R',
click: () => {
this.mainWindow.webContents.reload();
},
label: '&Reload',
},
{
accelerator: 'F11',
click: () => {
this.mainWindow.setFullScreen(
!this.mainWindow.isFullScreen(),
);
},
label: 'Toggle &Full Screen',
},
{
accelerator: 'Alt+Ctrl+I',
click: () => {
this.mainWindow.webContents.toggleDevTools();
},
label: 'Toggle &Developer Tools',
},
]
: [
{
accelerator: 'F11',
click: () => {
this.mainWindow.setFullScreen(
!this.mainWindow.isFullScreen(),
);
},
label: 'Toggle &Full Screen',
},
],
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
{
label: 'Help',
submenu: [
{
click() {
shell.openExternal('https://electronjs.org');
},
label: 'Learn More',
},
{
click() {
shell.openExternal(
'https://github.com/electron/electron/tree/main/docs#readme',
);
},
label: 'Documentation',
},
{
click() {
shell.openExternal('https://www.electronjs.org/community');
},
label: 'Community Discussions',
},
{
click() {
shell.openExternal('https://github.com/electron/electron/issues');
},
label: 'Search Issues',
},
],
2022-12-19 15:59:14 -08:00
},
2023-07-01 19:10:05 -07:00
];
2022-12-19 15:59:14 -08:00
2023-07-01 19:10:05 -07:00
return templateDefault;
}
2022-12-19 15:59:14 -08:00
}