Implement 'tray.popContextMenu' API on OS X.

This commit is contained in:
Haojian Wu 2015-07-16 10:50:53 +08:00
parent 002eb1a326
commit 4421fbf9f3
6 changed files with 27 additions and 0 deletions

View file

@ -106,6 +106,10 @@ void Tray::DisplayBalloon(mate::Arguments* args,
tray_icon_->DisplayBalloon(icon, title, content); tray_icon_->DisplayBalloon(icon, title, content);
} }
void Tray::PopContextMenu() {
tray_icon_->PopContextMenu();
}
void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) { void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {
tray_icon_->SetContextMenu(menu->model()); tray_icon_->SetContextMenu(menu->model());
} }
@ -121,6 +125,7 @@ void Tray::BuildPrototype(v8::Isolate* isolate,
.SetMethod("setTitle", &Tray::SetTitle) .SetMethod("setTitle", &Tray::SetTitle)
.SetMethod("setHighlightMode", &Tray::SetHighlightMode) .SetMethod("setHighlightMode", &Tray::SetHighlightMode)
.SetMethod("displayBalloon", &Tray::DisplayBalloon) .SetMethod("displayBalloon", &Tray::DisplayBalloon)
.SetMethod("popContextMenu", &Tray::PopContextMenu)
.SetMethod("_setContextMenu", &Tray::SetContextMenu); .SetMethod("_setContextMenu", &Tray::SetContextMenu);
} }

View file

@ -58,6 +58,7 @@ class Tray : public mate::EventEmitter,
void SetTitle(mate::Arguments* args, const std::string& title); void SetTitle(mate::Arguments* args, const std::string& title);
void SetHighlightMode(mate::Arguments* args, bool highlight); void SetHighlightMode(mate::Arguments* args, bool highlight);
void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options); void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options);
void PopContextMenu();
void SetContextMenu(mate::Arguments* args, Menu* menu); void SetContextMenu(mate::Arguments* args, Menu* menu);
private: private:

View file

@ -26,6 +26,9 @@ void TrayIcon::DisplayBalloon(const gfx::Image& icon,
const base::string16& contents) { const base::string16& contents) {
} }
void TrayIcon::PopContextMenu() {
}
void TrayIcon::NotifyClicked(const gfx::Rect& bounds) { void TrayIcon::NotifyClicked(const gfx::Rect& bounds) {
FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked(bounds)); FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked(bounds));
} }

View file

@ -46,6 +46,8 @@ class TrayIcon {
const base::string16& title, const base::string16& title,
const base::string16& contents); const base::string16& contents);
virtual void PopContextMenu();
// Set the context menu for this icon. // Set the context menu for this icon.
virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) = 0; virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) = 0;

View file

@ -27,6 +27,7 @@ class TrayIconCocoa : public TrayIcon {
void SetToolTip(const std::string& tool_tip) override; void SetToolTip(const std::string& tool_tip) override;
void SetTitle(const std::string& title) override; void SetTitle(const std::string& title) override;
void SetHighlightMode(bool highlight) override; void SetHighlightMode(bool highlight) override;
void PopContextMenu() override;
void SetContextMenu(ui::SimpleMenuModel* menu_model) override; void SetContextMenu(ui::SimpleMenuModel* menu_model) override;
private: private:

View file

@ -153,6 +153,17 @@ const CGFloat kMargin = 3;
[self setNeedsDisplay:YES]; [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 { - (void)rightMouseUp:(NSEvent*)event {
trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]); trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]);
} }
@ -201,6 +212,10 @@ void TrayIconCocoa::SetHighlightMode(bool highlight) {
[status_item_view_ setHighlight:highlight]; [status_item_view_ setHighlight:highlight];
} }
void TrayIconCocoa::PopContextMenu() {
[status_item_view_ popContextMenu];
}
void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) { void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]); menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]);
[status_item_view_ setMenuController:menu_.get()]; [status_item_view_ setMenuController:menu_.get()];