Chromium Code Reviews| Index: chrome/browser/ui/cocoa/location_bar/action_box_menu_bubble_controller.mm |
| diff --git a/chrome/browser/ui/cocoa/location_bar/action_box_menu_bubble_controller.mm b/chrome/browser/ui/cocoa/location_bar/action_box_menu_bubble_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e92fa55c2a644792810463c4f4ac9e04067d4546 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/location_bar/action_box_menu_bubble_controller.mm |
| @@ -0,0 +1,387 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "chrome/browser/ui/cocoa/location_bar/action_box_menu_bubble_controller.h" |
| + |
| +#include "base/mac/bundle_locations.h" |
| +#include "base/mac/mac_util.h" |
| +#include "base/sys_string_conversions.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#import "chrome/browser/ui/cocoa/browser_window_utils.h" |
| +#import "chrome/browser/ui/cocoa/event_utils.h" |
| +#import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| +#import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| +#include "grit/generated_resources.h" |
| +#include "grit/theme_resources.h" |
| +#import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| +#include "ui/base/models/simple_menu_model.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| +#include "ui/gfx/image/image.h" |
| + |
| +@interface ActionBoxMenuBubbleController (Private) |
| +- (ui::MenuModel*)model; |
| +- (void)keyDown:(NSEvent*)theEvent; |
| +- (void)moveDown:(id)sender; |
| +- (void)moveUp:(id)sender; |
| +- (void)highlightNextItemByDelta:(NSInteger)delta; |
| +- (void)highlightItem:(ActionBoxMenuItemController*)newItem; |
| +@end |
| + |
| +namespace { |
| + |
| +// Some reasonable values for the menu geometry. |
| +const CGFloat kBubbleMinWidth = 175; |
| +const CGFloat kBubbleMaxWidth = 800; |
| + |
| +// Distance between the top/bottom of the bubble and the first/last menu item. |
| +const CGFloat kVerticalPadding = 7.0; |
| + |
| +// Minimum distance between the right of a menu item and the right border. |
| +const CGFloat kRightMargin = 20.0; |
| + |
| +} // namespace |
| + |
| +@implementation ActionBoxMenuBubbleController |
| + |
| +- (id)initWithBrowser:(Browser*)parentBrowser |
| + usingModel:(scoped_ptr<ui::MenuModel>)model |
| + anchoredAt:(NSPoint)point { |
| + if ((self = [self initWithModel:model.Pass() |
| + parentWindow:parentBrowser->window()->GetNativeWindow() |
| + anchoredAt:point])) { |
| + } |
| + return self; |
| +} |
| + |
| +- (ui::MenuModel*)model { |
| + return model_.get(); |
| +} |
| + |
| +- (IBAction)itemSelected:(id)sender { |
| + // Close the current window and activate the parent browser window, otherwise |
| + // the bookmark popup refuses to show. |
| + [self close]; |
| + [BrowserWindowUtils |
| + activateWindowForController:[[self parentWindow] windowController]]; |
| + size_t modelIndex = [sender modelIndex]; |
| + DCHECK(model_.get()); |
| + int event_flags = event_utils::EventFlagsFromNSEvent([NSApp currentEvent]); |
| + model_->ActivatedAt(modelIndex, event_flags); |
| +} |
| + |
| +// Private ///////////////////////////////////////////////////////////////////// |
| + |
| +- (id)initWithModel:(scoped_ptr<ui::MenuModel>)model |
| + parentWindow:(NSWindow*)parent |
| + anchoredAt:(NSPoint)point { |
| + // Use an arbitrary height because it will reflect the size of the content. |
| + NSRect contentRect = NSMakeRect(0, 0, kBubbleMinWidth, 150); |
| + // Create an empty window into which content is placed. |
| + scoped_nsobject<InfoBubbleWindow> window( |
| + [[InfoBubbleWindow alloc] initWithContentRect:contentRect |
| + styleMask:NSBorderlessWindowMask |
| + backing:NSBackingStoreBuffered |
| + defer:NO]); |
| + if ((self = [super initWithWindow:window |
| + parentWindow:parent |
| + anchoredAt:point])) { |
| + model_.reset(model.release()); |
| + |
| + [[self bubble] setAlignment:info_bubble::kAlignRightEdgeToAnchorEdge]; |
| + [[self bubble] setArrowLocation:info_bubble::kNoArrow]; |
| + [[self bubble] setBackgroundColor: |
| + [NSColor colorWithDeviceRed:(251.0f/255.0f) |
| + green:(251.0f/255.0f) |
| + blue:(251.0f/255.0f) |
| + alpha:1.0]]; |
| + [self performLayout]; |
| + } |
| + return self; |
| +} |
| + |
| +- (void)performLayout { |
| + NSView* contentView = [[self window] contentView]; |
| + |
| + // Reset the array of controllers and remove all the views. |
| + items_.reset([[NSMutableArray alloc] init]); |
| + [contentView setSubviews:[NSArray array]]; |
| + |
| + // Leave some space at the bottom of the menu. |
| + CGFloat yOffset = kVerticalPadding; |
| + |
| + // Loop over the items in reverse, constructing the menu items. |
| + CGFloat width = kBubbleMinWidth; |
| + for (int i = model_->GetItemCount() - 1; i >= 0; --i) { |
| + // Create the item controller. Autorelease it because it will be owned |
| + // by the |items_| array. |
| + ActionBoxMenuItemController* itemController = |
| + [[[ActionBoxMenuItemController alloc] initWithModelIndex:i |
| + menuController:self] autorelease]; |
| + |
| + // Adjust the name field to fit the string. |
| + [GTMUILocalizerAndLayoutTweaker sizeToFitView:itemController.nameField]; |
| + |
| + // Expand the size of the window if required to fit the menu item. |
| + width = std::max(width, |
| + NSMaxX([itemController.nameField frame]) + kRightMargin); |
| + |
| + // Add the item to the content view. |
| + [[itemController view] setFrameOrigin:NSMakePoint(0, yOffset)]; |
| + [contentView addSubview:[itemController view]]; |
| + yOffset += NSHeight([[itemController view] frame]); |
| + |
| + // Keep track of the view controller. |
| + [items_ addObject:itemController]; |
| + } |
| + |
| + // Leave some space at the top of the menu. |
| + yOffset += kVerticalPadding; |
| + |
| + // Set the window frame, clamping the width at a sensible max. |
| + NSRect frame = [[self window] frame]; |
| + frame.size.height = yOffset; |
| + frame.size.width = std::min(width, kBubbleMaxWidth); |
| + [[self window] setFrame:frame display:YES]; |
| +} |
| + |
| +- (NSMutableArray*)items { |
| + 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)highlightNextItemByDelta:(NSInteger)delta { |
| + NSUInteger count = [items_ count]; |
| + if (count == 0) |
| + return; |
| + |
| + NSInteger old_index = -1; |
| + for (NSUInteger i = 0; i < count; ++i) { |
| + if ([[items_ objectAtIndex:i] isHighlighted]) { |
| + old_index = i; |
| + break; |
| + } |
| + } |
| + |
| + NSInteger 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(static_cast<NSInteger>(0), new_index), |
| + static_cast<NSInteger>(count - 1)); |
| + |
| + [self highlightItem:[items_ objectAtIndex:new_index]]; |
| +} |
| + |
| +- (void)highlightItem:(ActionBoxMenuItemController*)newItem { |
| + ActionBoxMenuItemController* oldItem = nil; |
| + for (ActionBoxMenuItemController* item in items_.get()) { |
| + if ([item isHighlighted]) { |
| + oldItem = item; |
| + break; |
| + } |
| + } |
| + |
| + if (oldItem == newItem) |
| + return; |
| + |
| + [oldItem setIsHighlighted:NO]; |
| + [newItem setIsHighlighted:YES]; |
| +} |
| + |
| +@end |
| + |
| +// Menu Item Controller //////////////////////////////////////////////////////// |
| + |
| +@implementation ActionBoxMenuItemController |
| + |
| +@synthesize modelIndex = modelIndex_; |
| +@synthesize isHighlighted = isHighlighted_; |
| +@synthesize iconView = iconView_; |
| +@synthesize nameField = nameField_; |
| +@synthesize controller = controller_; |
| + |
| +- (id)initWithModelIndex:(size_t)modelIndex |
| + menuController:(ActionBoxMenuBubbleController*)controller { |
| + if ((self = [super initWithNibName:@"ActionBoxMenuItem" |
| + bundle:base::mac::FrameworkBundle()])) { |
| + modelIndex_ = modelIndex; |
| + controller_ = controller; |
| + |
| + [self loadView]; |
| + |
| + gfx::Image icon = gfx::Image(); |
| + |
| + if ([controller model]->GetIconAt(modelIndex_, &icon)) |
| + self.iconView.image = icon.ToNSImage(); |
| + else |
| + self.iconView.image = nil; |
| + |
| + [controller model]->GetIconAt(modelIndex_, &icon); |
| + |
| + self.iconView.image = icon.ToNSImage(); |
| + self.nameField.stringValue = base::SysUTF16ToNSString( |
| + controller.model->GetLabelAt(modelIndex_)); |
| + } |
| + return self; |
| +} |
| + |
| +- (void)dealloc { |
| + static_cast<ActionBoxMenuItemView*>(self.view).viewController = nil; |
| + [super dealloc]; |
| +} |
| + |
| +- (void)highlightForEventType:(NSEventType)type { |
| + switch (type) { |
| + case NSMouseEntered: |
| + [controller_ highlightItem:self]; |
| + break; |
| + |
| + case NSMouseExited: |
| + [controller_ highlightItem:nil]; |
| + break; |
| + |
| + default: |
| + NOTREACHED(); |
| + }; |
| +} |
| + |
| +- (IBAction)itemSelected:(id)sender { |
| + [controller_ itemSelected:self]; |
| +} |
| + |
| +- (void)setIsHighlighted:(BOOL)isHighlighted { |
| + if (isHighlighted_ == isHighlighted) |
| + return; |
| + |
| + isHighlighted_ = isHighlighted; |
| + [[self view] setNeedsDisplay:YES]; |
| +} |
| + |
| +@end |
| + |
| +// Items from the action box menu ////////////////////////////////////////////// |
| + |
| +@implementation ActionBoxMenuItemView |
| + |
| +@synthesize viewController = viewController_; |
| + |
| +- (void)awakeFromNib { |
| + [self updateTrackingAreas]; |
|
Scott Hess - ex-Googler
2012/10/12 23:02:47
You sure you need this? At this point, I wouldn't
beaudoin
2012/10/13 00:20:55
Done.
|
| +} |
| + |
| +- (void)updateTrackingAreas { |
| + if (trackingArea_.get()) |
| + [self removeTrackingArea:trackingArea_.get()]; |
| + |
| + trackingArea_.reset( |
| + [[CrTrackingArea alloc] initWithRect:[self bounds] |
| + options:NSTrackingMouseEnteredAndExited | |
| + NSTrackingActiveInKeyWindow |
| + owner:self |
| + userInfo:nil]); |
| + [self addTrackingArea:trackingArea_.get()]; |
| + |
| + [super updateTrackingAreas]; |
| +} |
| + |
| +- (BOOL)acceptsFirstResponder { |
| + return YES; |
| +} |
| + |
| +- (void)mouseDown:(NSEvent*)theEvent { |
| +} |
| + |
| +- (void)mouseUp:(NSEvent*)theEvent { |
| + [viewController_ itemSelected:self]; |
| +} |
| + |
| +- (void)mouseEntered:(id)sender { |
| + [viewController_ highlightForEventType:[[NSApp currentEvent] type]]; |
| + [self setNeedsDisplay:YES]; |
| +} |
| + |
| +- (void)mouseExited:(id)sender { |
| + [viewController_ highlightForEventType:[[NSApp currentEvent] type]]; |
| + [self setNeedsDisplay:YES]; |
| +} |
| + |
| +- (void)drawRect:(NSRect)dirtyRect { |
| + NSColor* backgroundColor = nil; |
| + if ([viewController_ isHighlighted]) { |
| + backgroundColor = [NSColor colorWithDeviceRed:0.0 |
| + green:0.0 |
| + blue:0.0 |
| + alpha:0.06]; |
|
Scott Hess - ex-Googler
2012/10/12 23:02:47
Could probably get by with +colorWithDeviceWhite:0
beaudoin
2012/10/13 00:20:55
As in: The UI designer said 6%? :)
Done.
|
| + } else { |
| + backgroundColor = [NSColor clearColor]; |
| + } |
| + |
| + [backgroundColor set]; |
| + [NSBezierPath fillRect:[self bounds]]; |
| +} |
| + |
| +// Make sure the element is focusable for accessibility. |
| +- (BOOL)canBecomeKeyView { |
| + return YES; |
| +} |
| + |
| +- (BOOL)accessibilityIsIgnored { |
| + return NO; |
| +} |
| + |
| +- (NSArray*)accessibilityAttributeNames { |
| + NSMutableArray* attributes = |
| + [[super accessibilityAttributeNames] mutableCopy]; |
|
Scott Hess - ex-Googler
2012/10/12 23:02:47
I would pull the -autorelease right into here so t
beaudoin
2012/10/13 00:20:55
Done.
|
| + [attributes addObject:NSAccessibilityTitleAttribute]; |
| + [attributes addObject:NSAccessibilityEnabledAttribute]; |
| + |
| + return [attributes autorelease]; |
| +} |
| + |
| +- (NSArray*)accessibilityActionNames { |
| + NSArray* parentActions = [super accessibilityActionNames]; |
| + return [parentActions arrayByAddingObject:NSAccessibilityPressAction]; |
| +} |
| + |
| +- (id)accessibilityAttributeValue:(NSString*)attribute { |
| + if ([attribute isEqual:NSAccessibilityRoleAttribute]) |
| + return NSAccessibilityButtonRole; |
| + |
| + if ([attribute isEqual:NSAccessibilityRoleDescriptionAttribute]) |
| + return NSAccessibilityRoleDescription(NSAccessibilityButtonRole, nil); |
| + |
| + if ([attribute isEqual:NSAccessibilityEnabledAttribute]) |
| + return [NSNumber numberWithBool:YES]; |
| + |
| + return [super accessibilityAttributeValue:attribute]; |
| +} |
| + |
| +- (void)accessibilityPerformAction:(NSString*)action { |
| + if ([action isEqual:NSAccessibilityPressAction]) { |
| + [viewController_ itemSelected:self]; |
| + return; |
| + } |
| + |
| + [super accessibilityPerformAction:action]; |
| +} |
| + |
| +@end |