Skip to content

Commit fe02e16

Browse files
committed
Merge pull request nwjs#1635 from FWeinb/fix-tray-click-mac
[os x]Add support for tray click event on os x
2 parents d5a85ee + 5ed095d commit fe02e16

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/api/tray/tray.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
#if defined(OS_MACOSX)
3030
#if __OBJC__
3131
@class NSStatusItem;
32+
@class MacTrayObserver;
3233
#else
3334
class NSStatusItem;
35+
class MacTrayObserver;
3436
#endif // __OBJC__
3537
#elif defined(TOOLKIT_GTK)
3638
#include <gtk/gtk.h>
@@ -70,6 +72,7 @@ class Tray : public Base {
7072

7173
#if defined(OS_MACOSX)
7274
__block NSStatusItem* status_item_;
75+
MacTrayObserver* status_observer_;
7376
#elif defined(TOOLKIT_GTK)
7477
GtkStatusIcon* status_item_;
7578

src/api/tray/tray.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Tray(option) {
3636
option.shadowIcon = String(option.icon);
3737
option.icon = nw.getAbsolutePath(option.icon);
3838
}
39-
39+
4040
if (option.hasOwnProperty('alticon')) {
4141
option.shadowAlticon = String(option.alticon);
4242
option.alticon = nw.getAbsolutePath(option.alticon);
@@ -45,6 +45,14 @@ function Tray(option) {
4545
if (option.hasOwnProperty('tooltip'))
4646
option.tooltip = String(option.tooltip);
4747

48+
if (option.hasOwnProperty('click')) {
49+
if (typeof option.click != 'function') {
50+
throw new String("'click' must be a valid Function");
51+
} else {
52+
this.click = option.click;
53+
}
54+
}
55+
4856
if (option.hasOwnProperty('menu')) {
4957
if (v8_util.getConstructorName(option.menu) != 'Menu')
5058
throw new String("'menu' must be a valid Menu");
@@ -119,4 +127,14 @@ Tray.prototype.remove = function() {
119127
nw.callObjectMethod(this, 'Remove', []);
120128
}
121129

130+
Tray.prototype.handleEvent = function(ev) {
131+
if (ev == 'click') {
132+
// Emit click handler
133+
if (typeof this.click == 'function'){
134+
this.click();
135+
}
136+
}
137+
// Emit generate event handler
138+
exports.Base.prototype.handleEvent.apply(this, arguments);
139+
}
122140
exports.Tray = Tray;

src/api/tray/tray_mac.mm

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,39 @@
2222

2323
#include "base/values.h"
2424
#import <Cocoa/Cocoa.h>
25+
#include "content/nw/src/api/dispatcher_host.h"
2526
#include "content/nw/src/api/menu/menu.h"
2627

27-
namespace nwapi {
2828

29+
@interface MacTrayObserver : NSObject {
30+
@private
31+
nwapi::Tray* tray_;
32+
}
33+
- (void)setBacking:(nwapi::Tray*)tray_;
34+
- (void)onClick:(id)sender;
35+
@end
36+
37+
@implementation MacTrayObserver
38+
- (void)setBacking:(nwapi::Tray*)newTray {
39+
tray_ = newTray;
40+
}
41+
- (void)onClick:(id)sender {
42+
base::ListValue args;
43+
tray_->dispatcher_host()->SendEvent(tray_,"click",args);
44+
}
45+
@end
46+
47+
namespace nwapi {
48+
2949
void Tray::Create(const base::DictionaryValue& option) {
3050
NSStatusBar *status_bar = [NSStatusBar systemStatusBar];
51+
MacTrayObserver* observer = [[MacTrayObserver alloc] init];
52+
[observer setBacking:this];
3153
status_item_ = [status_bar statusItemWithLength:NSVariableStatusItemLength];
3254
[status_item_ setHighlightMode:YES];
3355
[status_item_ retain];
56+
[status_item_ setTarget:observer];
57+
[status_item_ setAction:@selector(onClick:)];
3458
}
3559

3660
void Tray::ShowAfterCreate() {
@@ -71,6 +95,8 @@
7195
}
7296

7397
void Tray::SetMenu(Menu* menu) {
98+
[status_item_ setTarget:nil];
99+
[status_item_ setAction:nil];
74100
[status_item_ setMenu:menu->menu_];
75101
}
76102

0 commit comments

Comments
 (0)