Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: ash/common/system/tray/three_view_layout.cc

Issue 2414103003: Added common layout framework for system menu rows. (Closed)
Patch Set: Moved |layout_manager_| destruction order in ~View(). Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/three_view_layout.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
13 namespace ash {
14 namespace {
15
16 // Converts ThreeViewLayout::Orientation values to
17 // views::BoxLayout::Orientation values.
18 views::BoxLayout::Orientation GetOrientation(
19 ThreeViewLayout::Orientation orientation) {
20 switch (orientation) {
21 case ThreeViewLayout::Orientation::HORIZONTAL:
22 return views::BoxLayout::kHorizontal;
23 case ThreeViewLayout::Orientation::VERTICAL:
24 return views::BoxLayout::kVertical;
25 }
26 // Required for some compilers.
27 NOTREACHED();
28 return views::BoxLayout::kHorizontal;
29 }
30
31 } // namespace
32
33 ThreeViewLayout::ThreeViewLayout() : ThreeViewLayout(0) {}
34
35 ThreeViewLayout::ThreeViewLayout(int padding_between_containers)
36 : ThreeViewLayout(Orientation::HORIZONTAL, padding_between_containers) {}
37
38 ThreeViewLayout::ThreeViewLayout(Orientation orientation)
39 : ThreeViewLayout(orientation, 0) {}
40
41 ThreeViewLayout::ThreeViewLayout(Orientation orientation,
42 int padding_between_containers)
43 : box_layout_(new views::BoxLayout(GetOrientation(orientation),
44 0,
45 0,
46 padding_between_containers)),
47 start_container_(new views::View),
48 start_container_layout_manager_(new SizeRangeLayout),
49 center_container_(new views::View),
50 center_container_layout_manager_(new SizeRangeLayout),
51 end_container_(new views::View),
52 end_container_layout_manager_(new SizeRangeLayout) {
53 start_container_->SetLayoutManager(GetLayoutManager(Container::START));
54 center_container_->SetLayoutManager(GetLayoutManager(Container::CENTER));
55 end_container_->SetLayoutManager(GetLayoutManager(Container::END));
56 GetLayoutManager(Container::START)
57 ->SetLayoutManager(CreateDefaultLayoutManager(orientation));
58 GetLayoutManager(Container::CENTER)
59 ->SetLayoutManager(CreateDefaultLayoutManager(orientation));
60 GetLayoutManager(Container::END)
61 ->SetLayoutManager(CreateDefaultLayoutManager(orientation));
62 }
63
64 ThreeViewLayout::~ThreeViewLayout() {
65 if (host_) {
66 host_->RemoveChildView(GetContainer(Container::START));
67 host_->RemoveChildView(GetContainer(Container::CENTER));
68 host_->RemoveChildView(GetContainer(Container::END));
69 }
70 }
71
72 void ThreeViewLayout::SetMinCrossAxisSize(int min_size) {
73 box_layout_->set_minimum_cross_axis_size(min_size);
74 }
75
76 void ThreeViewLayout::SetMinSize(Container container, const gfx::Size& size) {
77 GetLayoutManager(container)->SetMinSize(size);
78 }
79
80 void ThreeViewLayout::SetMaxSize(Container container, const gfx::Size& size) {
81 GetLayoutManager(container)->SetMaxSize(size);
82 }
83
84 void ThreeViewLayout::AddView(Container container, views::View* view) {
85 GetContainer(container)->AddChildView(view);
86 }
87
88 void ThreeViewLayout::SetView(Container container, views::View* view) {
89 views::View* container_view = GetContainer(container);
90 container_view->RemoveAllChildViews(true);
91 container_view->AddChildView(view);
92 }
93
94 void ThreeViewLayout::SetInsets(const gfx::Insets& insets) {
95 box_layout_->set_inside_border_insets(insets);
96 }
97
98 void ThreeViewLayout::SetBorder(Container container,
99 std::unique_ptr<views::Border> border) {
100 GetContainer(container)->SetBorder(std::move(border));
101 }
102
103 void ThreeViewLayout::SetContainerVisible(Container container, bool visible) {
104 GetContainer(container)->SetVisible(visible);
105 }
106
107 void ThreeViewLayout::SetFlexForContainer(Container container, int flex) {
108 box_layout_->SetFlexForView(GetContainer(container), flex);
109 }
110
111 void ThreeViewLayout::SetLayoutManager(
112 Container container,
113 std::unique_ptr<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 ThreeViewLayout::Installed(views::View* host) {
120 DCHECK(!host_);
121 host_ = host;
122 if (host_) {
123 host_->AddChildView(GetContainer(Container::START));
124 host_->AddChildView(GetContainer(Container::CENTER));
125 host_->AddChildView(GetContainer(Container::END));
126 }
127 box_layout_->Installed(host_);
128 }
129
130 void ThreeViewLayout::Layout(views::View* host) {
131 box_layout_->Layout(host);
132 }
133
134 gfx::Size ThreeViewLayout::GetPreferredSize(const views::View* host) const {
135 return box_layout_->GetPreferredSize(host);
136 }
137
138 int ThreeViewLayout::GetPreferredHeightForWidth(const views::View* host,
139 int width) const {
140 return box_layout_->GetPreferredHeightForWidth(host, width);
141 }
142
143 void ThreeViewLayout::ViewAdded(views::View* host, views::View* view) {
144 box_layout_->ViewAdded(host, view);
145 }
146
147 void ThreeViewLayout::ViewRemoved(views::View* host, views::View* view) {
148 box_layout_->ViewRemoved(host, view);
149 }
150
151 std::unique_ptr<views::LayoutManager>
152 ThreeViewLayout::CreateDefaultLayoutManager(Orientation orientation) const {
153 views::BoxLayout* box_layout =
154 new views::BoxLayout(GetOrientation(orientation), 0, 0, 0);
155 box_layout->set_main_axis_alignment(
156 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
157 box_layout->set_cross_axis_alignment(
158 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
159 return std::unique_ptr<views::LayoutManager>(box_layout);
160 }
161
162 views::View* ThreeViewLayout::GetContainer(Container container) const {
163 switch (container) {
164 case Container::START:
165 return start_container_;
166 case Container::CENTER:
167 return center_container_;
168 case Container::END:
169 return end_container_;
170 }
171 // Required for some compilers.
172 NOTREACHED();
173 return nullptr;
174 }
175
176 SizeRangeLayout* ThreeViewLayout::GetLayoutManager(Container container) const {
177 switch (container) {
178 case Container::START:
179 return start_container_layout_manager_;
180 case Container::CENTER:
181 return center_container_layout_manager_;
182 case Container::END:
183 return end_container_layout_manager_;
184 }
185 // Required for some compilers.
186 NOTREACHED();
187 return nullptr;
188 }
189
190 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/tray/three_view_layout.h ('k') | ash/common/system/tray/three_view_layout_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698