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 "chrome/browser/ui/views/frame/browser_view.h" | 7 #include "chrome/browser/ui/views/frame/browser_view.h" |
8 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" | 8 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h" |
9 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" | 9 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
10 #include "chrome/browser/ui/views/toolbar/wrench_menu.h" | 10 #include "chrome/browser/ui/views/toolbar/wrench_menu.h" |
11 #include "ui/views/controls/menu/menu_item_view.h" | 11 #include "ui/views/controls/menu/menu_item_view.h" |
| 12 #include "ui/views/controls/menu/submenu_view.h" |
12 | 13 |
13 ExtensionToolbarMenuView::ExtensionToolbarMenuView(Browser* browser, | 14 ExtensionToolbarMenuView::ExtensionToolbarMenuView(Browser* browser, |
14 WrenchMenu* wrench_menu) | 15 WrenchMenu* wrench_menu) |
15 : browser_(browser), | 16 : browser_(browser), |
16 wrench_menu_(wrench_menu), | 17 wrench_menu_(wrench_menu), |
17 container_(NULL), | 18 container_(NULL), |
18 browser_actions_container_observer_(this) { | 19 browser_actions_container_observer_(this), |
| 20 start_padding_(0), |
| 21 end_padding_(0) { |
19 BrowserActionsContainer* main = | 22 BrowserActionsContainer* main = |
20 BrowserView::GetBrowserViewForBrowser(browser_) | 23 BrowserView::GetBrowserViewForBrowser(browser_) |
21 ->toolbar()->browser_actions(); | 24 ->toolbar()->browser_actions(); |
22 container_ = new BrowserActionsContainer( | 25 container_ = new BrowserActionsContainer( |
23 browser_, | 26 browser_, |
24 NULL, // No owner view, means no extra keybindings are registered. | 27 NULL, // No owner view, means no extra keybindings are registered. |
25 main); | 28 main); |
26 container_->Init(); | 29 container_->Init(); |
27 AddChildView(container_); | 30 AddChildView(container_); |
28 | |
29 // If we were opened for a drop command, we have to wait for the drop to | 31 // If we were opened for a drop command, we have to wait for the drop to |
30 // finish so we can close the wrench menu. | 32 // finish so we can close the wrench menu. |
31 if (wrench_menu_->for_drop()) { | 33 if (wrench_menu_->for_drop()) { |
32 browser_actions_container_observer_.Add(container_); | 34 browser_actions_container_observer_.Add(container_); |
33 browser_actions_container_observer_.Add(main); | 35 browser_actions_container_observer_.Add(main); |
34 } | 36 } |
35 } | 37 } |
36 | 38 |
37 ExtensionToolbarMenuView::~ExtensionToolbarMenuView() { | 39 ExtensionToolbarMenuView::~ExtensionToolbarMenuView() { |
38 } | 40 } |
39 | 41 |
| 42 void ExtensionToolbarMenuView::Init(views::MenuItemView* menu_root) { |
| 43 // We should pad on the left enough so that the first icon starts at the same |
| 44 // point as the labels, and enough on the right so that the final icon ends |
| 45 // at the same point as the arrows in the menu. However, there is outer |
| 46 // padding on the container (so we can see the drop indicator), so we need to |
| 47 // compensate. |
| 48 // We subtract one from label_start because we want the pixel *before* the |
| 49 // label. |
| 50 start_padding_ = views::MenuItemView::label_start() - 1 - |
| 51 BrowserActionsContainer::kItemSpacing; |
| 52 end_padding_ = menu_root->GetMenuConfig().arrow_to_edge_padding - |
| 53 BrowserActionsContainer::kItemSpacing; |
| 54 |
| 55 // Find the width we have for possible content. |
| 56 int content_width = menu_root->GetSubmenu()->GetPreferredSize().width() - |
| 57 start_padding_ - end_padding_; |
| 58 // We subtract an extra padding from the content width to make division easy. |
| 59 content_width -= BrowserActionsContainer::kItemSpacing; |
| 60 |
| 61 int icons_per_row = content_width / BrowserActionsContainer::IconWidth(true); |
| 62 // If it's not a perfect fit (which is very likely), we add another icon and |
| 63 // increase the menu size. |
| 64 if (content_width % BrowserActionsContainer::IconWidth(true)) |
| 65 ++icons_per_row; |
| 66 |
| 67 // We should definitely have more than 0 icons per row. |
| 68 DCHECK_NE(0, icons_per_row); |
| 69 BrowserActionsContainer::icons_per_menu_row = icons_per_row; |
| 70 PreferredSizeChanged(); |
| 71 } |
| 72 |
40 gfx::Size ExtensionToolbarMenuView::GetPreferredSize() const { | 73 gfx::Size ExtensionToolbarMenuView::GetPreferredSize() const { |
41 return container_->GetPreferredSize(); | 74 gfx::Size size = container_->GetPreferredSize(); |
| 75 size.set_width(size.width() + start_padding_ + end_padding_); |
| 76 return size; |
42 } | 77 } |
43 | 78 |
44 void ExtensionToolbarMenuView::Layout() { | 79 void ExtensionToolbarMenuView::Layout() { |
45 // All buttons are given the same width. | |
46 gfx::Size sz = GetPreferredSize(); | 80 gfx::Size sz = GetPreferredSize(); |
47 SetBounds(views::MenuItemView::label_start(), 0, sz.width(), sz.height()); | 81 container_->SetBounds( |
48 container_->SetBounds(0, 0, sz.width(), sz.height()); | 82 start_padding_ + 1, 0, sz.width() - end_padding_, sz.height()); |
49 } | 83 } |
50 | 84 |
51 void ExtensionToolbarMenuView::OnBrowserActionDragDone() { | 85 void ExtensionToolbarMenuView::OnBrowserActionDragDone() { |
52 DCHECK(wrench_menu_->for_drop()); | 86 DCHECK(wrench_menu_->for_drop()); |
53 wrench_menu_->CloseMenu(); | 87 wrench_menu_->CloseMenu(); |
54 } | 88 } |
OLD | NEW |