electron menu click params menu主菜单点击事件回调参数
#在官方的menu click事件教程中,我没有找到回调参数的用法,遂作此文以便查阅(未经本人许可请勿转载)。
#click: function (event, focusedWindow, focusedWebContents)
event:事件对象
focusedWindow:当前打开的窗口对象
focusedWebContents:web内容对象
参数值样本
// event
MenuItem {
label: '鏂扮獥鍙?,
accelerator: 'CommandOrControl+n',
click: [Function (anonymous)],
submenu: null,
type: 'normal',
role: null,
icon: null,
sublabel: '',
toolTip: '',
enabled: true,
visible: true,
checked: false,
acceleratorWorksWhenHidden: true,
registerAccelerator: true,
commandId: 32,
userAccelerator: [Getter],
menu: EventEmitter {
insertItem: [Function: insertItem],
insertCheckItem: [Function: insertCheckItem],
insertRadioItem: [Function: insertRadioItem],
insertSeparator: [Function: insertSeparator],
insertSubMenu: [Function: insertSubMenu],
setIcon: [Function: setIcon],
setSublabel: [Function: setSublabel],
setToolTip: [Function: setToolTip],
setRole: [Function: setRole],
clear: [Function: clear],
getIndexOfCommandId: [Function: getIndexOfCommandId],
getItemCount: [Function: getItemCount],
getCommandIdAt: [Function: getCommandIdAt],
getLabelAt: [Function: getLabelAt],
getSublabelAt: [Function: getSublabelAt],
getToolTipAt: [Function: getToolTipAt],
isItemCheckedAt: [Function: isItemCheckedAt],
isEnabledAt: [Function: isEnabledAt],
worksWhenHiddenAt: [Function: worksWhenHiddenAt],
isVisibleAt: [Function: isVisibleAt],
popupAt: [Function: popupAt],
closePopupAt: [Function: closePopupAt],
_getAcceleratorTextAt: [Function: _getAcceleratorTextAt],
commandsMap: { '32': [Circular *1], '33': [MenuItem], '34': [MenuItem] },
groupsMap: {},
items: [ [Circular *1], [MenuItem], [MenuItem] ]
}
}
// focusedWindow
BrowserWindow {
setBounds: [Function (anonymous)],
_events: [Object: null prototype] {
blur: [Function (anonymous)],
focus: [Function (anonymous)],
show: [Function: visibilityChanged],
hide: [Function: visibilityChanged],
minimize: [Function: visibilityChanged],
maximize: [Function: visibilityChanged],
restore: [Function: visibilityChanged]
},
_eventsCount: 7,
devToolsWebContents: [Getter]
}
//focusedWebContents
{
shiftKey: false,
ctrlKey: true,
altKey: false,
metaKey: false,
triggeredByAccelerator: true
}
样例
/*
* @Description:
* @Version: 1.0
* @Autor: Tj
*/
const bytenode = require("bytenode");
const { app, Menu, BrowserView, screen } = require("electron");
const {
createWindow,
historyUrl,
homePageUrl,
} = require("../dist-js/main.jsc");
let width = screen.getPrimaryDisplay().workAreaSize.width;
let height = screen.getPrimaryDisplay().workAreaSize.height;
//顶部菜单
var template = [
{
label: "文件",
submenu: [
// {
// label: "新建文件",
// accelerator: "ctrl+n",
// click: function () {
// console.log("ctrl+n");
// view.webContents.loadURL(homePageUrl);
// },
// },
{
label: "新窗口",
accelerator: "CommandOrControl+n",
click: function (event, focusedWindow, focusedWebContents) {
console.log("new window");
console.log(event, focusedWindow, focusedWebContents);
createWindow();
},
},
{
type: "separator",
},
{
label: "关闭",
role: "close",
},
],
},
{
type: "separator",
},
{
label: "首页",
click: function (event, focusedWindow, focusedWebContents) {
console.log("Go home");
console.log(historyUrl);
openUrl(focusedWindow, homePageUrl);
},
},
{
label: "后退",
click: function (event, focusedWindow, focusedWebContents) {
historyCount = historyUrl.length;
console.log("Back Page: " + historyCount);
if (historyCount > 0 && historyCount - 2 >= 0) {
openUrl(focusedWindow, historyUrl[historyCount - 2]);
historyUrl.pop();
historyUrl.pop();
}
},
},
{
label: "刷新",
role: "reload",
},
{
label: "强制刷新",
role: "forceReload",
},
{
label: "编辑",
submenu: [
{
label: "全选",
role: "selectAll",
},
{
type: "separator",
},
{
label: "复制",
role: "copy",
},
{
label: "剪切",
role: "cut",
},
{
type: "separator",
},
{
label: "粘贴",
role: "paste",
},
],
},
{
label: "调试",
submenu: [
{
label: "打开",
accelerator: "F12",
click: function (event, focusedWindow, focusedWebContents) {
focusedWindow.openDevTools();
},
},
{
label: "关闭",
accelerator: "CommandOrControl+F12",
click: function (event, focusedWindow, focusedWebContents) {
focusedWindow.closeDevTools();
},
},
],
},
];
var m = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(m);
function openUrl(win, url) {
win.loadURL(url);
}
function openViewUrl(win, url) {
const view = new BrowserView({});
win.setBrowserView(view);
console.log(win);
view.setBounds({ x: 0, y: 0, width: width, height: height });
view.setAutoResize({
width: true,
height: true,
horizontal: true,
vertical: true,
});
view.setBackgroundColor("#FF6666");
view.webContents.loadURL(url);
}