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 virtual ~IconUpdater() { | 46 virtual ~IconUpdater() { |
47 view_controller_->set_icon_observer(NULL); | 47 view_controller_->set_icon_observer(NULL); |
48 } | 48 } |
49 | 49 |
50 // BrowserActionView::IconObserver: | 50 // ExtensionActionIconFactory::Observer: |
51 virtual void OnIconUpdated() override { | 51 virtual void OnIconUpdated() override { |
52 menu_item_view_->SetIcon(view_controller_->GetIconWithBadge()); | 52 menu_item_view_->SetIcon(view_controller_->GetIconWithBadge()); |
53 } | 53 } |
54 | 54 |
55 private: | 55 private: |
56 // The menu item view whose icon might be updated. | 56 // The menu item view whose icon might be updated. |
57 views::MenuItemView* menu_item_view_; | 57 views::MenuItemView* menu_item_view_; |
58 | 58 |
59 // The view controller to be observed. When its icon changes, update the | 59 // The view controller to be observed. When its icon changes, update the |
60 // corresponding menu item view's icon. | 60 // corresponding menu item view's icon. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 118 |
119 // A pointer to the browser action container. | 119 // A pointer to the browser action container. |
120 BrowserActionsContainer* browser_actions_container_; | 120 BrowserActionsContainer* browser_actions_container_; |
121 | 121 |
122 // The overflow menu for the menu button. Owned by |menu_runner_|. | 122 // The overflow menu for the menu button. Owned by |menu_runner_|. |
123 views::MenuItemView* menu_; | 123 views::MenuItemView* menu_; |
124 | 124 |
125 // Resposible for running the menu. | 125 // Resposible for running the menu. |
126 scoped_ptr<views::MenuRunner> menu_runner_; | 126 scoped_ptr<views::MenuRunner> menu_runner_; |
127 | 127 |
128 // The index into the BrowserActionView vector, indicating where to start | 128 // The index into the ToolbarActionView vector, indicating where to start |
129 // picking browser actions to draw. | 129 // picking browser actions to draw. |
130 int start_index_; | 130 int start_index_; |
131 | 131 |
132 // Whether this controller is being used for drop. | 132 // Whether this controller is being used for drop. |
133 bool for_drop_; | 133 bool for_drop_; |
134 | 134 |
135 // The vector keeps all icon updaters associated with menu item views in the | 135 // The vector keeps all icon updaters associated with menu item views in the |
136 // controller. The icon updater will update the menu item view's icon when | 136 // controller. The icon updater will update the menu item view's icon when |
137 // the browser action view's icon has been updated. | 137 // the browser action view's icon has been updated. |
138 ScopedVector<IconUpdater> icon_updaters_; | 138 ScopedVector<IconUpdater> icon_updaters_; |
(...skipping 11 matching lines...) Expand all Loading... |
150 start_index_( | 150 start_index_( |
151 browser_actions_container_->VisibleBrowserActionsAfterAnimation()), | 151 browser_actions_container_->VisibleBrowserActionsAfterAnimation()), |
152 for_drop_(for_drop) { | 152 for_drop_(for_drop) { |
153 menu_ = new views::MenuItemView(this); | 153 menu_ = new views::MenuItemView(this); |
154 menu_runner_.reset(new views::MenuRunner( | 154 menu_runner_.reset(new views::MenuRunner( |
155 menu_, for_drop_ ? views::MenuRunner::FOR_DROP : 0)); | 155 menu_, for_drop_ ? views::MenuRunner::FOR_DROP : 0)); |
156 menu_->set_has_icons(true); | 156 menu_->set_has_icons(true); |
157 | 157 |
158 size_t command_id = 1; // Menu id 0 is reserved, start with 1. | 158 size_t command_id = 1; // Menu id 0 is reserved, start with 1. |
159 for (size_t i = start_index_; | 159 for (size_t i = start_index_; |
160 i < browser_actions_container_->num_browser_actions(); ++i) { | 160 i < browser_actions_container_->num_toolbar_actions(); ++i) { |
161 BrowserActionView* view = | 161 ToolbarActionView* view = |
162 browser_actions_container_->GetBrowserActionViewAt(i); | 162 browser_actions_container_->GetToolbarActionViewAt(i); |
163 ExtensionActionViewController* view_controller = | 163 ExtensionActionViewController* view_controller = |
164 static_cast<ExtensionActionViewController*>(view->view_controller()); | 164 static_cast<ExtensionActionViewController*>(view->view_controller()); |
165 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( | 165 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( |
166 command_id, | 166 command_id, |
167 base::UTF8ToUTF16(view_controller->extension()->name()), | 167 base::UTF8ToUTF16(view_controller->extension()->name()), |
168 view_controller->GetIconWithBadge()); | 168 view_controller->GetIconWithBadge()); |
169 | 169 |
170 // Set the tooltip for this item. | 170 // Set the tooltip for this item. |
171 menu_->SetTooltip( | 171 menu_->SetTooltip( |
172 view_controller->GetTooltip(view->GetCurrentWebContents()), | 172 view_controller->GetTooltip(view->GetCurrentWebContents()), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 base::Bind(&ChevronMenuButton::MenuDone, | 204 base::Bind(&ChevronMenuButton::MenuDone, |
205 owner_->weak_factory_.GetWeakPtr())); | 205 owner_->weak_factory_.GetWeakPtr())); |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 void ChevronMenuButton::MenuController::CloseMenu() { | 209 void ChevronMenuButton::MenuController::CloseMenu() { |
210 menu_->Cancel(); | 210 menu_->Cancel(); |
211 } | 211 } |
212 | 212 |
213 bool ChevronMenuButton::MenuController::IsCommandEnabled(int id) const { | 213 bool ChevronMenuButton::MenuController::IsCommandEnabled(int id) const { |
214 BrowserActionView* view = | 214 ToolbarActionView* view = |
215 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1); | 215 browser_actions_container_->GetToolbarActionViewAt(start_index_ + id - 1); |
216 return view->view_controller()->IsEnabled(view->GetCurrentWebContents()); | 216 return view->view_controller()->IsEnabled(view->GetCurrentWebContents()); |
217 } | 217 } |
218 | 218 |
219 void ChevronMenuButton::MenuController::ExecuteCommand(int id) { | 219 void ChevronMenuButton::MenuController::ExecuteCommand(int id) { |
220 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1)-> | 220 browser_actions_container_->GetToolbarActionViewAt(start_index_ + id - 1)-> |
221 view_controller()->ExecuteAction(true); | 221 view_controller()->ExecuteAction(true); |
222 } | 222 } |
223 | 223 |
224 bool ChevronMenuButton::MenuController::ShowContextMenu( | 224 bool ChevronMenuButton::MenuController::ShowContextMenu( |
225 views::MenuItemView* source, | 225 views::MenuItemView* source, |
226 int id, | 226 int id, |
227 const gfx::Point& p, | 227 const gfx::Point& p, |
228 ui::MenuSourceType source_type) { | 228 ui::MenuSourceType source_type) { |
229 BrowserActionView* view = browser_actions_container_->GetBrowserActionViewAt( | 229 ToolbarActionView* view = browser_actions_container_->GetToolbarActionViewAt( |
230 start_index_ + id - 1); | 230 start_index_ + id - 1); |
231 ExtensionActionViewController* view_controller = | 231 ExtensionActionViewController* view_controller = |
232 static_cast<ExtensionActionViewController*>(view->view_controller()); | 232 static_cast<ExtensionActionViewController*>(view->view_controller()); |
233 if (!view_controller->extension()->ShowConfigureContextMenus()) | 233 if (!view_controller->extension()->ShowConfigureContextMenus()) |
234 return false; | 234 return false; |
235 | 235 |
236 scoped_refptr<ExtensionContextMenuModel> context_menu_contents = | 236 scoped_refptr<ExtensionContextMenuModel> context_menu_contents = |
237 new ExtensionContextMenuModel(view_controller->extension(), | 237 new ExtensionContextMenuModel(view_controller->extension(), |
238 view->browser(), | 238 view->browser(), |
239 view_controller); | 239 view_controller); |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 void ChevronMenuButton::ShowOverflowMenu(bool for_drop) { | 430 void ChevronMenuButton::ShowOverflowMenu(bool for_drop) { |
431 DCHECK(!menu_controller_); | 431 DCHECK(!menu_controller_); |
432 menu_controller_.reset(new MenuController( | 432 menu_controller_.reset(new MenuController( |
433 this, browser_actions_container_, for_drop)); | 433 this, browser_actions_container_, for_drop)); |
434 menu_controller_->RunMenu(GetWidget()); | 434 menu_controller_->RunMenu(GetWidget()); |
435 } | 435 } |
436 | 436 |
437 void ChevronMenuButton::MenuDone() { | 437 void ChevronMenuButton::MenuDone() { |
438 menu_controller_.reset(); | 438 menu_controller_.reset(); |
439 } | 439 } |
OLD | NEW |