我正在尝试使用Electron / React从菜单为桌面应用程序显示自定义对话框。
我有一个menu.js文件,其中包含我所有的菜单选项。在其中,我传入一个包含标签和click事件的菜单对象。 click事件与包含我想要对话框的文件相关联。由于某种原因,我无法使两者连接。
如果您在此处查看简单注释Application-simpleNote Github的资源,它们将使其起作用。但是,当我尝试从头开始构建它并遵循他们的步骤时,我无法使其正常运行。
我专门指的是“应用程序”菜单选项卡,并且在该菜单文件下。关于部分。
menu.js
// @flow
import { app, Menu, shell, BrowserWindow } from 'electron';
const { appCommandSender } = require('./menu/utils');
const DialogTypes = require('./menu/dialog-types');
const menuItems = require('./menu/menu-items');
export default class MenuBuilder {
mainWindow: BrowserWindow;
constructor(mainWindow: BrowserWindow) {
this.mainWindow = mainWindow;
}
buildMenu() {
if (
process.env.NODE_ENV === 'development' ||
process.env.DEBUG_PROD === 'true'
) {
this.setupDevelopmentEnvironment();
}
const template =
process.platform === 'darwin'
? this.buildDarwinTemplate()
: this.buildDefaultTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
return menu;
}
setupDevelopmentEnvironment() {
this.mainWindow.openDevTools();
this.mainWindow.webContents.on('context-menu', (e, props) => {
const { x, y } = props;
Menu.buildFromTemplate([
{
label: 'Inspect element',
click: () => {
this.mainWindow.inspectElement(x, y);
}
}
]).popup(this.mainWindow);
});
}
buildDarwinTemplate() {
const subMenuAbout = {
label: 'Application',
submenu: [
menuItems.about,
{ type: 'separator' },
menuItems.preferences,
{ type: 'separator' },
{ label: 'Services', submenu: [] },
{ type: 'separator' },
{
label: 'Hide',
accelerator: 'Command+H',
selector: 'hide:'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
selector: 'hideOtherApplications:'
},
{ label: 'Show All', selector: 'unhideAllApplications:' },
{ type: 'separator' },
{
label: 'Quit',
accelerator: 'Command+Q',
click: () => {
app.quit();
}
}
]
};
const subMenuEdit = {
label: 'Edit',
submenu: [
{ label: 'Undo', accelerator: 'Command+Z', selector: 'undo:' },
{ label: 'Redo', accelerator: 'Shift+Command+Z', selector: 'redo:' },
{ type: 'separator' },
{ label: 'Cut', accelerator: 'Command+X', selector: 'cut:' },
{ label: 'Copy', accelerator: 'Command+C', selector: 'copy:' },
{ label: 'Paste', accelerator: 'Command+V', selector: 'paste:' },
{
label: 'Select All',
accelerator: 'Command+A',
selector: 'selectAll:'
}
]
};
const subMenuViewDev = {
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'Command+R',
click: () => {
this.mainWindow.webContents.reload();
}
},
{
label: 'Toggle Full Screen',
accelerator: 'Ctrl+Command+F',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
}
},
{
label: 'Toggle Developer Tools',
accelerator: 'Alt+Command+I',
click: () => {
this.mainWindow.toggleDevTools();
}
}
]
};
const subMenuViewProd = {
label: 'View',
submenu: [
{
label: 'Toggle Full Screen',
accelerator: 'Ctrl+Command+F',
click: () => {
this.mainWindow.setFullScreen(!this.mainWindow.isFullScreen());
}
}
]
};
const subMenuWindow = {
label: 'Window',
submenu: [
{
label: 'Minimize',
accelerator: 'Command+M',
selector: 'performMiniaturize:'
},
{ label: 'Close', accelerator: 'Command+W', selector: 'performClose:' },
{ type: 'separator' },
{ label: 'Bring All to Front', selector: 'arrangeInFront:' }
]
};
const subMenuHelp = {
label: 'Help',
submenu: [
{
label: 'Learn More',
click() {
shell.openExternal('http://electron.atom.io');
}
},
{
label: 'Documentation',
click() {
shell.openExternal(
'https://github.com/atom/electron/tree/master/docs#readme'
);
}
},
{
label: 'Community Discussions',
click() {
shell.openExternal('https://discuss.atom.io/c/electron');
}
},
{
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/atom/electron/issues');
}
}
]
};
const subMenuView =
process.env.NODE_ENV === 'development' ? subMenuViewDev : subMenuViewProd;
return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow, subMenuHelp];
}
buildDefaultTemplate() {
const templateDefault = [
{
label: '&File',
submenu: [
{
label: '&Open',
accelerator: 'Ctrl+O'
},
{
label: '&Close',
accelerator: 'Ctrl+W',
click: () => {
this.mainWindow.close();
}
}
]
},
{
label: '&View',
submenu:
process.env.NODE_ENV === 'development'
? [
{
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: 'Alt+Ctrl+I',
click: () => {
this.mainWindow.toggleDevTools();
}
}
]
: [
{
label: 'Toggle &Full Screen',
accelerator: 'F11',
click: () => {
this.mainWindow.setFullScreen(
!this.mainWindow.isFullScreen()
);
}
}
]
},
{
label: 'Help',
submenu: [
{
label: 'Learn More',
click() {
shell.openExternal('http://electron.atom.io');
}
},
{
label: 'Documentation',
click() {
shell.openExternal(
'https://github.com/atom/electron/tree/master/docs#readme'
);
}
},
{
label: 'Community Discussions',
click() {
shell.openExternal('https://discuss.atom.io/c/electron');
}
},
{
label: 'Search Issues',
click() {
shell.openExternal('https://github.com/atom/electron/issues');
}
}
]
}
];
return templateDefault;
}
}
menu-items.js
const { app } = require('electron');
const { appCommandSender } = require('./utils');
const DialogTypes = require('./dialog-types');
const about = {
label: '&About NetNode Wallet',
click: appCommandSender({
action: 'showDialog',
dialog: DialogTypes.ABOUT,
}),
};
const preferences = {
label: 'P&references',
accelerator: 'CommandOrControl+,',
click: appCommandSender({
action: 'showDialog',
dialog: DialogTypes.SETTINGS,
}),
};
module.exports = {
about,
preferences,
};
dialog-types.js
// "type" must match the name of the React element
// "title" is required for accessibility (Will be read to screen readers)
const DialogTypes = {
ABOUT: {
type: 'About',
title: 'About',
},
IMPORT: {
type: 'Import',
title: 'Import Notes',
},
SETTINGS: {
type: 'Settings',
title: 'Settings',
},
SHARE: {
type: 'Share',
title: 'Share',
},
};
// Needs to be Node export so Electron menus can `require` it
module.exports = DialogTypes;
utils.js
const buildRadioGroup = ({ action, propName, settings }) => {
return item => {
const { id, ...props } = item;
return {
type: 'radio',
checked: id === settings[propName],
click: appCommandSender({
action,
[propName]: id,
}),
...props,
};
};
};
const appCommandSender = arg => {
return (item, focusedWindow) => {
if (focusedWindow) {
focusedWindow.webContents.send('appCommand', arg);
}
};
};
module.exports = {
buildRadioGroup,
appCommandSender,
};