OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/toolbar/chevron_menu_button.h" | 5 #include "chrome/browser/ui/views/toolbar/chevron_menu_button.h" |
6 | 6 |
7 #include "base/memory/scoped_vector.h" | 7 #include "base/memory/scoped_vector.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
11 #include "chrome/browser/extensions/extension_action_icon_factory.h" | 11 #include "chrome/browser/extensions/extension_action_icon_factory.h" |
12 #include "chrome/browser/extensions/extension_context_menu_model.h" | 12 #include "chrome/browser/extensions/extension_context_menu_model.h" |
13 #include "chrome/browser/extensions/extension_toolbar_model.h" | 13 #include "chrome/browser/extensions/extension_toolbar_model.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" | 16 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" |
17 #include "chrome/browser/ui/views/extensions/extension_action_view_controller.h" | 17 #include "chrome/browser/ui/views/extensions/extension_action_view_controller.h" |
18 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" | |
19 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | 18 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
| 19 #include "chrome/browser/ui/views/toolbar/toolbar_action_view.h" |
20 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
21 #include "ui/views/border.h" | 21 #include "ui/views/border.h" |
22 #include "ui/views/controls/button/label_button_border.h" | 22 #include "ui/views/controls/button/label_button_border.h" |
23 #include "ui/views/controls/menu/menu_delegate.h" | 23 #include "ui/views/controls/menu/menu_delegate.h" |
24 #include "ui/views/controls/menu/menu_item_view.h" | 24 #include "ui/views/controls/menu/menu_item_view.h" |
25 #include "ui/views/controls/menu/menu_runner.h" | 25 #include "ui/views/controls/menu/menu_runner.h" |
26 #include "ui/views/metrics.h" | 26 #include "ui/views/metrics.h" |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // In the browser actions container's chevron menu, a menu item view's icon | 30 // In the browser actions container's chevron menu, a menu item view's icon |
31 // comes from BrowserActionView::GetIconWithBadge() when the menu item view is | 31 // comes from ToolbarActionView::GetIconWithBadge() when the menu item view is |
32 // created. But, the browser action's icon may not be loaded in time because it | 32 // created. But, the browser action's icon may not be loaded in time because it |
33 // is read from file system in another thread. | 33 // is read from file system in another thread. |
34 // The IconUpdater will update the menu item view's icon when the browser | 34 // The IconUpdater will update the menu item view's icon when the browser |
35 // action's icon has been updated. | 35 // action's icon has been updated. |
36 class IconUpdater : public ExtensionActionIconFactory::Observer { | 36 class IconUpdater : public ExtensionActionIconFactory::Observer { |
37 public: | 37 public: |
38 IconUpdater(views::MenuItemView* menu_item_view, | 38 IconUpdater(views::MenuItemView* menu_item_view, |
39 ExtensionActionViewController* view_controller) | 39 ExtensionActionViewController* view_controller) |
40 : menu_item_view_(menu_item_view), | 40 : menu_item_view_(menu_item_view), |
41 view_controller_(view_controller) { | 41 view_controller_(view_controller) { |
42 DCHECK(menu_item_view); | 42 DCHECK(menu_item_view); |
43 DCHECK(view_controller); | 43 DCHECK(view_controller); |
44 view_controller->set_icon_observer(this); | 44 view_controller->set_icon_observer(this); |
45 } | 45 } |
46 ~IconUpdater() override { view_controller_->set_icon_observer(NULL); } | 46 ~IconUpdater() override { view_controller_->set_icon_observer(NULL); } |
47 | 47 |
48 // BrowserActionView::IconObserver: | 48 // ExtensionActionIconFactory::Observer: |
49 void OnIconUpdated() override { | 49 void OnIconUpdated() override { |
50 menu_item_view_->SetIcon(view_controller_->GetIconWithBadge()); | 50 menu_item_view_->SetIcon(view_controller_->GetIconWithBadge()); |
51 } | 51 } |
52 | 52 |
53 private: | 53 private: |
54 // The menu item view whose icon might be updated. | 54 // The menu item view whose icon might be updated. |
55 views::MenuItemView* menu_item_view_; | 55 views::MenuItemView* menu_item_view_; |
56 | 56 |
57 // The view controller to be observed. When its icon changes, update the | 57 // The view controller to be observed. When its icon changes, update the |
58 // corresponding menu item view's icon. | 58 // corresponding menu item view's icon. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 116 |
117 // A pointer to the browser action container. | 117 // A pointer to the browser action container. |
118 BrowserActionsContainer* browser_actions_container_; | 118 BrowserActionsContainer* browser_actions_container_; |
119 | 119 |
120 // The overflow menu for the menu button. Owned by |menu_runner_|. | 120 // The overflow menu for the menu button. Owned by |menu_runner_|. |
121 views::MenuItemView* menu_; | 121 views::MenuItemView* menu_; |
122 | 122 |
123 // Resposible for running the menu. | 123 // Resposible for running the menu. |
124 scoped_ptr<views::MenuRunner> menu_runner_; | 124 scoped_ptr<views::MenuRunner> menu_runner_; |
125 | 125 |
126 // The index into the BrowserActionView vector, indicating where to start | 126 // The index into the ToolbarActionView vector, indicating where to start |
127 // picking browser actions to draw. | 127 // picking browser actions to draw. |
128 int start_index_; | 128 int start_index_; |
129 | 129 |
130 // Whether this controller is being used for drop. | 130 // Whether this controller is being used for drop. |
131 bool for_drop_; | 131 bool for_drop_; |
132 | 132 |
133 // The vector keeps all icon updaters associated with menu item views in the | 133 // The vector keeps all icon updaters associated with menu item views in the |
134 // controller. The icon updater will update the menu item view's icon when | 134 // controller. The icon updater will update the menu item view's icon when |
135 // the browser action view's icon has been updated. | 135 // the browser action view's icon has been updated. |
136 ScopedVector<IconUpdater> icon_updaters_; | 136 ScopedVector<IconUpdater> icon_updaters_; |
(...skipping 11 matching lines...) Expand all Loading... |
148 start_index_( | 148 start_index_( |
149 browser_actions_container_->VisibleBrowserActionsAfterAnimation()), | 149 browser_actions_container_->VisibleBrowserActionsAfterAnimation()), |
150 for_drop_(for_drop) { | 150 for_drop_(for_drop) { |
151 menu_ = new views::MenuItemView(this); | 151 menu_ = new views::MenuItemView(this); |
152 menu_runner_.reset(new views::MenuRunner( | 152 menu_runner_.reset(new views::MenuRunner( |
153 menu_, for_drop_ ? views::MenuRunner::FOR_DROP : 0)); | 153 menu_, for_drop_ ? views::MenuRunner::FOR_DROP : 0)); |
154 menu_->set_has_icons(true); | 154 menu_->set_has_icons(true); |
155 | 155 |
156 size_t command_id = 1; // Menu id 0 is reserved, start with 1. | 156 size_t command_id = 1; // Menu id 0 is reserved, start with 1. |
157 for (size_t i = start_index_; | 157 for (size_t i = start_index_; |
158 i < browser_actions_container_->num_browser_actions(); ++i) { | 158 i < browser_actions_container_->num_toolbar_actions(); ++i) { |
159 BrowserActionView* view = | 159 ToolbarActionView* view = |
160 browser_actions_container_->GetBrowserActionViewAt(i); | 160 browser_actions_container_->GetToolbarActionViewAt(i); |
161 ExtensionActionViewController* view_controller = | 161 ExtensionActionViewController* view_controller = |
162 static_cast<ExtensionActionViewController*>(view->view_controller()); | 162 static_cast<ExtensionActionViewController*>(view->view_controller()); |
163 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( | 163 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( |
164 command_id, | 164 command_id, |
165 base::UTF8ToUTF16(view_controller->extension()->name()), | 165 base::UTF8ToUTF16(view_controller->extension()->name()), |
166 view_controller->GetIconWithBadge()); | 166 view_controller->GetIconWithBadge()); |
167 | 167 |
168 // Set the tooltip for this item. | 168 // Set the tooltip for this item. |
169 menu_->SetTooltip( | 169 menu_->SetTooltip( |
170 view_controller->GetTooltip(view->GetCurrentWebContents()), | 170 view_controller->GetTooltip(view->GetCurrentWebContents()), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 base::Bind(&ChevronMenuButton::MenuDone, | 202 base::Bind(&ChevronMenuButton::MenuDone, |
203 owner_->weak_factory_.GetWeakPtr())); | 203 owner_->weak_factory_.GetWeakPtr())); |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 void ChevronMenuButton::MenuController::CloseMenu() { | 207 void ChevronMenuButton::MenuController::CloseMenu() { |
208 menu_->Cancel(); | 208 menu_->Cancel(); |
209 } | 209 } |
210 | 210 |
211 bool ChevronMenuButton::MenuController::IsCommandEnabled(int id) const { | 211 bool ChevronMenuButton::MenuController::IsCommandEnabled(int id) const { |
212 BrowserActionView* view = | 212 ToolbarActionView* view = |
213 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1); | 213 browser_actions_container_->GetToolbarActionViewAt(start_index_ + id - 1); |
214 return view->view_controller()->IsEnabled(view->GetCurrentWebContents()); | 214 return view->view_controller()->IsEnabled(view->GetCurrentWebContents()); |
215 } | 215 } |
216 | 216 |
217 void ChevronMenuButton::MenuController::ExecuteCommand(int id) { | 217 void ChevronMenuButton::MenuController::ExecuteCommand(int id) { |
218 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1)-> | 218 browser_actions_container_->GetToolbarActionViewAt(start_index_ + id - 1)-> |
219 view_controller()->ExecuteAction(true); | 219 view_controller()->ExecuteAction(true); |
220 } | 220 } |
221 | 221 |
222 bool ChevronMenuButton::MenuController::ShowContextMenu( | 222 bool ChevronMenuButton::MenuController::ShowContextMenu( |
223 views::MenuItemView* source, | 223 views::MenuItemView* source, |
224 int id, | 224 int id, |
225 const gfx::Point& p, | 225 const gfx::Point& p, |
226 ui::MenuSourceType source_type) { | 226 ui::MenuSourceType source_type) { |
227 BrowserActionView* view = browser_actions_container_->GetBrowserActionViewAt( | 227 ToolbarActionView* view = browser_actions_container_->GetToolbarActionViewAt( |
228 start_index_ + id - 1); | 228 start_index_ + id - 1); |
229 ExtensionActionViewController* view_controller = | 229 ExtensionActionViewController* view_controller = |
230 static_cast<ExtensionActionViewController*>(view->view_controller()); | 230 static_cast<ExtensionActionViewController*>(view->view_controller()); |
231 if (!view_controller->extension()->ShowConfigureContextMenus()) | 231 if (!view_controller->extension()->ShowConfigureContextMenus()) |
232 return false; | 232 return false; |
233 | 233 |
234 scoped_refptr<ExtensionContextMenuModel> context_menu_contents = | 234 scoped_refptr<ExtensionContextMenuModel> context_menu_contents = |
235 new ExtensionContextMenuModel(view_controller->extension(), | 235 new ExtensionContextMenuModel(view_controller->extension(), |
236 view->browser(), | 236 view->browser(), |
237 view_controller); | 237 view_controller); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 void ChevronMenuButton::ShowOverflowMenu(bool for_drop) { | 428 void ChevronMenuButton::ShowOverflowMenu(bool for_drop) { |
429 DCHECK(!menu_controller_); | 429 DCHECK(!menu_controller_); |
430 menu_controller_.reset(new MenuController( | 430 menu_controller_.reset(new MenuController( |
431 this, browser_actions_container_, for_drop)); | 431 this, browser_actions_container_, for_drop)); |
432 menu_controller_->RunMenu(GetWidget()); | 432 menu_controller_->RunMenu(GetWidget()); |
433 } | 433 } |
434 | 434 |
435 void ChevronMenuButton::MenuDone() { | 435 void ChevronMenuButton::MenuDone() { |
436 menu_controller_.reset(); | 436 menu_controller_.reset(); |
437 } | 437 } |
OLD | NEW |