| 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/extension_toolbar_menu_view.h" | 5 #include "chrome/browser/ui/views/toolbar/extension_toolbar_menu_view.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | 12 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" |
| 13 #include "chrome/browser/ui/views/frame/browser_view.h" | 13 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 14 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | 14 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
| 15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 16 #include "chrome/browser/ui/views/toolbar/wrench_menu.h" | 16 #include "chrome/browser/ui/views/toolbar/wrench_menu.h" |
| 17 #include "ui/views/controls/menu/menu_item_view.h" | 17 #include "ui/views/controls/menu/menu_item_view.h" |
| 18 #include "ui/views/controls/menu/submenu_view.h" | 18 #include "ui/views/controls/menu/submenu_view.h" |
| 19 | 19 |
| 20 namespace { |
| 21 // In *very* extreme cases, it's possible that there are so many overflowed |
| 22 // actions, we won't be able to show them all. Cap the height so that the |
| 23 // overflow won't make the menu larger than the height of the screen. |
| 24 // Note: With this height, we can show 104 actions. Less than 0.0002% of our |
| 25 // users will be affected. |
| 26 const int kMaxOverflowContainerHeight = 416; |
| 27 } |
| 28 |
| 20 ExtensionToolbarMenuView::ExtensionToolbarMenuView(Browser* browser, | 29 ExtensionToolbarMenuView::ExtensionToolbarMenuView(Browser* browser, |
| 21 WrenchMenu* wrench_menu) | 30 WrenchMenu* wrench_menu) |
| 22 : browser_(browser), | 31 : browser_(browser), |
| 23 wrench_menu_(wrench_menu), | 32 wrench_menu_(wrench_menu), |
| 24 container_(NULL), | 33 container_(NULL), |
| 25 browser_actions_container_observer_(this), | 34 browser_actions_container_observer_(this), |
| 26 weak_factory_(this) { | 35 weak_factory_(this) { |
| 27 BrowserActionsContainer* main = | 36 BrowserActionsContainer* main = |
| 28 BrowserView::GetBrowserViewForBrowser(browser_) | 37 BrowserView::GetBrowserViewForBrowser(browser_) |
| 29 ->toolbar()->browser_actions(); | 38 ->toolbar()->browser_actions(); |
| 30 container_ = new BrowserActionsContainer(browser_, main); | 39 container_ = new BrowserActionsContainer(browser_, main); |
| 31 container_->Init(); | 40 container_->Init(); |
| 32 AddChildView(container_); | 41 SetContents(container_); |
| 33 // We Layout() the container here so that we know the number of actions | 42 // We Layout() the container here so that we know the number of actions |
| 34 // that will be visible in ShouldShow(). | 43 // that will be visible in ShouldShow(). |
| 35 container_->Layout(); | 44 container_->Layout(); |
| 36 | 45 |
| 37 // If we were opened for a drop command, we have to wait for the drop to | 46 // If we were opened for a drop command, we have to wait for the drop to |
| 38 // finish so we can close the wrench menu. | 47 // finish so we can close the wrench menu. |
| 39 if (wrench_menu_->for_drop()) { | 48 if (wrench_menu_->for_drop()) { |
| 40 browser_actions_container_observer_.Add(container_); | 49 browser_actions_container_observer_.Add(container_); |
| 41 browser_actions_container_observer_.Add(main); | 50 browser_actions_container_observer_.Add(main); |
| 42 } | 51 } |
| 52 |
| 53 ClipHeightTo(0, kMaxOverflowContainerHeight); |
| 43 } | 54 } |
| 44 | 55 |
| 45 ExtensionToolbarMenuView::~ExtensionToolbarMenuView() { | 56 ExtensionToolbarMenuView::~ExtensionToolbarMenuView() { |
| 46 } | 57 } |
| 47 | 58 |
| 48 bool ExtensionToolbarMenuView::ShouldShow() { | 59 bool ExtensionToolbarMenuView::ShouldShow() { |
| 49 return wrench_menu_->for_drop() || | 60 return wrench_menu_->for_drop() || |
| 50 container_->VisibleBrowserActionsAfterAnimation(); | 61 container_->VisibleBrowserActionsAfterAnimation(); |
| 51 } | 62 } |
| 52 | 63 |
| 53 gfx::Size ExtensionToolbarMenuView::GetPreferredSize() const { | 64 gfx::Size ExtensionToolbarMenuView::GetPreferredSize() const { |
| 54 return container_->GetPreferredSize(); | 65 gfx::Size s = views::ScrollView::GetPreferredSize(); |
| 66 // views::ScrollView::GetPreferredSize() includes the contents' size, but |
| 67 // not the scrollbar width. Add it in. |
| 68 s.set_width(s.width() + GetScrollBarWidth()); |
| 69 return s; |
| 55 } | 70 } |
| 56 | 71 |
| 57 int ExtensionToolbarMenuView::GetHeightForWidth(int width) const { | 72 int ExtensionToolbarMenuView::GetHeightForWidth(int width) const { |
| 73 // The width passed in here includes the full width of the menu, so we need |
| 74 // to omit the necessary padding. |
| 58 const views::MenuConfig& menu_config = | 75 const views::MenuConfig& menu_config = |
| 59 static_cast<const views::MenuItemView*>(parent())->GetMenuConfig(); | 76 static_cast<const views::MenuItemView*>(parent())->GetMenuConfig(); |
| 60 int end_padding = menu_config.arrow_to_edge_padding - | 77 int end_padding = menu_config.arrow_to_edge_padding - |
| 61 container_->toolbar_actions_bar()->platform_settings().item_spacing; | 78 container_->toolbar_actions_bar()->platform_settings().item_spacing; |
| 62 width -= start_padding() + end_padding; | 79 width -= start_padding() + end_padding; |
| 63 | 80 |
| 64 int height = container_->GetHeightForWidth(width); | 81 return views::ScrollView::GetHeightForWidth(width); |
| 65 return height; | |
| 66 } | 82 } |
| 67 | 83 |
| 68 void ExtensionToolbarMenuView::Layout() { | 84 void ExtensionToolbarMenuView::Layout() { |
| 69 gfx::Size sz = GetPreferredSize(); | 85 gfx::Size sz = GetPreferredSize(); |
| 70 SetBounds(start_padding() + 1, 0, sz.width(), sz.height()); | 86 SetBounds(start_padding() + 1, 0, sz.width(), sz.height()); |
| 71 container_->SetBounds(0, 0, sz.width(), sz.height()); | 87 views::ScrollView::Layout(); |
| 72 } | 88 } |
| 73 | 89 |
| 74 void ExtensionToolbarMenuView::OnBrowserActionDragDone() { | 90 void ExtensionToolbarMenuView::OnBrowserActionDragDone() { |
| 75 // The delay before we close the wrench menu if this was opened for a drop so | 91 // The delay before we close the wrench menu if this was opened for a drop so |
| 76 // that the user can see a browser action if one was moved. | 92 // that the user can see a browser action if one was moved. |
| 77 static const int kCloseMenuDelay = 300; | 93 static const int kCloseMenuDelay = 300; |
| 78 | 94 |
| 79 DCHECK(wrench_menu_->for_drop()); | 95 DCHECK(wrench_menu_->for_drop()); |
| 80 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 96 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 81 FROM_HERE, base::Bind(&ExtensionToolbarMenuView::CloseWrenchMenu, | 97 FROM_HERE, base::Bind(&ExtensionToolbarMenuView::CloseWrenchMenu, |
| 82 weak_factory_.GetWeakPtr()), | 98 weak_factory_.GetWeakPtr()), |
| 83 base::TimeDelta::FromMilliseconds(kCloseMenuDelay)); | 99 base::TimeDelta::FromMilliseconds(kCloseMenuDelay)); |
| 84 } | 100 } |
| 85 | 101 |
| 86 void ExtensionToolbarMenuView::CloseWrenchMenu() { | 102 void ExtensionToolbarMenuView::CloseWrenchMenu() { |
| 87 wrench_menu_->CloseMenu(); | 103 wrench_menu_->CloseMenu(); |
| 88 } | 104 } |
| 89 | 105 |
| 90 int ExtensionToolbarMenuView::start_padding() const { | 106 int ExtensionToolbarMenuView::start_padding() const { |
| 91 // We pad enough on the left so that the first icon starts at the same point | 107 // We pad enough on the left so that the first icon starts at the same point |
| 92 // as the labels. We need to subtract 1 because we want the pixel *before* | 108 // as the labels. We need to subtract 1 because we want the pixel *before* |
| 93 // the label, and we subtract kItemSpacing because there needs to be padding | 109 // the label, and we subtract kItemSpacing because there needs to be padding |
| 94 // so we can see the drop indicator. | 110 // so we can see the drop indicator. |
| 95 return views::MenuItemView::label_start() - 1 - | 111 return views::MenuItemView::label_start() - 1 - |
| 96 container_->toolbar_actions_bar()->platform_settings().item_spacing; | 112 container_->toolbar_actions_bar()->platform_settings().item_spacing; |
| 97 } | 113 } |
| 98 | 114 |
| OLD | NEW |