From da31588e0d254b3659887b1e09cf77dd975b6c44 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 2 Jun 2014 11:28:23 +0800 Subject: [PATCH] mac: Implement the "click" event of TrayIcon. --- atom/browser/api/atom_api_tray.cc | 1 + atom/browser/ui/tray_icon_cocoa.h | 3 +++ atom/browser/ui/tray_icon_cocoa.mm | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 1fc6aa936af0..49d954024437 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -21,6 +21,7 @@ namespace api { Tray::Tray(const gfx::ImageSkia& image) : tray_icon_(TrayIcon::Create()) { tray_icon_->SetImage(image); + tray_icon_->AddObserver(this); } Tray::~Tray() { diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index c030a1f98445..b6a710ce1272 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -13,6 +13,7 @@ #include "base/mac/scoped_nsobject.h" @class AtomMenuController; +@class StatusItemController; namespace atom { @@ -29,6 +30,8 @@ class TrayIconCocoa : public TrayIcon { private: base::scoped_nsobject item_; + base::scoped_nsobject controller_; + // Status menu shown when right-clicking the system icon. base::scoped_nsobject menu_; diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index f490015c97ee..03461634378c 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -8,16 +8,44 @@ #include "base/strings/sys_string_conversions.h" #include "skia/ext/skia_utils_mac.h" +@interface StatusItemController : NSObject { + atom::TrayIconCocoa* trayIcon_; // weak +} +- (id)initWithIcon:(atom::TrayIconCocoa*)icon; +- (void)handleClick:(id)sender; + +@end // @interface StatusItemController + +@implementation StatusItemController + +- (id)initWithIcon:(atom::TrayIconCocoa*)icon { + trayIcon_ = icon; + return self; +} + +- (void)handleClick:(id)sender { + DCHECK(trayIcon_); + trayIcon_->NotifyClicked(); +} + +@end + namespace atom { TrayIconCocoa::TrayIconCocoa() { + controller_.reset([[StatusItemController alloc] initWithIcon:this]); + item_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]); [item_ setEnabled:YES]; + [item_ setTarget:controller_]; + [item_ setAction:@selector(handleClick:)]; [item_ setHighlightMode:YES]; } TrayIconCocoa::~TrayIconCocoa() { + // Remove the status item from the status bar. + [[NSStatusBar systemStatusBar] removeStatusItem:item_]; } void TrayIconCocoa::SetImage(const gfx::ImageSkia& image) {