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/location.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "base/time/time.h"
12 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
15 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
16 #include "chrome/browser/ui/views/toolbar/wrench_menu.h"
17 #include "ui/views/controls/menu/menu_item_view.h"
18 #include "ui/views/controls/menu/submenu_view.h"
20 ExtensionToolbarMenuView::ExtensionToolbarMenuView(Browser
* browser
,
21 WrenchMenu
* wrench_menu
)
23 wrench_menu_(wrench_menu
),
25 browser_actions_container_observer_(this),
27 BrowserActionsContainer
* main
=
28 BrowserView::GetBrowserViewForBrowser(browser_
)
29 ->toolbar()->browser_actions();
30 container_
= new BrowserActionsContainer(browser_
, main
);
32 AddChildView(container_
);
33 // We Layout() the container here so that we know the number of actions
34 // that will be visible in ShouldShow().
37 // 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.
39 if (wrench_menu_
->for_drop()) {
40 browser_actions_container_observer_
.Add(container_
);
41 browser_actions_container_observer_
.Add(main
);
45 ExtensionToolbarMenuView::~ExtensionToolbarMenuView() {
48 bool ExtensionToolbarMenuView::ShouldShow() {
49 return wrench_menu_
->for_drop() ||
50 container_
->VisibleBrowserActionsAfterAnimation();
53 gfx::Size
ExtensionToolbarMenuView::GetPreferredSize() const {
54 return container_
->GetPreferredSize();
57 int ExtensionToolbarMenuView::GetHeightForWidth(int width
) const {
58 const views::MenuConfig
& menu_config
=
59 static_cast<const views::MenuItemView
*>(parent())->GetMenuConfig();
60 int end_padding
= menu_config
.arrow_to_edge_padding
-
61 container_
->toolbar_actions_bar()->platform_settings().item_spacing
;
62 width
-= start_padding() + end_padding
;
64 int height
= container_
->GetHeightForWidth(width
);
68 void ExtensionToolbarMenuView::Layout() {
69 gfx::Size sz
= GetPreferredSize();
70 SetBounds(start_padding() + 1, 0, sz
.width(), sz
.height());
71 container_
->SetBounds(0, 0, sz
.width(), sz
.height());
74 void ExtensionToolbarMenuView::OnBrowserActionDragDone() {
75 // 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.
77 static const int kCloseMenuDelay
= 300;
79 DCHECK(wrench_menu_
->for_drop());
80 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
81 FROM_HERE
, base::Bind(&ExtensionToolbarMenuView::CloseWrenchMenu
,
82 weak_factory_
.GetWeakPtr()),
83 base::TimeDelta::FromMilliseconds(kCloseMenuDelay
));
86 void ExtensionToolbarMenuView::CloseWrenchMenu() {
87 wrench_menu_
->CloseMenu();
90 int ExtensionToolbarMenuView::start_padding() const {
91 // 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*
93 // the label, and we subtract kItemSpacing because there needs to be padding
94 // so we can see the drop indicator.
95 return views::MenuItemView::label_start() - 1 -
96 container_
->toolbar_actions_bar()->platform_settings().item_spacing
;