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

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: 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::HORIZONTAL:
22 return views::BoxLayout::kHorizontal;
23 case ThreeViewLayout::VERTICAL:
24 return views::BoxLayout::kVertical;
25 }
26 }
27
28 } // namespace
29
30 ThreeViewLayout::ThreeViewLayout(int padding_between_items)
31 : ThreeViewLayout(HORIZONTAL, padding_between_items) {}
32
33 ThreeViewLayout::ThreeViewLayout(Orientation orientation,
34 int padding_between_items)
35 : box_layout_(new views::BoxLayout(GetOrientation(orientation),
36 0,
37 0,
38 padding_between_items)),
39 start_container_(new views::View),
40 start_container_layout_manager_(new SizeRangeLayout),
41 center_container_(new views::View),
42 center_container_layout_manager_(new SizeRangeLayout),
43 end_container_(new views::View),
44 end_container_layout_manager_(new SizeRangeLayout) {
45 start_container_->SetLayoutManager(GetLayoutManager(START));
46 center_container_->SetLayoutManager(GetLayoutManager(CENTER));
47 end_container_->SetLayoutManager(GetLayoutManager(END));
48 GetLayoutManager(START)->SetLayoutManager(
49 CreateDefaultLayoutManager(orientation));
50 GetLayoutManager(CENTER)->SetLayoutManager(
51 CreateDefaultLayoutManager(orientation));
52 GetLayoutManager(END)->SetLayoutManager(
53 CreateDefaultLayoutManager(orientation));
54 }
55
56 ThreeViewLayout::~ThreeViewLayout() {}
57
58 void ThreeViewLayout::SetMinCrossAxisSize(int min_size) {
59 box_layout_->set_minimum_cross_axis_size(min_size);
60 }
61
62 void ThreeViewLayout::SetMinSize(Container container, const gfx::Size& size) {
63 GetLayoutManager(container)->set_min_size(size);
64 }
65
66 void ThreeViewLayout::SetMaxSize(Container container, const gfx::Size& size) {
67 GetLayoutManager(container)->set_max_size(size);
68 }
69
70 void ThreeViewLayout::AddView(Container container, views::View* view) {
71 views::View* container_view = GetContainer(container);
72 container_view->AddChildView(view);
73 }
74
75 void ThreeViewLayout::SetView(Container container, views::View* view) {
76 views::View* container_view = GetContainer(container);
77 container_view->RemoveAllChildViews(true);
78 container_view->AddChildView(view);
79 }
80
81 void ThreeViewLayout::SetInsets(const gfx::Insets& insets) {
82 box_layout_->set_inside_border_insets(insets);
83 }
84
85 void ThreeViewLayout::SetInsets(Container container,
86 const gfx::Insets& insets) {
87 GetContainer(container)->SetBorder(views::Border::CreateEmptyBorder(insets));
88 }
89
90 void ThreeViewLayout::SetContainerVisible(Container container, bool visible) {
91 GetContainer(container)->SetVisible(visible);
92 }
93
94 void ThreeViewLayout::SetFlexForView(Container container, int flex) {
95 box_layout_->SetFlexForView(GetContainer(container), flex);
96 }
97
98 void ThreeViewLayout::SetLayoutManager(
99 Container container,
100 std::unique_ptr<LayoutManager> layout_manager) {
101 if (GetLayoutManager(container))
102 GetLayoutManager(container)->Uninstalled(GetContainer(container));
103 GetLayoutManager(container)->SetLayoutManager(std::move(layout_manager));
104 if (GetLayoutManager(container))
105 GetLayoutManager(container)->Installed(GetContainer(container));
106 }
107
108 void ThreeViewLayout::Installed(views::View* host) {
109 DCHECK(!host_);
110 host_ = host;
111 if (host_) {
112 host_->AddChildView(GetContainer(START));
113 host_->AddChildView(GetContainer(CENTER));
114 host_->AddChildView(GetContainer(END));
115 }
116 box_layout_->Installed(host_);
117 }
118
119 void ThreeViewLayout::Uninstalled(views::View* host) {
120 DCHECK_EQ(host_, host);
121 if (host_) {
122 host_->RemoveChildView(GetContainer(START));
123 host_->RemoveChildView(GetContainer(CENTER));
124 host_->RemoveChildView(GetContainer(END));
125 }
126 box_layout_->Uninstalled(host);
127 host_ = nullptr;
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 START:
165 return start_container_;
166 case CENTER:
167 return center_container_;
168 case END:
169 return end_container_;
170 }
171 }
172
173 SizeRangeLayout* ThreeViewLayout::GetLayoutManager(Container container) const {
174 switch (container) {
175 case START:
176 return start_container_layout_manager_;
177 case CENTER:
178 return center_container_layout_manager_;
179 case END:
180 return end_container_layout_manager_;
181 }
182 }
183
184 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698