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

Side by Side Diff: ui/views/examples/box_layout_example.cc

Issue 2836313002: BoxLayout now suports per-view margins. (Closed)
Patch Set: Added unit tests. Fixed GetPreferredSize() calculations. Refactored some code. Created 3 years, 7 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 2017 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 "box_layout_example.h"
6
7 #include <vector>
8
9 #include "base/memory/ptr_util.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #include "ui/base/models/combobox_model.h"
15 #include "ui/gfx/geometry/insets.h"
16 #include "ui/gfx/geometry/point.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/views/border.h"
19 #include "ui/views/controls/button/checkbox.h"
20 #include "ui/views/controls/button/md_text_button.h"
21 #include "ui/views/controls/combobox/combobox.h"
22 #include "ui/views/controls/label.h"
23 #include "ui/views/controls/textfield/textfield.h"
24 #include "ui/views/examples/example_combobox_model.h"
25 #include "ui/views/layout/fill_layout.h"
26 #include "ui/views/view.h"
27
28 namespace views {
29 namespace examples {
30
31 namespace {
32
33 // This View holds two other views which consists of a view on the left onto
34 // which the BoxLayout is attached for demonstrating its features. The view
35 // on the right contains all the various controls which allow the user to
36 // interactively control the various features/properties of BoxLayout. Layout()
37 // will ensure the left view takes 75% and the right view fills the remaining
38 // 25%.
39 class FullPanel : public View {
40 public:
41 FullPanel() {}
42 ~FullPanel() override {}
43
44 // View
45 void Layout() override;
46
47 private:
48 DISALLOW_COPY_AND_ASSIGN(FullPanel);
49 };
50
51 // This view is created and added to the left-side view in the FullPanel each
52 // time the "Add" button is pressed. It also will display Textfield controls
53 // when the mouse is pressed over the view. These Textfields allow the user to
54 // interactively set each margin and the "flex" for the given view.
55 class ChildPanel : public View, public TextfieldController {
56 public:
57 explicit ChildPanel(BoxLayoutExample* example);
58 ~ChildPanel() override {}
59
60 // View
61 gfx::Size GetPreferredSize() const override;
62 bool OnMousePressed(const ui::MouseEvent& event) override;
63 void Layout() override;
64
65 void SetSelected(bool value);
66 bool selected() const { return selected_; };
67
68 int GetFlex();
69
70 private:
71 // TextfieldController
72 void ContentsChanged(Textfield* sender,
73 const base::string16& new_contents) override;
74
75 Textfield* CreateTextfield();
76
77 BoxLayoutExample* example_;
78 bool selected_ = false;
79 Textfield* flex_;
80 Textfield* margin_[4];
81
82 DISALLOW_COPY_AND_ASSIGN(ChildPanel);
83 };
84
85 void FullPanel::Layout() {
86 DCHECK_EQ(child_count(), 2);
87 View* left_panel = child_at(0);
88 View* right_panel = child_at(1);
89 gfx::Rect bounds = GetContentsBounds();
90 left_panel->SetBounds(bounds.x(), bounds.y(), (bounds.width() * 75) / 100,
91 bounds.height());
92 right_panel->SetBounds(left_panel->width(), bounds.y(),
93 bounds.width() - left_panel->width(), bounds.height());
94 }
95
96 ChildPanel::ChildPanel(BoxLayoutExample* example) : View(), example_(example) {
97 SetBorder(CreateSolidBorder(1, SK_ColorGRAY));
98 for (unsigned i = 0; i < sizeof(margin_) / sizeof(margin_[0]); ++i)
99 margin_[i] = CreateTextfield();
100 flex_ = CreateTextfield();
101 flex_->SetText(base::ASCIIToUTF16(""));
102 }
103
104 gfx::Size ChildPanel::GetPreferredSize() const {
105 return gfx::Size(180, 90);
106 }
107
108 bool ChildPanel::OnMousePressed(const ui::MouseEvent& event) {
109 if (event.IsOnlyLeftMouseButton())
110 SetSelected(true);
111 return true;
112 }
113
114 void ChildPanel::Layout() {
115 const int kSpacing = 2;
116 if (selected_) {
117 gfx::Rect client = GetContentsBounds();
118 for (unsigned i = 0; i < sizeof(margin_) / sizeof(margin_[0]); ++i) {
119 gfx::Point pos;
120 Textfield* textfield = margin_[i];
121 switch (i) {
122 case 0:
123 pos = gfx::Point((client.width() - textfield->width()) / 2, kSpacing);
124 break;
125 case 1:
126 pos =
127 gfx::Point(kSpacing, (client.height() - textfield->height()) / 2);
128 break;
129 case 2:
130 pos = gfx::Point((client.width() - textfield->width()) / 2,
131 client.height() - textfield->height() - kSpacing);
132 break;
133 case 3:
134 pos = gfx::Point(client.width() - textfield->width() - kSpacing,
135 (client.height() - textfield->height()) / 2);
136 break;
137 default:
138 NOTREACHED();
139 }
140 textfield->SetPosition(pos);
141 }
142 flex_->SetPosition(gfx::Point((client.width() - flex_->width()) / 2,
143 (client.height() - flex_->height()) / 2));
144 }
145 }
146
147 void ChildPanel::SetSelected(bool value) {
148 if (value != selected_) {
149 selected_ = value;
150 SetBorder(CreateSolidBorder(1, selected_ ? SK_ColorBLACK : SK_ColorGRAY));
151 if (selected_ && parent()) {
152 for (int i = 0; i < parent()->child_count(); ++i) {
153 View* child = parent()->child_at(i);
154 if (child != this && child->GetGroup() == GetGroup()) {
155 ChildPanel* child_panel = static_cast<ChildPanel*>(child);
156 child_panel->SetSelected(false);
157 }
158 }
159 }
160 for (Textfield* textfield : margin_)
161 textfield->SetVisible(selected_);
162 flex_->SetVisible(selected_);
163 InvalidateLayout();
164 example_->RefreshLayoutPanel();
165 }
166 }
167
168 int ChildPanel::GetFlex() {
169 int flex;
170 if (base::StringToInt(flex_->text(), &flex))
171 return flex;
172 return -1;
173 }
174
175 void ChildPanel::ContentsChanged(Textfield* sender,
176 const base::string16& new_contents) {
177 int edges[4];
178 for (unsigned i = 0; i < sizeof(margin_) / sizeof(margin_[0]); ++i) {
179 base::StringToInt(margin_[i]->text(), &edges[i]);
180 }
181 gfx::Insets margins = gfx::Insets(edges[0], edges[1], edges[2], edges[3]);
182 if (!margins.IsEmpty())
183 this->SetProperty(kMarginsKey, new gfx::Insets(margins));
184 else
185 this->ClearProperty(kMarginsKey);
186 if (sender == flex_)
187 example_->UpdateLayoutManager();
188 example_->RefreshLayoutPanel();
189 }
190
191 Textfield* ChildPanel::CreateTextfield() {
192 Textfield* textfield = new Textfield();
193 textfield->set_default_width_in_chars(3);
194 textfield->SizeToPreferredSize();
195 textfield->SetText(base::ASCIIToUTF16("0"));
196 textfield->set_controller(this);
197 textfield->SetVisible(false);
198 AddChildView(textfield);
199 return textfield;
200 }
201
202 const int kSpacing = 3;
203 const int kPadding = 8;
204 const int kMaxPanels = 5;
205 const int kChildPanelGroup = 100;
206 const char* orientation_values[2] = {"Horizontal", "Vertical"};
207 const char* main_axis_values[3] = {"Start", "Center", "End"};
208 const char* cross_axis_values[4] = {"Stretch", "Start", "Center", "End"};
209 }
210
211 BoxLayoutExample::BoxLayoutExample() : ExampleBase("Box Layout") {}
212
213 BoxLayoutExample::~BoxLayoutExample() {}
214
215 Combobox* BoxLayoutExample::CreateCombobox(const base::string16& label_text,
216 const char** items,
217 int count,
218 int& vertical_pos) {
219 Label* label = new Label(label_text);
220 label->SetPosition(gfx::Point(kPadding, vertical_pos));
221 label->SizeToPreferredSize();
222 Combobox* combo_box =
223 new Combobox(base::MakeUnique<ExampleComboboxModel>(items, count));
224 combo_box->SetPosition(
225 gfx::Point(label->x() + label->width() + kSpacing, vertical_pos));
226 combo_box->SizeToPreferredSize();
227 combo_box->set_listener(this);
228 label->SetSize(gfx::Size(label->width(), combo_box->height()));
229 control_panel_->AddChildView(label);
230 control_panel_->AddChildView(combo_box);
231 vertical_pos += combo_box->height() + kSpacing;
232 return combo_box;
233 }
234
235 Textfield* BoxLayoutExample::CreateRawTextfield(int& horizontal_pos,
236 int vertical_pos,
237 bool add) {
238 Textfield* text_field = new Textfield();
239 text_field->SetPosition(gfx::Point(horizontal_pos, vertical_pos));
240 text_field->set_default_width_in_chars(3);
241 text_field->SetTextInputType(ui::TEXT_INPUT_TYPE_NUMBER);
242 text_field->SizeToPreferredSize();
243 text_field->SetText(base::ASCIIToUTF16("0"));
244 text_field->set_controller(this);
245 horizontal_pos += text_field->width() + kSpacing;
246 if (add)
247 control_panel_->AddChildView(text_field);
248 return text_field;
249 }
250
251 Textfield* BoxLayoutExample::CreateTextfield(const base::string16& label_text,
252 int& vertical_pos) {
253 Label* label = new Label(label_text);
254 label->SetPosition(gfx::Point(kPadding, vertical_pos));
255 label->SizeToPreferredSize();
256 int horizontal_pos = label->x() + label->width() + kSpacing;
257 Textfield* text_field =
258 CreateRawTextfield(horizontal_pos, vertical_pos, false);
259 label->SetSize(gfx::Size(label->width(), text_field->height()));
260 control_panel_->AddChildView(label);
261 control_panel_->AddChildView(text_field);
262 vertical_pos += text_field->height() + kSpacing;
263 return text_field;
264 }
265
266 void BoxLayoutExample::CreateExampleView(View* container) {
267 container->SetLayoutManager(new FillLayout());
268 full_panel_ = new FullPanel();
269 container->AddChildView(full_panel_);
270
271 box_layout_panel_ = new View();
272 box_layout_panel_->SetBorder(CreateSolidBorder(1, SK_ColorLTGRAY));
273 full_panel_->AddChildView(box_layout_panel_);
274 control_panel_ = new View();
275 full_panel_->AddChildView(control_panel_);
276
277 int vertical_pos = kSpacing;
278 add_button_ =
279 MdTextButton::CreateSecondaryUiButton(this, base::ASCIIToUTF16("Add"));
280 add_button_->SetPosition(gfx::Point(kPadding, vertical_pos));
281 add_button_->SizeToPreferredSize();
282 control_panel_->AddChildView(add_button_);
283 vertical_pos += add_button_->height() + kSpacing;
284
285 orientation_ = CreateCombobox(base::ASCIIToUTF16("Orientation"),
286 orientation_values, 2, vertical_pos);
287 main_axis_alignment_ = CreateCombobox(base::ASCIIToUTF16("Main axis"),
288 main_axis_values, 3, vertical_pos);
289 cross_axis_alignment_ = CreateCombobox(base::ASCIIToUTF16("Cross axis"),
290 cross_axis_values, 4, vertical_pos);
291
292 between_child_spacing_ =
293 CreateTextfield(base::ASCIIToUTF16("Child spacing"), vertical_pos);
294 default_flex_ =
295 CreateTextfield(base::ASCIIToUTF16("Default flex"), vertical_pos);
296 min_cross_axis_size_ =
297 CreateTextfield(base::ASCIIToUTF16("Min cross axis"), vertical_pos);
298
299 border_insets_[0] =
300 CreateTextfield(base::ASCIIToUTF16("Insets"), vertical_pos);
301 int horizontal_pos =
302 border_insets_[0]->x() + border_insets_[0]->width() + kSpacing;
303 for (unsigned i = 1; i < sizeof(border_insets_) / sizeof(border_insets_[0]);
304 ++i)
305 border_insets_[i] =
306 CreateRawTextfield(horizontal_pos, border_insets_[0]->y(), true);
307
308 collapse_margins_ = new Checkbox(base::ASCIIToUTF16("Collapse margins"));
309 collapse_margins_->SetPosition(gfx::Point(kPadding, vertical_pos));
310 collapse_margins_->SizeToPreferredSize();
311 collapse_margins_->set_listener(this);
312 control_panel_->AddChildView(collapse_margins_);
313
314 UpdateLayoutManager();
315 }
316
317 void BoxLayoutExample::ButtonPressed(Button* sender, const ui::Event& event) {
318 if (sender == add_button_) {
319 if (panel_count_ < kMaxPanels) {
320 ++panel_count_;
321 ChildPanel* panel = new ChildPanel(this);
322 panel->SetGroup(kChildPanelGroup);
323 box_layout_panel_->AddChildView(panel);
324 RefreshLayoutPanel();
325 } else {
326 PrintStatus("Only %i panels may be added", kMaxPanels);
327 }
328 } else if (sender == collapse_margins_) {
329 UpdateLayoutManager();
330 RefreshLayoutPanel();
331 }
332 }
333
334 void BoxLayoutExample::OnPerformAction(Combobox* combobox) {
335 if (combobox == orientation_) {
336 UpdateLayoutManager();
337 } else if (combobox == main_axis_alignment_) {
338 layout_->set_main_axis_alignment(static_cast<BoxLayout::MainAxisAlignment>(
339 main_axis_alignment_->selected_index()));
340 } else if (combobox == cross_axis_alignment_) {
341 layout_->set_cross_axis_alignment(
342 static_cast<BoxLayout::CrossAxisAlignment>(
343 cross_axis_alignment_->selected_index()));
344 }
345 RefreshLayoutPanel();
346 }
347
348 void BoxLayoutExample::ContentsChanged(Textfield* textfield,
349 const base::string16& new_contents) {
350 if (textfield == between_child_spacing_) {
351 UpdateLayoutManager();
352 } else if (textfield == default_flex_) {
353 int default_flex;
354 base::StringToInt(default_flex_->text(), &default_flex);
355 layout_->SetDefaultFlex(default_flex);
356 } else if (textfield == min_cross_axis_size_) {
357 int min_cross_size;
358 base::StringToInt(min_cross_axis_size_->text(), &min_cross_size);
359 layout_->set_minimum_cross_axis_size(min_cross_size);
360 } else if (textfield == border_insets_[0] || textfield == border_insets_[1] ||
361 textfield == border_insets_[2] || textfield == border_insets_[3]) {
362 UpdateBorderInsets();
363 }
364 RefreshLayoutPanel();
365 }
366
367 void BoxLayoutExample::RefreshLayoutPanel() {
368 box_layout_panel_->Layout();
369 box_layout_panel_->SchedulePaint();
370 }
371
372 void BoxLayoutExample::UpdateBorderInsets() {
373 int inset_values[4];
374 for (unsigned i = 0; i < sizeof(border_insets_) / sizeof(border_insets_[0]);
375 ++i)
376 base::StringToInt(border_insets_[i]->text(), &inset_values[i]);
377 layout_->set_inside_border_insets(gfx::Insets(
378 inset_values[0], inset_values[1], inset_values[2], inset_values[3]));
379 }
380
381 void BoxLayoutExample::UpdateLayoutManager() {
382 int child_spacing;
383 int default_flex;
384 int min_cross_size;
385 base::StringToInt(between_child_spacing_->text(), &child_spacing);
386 base::StringToInt(default_flex_->text(), &default_flex);
387 base::StringToInt(min_cross_axis_size_->text(), &min_cross_size);
388 layout_ = new BoxLayout(orientation_->selected_index() == 0
389 ? BoxLayout::Orientation::kHorizontal
390 : BoxLayout::Orientation::kVertical,
391 0, 0, child_spacing, collapse_margins_->checked());
392 layout_->set_cross_axis_alignment(static_cast<BoxLayout::CrossAxisAlignment>(
393 cross_axis_alignment_->selected_index()));
394 layout_->set_main_axis_alignment(static_cast<BoxLayout::MainAxisAlignment>(
395 main_axis_alignment_->selected_index()));
396 layout_->SetDefaultFlex(default_flex);
397 layout_->set_minimum_cross_axis_size(min_cross_size);
398 UpdateBorderInsets();
399 for (int i = 0; i < box_layout_panel_->child_count(); ++i) {
400 ChildPanel* panel =
401 static_cast<ChildPanel*>(box_layout_panel_->child_at(i));
402 int flex = panel->GetFlex();
403 if (flex < 0)
404 layout_->ClearFlexForView(panel);
405 else
406 layout_->SetFlexForView(panel, flex);
407 }
408 box_layout_panel_->SetLayoutManager(layout_);
409 }
410
411 } // namespace examples
412 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698