使用 Atom Shell/Electron 的上下文菜单单击/打开事件? [英] Context menu click/open event with Atom Shell/Electron?

查看:11
本文介绍了使用 Atom Shell/Electron 的上下文菜单单击/打开事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 OSX 上的上下文菜单捕获托盘图标单击,但根据文档,由于某种原因,这在 OSX 中被禁用:

I'm trying to capture the click even on a tray icon click with a context menu on OSX, but according to the docs this is disabled in OSX for some reason:

Platform limitations:

On OS X clicked event will be ignored if the tray icon has context menu.

我想知道是否有其他方法可以知道何时与带有上下文菜单的托盘图标交互?

I've wondering if there is another way to know when a tray icon with a context menu is interacted with?

相关代码:

var app = require('app');
var path = require('path')
var Menu = require('menu');
var MenuItem = require('menu-item');
var Tray = require('tray');

var appIcon = null;
var menu = null;
app.on('ready', function(){
  appIcon = new Tray(path.join(__dirname, 'images/icon.png'));

  appIcon.on('clicked', function(event, bounds) {
      console.log('clicked');
  });

  menu = new Menu();

  menu.append(new MenuItem({ label: 'Quit', id: 'quit', click: function() { app.quit(); } }));
  appIcon.setContextMenu(menu);

});

推荐答案

现在它可以在 OS X (10.11.4) 上运行.

Now it works on OS X (10.11.4).

请检查.main.js:

Please check. main.js:

// Load in dependencies
var app = require('app');
var Tray = require('tray');

// When the Electron has loaded
app.on('ready', function onready () {
  // Log to the console to verify logging works
  console.log('Hello World!');

  // Create a tray
  var tray = new Tray(__dirname + '/icon.png');

  // When the tray icon is clicked, log to our console
  tray.on('click', function handleClicked () {
    console.log('Tray clicked');
  });
});

运行:

electron main.js

这篇关于使用 Atom Shell/Electron 的上下文菜单单击/打开事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆