Context menu click/open event with Atom Shell/Electron?

江枫思渺然 提交于 2019-12-07 04:07:09

问题


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?

Relavent code:

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);

});

回答1:


Now it works on OS X (10.11.4).

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');
  });
});

Run:

electron main.js


来源:https://stackoverflow.com/questions/30758035/context-menu-click-open-event-with-atom-shell-electron

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!