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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/toolbar/extension_toolbar_menu_view.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/ui/views/frame/browser_view.h"
11 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
12 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
13 #include "chrome/browser/ui/views/toolbar/wrench_menu.h"
14 #include "ui/views/controls/menu/menu_item_view.h"
15 #include "ui/views/controls/menu/submenu_view.h"
19 // Returns the padding before the BrowserActionsContainer in the menu.
21 // We pad enough on the left so that the first icon starts at the same point
22 // as the labels. We need to subtract 1 because we want the pixel *before*
23 // the label, and we subtract kItemSpacing because there needs to be padding
24 // so we can see the drop indicator.
25 return views::MenuItemView::label_start() - 1 -
26 BrowserActionsContainer::kItemSpacing;
31 ExtensionToolbarMenuView::ExtensionToolbarMenuView(Browser* browser,
32 WrenchMenu* wrench_menu)
34 wrench_menu_(wrench_menu),
36 browser_actions_container_observer_(this),
38 BrowserActionsContainer* main =
39 BrowserView::GetBrowserViewForBrowser(browser_)
40 ->toolbar()->browser_actions();
41 container_ = new BrowserActionsContainer(browser_, main);
43 AddChildView(container_);
44 // We Layout() the container here so that we know the number of actions
45 // that will be visible in ShouldShow().
48 // If we were opened for a drop command, we have to wait for the drop to
49 // finish so we can close the wrench menu.
50 if (wrench_menu_->for_drop()) {
51 browser_actions_container_observer_.Add(container_);
52 browser_actions_container_observer_.Add(main);
56 ExtensionToolbarMenuView::~ExtensionToolbarMenuView() {
59 bool ExtensionToolbarMenuView::ShouldShow() {
60 return wrench_menu_->for_drop() ||
61 container_->VisibleBrowserActionsAfterAnimation();
64 gfx::Size ExtensionToolbarMenuView::GetPreferredSize() const {
65 return container_->GetPreferredSize();
68 int ExtensionToolbarMenuView::GetHeightForWidth(int width) const {
69 const views::MenuConfig& menu_config =
70 static_cast<const views::MenuItemView*>(parent())->GetMenuConfig();
71 int end_padding = menu_config.arrow_to_edge_padding -
72 BrowserActionsContainer::kItemSpacing;
73 width -= start_padding() + end_padding;
75 int height = container_->GetHeightForWidth(width);
79 void ExtensionToolbarMenuView::Layout() {
80 gfx::Size sz = GetPreferredSize();
81 SetBounds(start_padding() + 1, 0, sz.width(), sz.height());
82 container_->SetBounds(0, 0, sz.width(), sz.height());
85 void ExtensionToolbarMenuView::OnBrowserActionDragDone() {
86 // The delay before we close the wrench menu if this was opened for a drop so
87 // that the user can see a browser action if one was moved.
88 static const int kCloseMenuDelay = 300;
90 DCHECK(wrench_menu_->for_drop());
91 base::MessageLoop::current()->PostDelayedTask(
93 base::Bind(&ExtensionToolbarMenuView::CloseWrenchMenu,
94 weak_factory_.GetWeakPtr()),
95 base::TimeDelta::FromMilliseconds(kCloseMenuDelay));
98 void ExtensionToolbarMenuView::CloseWrenchMenu() {
99 wrench_menu_->CloseMenu();