Skip to content

Commit

Permalink
Extend Tray click event with position
Browse files Browse the repository at this point in the history
- Add a parameter to the `click` event of the  `Tray` API object containing the
coordinates of the tray icon/mouse pointer.
The parameter has `x` and `y` fields for the coordinates.
On Mac OS X the coordinates always refer to the position (lower left corner) of
the tray item (NSStatusItem).
On Aura (Linux, Windows) the coordinates refer to the cursor position (which
would be inside the bounding box of the status icon but with slight differences
depending on where effectively the user clicked).

Usage example:

    tray.on('click', function(pos) {
      // pos.x is x coordinate of the tray icon
      // pos.y is y coordinate of the tray icon
      showCustomTrayMenuAt(pos);
    }

FIX nwjs#1874
  • Loading branch information
mrfabbri committed Dec 14, 2014
1 parent 8e5b5fa commit 4effdaf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api/tray/tray_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "content/nw/src/api/menu/menu.h"
#include "content/nw/src/nw_package.h"
#include "content/nw/src/nw_shell.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/image/image.h"

namespace nwapi {
Expand All @@ -47,6 +48,12 @@ class TrayObserver : public StatusIconObserver {

virtual void OnStatusIconClicked() OVERRIDE {
base::ListValue args;
base::DictionaryValue* data = new base::DictionaryValue;
gfx::Point cursor_pos(
gfx::Screen::GetNativeScreen()->GetCursorScreenPoint());
data->SetInteger("x", cursor_pos.x());
data->SetInteger("y", cursor_pos.y());
args.Append(data);
tray_->dispatcher_host()->SendEvent(tray_, "click", args);
}

Expand Down
10 changes: 10 additions & 0 deletions src/api/tray/tray_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "base/values.h"
#import <Cocoa/Cocoa.h>
#include "ui/gfx/screen.h"
#include "content/nw/src/api/dispatcher_host.h"
#include "content/nw/src/api/menu/menu.h"

Expand All @@ -40,6 +41,15 @@ - (void)setBacking:(nwapi::Tray*)newTray {
}
- (void)onClick:(id)sender {
base::ListValue args;
base::DictionaryValue* data = new base::DictionaryValue;
// Get the position of the frame of the NSStatusItem
NSPoint pos = ([[[NSApp currentEvent] window] frame]).origin;
// Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
pos.y = NSMaxY([screen frame]) - pos.y;
data->SetInteger("x", pos.x);
data->SetInteger("y", pos.y);
args.Append(data);
tray_->dispatcher_host()->SendEvent(tray_,"click",args);
}
@end
Expand Down

0 comments on commit 4effdaf

Please sign in to comment.