From: Haojian Wu Date: Thu, 16 Jul 2015 02:50:53 +0000 (+0800) Subject: Implement 'tray.popContextMenu' API on OS X. X-Git-Tag: v0.30.1~16^2~5 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=4421fbf9f36986d9e8c0302b1ccbfdcebe67cd62;p=electron.git Implement 'tray.popContextMenu' API on OS X. --- diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 7c1bfdfa..6fd9599d 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -106,6 +106,10 @@ void Tray::DisplayBalloon(mate::Arguments* args, tray_icon_->DisplayBalloon(icon, title, content); } +void Tray::PopContextMenu() { + tray_icon_->PopContextMenu(); +} + void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) { tray_icon_->SetContextMenu(menu->model()); } @@ -121,6 +125,7 @@ void Tray::BuildPrototype(v8::Isolate* isolate, .SetMethod("setTitle", &Tray::SetTitle) .SetMethod("setHighlightMode", &Tray::SetHighlightMode) .SetMethod("displayBalloon", &Tray::DisplayBalloon) + .SetMethod("popContextMenu", &Tray::PopContextMenu) .SetMethod("_setContextMenu", &Tray::SetContextMenu); } diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 366a847a..e7df2854 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -58,6 +58,7 @@ class Tray : public mate::EventEmitter, void SetTitle(mate::Arguments* args, const std::string& title); void SetHighlightMode(mate::Arguments* args, bool highlight); void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options); + void PopContextMenu(); void SetContextMenu(mate::Arguments* args, Menu* menu); private: diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index 4f801faf..7f67a021 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -26,6 +26,9 @@ void TrayIcon::DisplayBalloon(const gfx::Image& icon, const base::string16& contents) { } +void TrayIcon::PopContextMenu() { +} + void TrayIcon::NotifyClicked(const gfx::Rect& bounds) { FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked(bounds)); } diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 4e097e0a..e57d751b 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -46,6 +46,8 @@ class TrayIcon { const base::string16& title, const base::string16& contents); + virtual void PopContextMenu(); + // Set the context menu for this icon. virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) = 0; diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index d4cfd431..eb80250f 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -27,6 +27,7 @@ class TrayIconCocoa : public TrayIcon { void SetToolTip(const std::string& tool_tip) override; void SetTitle(const std::string& title) override; void SetHighlightMode(bool highlight) override; + void PopContextMenu() override; void SetContextMenu(ui::SimpleMenuModel* menu_model) override; private: diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 514631c2..f751f131 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -153,6 +153,17 @@ const CGFloat kMargin = 3; [self setNeedsDisplay:YES]; } +- (void)popContextMenu { + if (menuController_ && ![menuController_ isMenuOpen]) { + // redraw the dray icon to show highlight if it is enabled. + [self setNeedsDisplay:YES]; + [statusItem_ popUpStatusItemMenu:[menuController_ menu]]; + // The popUpStatusItemMenu returns only after the showing menu is closed. + // When it returns, we need to redraw the tray icon to not show highlight. + [self setNeedsDisplay:YES]; + } +} + - (void)rightMouseUp:(NSEvent*)event { trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]); } @@ -201,6 +212,10 @@ void TrayIconCocoa::SetHighlightMode(bool highlight) { [status_item_view_ setHighlight:highlight]; } +void TrayIconCocoa::PopContextMenu() { + [status_item_view_ popContextMenu]; +} + void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) { menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]); [status_item_view_ setMenuController:menu_.get()];