81 lines
2 KiB
Text
81 lines
2 KiB
Text
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "atom/browser/ui/tray_icon_cocoa.h"
|
|
|
|
#include "atom/browser/ui/cocoa/atom_menu_controller.h"
|
|
#include "base/strings/sys_string_conversions.h"
|
|
#include "ui/gfx/image/image.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) {
|
|
if (!image.isNull()) {
|
|
gfx::Image neutral(image);
|
|
if (!neutral.IsEmpty())
|
|
[item_ setImage:neutral.ToNSImage()];
|
|
}
|
|
}
|
|
|
|
void TrayIconCocoa::SetPressedImage(const gfx::ImageSkia& image) {
|
|
if (!image.isNull()) {
|
|
gfx::Image neutral(image);
|
|
if (!neutral.IsEmpty())
|
|
[item_ setAlternateImage:neutral.ToNSImage()];
|
|
}
|
|
}
|
|
|
|
void TrayIconCocoa::SetToolTip(const std::string& tool_tip) {
|
|
[item_ setToolTip:base::SysUTF8ToNSString(tool_tip)];
|
|
}
|
|
|
|
void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {
|
|
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]);
|
|
[item_ setMenu:[menu_ menu]];
|
|
}
|
|
|
|
// static
|
|
TrayIcon* TrayIcon::Create() {
|
|
return new TrayIconCocoa;
|
|
}
|
|
|
|
} // namespace atom
|