在电子,是否有一种方法来启用/禁用上下文菜单中的特定MenuItem,这取决于用户右键单击的元素?我还需要关于哪个确切的元素被单击的信息,并将该信息传递给上下文菜单函数。
例如,假设我在渲染器进程中有以下html:
<p id="p1">First paragraph</p>
<p id="p2">Second paragraph</p>
<p id="p3">Third paragraph</p>
该窗口的上下文菜单如下所示:
var menu = new Menu();
menu.append(new MenuItem({label: "This menu item is always shown",}));
menu.append(new MenuItem({ // shown only when clicked on p1 or p3
label: "This menu is not always shown",
click: function(id){
// I want variable id to be an id of paragraph that I have clicked on
}
}));
因此,当我右击第一段或第三段时,会弹出一个包含2个项目的上下文菜单。但当我右击第二段时,会弹出一个包含1个项目的上下文菜单。另外,我想将段落id作为参数传递给上下文菜单函数,这样我就可以从那里知道我点击了哪个段落。
2条答案
按热度按时间kxxlusnw1#
我将在 contextmenu 事件处理程序中动态(重新)创建上下文菜单:
在主进程中:
在渲染器进程中:
lymnna712#
嘿,所以超级晚到这个,但任何一个试图找到一些工作,为这个。我设法把一些东西放在一起,我相信工作得很好。
对于我的应用程序,某些窗口需要某些上下文菜单,而某些窗口不应该有上下文菜单。
Context menu Demo