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"
|
2015-05-01 09:54:22 +00:00
|
|
|
#include "ui/gfx/screen.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 {
|
2015-05-04 03:43:05 +00:00
|
|
|
// Get the frame of the NSStatusItem.
|
|
|
|
NSRect frame = [NSApp currentEvent].window.frame;
|
|
|
|
gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
|
2015-05-01 09:54:22 +00:00
|
|
|
// Flip coordinates to gfx (0,0 in top-left corner) using current screen.
|
2015-05-10 08:51:18 +00:00
|
|
|
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
|
2015-05-04 03:43:05 +00:00
|
|
|
bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
|
2015-05-01 09:54:22 +00:00
|
|
|
|
2015-05-04 03:43:05 +00:00
|
|
|
trayIcon_->NotifyClicked(bounds);
|
2014-06-02 03:28:23 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-02-12 04:29:21 +00:00
|
|
|
[item_ setImage:image.AsNSImage()];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void TrayIconCocoa::SetPressedImage(const gfx::Image& image) {
|
2015-02-12 04:29:21 +00:00
|
|
|
[item_ setAlternateImage:image.AsNSImage()];
|
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
|