OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "ash/common/system/tray/tri_view.h" |
| 6 |
| 7 #include "ash/common/system/tray/size_range_layout.h" |
| 8 #include "base/logging.h" |
| 9 #include "ui/views/border.h" |
| 10 #include "ui/views/layout/box_layout.h" |
| 11 #include "ui/views/layout/fill_layout.h" |
| 12 #include "ui/views/layout/layout_manager.h" |
| 13 |
| 14 namespace ash { |
| 15 namespace { |
| 16 |
| 17 // Converts TriView::Orientation values to views::BoxLayout::Orientation values. |
| 18 views::BoxLayout::Orientation GetOrientation(TriView::Orientation orientation) { |
| 19 switch (orientation) { |
| 20 case TriView::Orientation::HORIZONTAL: |
| 21 return views::BoxLayout::kHorizontal; |
| 22 case TriView::Orientation::VERTICAL: |
| 23 return views::BoxLayout::kVertical; |
| 24 } |
| 25 // Required for some compilers. |
| 26 NOTREACHED(); |
| 27 return views::BoxLayout::kHorizontal; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 TriView::TriView() : TriView(0) {} |
| 33 |
| 34 TriView::TriView(int padding_between_containers) |
| 35 : TriView(Orientation::HORIZONTAL, padding_between_containers) {} |
| 36 |
| 37 TriView::TriView(Orientation orientation) : TriView(orientation, 0) {} |
| 38 |
| 39 TriView::TriView(Orientation orientation, int padding_between_containers) |
| 40 : box_layout_(new views::BoxLayout(GetOrientation(orientation), |
| 41 0, |
| 42 0, |
| 43 padding_between_containers)), |
| 44 start_container_layout_manager_(new SizeRangeLayout), |
| 45 center_container_layout_manager_(new SizeRangeLayout), |
| 46 end_container_layout_manager_(new SizeRangeLayout) { |
| 47 AddChildView(new views::View); |
| 48 AddChildView(new views::View); |
| 49 AddChildView(new views::View); |
| 50 |
| 51 GetContainer(Container::START) |
| 52 ->SetLayoutManager(GetLayoutManager(Container::START)); |
| 53 GetContainer(Container::CENTER) |
| 54 ->SetLayoutManager(GetLayoutManager(Container::CENTER)); |
| 55 GetContainer(Container::END) |
| 56 ->SetLayoutManager(GetLayoutManager(Container::END)); |
| 57 |
| 58 GetLayoutManager(Container::START) |
| 59 ->SetLayoutManager(CreateDefaultLayoutManager(orientation)); |
| 60 GetLayoutManager(Container::CENTER) |
| 61 ->SetLayoutManager(CreateDefaultLayoutManager(orientation)); |
| 62 GetLayoutManager(Container::END) |
| 63 ->SetLayoutManager(CreateDefaultLayoutManager(orientation)); |
| 64 |
| 65 SetLayoutManager(box_layout_); |
| 66 |
| 67 enable_hierarchy_changed_dcheck_ = true; |
| 68 } |
| 69 |
| 70 TriView::~TriView() { |
| 71 enable_hierarchy_changed_dcheck_ = false; |
| 72 } |
| 73 |
| 74 void TriView::SetMinCrossAxisSize(int min_size) { |
| 75 box_layout_->set_minimum_cross_axis_size(min_size); |
| 76 } |
| 77 |
| 78 void TriView::SetMinSize(Container container, const gfx::Size& size) { |
| 79 GetLayoutManager(container)->SetMinSize(size); |
| 80 } |
| 81 |
| 82 void TriView::SetMaxSize(Container container, const gfx::Size& size) { |
| 83 GetLayoutManager(container)->SetMaxSize(size); |
| 84 } |
| 85 |
| 86 void TriView::AddView(Container container, views::View* view) { |
| 87 GetContainer(container)->AddChildView(view); |
| 88 } |
| 89 |
| 90 void TriView::RemoveAllChildren(Container container, bool delete_children) { |
| 91 GetContainer(container)->RemoveAllChildViews(delete_children); |
| 92 } |
| 93 |
| 94 void TriView::SetInsets(const gfx::Insets& insets) { |
| 95 box_layout_->set_inside_border_insets(insets); |
| 96 } |
| 97 |
| 98 void TriView::SetContainerBorder(Container container, |
| 99 std::unique_ptr<views::Border> border) { |
| 100 GetContainer(container)->SetBorder(std::move(border)); |
| 101 } |
| 102 |
| 103 void TriView::SetContainerVisible(Container container, bool visible) { |
| 104 GetContainer(container)->SetVisible(visible); |
| 105 } |
| 106 |
| 107 void TriView::SetFlexForContainer(Container container, int flex) { |
| 108 box_layout_->SetFlexForView(GetContainer(container), flex); |
| 109 } |
| 110 |
| 111 void TriView::SetContainerLayout( |
| 112 Container container, |
| 113 std::unique_ptr<views::LayoutManager> layout_manager) { |
| 114 GetLayoutManager(container)->SetLayoutManager(std::move(layout_manager)); |
| 115 if (GetLayoutManager(container)) |
| 116 GetLayoutManager(container)->Installed(GetContainer(container)); |
| 117 } |
| 118 |
| 119 void TriView::ViewHierarchyChanged( |
| 120 const views::View::ViewHierarchyChangedDetails& details) { |
| 121 views::View::ViewHierarchyChanged(details); |
| 122 if (!enable_hierarchy_changed_dcheck_) |
| 123 return; |
| 124 |
| 125 if (details.parent == this) { |
| 126 if (details.is_add) { |
| 127 DCHECK(false) |
| 128 << "Child views should not be added directly. They should be added " |
| 129 "using TriView::AddView()."; |
| 130 } else { |
| 131 DCHECK(false) << "Container views should not be removed."; |
| 132 } |
| 133 } |
| 134 } |
| 135 |
| 136 std::unique_ptr<views::LayoutManager> TriView::CreateDefaultLayoutManager( |
| 137 Orientation orientation) const { |
| 138 views::BoxLayout* box_layout = |
| 139 new views::BoxLayout(GetOrientation(orientation), 0, 0, 0); |
| 140 box_layout->set_main_axis_alignment( |
| 141 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 142 box_layout->set_cross_axis_alignment( |
| 143 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 144 return std::unique_ptr<views::LayoutManager>(box_layout); |
| 145 } |
| 146 |
| 147 views::View* TriView::GetContainer(Container container) const { |
| 148 return const_cast<views::View*>(child_at(static_cast<int>(container))); |
| 149 } |
| 150 |
| 151 SizeRangeLayout* TriView::GetLayoutManager(Container container) const { |
| 152 switch (container) { |
| 153 case Container::START: |
| 154 return start_container_layout_manager_; |
| 155 case Container::CENTER: |
| 156 return center_container_layout_manager_; |
| 157 case Container::END: |
| 158 return end_container_layout_manager_; |
| 159 } |
| 160 // Required for some compilers. |
| 161 NOTREACHED(); |
| 162 return nullptr; |
| 163 } |
| 164 |
| 165 } // namespace ash |
OLD | NEW |