| 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" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 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 BrowserActionView::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 BrowserActionView::IconObserver { | 36 class IconUpdater : public ExtensionActionIconFactory::Observer { |
| 37 public: | 37 public: |
| 38 IconUpdater(views::MenuItemView* menu_item_view, BrowserActionView* view) | 38 IconUpdater(views::MenuItemView* menu_item_view, |
| 39 ExtensionActionViewController* view_controller) |
| 39 : menu_item_view_(menu_item_view), | 40 : menu_item_view_(menu_item_view), |
| 40 view_(view) { | 41 view_controller_(view_controller) { |
| 41 DCHECK(menu_item_view); | 42 DCHECK(menu_item_view); |
| 42 DCHECK(view); | 43 DCHECK(view_controller); |
| 43 view->set_icon_observer(this); | 44 view_controller->set_icon_observer(this); |
| 44 } | 45 } |
| 45 virtual ~IconUpdater() { | 46 virtual ~IconUpdater() { |
| 46 view_->set_icon_observer(NULL); | 47 view_controller_->set_icon_observer(NULL); |
| 47 } | 48 } |
| 48 | 49 |
| 49 // BrowserActionView::IconObserver: | 50 // BrowserActionView::IconObserver: |
| 50 virtual void OnIconUpdated(const gfx::ImageSkia& icon) override { | 51 virtual void OnIconUpdated() override { |
| 51 menu_item_view_->SetIcon(icon); | 52 menu_item_view_->SetIcon(view_controller_->GetIconWithBadge()); |
| 52 } | 53 } |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 // The menu item view whose icon might be updated. | 56 // The menu item view whose icon might be updated. |
| 56 views::MenuItemView* menu_item_view_; | 57 views::MenuItemView* menu_item_view_; |
| 57 | 58 |
| 58 // The view to be observed. When its icon changes, update the corresponding | 59 // The view controller to be observed. When its icon changes, update the |
| 59 // menu item view's icon. | 60 // corresponding menu item view's icon. |
| 60 BrowserActionView* view_; | 61 ExtensionActionViewController* view_controller_; |
| 61 | 62 |
| 62 DISALLOW_COPY_AND_ASSIGN(IconUpdater); | 63 DISALLOW_COPY_AND_ASSIGN(IconUpdater); |
| 63 }; | 64 }; |
| 64 | 65 |
| 65 } // namespace | 66 } // namespace |
| 66 | 67 |
| 67 // This class handles the overflow menu for browser actions. | 68 // This class handles the overflow menu for browser actions. |
| 68 class ChevronMenuButton::MenuController : public views::MenuDelegate { | 69 class ChevronMenuButton::MenuController : public views::MenuDelegate { |
| 69 public: | 70 public: |
| 70 MenuController(ChevronMenuButton* owner, | 71 MenuController(ChevronMenuButton* owner, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 menu_ = new views::MenuItemView(this); | 153 menu_ = new views::MenuItemView(this); |
| 153 menu_runner_.reset(new views::MenuRunner( | 154 menu_runner_.reset(new views::MenuRunner( |
| 154 menu_, for_drop_ ? views::MenuRunner::FOR_DROP : 0)); | 155 menu_, for_drop_ ? views::MenuRunner::FOR_DROP : 0)); |
| 155 menu_->set_has_icons(true); | 156 menu_->set_has_icons(true); |
| 156 | 157 |
| 157 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. |
| 158 for (size_t i = start_index_; | 159 for (size_t i = start_index_; |
| 159 i < browser_actions_container_->num_browser_actions(); ++i) { | 160 i < browser_actions_container_->num_browser_actions(); ++i) { |
| 160 BrowserActionView* view = | 161 BrowserActionView* view = |
| 161 browser_actions_container_->GetBrowserActionViewAt(i); | 162 browser_actions_container_->GetBrowserActionViewAt(i); |
| 163 ExtensionActionViewController* view_controller = |
| 164 view->GetExtensionActionViewController(); |
| 162 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( | 165 views::MenuItemView* menu_item = menu_->AppendMenuItemWithIcon( |
| 163 command_id, | 166 command_id, |
| 164 base::UTF8ToUTF16(view->extension()->name()), | 167 base::UTF8ToUTF16(view_controller->extension()->name()), |
| 165 view->GetIconWithBadge()); | 168 view_controller->GetIconWithBadge()); |
| 166 | 169 |
| 167 // Set the tooltip for this item. | 170 // Set the tooltip for this item. |
| 168 base::string16 tooltip = base::UTF8ToUTF16( | 171 menu_->SetTooltip( |
| 169 view->extension_action()->GetTitle( | 172 view_controller->GetTooltip(view->GetCurrentTabId()), |
| 170 view->view_controller()->GetCurrentTabId())); | 173 command_id); |
| 171 menu_->SetTooltip(tooltip, command_id); | |
| 172 | 174 |
| 173 icon_updaters_.push_back(new IconUpdater(menu_item, view)); | 175 icon_updaters_.push_back( |
| 176 new IconUpdater(menu_item, view->GetExtensionActionViewController())); |
| 174 | 177 |
| 175 ++command_id; | 178 ++command_id; |
| 176 } | 179 } |
| 177 } | 180 } |
| 178 | 181 |
| 179 ChevronMenuButton::MenuController::~MenuController() { | 182 ChevronMenuButton::MenuController::~MenuController() { |
| 180 } | 183 } |
| 181 | 184 |
| 182 void ChevronMenuButton::MenuController::RunMenu(views::Widget* window) { | 185 void ChevronMenuButton::MenuController::RunMenu(views::Widget* window) { |
| 183 gfx::Rect bounds = owner_->bounds(); | 186 gfx::Rect bounds = owner_->bounds(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 204 } | 207 } |
| 205 } | 208 } |
| 206 | 209 |
| 207 void ChevronMenuButton::MenuController::CloseMenu() { | 210 void ChevronMenuButton::MenuController::CloseMenu() { |
| 208 menu_->Cancel(); | 211 menu_->Cancel(); |
| 209 } | 212 } |
| 210 | 213 |
| 211 bool ChevronMenuButton::MenuController::IsCommandEnabled(int id) const { | 214 bool ChevronMenuButton::MenuController::IsCommandEnabled(int id) const { |
| 212 BrowserActionView* view = | 215 BrowserActionView* view = |
| 213 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1); | 216 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1); |
| 214 return view->IsEnabled(view->view_controller()->GetCurrentTabId()); | 217 return view->view_controller()->IsEnabled(view->GetCurrentTabId()); |
| 215 } | 218 } |
| 216 | 219 |
| 217 void ChevronMenuButton::MenuController::ExecuteCommand(int id) { | 220 void ChevronMenuButton::MenuController::ExecuteCommand(int id) { |
| 218 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1)-> | 221 browser_actions_container_->GetBrowserActionViewAt(start_index_ + id - 1)-> |
| 219 view_controller()->ExecuteActionByUser(); | 222 view_controller()->ExecuteActionByUser(); |
| 220 } | 223 } |
| 221 | 224 |
| 222 bool ChevronMenuButton::MenuController::ShowContextMenu( | 225 bool ChevronMenuButton::MenuController::ShowContextMenu( |
| 223 views::MenuItemView* source, | 226 views::MenuItemView* source, |
| 224 int id, | 227 int id, |
| 225 const gfx::Point& p, | 228 const gfx::Point& p, |
| 226 ui::MenuSourceType source_type) { | 229 ui::MenuSourceType source_type) { |
| 227 BrowserActionView* view = browser_actions_container_->GetBrowserActionViewAt( | 230 BrowserActionView* view = browser_actions_container_->GetBrowserActionViewAt( |
| 228 start_index_ + id - 1); | 231 start_index_ + id - 1); |
| 229 if (!view->extension()->ShowConfigureContextMenus()) | 232 ExtensionActionViewController* view_controller = |
| 233 view->GetExtensionActionViewController(); |
| 234 if (!view_controller->extension()->ShowConfigureContextMenus()) |
| 230 return false; | 235 return false; |
| 231 | 236 |
| 232 scoped_refptr<ExtensionContextMenuModel> context_menu_contents = | 237 scoped_refptr<ExtensionContextMenuModel> context_menu_contents = |
| 233 new ExtensionContextMenuModel(view->extension(), | 238 new ExtensionContextMenuModel(view_controller->extension(), |
| 234 view->view_controller()->browser(), | 239 view->browser(), |
| 235 view->view_controller()); | 240 view_controller); |
| 236 views::MenuRunner context_menu_runner(context_menu_contents.get(), | 241 views::MenuRunner context_menu_runner(context_menu_contents.get(), |
| 237 views::MenuRunner::HAS_MNEMONICS | | 242 views::MenuRunner::HAS_MNEMONICS | |
| 238 views::MenuRunner::IS_NESTED | | 243 views::MenuRunner::IS_NESTED | |
| 239 views::MenuRunner::CONTEXT_MENU); | 244 views::MenuRunner::CONTEXT_MENU); |
| 240 | 245 |
| 241 // We can ignore the result as we delete ourself. | 246 // We can ignore the result as we delete ourself. |
| 242 // This blocks until the user chooses something or dismisses the menu. | 247 // This blocks until the user chooses something or dismisses the menu. |
| 243 if (context_menu_runner.RunMenuAt(owner_->GetWidget(), | 248 if (context_menu_runner.RunMenuAt(owner_->GetWidget(), |
| 244 NULL, | 249 NULL, |
| 245 gfx::Rect(p, gfx::Size()), | 250 gfx::Rect(p, gfx::Size()), |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 } | 339 } |
| 335 | 340 |
| 336 bool ChevronMenuButton::MenuController::CanDrag(views::MenuItemView* menu) { | 341 bool ChevronMenuButton::MenuController::CanDrag(views::MenuItemView* menu) { |
| 337 return true; | 342 return true; |
| 338 } | 343 } |
| 339 | 344 |
| 340 void ChevronMenuButton::MenuController::WriteDragData( | 345 void ChevronMenuButton::MenuController::WriteDragData( |
| 341 views::MenuItemView* sender, OSExchangeData* data) { | 346 views::MenuItemView* sender, OSExchangeData* data) { |
| 342 size_t drag_index = IndexForId(sender->GetCommand()); | 347 size_t drag_index = IndexForId(sender->GetCommand()); |
| 343 const extensions::Extension* extension = | 348 const extensions::Extension* extension = |
| 344 browser_actions_container_->GetBrowserActionViewAt(drag_index)-> | 349 browser_actions_container_->GetExtensionAt(drag_index); |
| 345 extension(); | |
| 346 BrowserActionDragData drag_data(extension->id(), drag_index); | 350 BrowserActionDragData drag_data(extension->id(), drag_index); |
| 347 drag_data.Write(browser_actions_container_->profile(), data); | 351 drag_data.Write(browser_actions_container_->profile(), data); |
| 348 } | 352 } |
| 349 | 353 |
| 350 int ChevronMenuButton::MenuController::GetDragOperations( | 354 int ChevronMenuButton::MenuController::GetDragOperations( |
| 351 views::MenuItemView* sender) { | 355 views::MenuItemView* sender) { |
| 352 return ui::DragDropTypes::DRAG_MOVE; | 356 return ui::DragDropTypes::DRAG_MOVE; |
| 353 } | 357 } |
| 354 | 358 |
| 355 size_t ChevronMenuButton::MenuController::IndexForId(int id) const { | 359 size_t ChevronMenuButton::MenuController::IndexForId(int id) const { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 void ChevronMenuButton::ShowOverflowMenu(bool for_drop) { | 435 void ChevronMenuButton::ShowOverflowMenu(bool for_drop) { |
| 432 DCHECK(!menu_controller_); | 436 DCHECK(!menu_controller_); |
| 433 menu_controller_.reset(new MenuController( | 437 menu_controller_.reset(new MenuController( |
| 434 this, browser_actions_container_, for_drop)); | 438 this, browser_actions_container_, for_drop)); |
| 435 menu_controller_->RunMenu(GetWidget()); | 439 menu_controller_->RunMenu(GetWidget()); |
| 436 } | 440 } |
| 437 | 441 |
| 438 void ChevronMenuButton::MenuDone() { | 442 void ChevronMenuButton::MenuDone() { |
| 439 menu_controller_.reset(); | 443 menu_controller_.reset(); |
| 440 } | 444 } |
| OLD | NEW |