Introduce Electron custom statusItem view.

Reimplement tray functions on OS X by using custom statusItem view.
This commit is contained in:
Haojian Wu 2015-07-15 18:26:39 +08:00
parent e5f852d7d5
commit e54fda6b34
2 changed files with 135 additions and 38 deletions

View file

@ -13,7 +13,7 @@
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
@class AtomMenuController; @class AtomMenuController;
@class StatusItemController; @class StatusItemView;
namespace atom { namespace atom {
@ -30,9 +30,8 @@ class TrayIconCocoa : public TrayIcon {
void SetContextMenu(ui::SimpleMenuModel* menu_model) override; void SetContextMenu(ui::SimpleMenuModel* menu_model) override;
private: private:
base::scoped_nsobject<NSStatusItem> item_; // Atom custom view for NSStatusItem.
base::scoped_nsobject<StatusItemView> status_item_view_;
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

@ -9,35 +9,142 @@
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/gfx/screen.h" #include "ui/gfx/screen.h"
@interface StatusItemController : NSObject {
const CGFloat kStatusItemLength = 26;
const CGFloat kMargin = 3;
@interface StatusItemView : NSView {
atom::TrayIconCocoa* trayIcon_; // weak atom::TrayIconCocoa* trayIcon_; // weak
AtomMenuController* menu_controller_; // weak
BOOL isHighlightEnable_;
BOOL inMouseEventSequence_;
base::scoped_nsobject<NSImage> image_;
base::scoped_nsobject<NSImage> alternateImage_;
base::scoped_nsobject<NSString> title_;
base::scoped_nsobject<NSStatusItem> statusItem_;
} }
- (id)initWithIcon:(atom::TrayIconCocoa*)icon;
- (void)handleClick:(id)sender;
- (void)handleDoubleClick:(id)sender;
@end // @interface StatusItemController @end // @interface StatusItemView
@implementation StatusItemController @implementation StatusItemView
- (id)initWithIcon:(atom::TrayIconCocoa*)icon { - (id)initWithIcon:(atom::TrayIconCocoa*)icon {
trayIcon_ = icon; trayIcon_ = icon;
isHighlightEnable_ = YES;
statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:
NSVariableStatusItemLength] retain]);
NSRect frame = NSMakeRect(0, 0, 26, [[statusItem_ statusBar] thickness]);
if ((self = [super initWithFrame:frame])) {
[statusItem_ setView:self];
}
return self; return self;
} }
- (void)handleClick:(id)sender { - (void)removeItem {
// Get the frame of the NSStatusItem. [[NSStatusBar systemStatusBar] removeStatusItem:statusItem_];
NSRect frame = [NSApp currentEvent].window.frame; statusItem_.reset();
gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame));
// Flip coordinates to gfx (0,0 in top-left corner) using current screen.
NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame));
trayIcon_->NotifyClicked(bounds);
} }
- (void)handleDoubleClick:(id)sender { - (void)drawRect:(NSRect)dirtyRect {
trayIcon_->NotifyDoubleClicked(); BOOL highlight = [self shouldHighlight];
[statusItem_ drawStatusBarBackgroundInRect:[self bounds]
withHighlight:highlight];
NSRect icon_frame = NSMakeRect(0,
0,
kStatusItemLength,
[[statusItem_ statusBar] thickness]);
NSRect icon_draw_rect = NSInsetRect(icon_frame, kMargin, kMargin);
if (highlight && alternateImage_) {
[alternateImage_ drawInRect:icon_draw_rect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1];
} else {
[image_ drawInRect:icon_draw_rect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1];
}
if (title_) {
NSAttributedString* attributes =
[[NSAttributedString alloc] initWithString:title_
attributes:[self titleAttributes]];
CGFloat title_width = [attributes size].width;
NSRect title_rect = NSMakeRect(kStatusItemLength,
0,
title_width + kStatusItemLength,
[[statusItem_ statusBar] thickness]);
[title_ drawInRect:title_rect
withAttributes:[self titleAttributes]];
[statusItem_ setLength:title_width + kStatusItemLength];
}
}
- (NSDictionary *)titleAttributes {
NSFont* font = [NSFont menuBarFontOfSize:0];
NSColor* foregroundColor = [NSColor blackColor];
return [NSDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
foregroundColor, NSForegroundColorAttributeName,
nil];
}
- (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_= [title retain];
title_.reset([title copy]);
}
- (void)setMenuController:(AtomMenuController*)menu {
menu_controller_ = menu;
}
-(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) {
if (menu_controller_) {
[statusItem_ popUpStatusItemMenu:[menu_controller_ menu]];
}
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));
trayIcon_->NotifyClicked(bounds);
}
if (event.clickCount == 2 && !menu_controller_) {
trayIcon_->NotifyDoubleClicked();
}
[self setNeedsDisplay:YES];
}
-(BOOL) shouldHighlight {
BOOL is_menu_open = [menu_controller_ isMenuOpen];
return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open);
} }
@end @end
@ -45,45 +152,36 @@
namespace atom { namespace atom {
TrayIconCocoa::TrayIconCocoa() { TrayIconCocoa::TrayIconCocoa() {
controller_.reset([[StatusItemController alloc] initWithIcon:this]); status_item_view_.reset([[StatusItemView alloc] initWithIcon:this]);
item_.reset([[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength] retain]);
[item_ setEnabled:YES];
[item_ setTarget:controller_];
[item_ setAction:@selector(handleClick:)];
[item_ setDoubleAction:@selector(handleDoubleClick:)];
[item_ setHighlightMode:YES];
} }
TrayIconCocoa::~TrayIconCocoa() { TrayIconCocoa::~TrayIconCocoa() {
// Remove the status item from the status bar. [status_item_view_ removeItem];
[[NSStatusBar systemStatusBar] removeStatusItem:item_];
} }
void TrayIconCocoa::SetImage(const gfx::Image& image) { void TrayIconCocoa::SetImage(const gfx::Image& image) {
[item_ setImage:image.AsNSImage()]; [status_item_view_ setImage:image.AsNSImage()];
} }
void TrayIconCocoa::SetPressedImage(const gfx::Image& image) { void TrayIconCocoa::SetPressedImage(const gfx::Image& image) {
[item_ setAlternateImage:image.AsNSImage()]; [status_item_view_ setAlternateImage:image.AsNSImage()];
} }
void TrayIconCocoa::SetToolTip(const std::string& tool_tip) { void TrayIconCocoa::SetToolTip(const std::string& tool_tip) {
[item_ setToolTip:base::SysUTF8ToNSString(tool_tip)]; [status_item_view_ setToolTip:base::SysUTF8ToNSString(tool_tip)];
} }
void TrayIconCocoa::SetTitle(const std::string& title) { void TrayIconCocoa::SetTitle(const std::string& title) {
[item_ setTitle:base::SysUTF8ToNSString(title)]; [status_item_view_ setTitle:base::SysUTF8ToNSString(title)];
} }
void TrayIconCocoa::SetHighlightMode(bool highlight) { void TrayIconCocoa::SetHighlightMode(bool highlight) {
[item_ setHighlightMode:highlight]; [status_item_view_ setHighlight:highlight];
} }
void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) { void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {
menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]); menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]);
[item_ setMenu:[menu_ menu]]; [status_item_view_ setMenuController:menu_.get()];
} }
// static // static