Skip to content

Commit

Permalink
Merge pull request nwjs#1635 from FWeinb/fix-tray-click-mac
Browse files Browse the repository at this point in the history
[os x]Add support for tray click event on os x
  • Loading branch information
rogerwang committed Mar 3, 2014
2 parents d5a85ee + 5ed095d commit fe02e16
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/api/tray/tray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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_;

Expand Down
20 changes: 19 additions & 1 deletion src/api/tray/tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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;
28 changes: 27 additions & 1 deletion src/api/tray/tray_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -71,6 +95,8 @@
}

void Tray::SetMenu(Menu* menu) {
[status_item_ setTarget:nil];
[status_item_ setAction:nil];
[status_item_ setMenu:menu->menu_];
}

Expand Down

0 comments on commit fe02e16

Please sign in to comment.