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
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
const CGFloat kStatusItemLength = 26;
|
|
|
|
const CGFloat kMargin = 3;
|
|
|
|
|
|
|
|
@interface StatusItemView : NSView {
|
2014-06-02 03:28:23 +00:00
|
|
|
atom::TrayIconCocoa* trayIcon_; // weak
|
2015-07-16 02:49:55 +00:00
|
|
|
AtomMenuController* menuController_; // weak
|
2015-07-15 10:26:39 +00:00
|
|
|
BOOL isHighlightEnable_;
|
|
|
|
BOOL inMouseEventSequence_;
|
|
|
|
base::scoped_nsobject<NSImage> image_;
|
|
|
|
base::scoped_nsobject<NSImage> alternateImage_;
|
|
|
|
base::scoped_nsobject<NSString> title_;
|
|
|
|
base::scoped_nsobject<NSStatusItem> statusItem_;
|
2014-06-02 03:28:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
@end // @interface StatusItemView
|
2014-06-02 03:28:23 +00:00
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
@implementation StatusItemView
|
2014-06-02 03:28:23 +00:00
|
|
|
|
|
|
|
- (id)initWithIcon:(atom::TrayIconCocoa*)icon {
|
|
|
|
trayIcon_ = icon;
|
2015-07-15 10:26:39 +00:00
|
|
|
isHighlightEnable_ = YES;
|
|
|
|
statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:
|
|
|
|
NSVariableStatusItemLength] retain]);
|
2015-07-16 02:49:55 +00:00
|
|
|
NSRect frame = NSMakeRect(0,
|
|
|
|
0,
|
|
|
|
kStatusItemLength,
|
|
|
|
[[statusItem_ statusBar] thickness]);
|
2015-07-15 10:26:39 +00:00
|
|
|
if ((self = [super initWithFrame:frame])) {
|
|
|
|
[statusItem_ setView:self];
|
|
|
|
}
|
2014-06-02 03:28:23 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
- (void)removeItem {
|
|
|
|
[[NSStatusBar systemStatusBar] removeStatusItem:statusItem_];
|
|
|
|
statusItem_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)drawRect:(NSRect)dirtyRect {
|
2015-07-16 02:49:55 +00:00
|
|
|
// Draw the tray icon and title that align with NSSStatusItem, layout:
|
|
|
|
// ----------------
|
|
|
|
// | icon | title |
|
|
|
|
/// ----------------
|
2015-07-15 10:26:39 +00:00
|
|
|
BOOL highlight = [self shouldHighlight];
|
2015-07-16 02:49:55 +00:00
|
|
|
CGFloat titleWidth = [self titleWidth];
|
|
|
|
// Calculate the total icon bounds.
|
|
|
|
NSRect statusItemBounds = NSMakeRect(0,
|
|
|
|
0,
|
|
|
|
kStatusItemLength + titleWidth,
|
|
|
|
[[statusItem_ statusBar] thickness]);
|
|
|
|
[statusItem_ drawStatusBarBackgroundInRect:statusItemBounds
|
2015-07-15 10:26:39 +00:00
|
|
|
withHighlight:highlight];
|
2015-07-16 02:49:55 +00:00
|
|
|
[statusItem_ setLength:titleWidth + kStatusItemLength];
|
|
|
|
if (title_) {
|
|
|
|
NSRect titleDrawRect = NSMakeRect(kStatusItemLength,
|
|
|
|
0,
|
|
|
|
titleWidth + kStatusItemLength,
|
|
|
|
[[statusItem_ statusBar] thickness]);
|
|
|
|
[title_ drawInRect:titleDrawRect
|
|
|
|
withAttributes:[self titleAttributes]];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSRect iconRect = NSMakeRect(0,
|
|
|
|
0,
|
|
|
|
kStatusItemLength,
|
|
|
|
[[statusItem_ statusBar] thickness]);
|
2015-07-15 10:26:39 +00:00
|
|
|
if (highlight && alternateImage_) {
|
2015-07-16 02:49:55 +00:00
|
|
|
[alternateImage_ drawInRect:NSInsetRect(iconRect, kMargin, kMargin)
|
2015-07-15 10:26:39 +00:00
|
|
|
fromRect:NSZeroRect
|
|
|
|
operation:NSCompositeSourceOver
|
|
|
|
fraction:1];
|
|
|
|
} else {
|
2015-07-16 02:49:55 +00:00
|
|
|
[image_ drawInRect:NSInsetRect(iconRect, kMargin, kMargin)
|
2015-07-15 10:26:39 +00:00
|
|
|
fromRect:NSZeroRect
|
|
|
|
operation:NSCompositeSourceOver
|
|
|
|
fraction:1];
|
|
|
|
}
|
2015-07-16 02:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat) titleWidth {
|
|
|
|
if (!title_) return 0;
|
|
|
|
NSAttributedString* attributes =
|
|
|
|
[[NSAttributedString alloc] initWithString:title_
|
|
|
|
attributes:[self titleAttributes]];
|
|
|
|
return [attributes size].width;
|
2015-07-15 10:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)titleAttributes {
|
|
|
|
NSFont* font = [NSFont menuBarFontOfSize:0];
|
|
|
|
NSColor* foregroundColor = [NSColor blackColor];
|
2015-05-01 09:54:22 +00:00
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
font, NSFontAttributeName,
|
|
|
|
foregroundColor, NSForegroundColorAttributeName,
|
|
|
|
nil];
|
2014-06-02 03:28:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
- (void)setImage:(NSImage*)image {
|
|
|
|
image_.reset([image copy]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAlternateImage:(NSImage*)image {
|
|
|
|
alternateImage_.reset([image copy]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setHighlight:(BOOL)highlight {
|
|
|
|
isHighlightEnable_ = highlight;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setTitle:(NSString*) title {
|
|
|
|
title_.reset([title copy]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setMenuController:(AtomMenuController*)menu {
|
2015-07-16 02:49:55 +00:00
|
|
|
menuController_ = menu;
|
2015-07-15 10:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)mouseDown:(NSEvent *)event {
|
|
|
|
inMouseEventSequence_ = YES;
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)mouseUp:(NSEvent *)event {
|
|
|
|
if (!inMouseEventSequence_) {
|
|
|
|
// If the menu is showing, when user clicked the tray icon, the `mouseDown`
|
|
|
|
// event will be dissmissed, we need to close the menu at this time.
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inMouseEventSequence_ = NO;
|
|
|
|
if (event.clickCount == 1) {
|
2015-07-16 02:49:55 +00:00
|
|
|
if (menuController_) {
|
|
|
|
[statusItem_ popUpStatusItemMenu:[menuController_ menu]];
|
2015-07-15 10:26:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 11:23:12 +00:00
|
|
|
trayIcon_->NotifyClicked([self getBoundsFromEvent:event]);
|
2015-07-15 10:26:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 02:49:55 +00:00
|
|
|
if (event.clickCount == 2 && !menuController_) {
|
2015-07-15 10:26:39 +00:00
|
|
|
trayIcon_->NotifyDoubleClicked();
|
|
|
|
}
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
|
2015-07-16 02:50:53 +00:00
|
|
|
- (void)popContextMenu {
|
|
|
|
if (menuController_ && ![menuController_ isMenuOpen]) {
|
|
|
|
// redraw the dray icon to show highlight if it is enabled.
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
[statusItem_ popUpStatusItemMenu:[menuController_ menu]];
|
|
|
|
// The popUpStatusItemMenu returns only after the showing menu is closed.
|
|
|
|
// When it returns, we need to redraw the tray icon to not show highlight.
|
|
|
|
[self setNeedsDisplay:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-15 11:23:12 +00:00
|
|
|
- (void)rightMouseUp:(NSEvent*)event {
|
|
|
|
trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]);
|
|
|
|
}
|
|
|
|
|
2015-07-15 10:26:39 +00:00
|
|
|
-(BOOL) shouldHighlight {
|
2015-07-16 02:49:55 +00:00
|
|
|
BOOL is_menu_open = [menuController_ isMenuOpen];
|
2015-07-15 10:26:39 +00:00
|
|
|
return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open);
|
2014-09-09 11:45:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 11:23:12 +00:00
|
|
|
-(gfx::Rect) getBoundsFromEvent:(NSEvent*)event {
|
|
|
|
NSRect frame = event.window.frame;
|
|
|
|
gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
|
|
|
|
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
|
|
|
|
bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
|
|
|
|
return bounds;
|
|
|
|
}
|
2014-06-02 03:28:23 +00:00
|
|
|
@end
|
|
|
|
|
2014-05-30 02:31:27 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
TrayIconCocoa::TrayIconCocoa() {
|
2015-07-15 10:26:39 +00:00
|
|
|
status_item_view_.reset([[StatusItemView alloc] initWithIcon:this]);
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrayIconCocoa::~TrayIconCocoa() {
|
2015-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ removeItem];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 02:43:56 +00:00
|
|
|
void TrayIconCocoa::SetImage(const gfx::Image& image) {
|
2015-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ 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-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ setAlternateImage:image.AsNSImage()];
|
2014-05-30 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrayIconCocoa::SetToolTip(const std::string& tool_tip) {
|
2015-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ setToolTip:base::SysUTF8ToNSString(tool_tip)];
|
2014-05-30 06:37:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 11:33:58 +00:00
|
|
|
void TrayIconCocoa::SetTitle(const std::string& title) {
|
2015-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ setTitle:base::SysUTF8ToNSString(title)];
|
2014-09-09 11:33:58 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 11:39:39 +00:00
|
|
|
void TrayIconCocoa::SetHighlightMode(bool highlight) {
|
2015-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ setHighlight:highlight];
|
2014-09-09 11:39:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 02:50:53 +00:00
|
|
|
void TrayIconCocoa::PopContextMenu() {
|
|
|
|
[status_item_view_ popContextMenu];
|
|
|
|
}
|
|
|
|
|
2014-05-30 06:37:53 +00:00
|
|
|
void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {
|
|
|
|
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]);
|
2015-07-15 10:26:39 +00:00
|
|
|
[status_item_view_ setMenuController:menu_.get()];
|
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
|