Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/views/controls/menu/submenu_view.h" | 5 #include "ui/views/controls/menu/submenu_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "ui/accessibility/ax_view_state.h" | 10 #include "ui/accessibility/ax_view_state.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 | 123 |
| 124 gfx::Size SubmenuView::GetPreferredSize() const { | 124 gfx::Size SubmenuView::GetPreferredSize() const { |
| 125 if (!has_children()) | 125 if (!has_children()) |
| 126 return gfx::Size(); | 126 return gfx::Size(); |
| 127 | 127 |
| 128 max_minor_text_width_ = 0; | 128 max_minor_text_width_ = 0; |
| 129 // The maximum width of items which contain maybe a label and multiple views. | 129 // The maximum width of items which contain maybe a label and multiple views. |
| 130 int max_complex_width = 0; | 130 int max_complex_width = 0; |
| 131 // The max. width of items which contain a label and maybe an accelerator. | 131 // The max. width of items which contain a label and maybe an accelerator. |
| 132 int max_simple_width = 0; | 132 int max_simple_width = 0; |
| 133 int height = 0; | 133 |
| 134 // We perform the size calculation in two passes. In the first pass, we | |
| 135 // calculate the width of the menu. In the second, we calculate the height | |
| 136 // using that width. This allows views that have flexible widths to adjust | |
| 137 // accordingly. | |
| 134 for (int i = 0; i < child_count(); ++i) { | 138 for (int i = 0; i < child_count(); ++i) { |
| 135 const View* child = child_at(i); | 139 const View* child = child_at(i); |
| 136 if (!child->visible()) | 140 if (!child->visible()) |
| 137 continue; | 141 continue; |
| 138 if (child->id() == MenuItemView::kMenuItemViewID) { | 142 if (child->id() == MenuItemView::kMenuItemViewID) { |
| 139 const MenuItemView* menu = static_cast<const MenuItemView*>(child); | 143 const MenuItemView* menu = static_cast<const MenuItemView*>(child); |
| 140 const MenuItemView::MenuItemDimensions& dimensions = | 144 const MenuItemView::MenuItemDimensions& dimensions = |
| 141 menu->GetDimensions(); | 145 menu->GetDimensions(); |
| 142 max_simple_width = std::max( | 146 max_simple_width = std::max( |
| 143 max_simple_width, dimensions.standard_width); | 147 max_simple_width, dimensions.standard_width); |
| 144 max_minor_text_width_ = | 148 max_minor_text_width_ = |
| 145 std::max(max_minor_text_width_, dimensions.minor_text_width); | 149 std::max(max_minor_text_width_, dimensions.minor_text_width); |
| 146 max_complex_width = std::max(max_complex_width, | 150 max_complex_width = std::max(max_complex_width, |
| 147 dimensions.standard_width + dimensions.children_width); | 151 dimensions.standard_width + dimensions.children_width); |
| 148 height += dimensions.height; | |
| 149 } else { | 152 } else { |
| 150 gfx::Size child_pref_size = | 153 max_complex_width = std::max(max_complex_width, |
| 151 child->visible() ? child->GetPreferredSize() : gfx::Size(); | 154 child->GetPreferredSize().width()); |
| 152 max_complex_width = std::max(max_complex_width, child_pref_size.width()); | |
| 153 height += child_pref_size.height(); | |
| 154 } | 155 } |
| 155 } | 156 } |
| 156 if (max_minor_text_width_ > 0) { | 157 if (max_minor_text_width_ > 0) { |
| 157 max_minor_text_width_ += | 158 max_minor_text_width_ += |
| 158 GetMenuItem()->GetMenuConfig().label_to_minor_text_padding; | 159 GetMenuItem()->GetMenuConfig().label_to_minor_text_padding; |
| 159 } | 160 } |
| 161 // Finish calculating our optimum width. | |
| 160 gfx::Insets insets = GetInsets(); | 162 gfx::Insets insets = GetInsets(); |
| 161 return gfx::Size( | 163 int width = std::max(max_complex_width, |
| 162 std::max(max_complex_width, | 164 std::max(max_simple_width + max_minor_text_width_ + |
| 163 std::max(max_simple_width + max_minor_text_width_ + | 165 insets.width(), |
| 164 insets.width(), | 166 minimum_preferred_width_ - 2 * insets.width())); |
| 165 minimum_preferred_width_ - 2 * insets.width())), | 167 |
| 166 height + insets.height()); | 168 // Then, the height for that width. |
| 169 int height = 0; | |
| 170 for (int i = 0; i < child_count(); ++i) { | |
| 171 const View* child = child_at(i); | |
| 172 height += child->visible() ? child->GetHeightForWidth(width) : 0; | |
| 173 } | |
| 174 | |
| 175 return gfx::Size(width, height + insets.height()); | |
|
sky
2014/09/12 20:09:38
Can you write test coverage of this?
Devlin
2014/09/12 22:41:37
But of course! Done. :)
| |
| 167 } | 176 } |
| 168 | 177 |
| 169 void SubmenuView::GetAccessibleState(ui::AXViewState* state) { | 178 void SubmenuView::GetAccessibleState(ui::AXViewState* state) { |
| 170 // Inherit most of the state from the parent menu item, except the role. | 179 // Inherit most of the state from the parent menu item, except the role. |
| 171 if (GetMenuItem()) | 180 if (GetMenuItem()) |
| 172 GetMenuItem()->GetAccessibleState(state); | 181 GetMenuItem()->GetAccessibleState(state); |
| 173 state->role = ui::AX_ROLE_MENU_LIST_POPUP; | 182 state->role = ui::AX_ROLE_MENU_LIST_POPUP; |
| 174 } | 183 } |
| 175 | 184 |
| 176 ui::TextInputClient* SubmenuView::GetTextInputClient() { | 185 ui::TextInputClient* SubmenuView::GetTextInputClient() { |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 492 y = std::max(y, 0); | 501 y = std::max(y, 0); |
| 493 gfx::Rect new_vis_bounds(x, y, vis_bounds.width(), vis_bounds.height()); | 502 gfx::Rect new_vis_bounds(x, y, vis_bounds.width(), vis_bounds.height()); |
| 494 if (new_vis_bounds != vis_bounds) { | 503 if (new_vis_bounds != vis_bounds) { |
| 495 ScrollRectToVisible(new_vis_bounds); | 504 ScrollRectToVisible(new_vis_bounds); |
| 496 return true; | 505 return true; |
| 497 } | 506 } |
| 498 return false; | 507 return false; |
| 499 } | 508 } |
| 500 | 509 |
| 501 } // namespace views | 510 } // namespace views |
| OLD | NEW |