2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-05-30 02:31:27 +00:00
|
|
|
// 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"
|
|
|
|
|
2014-05-30 06:37:53 +00:00
|
|
|
#include "atom/browser/ui/cocoa/atom_menu_controller.h"
|
|
|
|
#include "base/strings/sys_string_conversions.h"
|
2014-06-23 04:59:10 +00:00
|
|
|
#include "ui/gfx/image/image.h"
|
2014-05-30 06:37:53 +00:00
|
|
|
|
2014-06-02 03:28:23 +00:00
|
|
|
@interface StatusItemController : NSObject {
|
|
|
|
atom::TrayIconCocoa* trayIcon_; // weak
|
|
|
|
}
|
|
|
|
- (id)initWithIcon:(atom::TrayIconCocoa*)icon;
|
|
|
|
- (void)handleClick:(id)sender;
|
2014-09-09 11:45:21 +00:00
|
|
|
- (void)handleDoubleClick:(id)sender;
|
2014-06-02 03:28:23 +00:00
|
|
|
|
|
|
|
@end // @interface StatusItemController
|
|
|
|
|
|
|
|
@implementation StatusItemController
|
|
|
|
|
|
|
|
- (id)initWithIcon:(atom::TrayIconCocoa*)icon {
|
|
|
|
trayIcon_ = icon;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleClick:(id)sender {
|
|
|
|
trayIcon_->NotifyClicked();
|
|
|
|
}
|
|
|
|
|
2014-09-09 11:45:21 +00:00
|
|
|
- (void)handleDoubleClick:(id)sender {
|
|
|
|
trayIcon_->NotifyDoubleClicked();
|
|
|
|
}
|
|
|
|
|
2014-06-02 03:28:23 +00:00
|
|
|
@end
|
|
|
|
|
2014-05-30 02:31:27 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
TrayIconCocoa::TrayIconCocoa() {
|
2014-06-02 03:28:23 +00:00
|
|
|
controller_.reset([[StatusItemController alloc] initWithIcon:this]);
|
|
|
|
|
2014-05-30 06:37:53 +00:00
|
|
|
item_.reset([[[NSStatusBar systemStatusBar]
|
|
|
|
statusItemWithLength:NSVariableStatusItemLength] retain]);
|
|
|
|
[item_ setEnabled:YES];
|
2014-06-02 03:28:23 +00:00
|
|
|
[item_ setTarget:controller_];
|
|
|
|
[item_ setAction:@selector(handleClick:)];
|
2014-09-09 11:45:21 +00:00
|
|
|
[item_ setDoubleAction:@selector(handleDoubleClick:)];
|
2014-05-30 06:37:53 +00:00
|
|
|
[item_ setHighlightMode:YES];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrayIconCocoa::~TrayIconCocoa() {
|
2014-06-02 03:28:23 +00:00
|
|
|
// Remove the status item from the status bar.
|
|
|
|
[[NSStatusBar systemStatusBar] removeStatusItem:item_];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void TrayIconCocoa::SetImage(const gfx::Image& image) {
|
|
|
|
if (!image.IsEmpty())
|
|
|
|
[item_ setImage:image.ToNSImage()];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void TrayIconCocoa::SetPressedImage(const gfx::Image& image) {
|
|
|
|
if (!image.IsEmpty())
|
|
|
|
[item_ setAlternateImage:image.ToNSImage()];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIconCocoa::SetToolTip(const std::string& tool_tip) {
|
2014-05-30 06:37:53 +00:00
|
|
|
[item_ setToolTip:base::SysUTF8ToNSString(tool_tip)];
|
|
|
|
}
|
|
|
|
|
2014-09-09 11:33:58 +00:00
|
|
|
void TrayIconCocoa::SetTitle(const std::string& title) {
|
|
|
|
[item_ setTitle:base::SysUTF8ToNSString(title)];
|
|
|
|
}
|
|
|
|
|
2014-09-09 11:39:39 +00:00
|
|
|
void TrayIconCocoa::SetHighlightMode(bool highlight) {
|
|
|
|
[item_ setHighlightMode:highlight];
|
|
|
|
}
|
|
|
|
|
2014-05-30 06:37:53 +00:00
|
|
|
void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {
|
|
|
|
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]);
|
|
|
|
[item_ setMenu:[menu_ menu]];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 15:57:54 +00:00
|
|
|
// static
|
|
|
|
TrayIcon* TrayIcon::Create() {
|
|
|
|
return new TrayIconCocoa;
|
|
|
|
}
|
|
|
|
|
2014-05-30 02:31:27 +00:00
|
|
|
} // namespace atom
|