Index: chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.mm |
diff --git a/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.mm b/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.mm |
index c75fcd2a562bc4940fa8ae1d2cc022c02af4e2c6..58e3ecd0c764b2485ef04b17e0647db235e09796 100644 |
--- a/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.mm |
+++ b/chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.mm |
@@ -27,6 +27,12 @@ |
@interface AvatarMenuBubbleController (Private) |
- (AvatarMenuModel*)model; |
- (NSButton*)configureNewUserButton:(CGFloat)yOffset; |
+- (void)keyDown:(NSEvent *)theEvent; |
+- (void)moveDown:(id)sender; |
+- (void)moveUp:(id)sender; |
+- (void)insertNewline:(id)sender; |
+- (void)highlightNextItemByDelta:(int)delta; |
+- (void)highlightItem:(AvatarMenuItemController*)newItem; |
@end |
namespace AvatarMenuInternal { |
@@ -227,6 +233,70 @@ const CGFloat kLabelInset = 49.0; |
return items_.get(); |
} |
+- (void)keyDown:(NSEvent *)theEvent { |
+ [self interpretKeyEvents:[NSArray arrayWithObject:theEvent]]; |
+} |
+ |
+- (void)moveDown:(id)sender { |
+ [self highlightNextItemByDelta:-1]; |
+} |
+ |
+- (void)moveUp:(id)sender { |
+ [self highlightNextItemByDelta:1]; |
+} |
+ |
+- (void)insertNewline:(id)sender { |
+ for (AvatarMenuItemController* item in items_.get()) { |
+ if ([item isHighlighted]) { |
+ [self switchToProfile:item]; |
+ return; |
+ } |
+ } |
+} |
+ |
+- (void)highlightNextItemByDelta:(int)delta { |
Robert Sesek
2011/10/24 19:07:04
NSInteger
sail
2011/10/24 19:16:38
Done.
|
+ int count = [items_ count]; |
Robert Sesek
2011/10/24 19:07:04
NSUInteger
sail
2011/10/24 19:16:38
Done.
|
+ if (count == 0) |
+ return; |
+ |
+ int old_index = -1; |
Robert Sesek
2011/10/24 19:07:04
NSInteger
sail
2011/10/24 19:16:38
Done.
|
+ for (int i = 0; i < count; i++) { |
Robert Sesek
2011/10/24 19:07:04
NSUInteger
sail
2011/10/24 19:16:38
Done.
|
+ if ([[items_ objectAtIndex:i] isHighlighted]) { |
+ old_index = i; |
+ break; |
+ } |
+ } |
+ |
+ int new_index; |
+ // If nothing is selected then start at the top if we're going down and start |
+ // at the bottom if we're going up. |
+ if (old_index == -1) |
+ new_index = delta < 0 ? (count - 1) : 0; |
+ else |
+ new_index = old_index + delta; |
+ // Cap the index. We don't wrap around to match the behavior of Mac menus. |
+ new_index = std::min(std::max(0, new_index), count - 1); |
+ |
+ [self highlightItem:[items_ objectAtIndex:new_index]]; |
+} |
+ |
+- (void)highlightItem:(AvatarMenuItemController*)newItem { |
+ AvatarMenuItemController* oldItem = nil; |
Robert Sesek
2011/10/24 19:07:04
No need to initialize.
sail
2011/10/24 19:16:38
Are stack variables automatically initialized to n
Robert Sesek
2011/10/24 19:25:53
Oops, no. Only global scope variables and instance
|
+ for (AvatarMenuItemController* item in items_.get()) { |
+ if ([item isHighlighted]) { |
+ oldItem = item; |
+ break; |
+ } |
+ } |
+ |
+ if (oldItem == newItem) |
+ return; |
+ |
+ [oldItem setIsHighlighted:NO]; |
+ [newItem setIsHighlighted:YES]; |
+} |
+ |
+ |
@end |
// Menu Item Controller //////////////////////////////////////////////////////// |
@@ -238,6 +308,7 @@ const CGFloat kLabelInset = 49.0; |
@implementation AvatarMenuItemController |
@synthesize modelIndex = modelIndex_; |
+@synthesize isHighlighted = isHighlighted_; |
@synthesize iconView = iconView_; |
@synthesize activeView = activeView_; |
@synthesize nameField = nameField_; |
@@ -274,16 +345,13 @@ const CGFloat kLabelInset = 49.0; |
} |
- (void)highlightForEventType:(NSEventType)type { |
- BOOL active = !self.activeView.isHidden; |
switch (type) { |
case NSMouseEntered: |
- if (active) |
- [self animateFromView:self.emailField toView:self.editButton]; |
+ [controller_ highlightItem:self]; |
break; |
case NSMouseExited: |
- if (active) |
- [self animateFromView:self.editButton toView:self.emailField]; |
+ [controller_ highlightItem:nil]; |
break; |
default: |
@@ -291,6 +359,21 @@ const CGFloat kLabelInset = 49.0; |
}; |
} |
+- (void)setIsHighlighted:(BOOL)isHighlighted { |
+ if (isHighlighted_ == isHighlighted) |
+ return; |
+ |
+ isHighlighted_ = isHighlighted; |
+ [[self view] setNeedsDisplay:YES]; |
+ |
+ if (!self.activeView.isHidden) { |
+ if (isHighlighted_) |
+ [self animateFromView:self.emailField toView:self.editButton]; |
+ else |
+ [self animateFromView:self.editButton toView:self.emailField]; |
+ } |
+} |
+ |
- (void)animateFromView:(NSView*)outView toView:(NSView*)inView { |
const NSTimeInterval kAnimationDuration = 0.175; |
@@ -345,13 +428,11 @@ const CGFloat kLabelInset = 49.0; |
- (void)mouseEntered:(id)sender { |
[viewController_ highlightForEventType:[[NSApp currentEvent] type]]; |
- mouseInside_ = YES; |
[self setNeedsDisplay:YES]; |
} |
- (void)mouseExited:(id)sender { |
[viewController_ highlightForEventType:[[NSApp currentEvent] type]]; |
- mouseInside_ = NO; |
[self setNeedsDisplay:YES]; |
} |
@@ -361,7 +442,7 @@ const CGFloat kLabelInset = 49.0; |
- (void)drawRect:(NSRect)dirtyRect { |
NSColor* backgroundColor = nil; |
- if (mouseInside_) { |
+ if ([viewController_ isHighlighted]) { |
backgroundColor = [NSColor colorWithCalibratedRed:223.0/255 |
green:238.0/255 |
blue:246.0/255 |