| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/location_bar/plus_decoration.h" | 5 #import "chrome/browser/ui/cocoa/location_bar/plus_decoration.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/browser.h" | 7 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/browser/ui/browser_commands.h" | 8 #include "chrome/browser/ui/browser_window.h" |
| 9 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 9 #import "chrome/browser/ui/cocoa/location_bar/action_box_menu_bubble_controller.
h" |
| 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" | 10 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" |
| 11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | 11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| 12 #import "chrome/browser/ui/cocoa/menu_controller.h" | 12 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
| 13 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
| 14 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
| 15 #include "ui/base/l10n/l10n_util_mac.h" | 15 #include "ui/base/l10n/l10n_util_mac.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 // The offset to apply to the menu so that it clears the bottom border of the | 18 // The offset to apply to the menu so that it appears just attached to the |
| 19 // omnibox. | 19 // right-hand side of the omnibox while slightly overlapping vertically. |
| 20 const CGFloat kAnchorPointYOffset = 4.0; | 20 const CGFloat kAnchorPointXOffset = 1.0; |
| 21 const CGFloat kAnchorPointFrameHeight = 23.0; | 21 const CGFloat kAnchorPointYOffset = 2.0; |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 PlusDecoration::PlusDecoration(LocationBarViewMac* owner, | 24 PlusDecoration::PlusDecoration(LocationBarViewMac* owner, |
| 25 Browser* browser) | 25 Browser* browser) |
| 26 : owner_(owner), | 26 : owner_(owner), |
| 27 browser_(browser), | 27 browser_(browser), |
| 28 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(browser, this)) { | 28 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(browser, this)) { |
| 29 SetVisible(true); | 29 SetVisible(true); |
| 30 ResetIcon(); | 30 ResetIcon(); |
| 31 } | 31 } |
| 32 | 32 |
| 33 PlusDecoration::~PlusDecoration() { | 33 PlusDecoration::~PlusDecoration() { |
| 34 } | 34 } |
| 35 | 35 |
| 36 NSPoint PlusDecoration::GetActionBoxAnchorPoint() { | 36 NSPoint PlusDecoration::GetActionBoxAnchorPoint() { |
| 37 AutocompleteTextField* field = owner_->GetAutocompleteTextField(); | 37 AutocompleteTextField* field = owner_->GetAutocompleteTextField(); |
| 38 NSRect bounds = [field bounds]; | 38 NSRect bounds = [field bounds]; |
| 39 return NSMakePoint(NSMaxX(bounds), NSMaxY(bounds)); | 39 NSPoint anchor = NSMakePoint(NSMaxX(bounds) - kAnchorPointXOffset, |
| 40 NSMaxY(bounds) - kAnchorPointYOffset); |
| 41 return [field convertPoint:anchor toView:nil]; |
| 40 } | 42 } |
| 41 | 43 |
| 42 void PlusDecoration::ResetIcon() { | 44 void PlusDecoration::ResetIcon() { |
| 43 SetIcons( | 45 SetIcons( |
| 44 IDR_ACTION_BOX_BUTTON_NORMAL, | 46 IDR_ACTION_BOX_BUTTON_NORMAL, |
| 45 IDR_ACTION_BOX_BUTTON_HOVER, | 47 IDR_ACTION_BOX_BUTTON_HOVER, |
| 46 IDR_ACTION_BOX_BUTTON_PUSHED); | 48 IDR_ACTION_BOX_BUTTON_PUSHED); |
| 47 } | 49 } |
| 48 | 50 |
| 49 void PlusDecoration::SetTemporaryIcon(int image_id) { | 51 void PlusDecoration::SetTemporaryIcon(int image_id) { |
| 50 SetIcons(image_id, image_id, image_id); | 52 SetIcons(image_id, image_id, image_id); |
| 51 } | 53 } |
| 52 | 54 |
| 53 bool PlusDecoration::AcceptsMousePress() { | 55 bool PlusDecoration::AcceptsMousePress() { |
| 54 return true; | 56 return true; |
| 55 } | 57 } |
| 56 | 58 |
| 57 bool PlusDecoration::OnMousePressed(NSRect frame) { | 59 bool PlusDecoration::OnMousePressed(NSRect frame) { |
| 58 controller_.OnButtonClicked(); | 60 controller_.OnButtonClicked(); |
| 59 return true; | 61 return true; |
| 60 } | 62 } |
| 61 | 63 |
| 62 NSString* PlusDecoration::GetToolTip() { | 64 NSString* PlusDecoration::GetToolTip() { |
| 63 return l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_ACTION_BOX_BUTTON); | 65 return l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_ACTION_BOX_BUTTON); |
| 64 } | 66 } |
| 65 | 67 |
| 66 void PlusDecoration::ShowMenu(scoped_ptr<ActionBoxMenuModel> menu_model) { | 68 void PlusDecoration::ShowMenu(scoped_ptr<ActionBoxMenuModel> menu_model) { |
| 67 // Controller for the menu attached to the plus decoration. | 69 // Controller for the menu attached to the plus decoration. |
| 68 scoped_nsobject<MenuController> menu_controller( | 70 // |menu_controller| will automatically release itself on close. |
| 69 [[MenuController alloc] initWithModel:menu_model.get() | 71 NSWindow* parent = browser_->window()->GetNativeWindow(); |
| 70 useWithPopUpButtonCell:YES]); | 72 ActionBoxMenuBubbleController* menu_controller = |
| 73 [[ActionBoxMenuBubbleController alloc] |
| 74 initWithModel:menu_model.PassAs<ui::MenuModel>() |
| 75 parentWindow:parent |
| 76 anchoredAt:[parent convertBaseToScreen:GetActionBoxAnchorPoint()]]; |
| 71 | 77 |
| 72 NSMenu* menu = [menu_controller menu]; | 78 [menu_controller showWindow:nil]; |
| 73 | |
| 74 // Align the menu popup to that its top-right corner matches the bottom-right | |
| 75 // corner of the omnibox. | |
| 76 AutocompleteTextField* field = owner_->GetAutocompleteTextField(); | |
| 77 NSPoint point = GetActionBoxAnchorPoint(); | |
| 78 NSRect popUpFrame = NSMakeRect(point.x - menu.size.width, | |
| 79 kAnchorPointYOffset, menu.size.width, kAnchorPointFrameHeight); | |
| 80 scoped_nsobject<NSPopUpButtonCell> pop_up_cell( | |
| 81 [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:YES]); | |
| 82 DCHECK(pop_up_cell.get()); | |
| 83 | |
| 84 [pop_up_cell setMenu:menu]; | |
| 85 [pop_up_cell selectItem:nil]; | |
| 86 [pop_up_cell attachPopUpWithFrame:popUpFrame inView:field]; | |
| 87 [pop_up_cell performClickWithFrame:popUpFrame inView:field]; | |
| 88 } | 79 } |
| 89 | 80 |
| 90 void PlusDecoration::SetIcons(int normal_id, int hover_id, int pressed_id) { | 81 void PlusDecoration::SetIcons(int normal_id, int hover_id, int pressed_id) { |
| 91 SetNormalImage(OmniboxViewMac::ImageForResource(normal_id)); | 82 SetNormalImage(OmniboxViewMac::ImageForResource(normal_id)); |
| 92 SetHoverImage(OmniboxViewMac::ImageForResource(hover_id)); | 83 SetHoverImage(OmniboxViewMac::ImageForResource(hover_id)); |
| 93 SetPressedImage(OmniboxViewMac::ImageForResource(pressed_id)); | 84 SetPressedImage(OmniboxViewMac::ImageForResource(pressed_id)); |
| 94 } | 85 } |
| OLD | NEW |