Simplify code logic and fix object-c code style.
This commit is contained in:
parent
38c33d69ae
commit
002eb1a326
1 changed files with 45 additions and 30 deletions
|
@ -9,13 +9,12 @@
|
||||||
#include "ui/gfx/image/image.h"
|
#include "ui/gfx/image/image.h"
|
||||||
#include "ui/gfx/screen.h"
|
#include "ui/gfx/screen.h"
|
||||||
|
|
||||||
|
|
||||||
const CGFloat kStatusItemLength = 26;
|
const CGFloat kStatusItemLength = 26;
|
||||||
const CGFloat kMargin = 3;
|
const CGFloat kMargin = 3;
|
||||||
|
|
||||||
@interface StatusItemView : NSView {
|
@interface StatusItemView : NSView {
|
||||||
atom::TrayIconCocoa* trayIcon_; // weak
|
atom::TrayIconCocoa* trayIcon_; // weak
|
||||||
AtomMenuController* menu_controller_; // weak
|
AtomMenuController* menuController_; // weak
|
||||||
BOOL isHighlightEnable_;
|
BOOL isHighlightEnable_;
|
||||||
BOOL inMouseEventSequence_;
|
BOOL inMouseEventSequence_;
|
||||||
base::scoped_nsobject<NSImage> image_;
|
base::scoped_nsobject<NSImage> image_;
|
||||||
|
@ -33,7 +32,10 @@ const CGFloat kMargin = 3;
|
||||||
isHighlightEnable_ = YES;
|
isHighlightEnable_ = YES;
|
||||||
statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:
|
statusItem_.reset([[[NSStatusBar systemStatusBar] statusItemWithLength:
|
||||||
NSVariableStatusItemLength] retain]);
|
NSVariableStatusItemLength] retain]);
|
||||||
NSRect frame = NSMakeRect(0, 0, 26, [[statusItem_ statusBar] thickness]);
|
NSRect frame = NSMakeRect(0,
|
||||||
|
0,
|
||||||
|
kStatusItemLength,
|
||||||
|
[[statusItem_ statusBar] thickness]);
|
||||||
if ((self = [super initWithFrame:frame])) {
|
if ((self = [super initWithFrame:frame])) {
|
||||||
[statusItem_ setView:self];
|
[statusItem_ setView:self];
|
||||||
}
|
}
|
||||||
|
@ -46,38 +48,52 @@ const CGFloat kMargin = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)drawRect:(NSRect)dirtyRect {
|
- (void)drawRect:(NSRect)dirtyRect {
|
||||||
|
// Draw the tray icon and title that align with NSSStatusItem, layout:
|
||||||
|
// ----------------
|
||||||
|
// | icon | title |
|
||||||
|
/// ----------------
|
||||||
BOOL highlight = [self shouldHighlight];
|
BOOL highlight = [self shouldHighlight];
|
||||||
[statusItem_ drawStatusBarBackgroundInRect:[self bounds]
|
CGFloat titleWidth = [self titleWidth];
|
||||||
|
// Calculate the total icon bounds.
|
||||||
|
NSRect statusItemBounds = NSMakeRect(0,
|
||||||
|
0,
|
||||||
|
kStatusItemLength + titleWidth,
|
||||||
|
[[statusItem_ statusBar] thickness]);
|
||||||
|
[statusItem_ drawStatusBarBackgroundInRect:statusItemBounds
|
||||||
withHighlight:highlight];
|
withHighlight:highlight];
|
||||||
NSRect icon_frame = NSMakeRect(0,
|
[statusItem_ setLength:titleWidth + kStatusItemLength];
|
||||||
0,
|
if (title_) {
|
||||||
kStatusItemLength,
|
NSRect titleDrawRect = NSMakeRect(kStatusItemLength,
|
||||||
[[statusItem_ statusBar] thickness]);
|
0,
|
||||||
NSRect icon_draw_rect = NSInsetRect(icon_frame, kMargin, kMargin);
|
titleWidth + kStatusItemLength,
|
||||||
|
[[statusItem_ statusBar] thickness]);
|
||||||
|
[title_ drawInRect:titleDrawRect
|
||||||
|
withAttributes:[self titleAttributes]];
|
||||||
|
}
|
||||||
|
|
||||||
|
NSRect iconRect = NSMakeRect(0,
|
||||||
|
0,
|
||||||
|
kStatusItemLength,
|
||||||
|
[[statusItem_ statusBar] thickness]);
|
||||||
if (highlight && alternateImage_) {
|
if (highlight && alternateImage_) {
|
||||||
[alternateImage_ drawInRect:icon_draw_rect
|
[alternateImage_ drawInRect:NSInsetRect(iconRect, kMargin, kMargin)
|
||||||
fromRect:NSZeroRect
|
fromRect:NSZeroRect
|
||||||
operation:NSCompositeSourceOver
|
operation:NSCompositeSourceOver
|
||||||
fraction:1];
|
fraction:1];
|
||||||
} else {
|
} else {
|
||||||
[image_ drawInRect:icon_draw_rect
|
[image_ drawInRect:NSInsetRect(iconRect, kMargin, kMargin)
|
||||||
fromRect:NSZeroRect
|
fromRect:NSZeroRect
|
||||||
operation:NSCompositeSourceOver
|
operation:NSCompositeSourceOver
|
||||||
fraction:1];
|
fraction:1];
|
||||||
}
|
}
|
||||||
if (title_) {
|
}
|
||||||
NSAttributedString* attributes =
|
|
||||||
[[NSAttributedString alloc] initWithString:title_
|
- (CGFloat) titleWidth {
|
||||||
attributes:[self titleAttributes]];
|
if (!title_) return 0;
|
||||||
CGFloat title_width = [attributes size].width;
|
NSAttributedString* attributes =
|
||||||
NSRect title_rect = NSMakeRect(kStatusItemLength,
|
[[NSAttributedString alloc] initWithString:title_
|
||||||
0,
|
attributes:[self titleAttributes]];
|
||||||
title_width + kStatusItemLength,
|
return [attributes size].width;
|
||||||
[[statusItem_ statusBar] thickness]);
|
|
||||||
[title_ drawInRect:title_rect
|
|
||||||
withAttributes:[self titleAttributes]];
|
|
||||||
[statusItem_ setLength:title_width + kStatusItemLength];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSDictionary *)titleAttributes {
|
- (NSDictionary *)titleAttributes {
|
||||||
|
@ -103,12 +119,11 @@ const CGFloat kMargin = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)setTitle:(NSString*) title {
|
-(void)setTitle:(NSString*) title {
|
||||||
//title_= [title retain];
|
|
||||||
title_.reset([title copy]);
|
title_.reset([title copy]);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setMenuController:(AtomMenuController*)menu {
|
- (void)setMenuController:(AtomMenuController*)menu {
|
||||||
menu_controller_ = menu;
|
menuController_ = menu;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(void)mouseDown:(NSEvent *)event {
|
-(void)mouseDown:(NSEvent *)event {
|
||||||
|
@ -125,14 +140,14 @@ const CGFloat kMargin = 3;
|
||||||
}
|
}
|
||||||
inMouseEventSequence_ = NO;
|
inMouseEventSequence_ = NO;
|
||||||
if (event.clickCount == 1) {
|
if (event.clickCount == 1) {
|
||||||
if (menu_controller_) {
|
if (menuController_) {
|
||||||
[statusItem_ popUpStatusItemMenu:[menu_controller_ menu]];
|
[statusItem_ popUpStatusItemMenu:[menuController_ menu]];
|
||||||
}
|
}
|
||||||
|
|
||||||
trayIcon_->NotifyClicked([self getBoundsFromEvent:event]);
|
trayIcon_->NotifyClicked([self getBoundsFromEvent:event]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.clickCount == 2 && !menu_controller_) {
|
if (event.clickCount == 2 && !menuController_) {
|
||||||
trayIcon_->NotifyDoubleClicked();
|
trayIcon_->NotifyDoubleClicked();
|
||||||
}
|
}
|
||||||
[self setNeedsDisplay:YES];
|
[self setNeedsDisplay:YES];
|
||||||
|
@ -143,7 +158,7 @@ const CGFloat kMargin = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
-(BOOL) shouldHighlight {
|
-(BOOL) shouldHighlight {
|
||||||
BOOL is_menu_open = [menu_controller_ isMenuOpen];
|
BOOL is_menu_open = [menuController_ isMenuOpen];
|
||||||
return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open);
|
return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue