From 6d7d068e493f155ea735f2e9f0451a32640b24e1 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Fri, 1 May 2015 15:24:22 +0530 Subject: [PATCH] tray: send tray icon position as payload onclick OSX --- atom/browser/api/atom_api_tray.cc | 5 +++-- atom/browser/api/atom_api_tray.h | 2 +- atom/browser/ui/tray_icon.cc | 4 ++-- atom/browser/ui/tray_icon.h | 2 +- atom/browser/ui/tray_icon_cocoa.mm | 9 ++++++++- atom/browser/ui/tray_icon_observer.h | 6 +++++- atom/browser/ui/win/notify_icon.cc | 2 +- docs/api/tray.md | 5 +++++ 8 files changed, 26 insertions(+), 9 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 94af89b81ecd..c7e0149b0114 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -9,6 +9,7 @@ #include "atom/browser/api/atom_api_menu.h" #include "atom/browser/browser.h" #include "atom/browser/ui/tray_icon.h" +#include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "native_mate/constructor.h" @@ -39,8 +40,8 @@ mate::Wrappable* Tray::New(const gfx::Image& image) { return new Tray(image); } -void Tray::OnClicked() { - Emit("clicked"); +void Tray::OnClicked(const gfx::Point& pos) { + Emit("clicked", pos); } void Tray::OnDoubleClicked() { diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 48828d2c9366..38fb044d9dab 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -41,7 +41,7 @@ class Tray : public mate::EventEmitter, virtual ~Tray(); // TrayIconObserver: - void OnClicked() override; + void OnClicked(const gfx::Point&) override; void OnDoubleClicked() override; void OnBalloonShow() override; void OnBalloonClicked() override; diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index 68770c1e5cfd..06c1f4be5967 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -26,8 +26,8 @@ void TrayIcon::DisplayBalloon(const gfx::Image& icon, const base::string16& contents) { } -void TrayIcon::NotifyClicked() { - FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked()); +void TrayIcon::NotifyClicked(const gfx::Point& pos) { + FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked(pos)); } void TrayIcon::NotifyDoubleClicked() { diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 6293b39002b6..26f31a12e072 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -50,7 +50,7 @@ class TrayIcon { void AddObserver(TrayIconObserver* obs) { observers_.AddObserver(obs); } void RemoveObserver(TrayIconObserver* obs) { observers_.RemoveObserver(obs); } - void NotifyClicked(); + void NotifyClicked(const gfx::Point&); void NotifyDoubleClicked(); void NotifyBalloonShow(); void NotifyBalloonClicked(); diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index db2508ecc6a4..a68dd1c48e60 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -7,6 +7,7 @@ #include "atom/browser/ui/cocoa/atom_menu_controller.h" #include "base/strings/sys_string_conversions.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/screen.h" @interface StatusItemController : NSObject { atom::TrayIconCocoa* trayIcon_; // weak @@ -25,7 +26,13 @@ } - (void)handleClick:(id)sender { - trayIcon_->NotifyClicked(); + // 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 current screen. + NSScreen* screen = [NSScreen mainScreen]; + pos.y = NSMaxY([screen frame]) - pos.y; + + trayIcon_->NotifyClicked(gfx::Point(pos.x, pos.y)); } - (void)handleDoubleClick:(id)sender { diff --git a/atom/browser/ui/tray_icon_observer.h b/atom/browser/ui/tray_icon_observer.h index 293e5371c4cf..266654af21bf 100644 --- a/atom/browser/ui/tray_icon_observer.h +++ b/atom/browser/ui/tray_icon_observer.h @@ -5,11 +5,15 @@ #ifndef ATOM_BROWSER_UI_TRAY_ICON_OBSERVER_H_ #define ATOM_BROWSER_UI_TRAY_ICON_OBSERVER_H_ +namespace gfx { +class Point; +} + namespace atom { class TrayIconObserver { public: - virtual void OnClicked() {} + virtual void OnClicked(const gfx::Point&) {} virtual void OnDoubleClicked() {} virtual void OnBalloonShow() {} virtual void OnBalloonClicked() {} diff --git a/atom/browser/ui/win/notify_icon.cc b/atom/browser/ui/win/notify_icon.cc index 99b7153631af..4c227c8044ae 100644 --- a/atom/browser/ui/win/notify_icon.cc +++ b/atom/browser/ui/win/notify_icon.cc @@ -49,7 +49,7 @@ void NotifyIcon::HandleClickEvent(const gfx::Point& cursor_pos, bool left_mouse_click) { // Pass to the observer if appropriate. if (left_mouse_click) { - NotifyClicked(); + NotifyClicked(cursor_pos); return; } diff --git a/docs/api/tray.md b/docs/api/tray.md index 6d16e3b50703..dd030385ad38 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -46,6 +46,11 @@ Creates a new tray icon associated with the `image`. ### Event: 'clicked' +* `event` +* `point` Object + * `x` Integer + * `y` Integer + Emitted when the tray icon is clicked. ### Event: 'double-clicked'