mac: Implement the "click" event of TrayIcon.

This commit is contained in:
Cheng Zhao 2014-06-02 11:28:23 +08:00
parent cbd9366898
commit da31588e0d
3 changed files with 32 additions and 0 deletions

View file

@ -21,6 +21,7 @@ namespace api {
Tray::Tray(const gfx::ImageSkia& image) Tray::Tray(const gfx::ImageSkia& image)
: tray_icon_(TrayIcon::Create()) { : tray_icon_(TrayIcon::Create()) {
tray_icon_->SetImage(image); tray_icon_->SetImage(image);
tray_icon_->AddObserver(this);
} }
Tray::~Tray() { Tray::~Tray() {

View file

@ -13,6 +13,7 @@
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
@class AtomMenuController; @class AtomMenuController;
@class StatusItemController;
namespace atom { namespace atom {
@ -29,6 +30,8 @@ class TrayIconCocoa : public TrayIcon {
private: private:
base::scoped_nsobject<NSStatusItem> item_; base::scoped_nsobject<NSStatusItem> item_;
base::scoped_nsobject<StatusItemController> controller_;
// Status menu shown when right-clicking the system icon. // Status menu shown when right-clicking the system icon.
base::scoped_nsobject<AtomMenuController> menu_; base::scoped_nsobject<AtomMenuController> menu_;

View file

@ -8,16 +8,44 @@
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "skia/ext/skia_utils_mac.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 { namespace atom {
TrayIconCocoa::TrayIconCocoa() { TrayIconCocoa::TrayIconCocoa() {
controller_.reset([[StatusItemController alloc] initWithIcon:this]);
item_.reset([[[NSStatusBar systemStatusBar] item_.reset([[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength] retain]); statusItemWithLength:NSVariableStatusItemLength] retain]);
[item_ setEnabled:YES]; [item_ setEnabled:YES];
[item_ setTarget:controller_];
[item_ setAction:@selector(handleClick:)];
[item_ setHighlightMode:YES]; [item_ setHighlightMode:YES];
} }
TrayIconCocoa::~TrayIconCocoa() { TrayIconCocoa::~TrayIconCocoa() {
// Remove the status item from the status bar.
[[NSStatusBar systemStatusBar] removeStatusItem:item_];
} }
void TrayIconCocoa::SetImage(const gfx::ImageSkia& image) { void TrayIconCocoa::SetImage(const gfx::ImageSkia& image) {