Chromium Code Reviews| 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 #include "chrome/browser/ui/views/extensions/browser_action_overflow_menu_contro ller.h" | 5 #include "chrome/browser/ui/views/extensions/browser_action_overflow_menu_contro ller.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/extension_action.h" | 9 #include "chrome/browser/extensions/extension_action.h" |
| 10 #include "chrome/browser/extensions/extension_action_manager.h" | 10 #include "chrome/browser/extensions/extension_action_manager.h" |
| 11 #include "chrome/browser/extensions/extension_context_menu_model.h" | 11 #include "chrome/browser/extensions/extension_context_menu_model.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser_list.h" | 13 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" | 14 #include "chrome/browser/ui/views/extensions/browser_action_drag_data.h" |
| 15 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" | 15 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" |
| 16 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | 16 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
| 17 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/views/controls/menu/menu_item_view.h" | 19 #include "ui/views/controls/menu/menu_item_view.h" |
| 20 #include "ui/views/controls/menu/menu_runner.h" | 20 #include "ui/views/controls/menu/menu_runner.h" |
| 21 #include "ui/views/controls/menu/submenu_view.h" | 21 #include "ui/views/controls/menu/submenu_view.h" |
| 22 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
| 23 | 23 |
| 24 // In the browser actions container's chevron menu, a MenuItemView's icon comes | |
| 25 // from BrowserActionView::GetIconWithBadge() (which comes from | |
| 26 // BrowserActionButton's icon) when the MenuItemView is created. But, | |
| 27 // BrowserActionButton's icon may be not yet loaded in that timing because it | |
|
Finnur
2014/02/10 10:41:41
s/in that timing/in time/
| |
| 28 // is read from filesystem in another thread. | |
| 29 // The IconStateSensitiveMenuItemViewHelper will change the MenuItemView's | |
|
Finnur
2014/02/10 10:41:41
s/change/update/
| |
| 30 // icon when the BrowserActionButton's icon has been updated. | |
| 31 class IconStateSensitiveMenuItemViewHelper | |
|
Finnur
2014/02/10 10:41:41
Nit: This name is a bit long and unwieldy. How abo
| |
| 32 : public BrowserActionButton::IconObserver, | |
| 33 public views::MenuItemView::LifecycleObserver { | |
| 34 public: | |
| 35 IconStateSensitiveMenuItemViewHelper(views::MenuItemView* host, | |
| 36 BrowserActionButton* button) | |
| 37 : host_(host), | |
| 38 button_(button) { | |
| 39 DCHECK(host); | |
| 40 DCHECK(button); | |
| 41 button->set_icon_observer(this); | |
| 42 } | |
| 43 virtual ~IconStateSensitiveMenuItemViewHelper() {} | |
| 44 virtual void OnIconUpdated(const gfx::ImageSkia& icon) OVERRIDE { | |
|
Finnur
2014/02/10 10:41:41
Add a comment:
// BrowserActionButton::IconObserv
| |
| 45 host_->SetIcon(icon); | |
| 46 } | |
| 47 virtual void OnDeleted(views::MenuItemView* view) OVERRIDE { | |
|
Finnur
2014/02/10 10:41:41
Add a comment:
// views::MenuItemView::LifecycleO
| |
| 48 button_->set_icon_observer(NULL); | |
| 49 delete this; | |
| 50 } | |
| 51 private: | |
| 52 views::MenuItemView* host_; | |
|
Finnur
2014/02/10 10:41:41
Not sure I'd use the word host_ here. Why not just
| |
| 53 BrowserActionButton* button_; | |
|
Finnur
2014/02/10 10:41:41
Document these two members.
| |
| 54 }; | |
|
Finnur
2014/02/10 10:41:41
Add DISABLE_COPY_AND_ASSIGN(IconStateSensitiveMenu
| |
| 55 | |
| 24 BrowserActionOverflowMenuController::BrowserActionOverflowMenuController( | 56 BrowserActionOverflowMenuController::BrowserActionOverflowMenuController( |
| 25 BrowserActionsContainer* owner, | 57 BrowserActionsContainer* owner, |
| 26 Browser* browser, | 58 Browser* browser, |
| 27 views::MenuButton* menu_button, | 59 views::MenuButton* menu_button, |
| 28 const std::vector<BrowserActionView*>& views, | 60 const std::vector<BrowserActionView*>& views, |
| 29 int start_index) | 61 int start_index) |
| 30 : owner_(owner), | 62 : owner_(owner), |
| 31 browser_(browser), | 63 browser_(browser), |
| 32 observer_(NULL), | 64 observer_(NULL), |
| 33 menu_button_(menu_button), | 65 menu_button_(menu_button), |
| 34 menu_(NULL), | 66 menu_(NULL), |
| 35 views_(&views), | 67 views_(&views), |
| 36 start_index_(start_index), | 68 start_index_(start_index), |
| 37 for_drop_(false) { | 69 for_drop_(false) { |
| 38 menu_ = new views::MenuItemView(this); | 70 menu_ = new views::MenuItemView(this); |
| 39 menu_runner_.reset(new views::MenuRunner(menu_)); | 71 menu_runner_.reset(new views::MenuRunner(menu_)); |
| 40 menu_->set_has_icons(true); | 72 menu_->set_has_icons(true); |
| 41 | 73 |
| 42 size_t command_id = 1; // Menu id 0 is reserved, start with 1. | 74 size_t command_id = 1; // Menu id 0 is reserved, start with 1. |
| 43 for (size_t i = start_index; i < views_->size(); ++i) { | 75 for (size_t i = start_index; i < views_->size(); ++i) { |
| 44 BrowserActionView* view = (*views_)[i]; | 76 BrowserActionView* view = (*views_)[i]; |
| 45 menu_->AppendMenuItemWithIcon( | 77 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( |
| 46 command_id, | 78 command_id, |
| 47 base::UTF8ToUTF16(view->button()->extension()->name()), | 79 base::UTF8ToUTF16(view->button()->extension()->name()), |
| 48 view->GetIconWithBadge()); | 80 view->GetIconWithBadge()); |
| 49 | 81 |
| 50 // Set the tooltip for this item. | 82 // Set the tooltip for this item. |
| 51 base::string16 tooltip = base::UTF8ToUTF16( | 83 base::string16 tooltip = base::UTF8ToUTF16( |
| 52 extensions::ExtensionActionManager::Get(owner_->profile())-> | 84 extensions::ExtensionActionManager::Get(owner_->profile())-> |
| 53 GetBrowserAction(*view->button()->extension())-> | 85 GetBrowserAction(*view->button()->extension())-> |
| 54 GetTitle(owner_->GetCurrentTabId())); | 86 GetTitle(owner_->GetCurrentTabId())); |
| 55 menu_->SetTooltip(tooltip, command_id); | 87 menu_->SetTooltip(tooltip, command_id); |
| 56 | 88 |
| 89 IconStateSensitiveMenuItemViewHelper* helper = | |
| 90 new IconStateSensitiveMenuItemViewHelper(menu_item, view->button()); | |
| 91 menu_item->set_lifecycle_observer(helper); | |
| 92 | |
| 57 ++command_id; | 93 ++command_id; |
| 58 } | 94 } |
| 59 } | 95 } |
| 60 | 96 |
| 61 BrowserActionOverflowMenuController::~BrowserActionOverflowMenuController() { | 97 BrowserActionOverflowMenuController::~BrowserActionOverflowMenuController() { |
| 62 if (observer_) | 98 if (observer_) |
| 63 observer_->NotifyMenuDeleted(this); | 99 observer_->NotifyMenuDeleted(this); |
| 64 } | 100 } |
| 65 | 101 |
| 66 bool BrowserActionOverflowMenuController::RunMenu(views::Widget* window, | 102 bool BrowserActionOverflowMenuController::RunMenu(views::Widget* window, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 | 255 |
| 220 BrowserActionView* BrowserActionOverflowMenuController::ViewForId( | 256 BrowserActionView* BrowserActionOverflowMenuController::ViewForId( |
| 221 int id, size_t* index) { | 257 int id, size_t* index) { |
| 222 // The index of the view being dragged (GetCommand gives a 1-based index into | 258 // The index of the view being dragged (GetCommand gives a 1-based index into |
| 223 // the overflow menu). | 259 // the overflow menu). |
| 224 size_t view_index = owner_->VisibleBrowserActions() + id - 1; | 260 size_t view_index = owner_->VisibleBrowserActions() + id - 1; |
| 225 if (index) | 261 if (index) |
| 226 *index = view_index; | 262 *index = view_index; |
| 227 return owner_->GetBrowserActionViewAt(view_index); | 263 return owner_->GetBrowserActionViewAt(view_index); |
| 228 } | 264 } |
| OLD | NEW |