OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h" | 5 #include "chrome/browser/ui/views/location_bar/page_action_image_view.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/extension_browser_event_router.h" | 8 #include "chrome/browser/extensions/extension_browser_event_router.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/platform_util.h" | 10 #include "chrome/browser/platform_util.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/browser_list.h" | 12 #include "chrome/browser/ui/browser_list.h" |
13 #include "chrome/browser/ui/views/frame/browser_view.h" | 13 #include "chrome/browser/ui/views/frame/browser_view.h" |
14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
15 #include "chrome/common/extensions/extension_action.h" | 15 #include "chrome/common/extensions/extension_action.h" |
16 #include "chrome/common/extensions/extension_resource.h" | 16 #include "chrome/common/extensions/extension_resource.h" |
17 #include "ui/base/accessibility/accessible_view_state.h" | 17 #include "ui/base/accessibility/accessible_view_state.h" |
18 #include "views/controls/menu/menu_2.h" | 18 #include "views/controls/menu/menu_item_view.h" |
| 19 #include "views/controls/menu/menu_model_adapter.h" |
19 | 20 |
20 PageActionImageView::PageActionImageView(LocationBarView* owner, | 21 PageActionImageView::PageActionImageView(LocationBarView* owner, |
21 Profile* profile, | 22 Profile* profile, |
22 ExtensionAction* page_action) | 23 ExtensionAction* page_action) |
23 : owner_(owner), | 24 : owner_(owner), |
24 profile_(profile), | 25 profile_(profile), |
25 page_action_(page_action), | 26 page_action_(page_action), |
26 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 27 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
27 current_tab_id_(-1), | 28 current_tab_id_(-1), |
28 preview_enabled_(false), | 29 preview_enabled_(false), |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 void PageActionImageView::OnMouseReleased(const views::MouseEvent& event) { | 117 void PageActionImageView::OnMouseReleased(const views::MouseEvent& event) { |
117 if (!HitTest(event.location())) | 118 if (!HitTest(event.location())) |
118 return; | 119 return; |
119 | 120 |
120 int button = -1; | 121 int button = -1; |
121 if (event.IsLeftMouseButton()) { | 122 if (event.IsLeftMouseButton()) { |
122 button = 1; | 123 button = 1; |
123 } else if (event.IsMiddleMouseButton()) { | 124 } else if (event.IsMiddleMouseButton()) { |
124 button = 2; | 125 button = 2; |
125 } else if (event.IsRightMouseButton()) { | 126 } else if (event.IsRightMouseButton()) { |
126 // Get the top left point of this button in screen coordinates. | 127 ShowContextMenu(gfx::Point(), true); |
127 gfx::Point menu_origin; | |
128 ConvertPointToScreen(this, &menu_origin); | |
129 // Make the menu appear below the button. | |
130 menu_origin.Offset(0, height()); | |
131 ShowContextMenu(menu_origin, true); | |
132 return; | 128 return; |
133 } | 129 } |
134 | 130 |
135 ExecuteAction(button, false); // inspect_with_devtools | 131 ExecuteAction(button, false); // inspect_with_devtools |
136 } | 132 } |
137 | 133 |
138 bool PageActionImageView::OnKeyPressed(const views::KeyEvent& event) { | 134 bool PageActionImageView::OnKeyPressed(const views::KeyEvent& event) { |
139 if (event.key_code() == ui::VKEY_SPACE || | 135 if (event.key_code() == ui::VKEY_SPACE || |
140 event.key_code() == ui::VKEY_RETURN) { | 136 event.key_code() == ui::VKEY_RETURN) { |
141 ExecuteAction(1, false); | 137 ExecuteAction(1, false); |
142 return true; | 138 return true; |
143 } | 139 } |
144 return false; | 140 return false; |
145 } | 141 } |
146 | 142 |
147 void PageActionImageView::ShowContextMenu(const gfx::Point& p, | 143 void PageActionImageView::ShowContextMenu(const gfx::Point& p, |
148 bool is_mouse_gesture) { | 144 bool is_mouse_gesture) { |
149 const Extension* extension = profile_->GetExtensionService()-> | 145 const Extension* extension = profile_->GetExtensionService()-> |
150 GetExtensionById(page_action()->extension_id(), false); | 146 GetExtensionById(page_action()->extension_id(), false); |
151 if (!extension->ShowConfigureContextMenus()) | 147 if (!extension->ShowConfigureContextMenus()) |
152 return; | 148 return; |
153 | 149 |
154 Browser* browser = BrowserView::GetBrowserViewForNativeWindow( | 150 Browser* browser = BrowserView::GetBrowserViewForNativeWindow( |
155 platform_util::GetTopLevel(GetWidget()->GetNativeView()))->browser(); | 151 platform_util::GetTopLevel(GetWidget()->GetNativeView()))->browser(); |
156 context_menu_contents_ = | 152 |
157 new ExtensionContextMenuModel(extension, browser, this); | 153 scoped_refptr<ExtensionContextMenuModel> context_menu_model( |
158 context_menu_menu_.reset(new views::Menu2(context_menu_contents_.get())); | 154 new ExtensionContextMenuModel(extension, browser, this)); |
159 context_menu_menu_->RunContextMenuAt(p); | 155 views::MenuModelAdapter menu_model_adapter(context_menu_model.get()); |
| 156 views::MenuItemView menu(&menu_model_adapter); |
| 157 menu_model_adapter.BuildMenu(&menu); |
| 158 |
| 159 gfx::Point screen_loc; |
| 160 views::View::ConvertPointToScreen(this, &screen_loc); |
| 161 menu.RunMenuAt(GetWidget()->GetNativeWindow(), NULL, |
| 162 gfx::Rect(screen_loc ,size()), views::MenuItemView::TOPLEFT, true); |
160 } | 163 } |
161 | 164 |
162 void PageActionImageView::OnImageLoaded( | 165 void PageActionImageView::OnImageLoaded( |
163 SkBitmap* image, const ExtensionResource& resource, int index) { | 166 SkBitmap* image, const ExtensionResource& resource, int index) { |
164 // We loaded icons()->size() icons, plus one extra if the page action had | 167 // We loaded icons()->size() icons, plus one extra if the page action had |
165 // a default icon. | 168 // a default icon. |
166 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); | 169 int total_icons = static_cast<int>(page_action_->icon_paths()->size()); |
167 if (!page_action_->default_icon_path().empty()) | 170 if (!page_action_->default_icon_path().empty()) |
168 total_icons++; | 171 total_icons++; |
169 DCHECK(index < total_icons); | 172 DCHECK(index < total_icons); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 void PageActionImageView::ExtensionPopupIsClosing(ExtensionPopup* popup) { | 240 void PageActionImageView::ExtensionPopupIsClosing(ExtensionPopup* popup) { |
238 DCHECK_EQ(popup_, popup); | 241 DCHECK_EQ(popup_, popup); |
239 // ExtensionPopup is ref-counted, so we don't need to delete it. | 242 // ExtensionPopup is ref-counted, so we don't need to delete it. |
240 popup_ = NULL; | 243 popup_ = NULL; |
241 } | 244 } |
242 | 245 |
243 void PageActionImageView::HidePopup() { | 246 void PageActionImageView::HidePopup() { |
244 if (popup_) | 247 if (popup_) |
245 popup_->Close(); | 248 popup_->Close(); |
246 } | 249 } |
OLD | NEW |