Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

[os x]Add support for tray click event on os x #1635

Merged
merged 1 commit into from
@FWeinb

As @trevorlinton doesn't created a pull request for this for the last 4 month I am doing it now. This will fix Issue #510.

Simple example:

<html>
<body>
  <div>Click on the tray icon</div>
  <script>
    // Load library
    var gui = require('nw.gui');

    // Reference to tray
    var tray = new gui.Tray({ icon: 'icon.png' });

    // Alert on click
    tray.on('click', function() {
      alert("tray clicked");
    });
  </script>
</body>
</html>
@miklschmidt

Awesome! Looking forward to using this!

@rogerwang rogerwang merged commit fe02e16 into nwjs:master
@rogerwang
Owner

@FWeinb please add another pull request to add yourself to AUTHORS Thanks.

@FWeinb FWeinb referenced this pull request
Merged

Added myself to AUTHORS #1653

@mauvm

Will this be merged anytime soon?

@Mithgol

The code is already merged to the master branch as seen in the GitHub message above.

However, a new node-webkit's version containing the new code has not yet been released (the last release is dated Feb 2014, while the merge happened in March).

@joernroeder

Hey,
i just stumbled into the same issue on the current 0.8.6 release. Is this fix already released?

@cz-Michael

Could you please fix this in 0.8.4 and release it?

@ninjamonkno1

This is fixed, check the rc1 release.

@cz-Michael

@ninjamonkno1 Do you mean 0.8.4rc1? I need to use 0.8.4 which support child_process of Node.

@ninjamonkno1

@cz-Michael aha no, I only noticed it was in the latest release and can confirm it works in there. I would look at the release notes to see when the fix was added between 0.8.4 and 1rc.

@cz-Michael

@ninjamonkno1 cool, thank you. Cause higher versions of node-webkit do not support child_process of node right now. Only 0.8.4 works good for me.

@sashahilton00

Hi, I'm using the latest NW binary, and am once again seeing this problem:

var tray = new gui.Tray({ title: '', icon: 'images/Icon-Small.png' });

var menu = new gui.Menu();
//menu.append(new gui.MenuItem({ type: 'checkbox', label: 'box1' }));
tray.menu = menu;
tray.on('click',function() {
win.maximize();
console.log('maximized');
});

The code above is not working, any ideas why/mistakes I have made? I have defined the gui btw ;)

@mrfabbri
Collaborator
@sashahilton00

Nothing at all works. Not the maximize, or anything else I put in the function. The callback just isn't being executed. Nothing is logged to the console. I'm running OSX 10.9 btw.

I have just created a new project, and used the sample code to check if it is my fault, but still the same result, with the following:

var gui = require('nw.gui');
var tray = new gui.Tray({ title: '', icon: 'images/Icon-Small.png' });

tray.on('click', function() {
alert('Success');
});

@mrfabbri
Collaborator

@sashahilton00 I couldn't reproduce it (example app: http://cl.ly/3u402n0y3j0N ). Tried on Mac OS X 10.10.2 (either with v0.11.6 or with v0.12.0-alpha2, 32-bit and 64-bit) and on Mac OS X 10.9.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2014
  1. @FWeinb
This page is out of date. Refresh to see the latest.
View
3  src/api/tray/tray.h
@@ -29,8 +29,10 @@
#if defined(OS_MACOSX)
#if __OBJC__
@class NSStatusItem;
+@class MacTrayObserver;
#else
class NSStatusItem;
+class MacTrayObserver;
#endif // __OBJC__
#elif defined(TOOLKIT_GTK)
#include <gtk/gtk.h>
@@ -70,6 +72,7 @@ class Tray : public Base {
#if defined(OS_MACOSX)
__block NSStatusItem* status_item_;
+ MacTrayObserver* status_observer_;
#elif defined(TOOLKIT_GTK)
GtkStatusIcon* status_item_;
View
20 src/api/tray/tray.js
@@ -36,7 +36,7 @@ function Tray(option) {
option.shadowIcon = String(option.icon);
option.icon = nw.getAbsolutePath(option.icon);
}
-
+
if (option.hasOwnProperty('alticon')) {
option.shadowAlticon = String(option.alticon);
option.alticon = nw.getAbsolutePath(option.alticon);
@@ -45,6 +45,14 @@ function Tray(option) {
if (option.hasOwnProperty('tooltip'))
option.tooltip = String(option.tooltip);
+ if (option.hasOwnProperty('click')) {
+ if (typeof option.click != 'function') {
+ throw new String("'click' must be a valid Function");
+ } else {
+ this.click = option.click;
+ }
+ }
+
if (option.hasOwnProperty('menu')) {
if (v8_util.getConstructorName(option.menu) != 'Menu')
throw new String("'menu' must be a valid Menu");
@@ -119,4 +127,14 @@ Tray.prototype.remove = function() {
nw.callObjectMethod(this, 'Remove', []);
}
+Tray.prototype.handleEvent = function(ev) {
+ if (ev == 'click') {
+ // Emit click handler
+ if (typeof this.click == 'function'){
+ this.click();
+ }
+ }
+ // Emit generate event handler
+ exports.Base.prototype.handleEvent.apply(this, arguments);
+}
exports.Tray = Tray;
View
28 src/api/tray/tray_mac.mm
@@ -22,15 +22,39 @@
#include "base/values.h"
#import <Cocoa/Cocoa.h>
+#include "content/nw/src/api/dispatcher_host.h"
#include "content/nw/src/api/menu/menu.h"
-namespace nwapi {
+@interface MacTrayObserver : NSObject {
+@private
+ nwapi::Tray* tray_;
+}
+- (void)setBacking:(nwapi::Tray*)tray_;
+- (void)onClick:(id)sender;
+@end
+
+@implementation MacTrayObserver
+- (void)setBacking:(nwapi::Tray*)newTray {
+ tray_ = newTray;
+}
+- (void)onClick:(id)sender {
+ base::ListValue args;
+ tray_->dispatcher_host()->SendEvent(tray_,"click",args);
+}
+@end
+
+namespace nwapi {
+
void Tray::Create(const base::DictionaryValue& option) {
NSStatusBar *status_bar = [NSStatusBar systemStatusBar];
+ MacTrayObserver* observer = [[MacTrayObserver alloc] init];
+ [observer setBacking:this];
status_item_ = [status_bar statusItemWithLength:NSVariableStatusItemLength];
[status_item_ setHighlightMode:YES];
[status_item_ retain];
+ [status_item_ setTarget:observer];
+ [status_item_ setAction:@selector(onClick:)];
}
void Tray::ShowAfterCreate() {
@@ -71,6 +95,8 @@
}
void Tray::SetMenu(Menu* menu) {
+ [status_item_ setTarget:nil];
+ [status_item_ setAction:nil];
[status_item_ setMenu:menu->menu_];
}
Something went wrong with that request. Please try again.