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/app/chrome_command_ids.h" | 7 #include "chrome/app/chrome_command_ids.h" |
8 #include "chrome/browser/command_updater.h" | 8 #include "chrome/browser/command_updater.h" |
9 #include "chrome/browser/ui/browser.h" | |
10 #include "chrome/browser/ui/browser_commands.h" | |
9 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" | 11 #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" |
12 #import "chrome/browser/ui/cocoa/location_bar/autocomplete_text_field.h" | |
13 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | |
14 #import "chrome/browser/ui/cocoa/menu_controller.h" | |
10 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
11 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
12 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
18 #include "ui/base/models/simple_menu_model.h" | |
19 #include "ui/base/resource/resource_bundle.h" | |
13 | 20 |
14 PlusDecoration::PlusDecoration(CommandUpdater* command_updater) | 21 namespace { |
15 : command_updater_(command_updater) { | 22 // The offset to apply to the menu so that it clears the bottom border of the |
23 // omnibox. | |
24 const CGFloat kOmniboxYOffset = 7.0; | |
25 } // namespace | |
26 | |
27 PlusDecoration::PlusDecoration(LocationBarViewMac* owner, | |
28 CommandUpdater* command_updater, Browser* browser) | |
29 : owner_(owner), | |
30 command_updater_(command_updater), | |
31 browser_(browser) { | |
16 SetVisible(true); | 32 SetVisible(true); |
17 | 33 |
18 const int image_id = IDR_ACTION_BOX_BUTTON; | 34 const int image_id = IDR_ACTION_BOX_BUTTON; |
19 SetImage(OmniboxViewMac::ImageForResource(image_id)); | 35 SetImage(OmniboxViewMac::ImageForResource(image_id)); |
20 const int tip_id = IDS_TOOLTIP_ACTION_BOX_BUTTON; | 36 const int tip_id = IDS_TOOLTIP_ACTION_BOX_BUTTON; |
21 tooltip_.reset([l10n_util::GetNSStringWithFixup(tip_id) retain]); | 37 tooltip_.reset([l10n_util::GetNSStringWithFixup(tip_id) retain]); |
38 | |
39 if (!pop_up_cell_.get()) { | |
40 pop_up_cell_.reset([[NSPopUpButtonCell alloc] initTextCell:@"" | |
41 pullsDown:YES]); | |
42 } | |
43 DCHECK(pop_up_cell_.get()); | |
Scott Hess - ex-Googler
2012/08/09 22:59:10
Guess what! Now that the model and controller are
beaudoin
2012/08/10 15:56:10
Done.
| |
22 } | 44 } |
23 | 45 |
24 PlusDecoration::~PlusDecoration() { | 46 PlusDecoration::~PlusDecoration() { |
25 } | 47 } |
26 | 48 |
27 bool PlusDecoration::AcceptsMousePress() { | 49 bool PlusDecoration::AcceptsMousePress() { |
28 return true; | 50 return true; |
29 } | 51 } |
30 | 52 |
31 bool PlusDecoration::OnMousePressed(NSRect frame) { | 53 bool PlusDecoration::OnMousePressed(NSRect frame) { |
32 // TODO(macourteau): trigger the menu when caitkp@ and beaudoin@'s CL is | 54 ui::SimpleMenuModel menu_model(NULL); |
33 // ready. | 55 |
56 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
57 | |
58 // TODO(beaudoin): Use a platform-independent menu model once the Windows | |
59 // patch introducing it lands. See: http://codereview.chromium.org/10533086/ | |
60 menu_model.InsertItemWithStringIdAt(0, IDC_CHROME_TO_MOBILE_PAGE, | |
61 IDS_CHROME_TO_MOBILE); | |
62 menu_model.SetIcon(0, *rb.GetImageSkiaNamed(IDR_MOBILE)); | |
63 menu_model.InsertItemWithStringIdAt(1, IDC_BOOKMARK_PAGE, | |
64 IDS_BOOKMARK_STAR); | |
65 menu_model.SetIcon(1, *rb.GetImageSkiaNamed(IDR_STAR)); | |
66 | |
67 // Controller for the menu attached to the plus decoration. | |
68 scoped_nsobject<MenuController> menu_controller( | |
69 [[MenuController alloc] initWithModel:&menu_model | |
70 useWithPopUpButtonCell:YES]); | |
71 | |
72 NSMenu* menu = [menu_controller menu]; | |
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 | |
78 NSRect popUpFrame = [field bounds]; | |
79 popUpFrame.origin.x = NSMaxX([field bounds]) - menu.size.width; | |
80 popUpFrame.size.width = menu.size.width; | |
81 | |
82 // Attach the menu to a slightly higher box, to clear the omnibox border. | |
83 popUpFrame.size.height += kOmniboxYOffset; | |
84 | |
85 [pop_up_cell_ setMenu:menu]; | |
86 [pop_up_cell_ selectItem:nil]; | |
87 [pop_up_cell_ attachPopUpWithFrame:popUpFrame inView:field]; | |
88 [pop_up_cell_ performClickWithFrame:popUpFrame inView:field]; | |
34 return true; | 89 return true; |
35 } | 90 } |
36 | 91 |
37 NSString* PlusDecoration::GetToolTip() { | 92 NSString* PlusDecoration::GetToolTip() { |
38 return tooltip_.get(); | 93 return tooltip_.get(); |
39 } | 94 } |
OLD | NEW |