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 // A View that will perform a layout if a child view's preferred size changes. | |
31 class RelayoutView : public views::View { | |
32 public: | |
33 RelayoutView() {} | |
34 | |
35 // views::View: | |
36 void ChildPreferredSizeChanged(View* child) override { Layout(); } | |
37 | |
38 private: | |
39 DISALLOW_COPY_AND_ASSIGN(RelayoutView); | |
40 }; | |
41 | |
42 } // namespace | |
43 | |
44 TriView::TriView() : TriView(0) {} | |
45 | |
46 TriView::TriView(int padding_between_containers) | |
47 : TriView(Orientation::HORIZONTAL, padding_between_containers) {} | |
48 | |
49 TriView::TriView(Orientation orientation) : TriView(orientation, 0) {} | |
50 | |
51 TriView::TriView(Orientation orientation, int padding_between_containers) | |
52 : box_layout_(new views::BoxLayout(GetOrientation(orientation), | |
53 0, | |
54 0, | |
55 padding_between_containers)), | |
56 start_container_layout_manager_(new SizeRangeLayout), | |
57 center_container_layout_manager_(new SizeRangeLayout), | |
58 end_container_layout_manager_(new SizeRangeLayout) { | |
59 AddChildView(new RelayoutView); | |
60 AddChildView(new RelayoutView); | |
61 AddChildView(new RelayoutView); | |
62 | |
63 GetContainer(Container::START) | |
64 ->SetLayoutManager(GetLayoutManager(Container::START)); | |
65 GetContainer(Container::CENTER) | |
66 ->SetLayoutManager(GetLayoutManager(Container::CENTER)); | |
67 GetContainer(Container::END) | |
68 ->SetLayoutManager(GetLayoutManager(Container::END)); | |
69 | |
70 box_layout_->set_cross_axis_alignment( | |
71 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | |
72 SetLayoutManager(box_layout_); | |
73 | |
74 enable_hierarchy_changed_dcheck_ = true; | |
75 } | |
76 | |
77 TriView::~TriView() { | |
78 enable_hierarchy_changed_dcheck_ = false; | |
79 } | |
80 | |
81 void TriView::SetMinHeight(int height) { | |
82 gfx::Size min_size; | |
83 | |
84 min_size = GetMinSize(TriView::Container::START); | |
85 min_size.set_height(height); | |
86 SetMinSize(TriView::Container::START, min_size); | |
87 | |
88 min_size = GetMinSize(TriView::Container::CENTER); | |
89 min_size.set_height(height); | |
90 SetMinSize(TriView::Container::CENTER, min_size); | |
91 | |
92 min_size = GetMinSize(TriView::Container::END); | |
93 min_size.set_height(height); | |
94 SetMinSize(TriView::Container::END, min_size); | |
95 } | |
96 | |
97 void TriView::SetMinSize(Container container, const gfx::Size& size) { | |
98 GetLayoutManager(container)->SetMinSize(size); | |
99 } | |
100 | |
101 gfx::Size TriView::GetMinSize(Container container) { | |
102 return GetLayoutManager(container)->min_size(); | |
103 } | |
104 | |
105 void TriView::SetMaxSize(Container container, const gfx::Size& size) { | |
106 GetLayoutManager(container)->SetMaxSize(size); | |
107 } | |
108 | |
109 void TriView::AddView(Container container, views::View* view) { | |
110 GetContainer(container)->AddChildView(view); | |
111 } | |
112 | |
113 void TriView::RemoveAllChildren(Container container, bool delete_children) { | |
114 GetContainer(container)->RemoveAllChildViews(delete_children); | |
115 } | |
116 | |
117 void TriView::SetInsets(const gfx::Insets& insets) { | |
118 box_layout_->set_inside_border_insets(insets); | |
119 } | |
120 | |
121 void TriView::SetContainerBorder(Container container, | |
122 std::unique_ptr<views::Border> border) { | |
123 GetContainer(container)->SetBorder(std::move(border)); | |
124 } | |
125 | |
126 void TriView::SetContainerVisible(Container container, bool visible) { | |
127 GetContainer(container)->SetVisible(visible); | |
128 } | |
129 | |
130 void TriView::SetFlexForContainer(Container container, int flex) { | |
131 box_layout_->SetFlexForView(GetContainer(container), flex); | |
132 } | |
133 | |
134 void TriView::SetContainerLayout( | |
135 Container container, | |
136 std::unique_ptr<views::LayoutManager> layout_manager) { | |
137 GetLayoutManager(container)->SetLayoutManager(std::move(layout_manager)); | |
138 } | |
139 | |
140 void TriView::ViewHierarchyChanged( | |
141 const views::View::ViewHierarchyChangedDetails& details) { | |
142 views::View::ViewHierarchyChanged(details); | |
143 if (!enable_hierarchy_changed_dcheck_) | |
144 return; | |
145 | |
146 if (details.parent == this) { | |
147 if (details.is_add) { | |
148 DCHECK(false) | |
149 << "Child views should not be added directly. They should be added " | |
150 "using TriView::AddView()."; | |
151 } else { | |
152 DCHECK(false) << "Container views should not be removed."; | |
153 } | |
154 } | |
155 } | |
156 | |
157 const char* TriView::GetClassName() const { | |
158 return "TriView"; | |
159 } | |
160 | |
161 views::View* TriView::GetContainer(Container container) { | |
162 return child_at(static_cast<int>(container)); | |
163 } | |
164 | |
165 SizeRangeLayout* TriView::GetLayoutManager(Container container) { | |
166 switch (container) { | |
167 case Container::START: | |
168 return start_container_layout_manager_; | |
169 case Container::CENTER: | |
170 return center_container_layout_manager_; | |
171 case Container::END: | |
172 return end_container_layout_manager_; | |
173 } | |
174 // Required for some compilers. | |
175 NOTREACHED(); | |
176 return nullptr; | |
177 } | |
178 | |
179 } // namespace ash | |
OLD | NEW |