| 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/location.h" | 7 #include "base/location.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 // In the browser actions container's chevron menu, a menu item view's icon | 32 // In the browser actions container's chevron menu, a menu item view's icon |
| 33 // comes from ToolbarActionView::GetIconWithBadge() when the menu item view is | 33 // comes from ToolbarActionView::GetIconWithBadge() when the menu item view is |
| 34 // created. But, the browser action's icon may not be loaded in time because it | 34 // created. But, the browser action's icon may not be loaded in time because it |
| 35 // is read from file system in another thread. | 35 // is read from file system in another thread. |
| 36 // The IconUpdater will update the menu item view's icon when the browser | 36 // The IconUpdater will update the menu item view's icon when the browser |
| 37 // action's icon has been updated. | 37 // action's icon has been updated. |
| 38 class IconUpdater : public ExtensionActionIconFactory::Observer { | 38 class IconUpdater : public ExtensionActionIconFactory::Observer { |
| 39 public: | 39 public: |
| 40 IconUpdater(views::MenuItemView* menu_item_view, | 40 IconUpdater(views::MenuItemView* menu_item_view, |
| 41 ExtensionActionViewController* view_controller) | 41 ToolbarActionView* represented_view) |
| 42 : menu_item_view_(menu_item_view), | 42 : menu_item_view_(menu_item_view), |
| 43 view_controller_(view_controller) { | 43 represented_view_(represented_view) { |
| 44 DCHECK(menu_item_view); | 44 DCHECK(menu_item_view); |
| 45 DCHECK(view_controller); | 45 DCHECK(represented_view); |
| 46 view_controller->set_icon_observer(this); | 46 view_controller()->set_icon_observer(this); |
| 47 } | 47 } |
| 48 ~IconUpdater() override { view_controller_->set_icon_observer(nullptr); } | 48 ~IconUpdater() override { view_controller()->set_icon_observer(nullptr); } |
| 49 | 49 |
| 50 // ExtensionActionIconFactory::Observer: | 50 // ExtensionActionIconFactory::Observer: |
| 51 void OnIconUpdated() override { | 51 void OnIconUpdated() override { |
| 52 menu_item_view_->SetIcon(view_controller_->GetIconWithBadge()); | 52 menu_item_view_->SetIcon( |
| 53 represented_view_->GetImage(views::Button::STATE_NORMAL)); |
| 53 } | 54 } |
| 54 | 55 |
| 55 private: | 56 private: |
| 57 ExtensionActionViewController* view_controller() { |
| 58 // Since the chevron overflow menu is only used in a world where toolbar |
| 59 // actions are only extensions, this cast is safe. |
| 60 return static_cast<ExtensionActionViewController*>( |
| 61 represented_view_->view_controller()); |
| 62 } |
| 63 |
| 56 // The menu item view whose icon might be updated. | 64 // The menu item view whose icon might be updated. |
| 57 views::MenuItemView* menu_item_view_; | 65 views::MenuItemView* menu_item_view_; |
| 58 | 66 |
| 59 // The view controller to be observed. When its icon changes, update the | 67 // The view this icon updater is helping represent in the chevron overflow |
| 60 // corresponding menu item view's icon. | 68 // menu. When its icon changes, this updates the corresponding menu item |
| 61 ExtensionActionViewController* view_controller_; | 69 // view's icon. |
| 70 ToolbarActionView* represented_view_; |
| 62 | 71 |
| 63 DISALLOW_COPY_AND_ASSIGN(IconUpdater); | 72 DISALLOW_COPY_AND_ASSIGN(IconUpdater); |
| 64 }; | 73 }; |
| 65 | 74 |
| 66 } // namespace | 75 } // namespace |
| 67 | 76 |
| 68 // This class handles the overflow menu for browser actions. | 77 // This class handles the overflow menu for browser actions. |
| 69 class ChevronMenuButton::MenuController : public views::MenuDelegate { | 78 class ChevronMenuButton::MenuController : public views::MenuDelegate { |
| 70 public: | 79 public: |
| 71 MenuController(ChevronMenuButton* owner, | 80 MenuController(ChevronMenuButton* owner, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 menu_->set_has_icons(true); | 165 menu_->set_has_icons(true); |
| 157 | 166 |
| 158 size_t command_id = 1; // Menu id 0 is reserved, start with 1. | 167 size_t command_id = 1; // Menu id 0 is reserved, start with 1. |
| 159 for (size_t i = start_index_; | 168 for (size_t i = start_index_; |
| 160 i < browser_actions_container_->num_toolbar_actions(); ++i) { | 169 i < browser_actions_container_->num_toolbar_actions(); ++i) { |
| 161 ToolbarActionView* view = | 170 ToolbarActionView* view = |
| 162 browser_actions_container_->GetToolbarActionViewAt(i); | 171 browser_actions_container_->GetToolbarActionViewAt(i); |
| 163 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( | 172 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( |
| 164 command_id, | 173 command_id, |
| 165 view->view_controller()->GetActionName(), | 174 view->view_controller()->GetActionName(), |
| 166 view->view_controller()->GetIconWithBadge()); | 175 view->GetImage(views::Button::STATE_NORMAL)); |
| 167 | 176 |
| 168 // Set the tooltip for this item. | 177 // Set the tooltip for this item. |
| 169 menu_->SetTooltip( | 178 menu_->SetTooltip( |
| 170 view->view_controller()->GetTooltip(view->GetCurrentWebContents()), | 179 view->view_controller()->GetTooltip(view->GetCurrentWebContents()), |
| 171 command_id); | 180 command_id); |
| 172 | 181 |
| 173 icon_updaters_.push_back(new IconUpdater( | 182 icon_updaters_.push_back(new IconUpdater(menu_item, view)); |
| 174 menu_item, | |
| 175 static_cast<ExtensionActionViewController*>(view->view_controller()))); | |
| 176 | 183 |
| 177 ++command_id; | 184 ++command_id; |
| 178 } | 185 } |
| 179 } | 186 } |
| 180 | 187 |
| 181 ChevronMenuButton::MenuController::~MenuController() { | 188 ChevronMenuButton::MenuController::~MenuController() { |
| 182 } | 189 } |
| 183 | 190 |
| 184 void ChevronMenuButton::MenuController::RunMenu(views::Widget* window) { | 191 void ChevronMenuButton::MenuController::RunMenu(views::Widget* window) { |
| 185 gfx::Rect bounds = owner_->bounds(); | 192 gfx::Rect bounds = owner_->bounds(); |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 // We should never try to show an overflow menu when one is already visible. | 439 // We should never try to show an overflow menu when one is already visible. |
| 433 DCHECK(!menu_controller_); | 440 DCHECK(!menu_controller_); |
| 434 menu_controller_.reset(new MenuController( | 441 menu_controller_.reset(new MenuController( |
| 435 this, browser_actions_container_, for_drop)); | 442 this, browser_actions_container_, for_drop)); |
| 436 menu_controller_->RunMenu(GetWidget()); | 443 menu_controller_->RunMenu(GetWidget()); |
| 437 } | 444 } |
| 438 | 445 |
| 439 void ChevronMenuButton::MenuDone() { | 446 void ChevronMenuButton::MenuDone() { |
| 440 menu_controller_.reset(); | 447 menu_controller_.reset(); |
| 441 } | 448 } |
| OLD | NEW |