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/anchor_example.cc

Issue 2230913003: Experimental alignment layout manager using a property on the views Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renamed AlignAttribute and associated types to FillAttribute 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
« no previous file with comments | « ui/views/examples/anchor_example.h ('k') | ui/views/examples/examples_window.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "ui/views/examples/anchor_example.h"
6
7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "build/build_config.h"
10 #include "ui/views/background.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/button/checkbox.h"
13 #include "ui/views/controls/button/md_text_button.h"
14 #include "ui/views/controls/button/radio_button.h"
15 #include "ui/views/layout/align_layout.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
18
19 using base::ASCIIToUTF16;
20
21 namespace views {
22 namespace examples {
23
24 static constexpr Fill Fill_None =
25 static_cast<Fill>(static_cast<int>(Fill::Content) + 1);
26
27 AnchorExample::AnchorExample() : ExampleBase("Anchor") {}
28
29 AnchorExample::~AnchorExample() {}
30
31 void AnchorExample::CreateExampleView(View* container) {
32 container->SetLayoutManager(new AlignLayout());
33 work_panel_ = new View();
34 work_panel_->SetLayoutManager(new AlignLayout());
35 work_panel_->SetBounds(
36 0, 0, container->parent()->GetContentsBounds().width() > 0
37 ? container->parent()->GetContentsBounds().width() - 175
38 : 600,
39 10);
40 work_panel_->attributes().Add(
41 base::WrapUnique(new FillAttribute(Fill::Right)));
42 work_panel_->attributes().Add(base::WrapUnique(new AnchorAttribute(
43 {Anchor::Left, Anchor::Top, Anchor::Right, Anchor::Bottom})));
44 work_panel_->SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY));
45 container->AddChildView(work_panel_);
46 for (int i = 0; i < 4; i++) {
47 Anchor anchor = static_cast<Anchor>(i);
48 Checkbox* checkbox = new Checkbox(AnchorText(anchor));
49 checkbox->set_listener(this);
50 checkbox->SetFocusForPlatform();
51 checkbox->SetGroup(10);
52 checkbox->set_id(i);
53 checkbox->SetBounds(10, 20 * i, 150, 20);
54 checkbox->SetChecked(current_anchors_.Contains(anchor));
55 container->AddChildView(checkbox);
56 }
57 for (int i = 0; i < 6; i++) {
58 Fill fill = static_cast<Fill>(i);
59 RadioButton* radiobutton = new RadioButton(FillText(fill), 5);
60 radiobutton->set_listener(this);
61 radiobutton->SetFocusForPlatform();
62 radiobutton->set_id(i);
63 radiobutton->SetBounds(10, 20 * i + 100, 150, 20);
64 container->AddChildView(radiobutton);
65 }
66 LabelButton* reset_button = MdTextButton::Create(this, ASCIIToUTF16("Reset"));
67 reset_button->SetFocusForPlatform();
68 reset_button->SetBounds(20, 240, 80, 25);
69 container->AddChildView(reset_button);
70 anchored_view_label_ = new Label(AnchorsText(current_anchors_));
71 anchored_view_label_->SetBounds(125, 150, 150, 30);
72 anchored_view_label_->SetBorder(Border::CreateSolidBorder(1, SK_ColorGRAY));
73 anchored_view_label_->attributes().Add(
74 base::WrapUnique(new AnchorAttribute(current_anchors_)));
75 work_panel_->AddChildView(anchored_view_label_);
76 }
77
78 void AnchorExample::ButtonPressed(Button* sender, const ui::Event& event) {
79 base::string16 class_name = ASCIIToUTF16(sender->GetClassName());
80 if (class_name == ASCIIToUTF16("Checkbox")) {
81 AnchorAttribute* attribute = nullptr;
82 Checkbox* checkbox = static_cast<Checkbox*>(sender);
83 Anchor anchor = static_cast<Anchor>(checkbox->id());
84 if (checkbox->checked())
85 current_anchors_.Include(anchor);
86 else
87 current_anchors_.Exclude(anchor);
88 if (anchored_view_label_->attributes().Find(attribute)) {
89 anchored_view_label_->SetText(AnchorsText(current_anchors_));
90 attribute->GetContent().SetAnchors(current_anchors_);
91 work_panel_->Layout();
92 }
93 } else if (class_name == ASCIIToUTF16("RadioButton")) {
94 FillAttribute* attribute = nullptr;
95 AnchorAttribute* anchors = nullptr;
96 RadioButton* radiobutton = static_cast<RadioButton*>(sender);
97 Fill fill = static_cast<Fill>(radiobutton->id());
98 if (fill == Fill_None) {
99 anchored_view_label_->attributes().Remove(
100 GetAttributeId<FillAttribute>());
101 } else {
102 if (!anchored_view_label_->attributes().Find(attribute))
103 anchored_view_label_->attributes().Add(
104 base::WrapUnique(new FillAttribute(fill)));
105 else
106 attribute->SetFill(fill);
107 }
108 if (anchored_view_label_->attributes().Find(anchors))
109 ReflectAnchors(sender->parent(), anchors);
110 work_panel_->Layout();
111 } else if (class_name == ASCIIToUTF16("LabelButton")) {
112 AnchorAttribute* anchors = nullptr;
113 anchored_view_label_->attributes().Remove(GetAttributeId<FillAttribute>());
114 anchored_view_label_->SetBounds(125, 150, 150, 30);
115 if (anchored_view_label_->attributes().Find(anchors))
116 ReflectAnchors(sender->parent(), anchors);
117 ClearAlignButtons(sender->parent());
118 }
119 }
120
121 base::string16 AnchorExample::FillText(Fill fill) {
122 switch (fill) {
123 case Fill::Left:
124 return ASCIIToUTF16("Fill::Left");
125 case Fill::Top:
126 return ASCIIToUTF16("Fill::Top");
127 case Fill::Right:
128 return ASCIIToUTF16("Fill::Right");
129 case Fill::Bottom:
130 return ASCIIToUTF16("Fill::Bottom");
131 case Fill::Content:
132 return ASCIIToUTF16("Fill::Content");
133 }
134 return ASCIIToUTF16("Fill_None");
135 }
136
137 base::string16 AnchorExample::AnchorText(Anchor anchor) {
138 switch (anchor) {
139 case Anchor::Left:
140 return ASCIIToUTF16("Anchor::Left");
141 case Anchor::Top:
142 return ASCIIToUTF16("Anchor::Top");
143 case Anchor::Right:
144 return ASCIIToUTF16("Anchor::Right");
145 case Anchor::Bottom:
146 return ASCIIToUTF16("Anchor::Bottom");
147 }
148 return ASCIIToUTF16("Invalid");
149 }
150
151 base::string16 AnchorExample::AnchorsText(Anchors anchors) {
152 base::string16 result;
153 for (int i = 0; i < 4; i++) {
154 Anchor anchor = static_cast<Anchor>(i);
155 if (anchors.Contains(anchor)) {
156 if (!result.empty())
157 result.append(ASCIIToUTF16(", "));
158 result.append(AnchorText(anchor));
159 }
160 }
161 return result;
162 }
163
164 void AnchorExample::ClearAlignButtons(View* view) {
165 for (int i = 0; i < view->child_count(); i++) {
166 View* child = view->child_at(i);
167 base::string16 classname = ASCIIToUTF16(child->GetClassName());
168 if (classname == ASCIIToUTF16("RadioButton")) {
169 RadioButton* radiobutton = static_cast<RadioButton*>(child);
170 radiobutton->SetChecked(false);
171 }
172 }
173 }
174
175 void AnchorExample::ReflectAnchors(View* view, AnchorAttribute* attribute) {
176 for (int i = 0; i < view->child_count(); i++) {
177 View* child = view->child_at(i);
178 base::string16 classname = ASCIIToUTF16(child->GetClassName());
179 if (classname == ASCIIToUTF16("Checkbox")) {
180 Checkbox* checkbox = static_cast<Checkbox*>(child);
181 Anchor anchor = static_cast<Anchor>(checkbox->id());
182 checkbox->SetChecked(attribute->GetContent().anchors().Contains(anchor));
183 }
184 }
185 }
186
187 } // namespace examples
188 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/examples/anchor_example.h ('k') | ui/views/examples/examples_window.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698