diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index c84e8a1d66b..85c0578a8e8 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -159,6 +159,10 @@ void Tray::SetContextMenu(v8::Isolate* isolate, mate::Handle menu) { tray_icon_->SetContextMenu(menu->model()); } +gfx::Rect Tray::GetBounds() { + return tray_icon_->GetBounds(); +} + v8::Local Tray::ModifiersToObject(v8::Isolate* isolate, int modifiers) { mate::Dictionary obj(isolate, v8::Object::New(isolate)); @@ -181,7 +185,8 @@ void Tray::BuildPrototype(v8::Isolate* isolate, .SetMethod("setHighlightMode", &Tray::SetHighlightMode) .SetMethod("displayBalloon", &Tray::DisplayBalloon) .SetMethod("popUpContextMenu", &Tray::PopUpContextMenu) - .SetMethod("setContextMenu", &Tray::SetContextMenu); + .SetMethod("setContextMenu", &Tray::SetContextMenu) + .SetMethod("getBounds", &Tray::GetBounds); } } // namespace api diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index a6c329567ca..0bc28af818c 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -65,6 +65,7 @@ class Tray : public mate::TrackableObject, void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options); void PopUpContextMenu(mate::Arguments* args); void SetContextMenu(v8::Isolate* isolate, mate::Handle menu); + gfx::Rect GetBounds(); private: v8::Local ModifiersToObject(v8::Isolate* isolate, int modifiers); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index dcdb90ac159..fda68b09cd1 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -30,6 +30,10 @@ void TrayIcon::PopUpContextMenu(const gfx::Point& pos, ui::SimpleMenuModel* menu_model) { } +gfx::Rect TrayIcon::GetBounds() { + return gfx::Rect(); +} + void TrayIcon::NotifyClicked(const gfx::Rect& bounds, int modifiers) { FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked(bounds, modifiers)); } diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 1916c11b23a..2763e50941b 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -60,8 +60,12 @@ class TrayIcon { // Set the context menu for this icon. virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) = 0; + // Returns the bounds of tray icon. + virtual gfx::Rect GetBounds(); + void AddObserver(TrayIconObserver* obs) { observers_.AddObserver(obs); } void RemoveObserver(TrayIconObserver* obs) { observers_.RemoveObserver(obs); } + void NotifyClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0); void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0); void NotifyBalloonShow(); diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index 59e2241aa48..cb972d54a9a 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -32,6 +32,7 @@ class TrayIconCocoa : public TrayIcon, void PopUpContextMenu(const gfx::Point& pos, ui::SimpleMenuModel* menu_model) override; void SetContextMenu(ui::SimpleMenuModel* menu_model) override; + gfx::Rect GetBounds() override; protected: // AtomMenuModel::Observer: diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 0dfd59132ae..8b25aa3540f 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -8,6 +8,7 @@ #include "base/strings/sys_string_conversions.h" #include "ui/events/cocoa/cocoa_event_utils.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/mac/coordinate_conversion.h" #include "ui/gfx/screen.h" namespace { @@ -236,13 +237,13 @@ const CGFloat kVerticalTitleMargin = 2; // Single click event. if (event.clickCount == 1) trayIcon_->NotifyClicked( - [self getBoundsFromEvent:event], + gfx::ScreenRectFromNSRect(event.window.frame), ui::EventFlagsFromModifiers([event modifierFlags])); // Double click event. if (event.clickCount == 2) trayIcon_->NotifyDoubleClicked( - [self getBoundsFromEvent:event], + gfx::ScreenRectFromNSRect(event.window.frame), ui::EventFlagsFromModifiers([event modifierFlags])); [self setNeedsDisplay:YES]; @@ -273,7 +274,7 @@ const CGFloat kVerticalTitleMargin = 2; - (void)rightMouseUp:(NSEvent*)event { trayIcon_->NotifyRightClicked( - [self getBoundsFromEvent:event], + gfx::ScreenRectFromNSRect(event.window.frame), ui::EventFlagsFromModifiers([event modifierFlags])); } @@ -324,13 +325,6 @@ const CGFloat kVerticalTitleMargin = 2; return isHighlightEnable_ && (inMouseEventSequence_ || isMenuOpen); } -- (gfx::Rect)getBoundsFromEvent:(NSEvent*)event { - NSRect frame = event.window.frame; - gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame)); - NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; - bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame)); - return bounds; -} @end namespace atom { @@ -386,6 +380,15 @@ void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) { [status_item_view_ setMenuController:menu_.get()]; } +gfx::Rect TrayIconCocoa::GetBounds() { + auto bounds = gfx::ScreenRectFromNSRect([status_item_view_ window].frame); + // Calling [window frame] immediately after the view gets created will have + // negative |y| sometimes. + if (bounds.y() < 0) + bounds.set_y(0); + return bounds; +} + void TrayIconCocoa::MenuClosed() { [status_item_view_ setNeedsDisplay:YES]; }