mac: Implement menu parameter

This commit is contained in:
Cheng Zhao 2015-12-02 19:05:22 +08:00
parent 2fba05b5e7
commit 3cdd0f35c7
2 changed files with 21 additions and 4 deletions

View file

@ -137,11 +137,11 @@ void Tray::DisplayBalloon(mate::Arguments* args,
} }
void Tray::PopUpContextMenu(mate::Arguments* args) { void Tray::PopUpContextMenu(mate::Arguments* args) {
Menu* menu = nullptr; mate::Handle<Menu> menu;
args->GetNext(&menu); args->GetNext(&menu);
gfx::Point pos; gfx::Point pos;
args->GetNext(&pos); args->GetNext(&pos);
tray_icon_->PopUpContextMenu(pos, menu ? menu->model() : nullptr); tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
} }
void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) { void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {

View file

@ -23,6 +23,7 @@ const CGFloat kVerticalTitleMargin = 2;
atom::TrayIconCocoa* trayIcon_; // weak atom::TrayIconCocoa* trayIcon_; // weak
AtomMenuController* menuController_; // weak AtomMenuController* menuController_; // weak
BOOL isHighlightEnable_; BOOL isHighlightEnable_;
BOOL forceHighlight_;
BOOL inMouseEventSequence_; BOOL inMouseEventSequence_;
base::scoped_nsobject<NSImage> image_; base::scoped_nsobject<NSImage> image_;
base::scoped_nsobject<NSImage> alternateImage_; base::scoped_nsobject<NSImage> alternateImage_;
@ -39,6 +40,8 @@ const CGFloat kVerticalTitleMargin = 2;
image_.reset([image copy]); image_.reset([image copy]);
trayIcon_ = icon; trayIcon_ = icon;
isHighlightEnable_ = YES; isHighlightEnable_ = YES;
forceHighlight_ = NO;
inMouseEventSequence_ = NO;
if ((self = [super initWithFrame: CGRectZero])) { if ((self = [super initWithFrame: CGRectZero])) {
// Setup the image view. // Setup the image view.
@ -238,7 +241,19 @@ const CGFloat kVerticalTitleMargin = 2;
[self setNeedsDisplay:YES]; [self setNeedsDisplay:YES];
} }
- (void)popUpContextMenu { - (void)popUpContextMenu:(ui::SimpleMenuModel*)menu_model {
// Show a custom menu.
if (menu_model) {
base::scoped_nsobject<AtomMenuController> menuController(
[[AtomMenuController alloc] initWithModel:menu_model]);
forceHighlight_ = YES; // Should highlight when showing menu.
[self setNeedsDisplay:YES];
[statusItem_ popUpStatusItemMenu:[menuController menu]];
forceHighlight_ = NO;
[self setNeedsDisplay:YES];
return;
}
if (menuController_ && ![menuController_ isMenuOpen]) { if (menuController_ && ![menuController_ isMenuOpen]) {
// Redraw the dray icon to show highlight if it is enabled. // Redraw the dray icon to show highlight if it is enabled.
[self setNeedsDisplay:YES]; [self setNeedsDisplay:YES];
@ -288,6 +303,8 @@ const CGFloat kVerticalTitleMargin = 2;
} }
- (BOOL)shouldHighlight { - (BOOL)shouldHighlight {
if (isHighlightEnable_ && forceHighlight_)
return true;
BOOL isMenuOpen = menuController_ && [menuController_ isMenuOpen]; BOOL isMenuOpen = menuController_ && [menuController_ isMenuOpen];
return isHighlightEnable_ && (inMouseEventSequence_ || isMenuOpen); return isHighlightEnable_ && (inMouseEventSequence_ || isMenuOpen);
} }
@ -340,7 +357,7 @@ void TrayIconCocoa::SetHighlightMode(bool highlight) {
void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos, void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos,
ui::SimpleMenuModel* menu_model) { ui::SimpleMenuModel* menu_model) {
[status_item_view_ popUpContextMenu]; [status_item_view_ popUpContextMenu:menu_model];
} }
void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) { void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {