Skip to content

Commit 4effdaf

Browse files
committed
Extend Tray click event with position
- 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
1 parent 8e5b5fa commit 4effdaf

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/api/tray/tray_aura.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "content/nw/src/api/menu/menu.h"
3131
#include "content/nw/src/nw_package.h"
3232
#include "content/nw/src/nw_shell.h"
33+
#include "ui/gfx/screen.h"
3334
#include "ui/gfx/image/image.h"
3435

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

4849
virtual void OnStatusIconClicked() OVERRIDE {
4950
base::ListValue args;
51+
base::DictionaryValue* data = new base::DictionaryValue;
52+
gfx::Point cursor_pos(
53+
gfx::Screen::GetNativeScreen()->GetCursorScreenPoint());
54+
data->SetInteger("x", cursor_pos.x());
55+
data->SetInteger("y", cursor_pos.y());
56+
args.Append(data);
5057
tray_->dispatcher_host()->SendEvent(tray_, "click", args);
5158
}
5259

src/api/tray/tray_mac.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

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

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

0 commit comments

Comments
 (0)