1 // Copyright 2015 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.
5 #include "ui/views/examples/vector_example.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "ui/gfx/paint_vector_icon.h"
12 #include "ui/gfx/vector_icons_public.h"
13 #include "ui/views/border.h"
14 #include "ui/views/controls/button/blue_button.h"
15 #include "ui/views/controls/button/button.h"
16 #include "ui/views/controls/image_view.h"
17 #include "ui/views/controls/textfield/textfield.h"
18 #include "ui/views/controls/textfield/textfield_controller.h"
19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/layout/fill_layout.h"
21 #include "ui/views/view.h"
28 class VectorIconGallery
: public View
,
29 public TextfieldController
,
30 public ButtonListener
{
33 : image_view_(new ImageView()),
34 image_view_container_(new views::View()),
35 size_input_(new Textfield()),
36 color_input_(new Textfield()),
37 file_chooser_(new Textfield()),
38 file_go_button_(new BlueButton(this, base::ASCIIToUTF16("Render"))),
40 // 36dp is one of the natural sizes for MD icons, and corresponds
41 // roughly to a 32dp usable area.
44 AddChildView(size_input_
);
45 AddChildView(color_input_
);
47 image_view_container_
->AddChildView(image_view_
);
48 BoxLayout
* image_layout
= new BoxLayout(BoxLayout::kHorizontal
, 0, 0, 0);
49 image_layout
->set_cross_axis_alignment(
50 BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER
);
51 image_layout
->set_main_axis_alignment(
52 BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER
);
53 image_view_container_
->SetLayoutManager(image_layout
);
54 image_view_
->SetBorder(
55 Border::CreateSolidSidedBorder(1, 1, 1, 1, SK_ColorBLACK
));
56 AddChildView(image_view_container_
);
58 BoxLayout
* box
= new BoxLayout(BoxLayout::kVertical
, 10, 10, 10);
59 SetLayoutManager(box
);
60 box
->SetFlexForView(image_view_container_
, 1);
62 file_chooser_
->set_placeholder_text(
63 base::ASCIIToUTF16("Or enter a file to read"));
64 View
* file_container
= new View();
65 BoxLayout
* file_box
= new BoxLayout(BoxLayout::kHorizontal
, 10, 10, 10);
66 file_container
->SetLayoutManager(file_box
);
67 file_container
->AddChildView(file_chooser_
);
68 file_container
->AddChildView(file_go_button_
);
69 file_box
->SetFlexForView(file_chooser_
, 1);
70 AddChildView(file_container
);
72 size_input_
->set_placeholder_text(base::ASCIIToUTF16("Size in dip"));
73 size_input_
->set_controller(this);
74 color_input_
->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)"));
75 color_input_
->set_controller(this);
80 ~VectorIconGallery() override
{}
82 // View implementation.
83 bool OnMousePressed(const ui::MouseEvent
& event
) override
{
84 if (GetEventHandlerForPoint(event
.location()) == image_view_container_
) {
85 int increment
= event
.IsOnlyRightMouseButton() ? -1 : 1;
86 int icon_count
= static_cast<int>(gfx::VectorIconId::VECTOR_ICON_NONE
);
87 vector_id_
= (icon_count
+ vector_id_
+ increment
) % icon_count
;
94 // TextfieldController implementation.
95 void ContentsChanged(Textfield
* sender
,
96 const base::string16
& new_contents
) override
{
97 if (sender
== size_input_
) {
98 if (base::StringToSizeT(new_contents
, &size_
))
101 size_input_
->SetText(base::string16());
106 DCHECK_EQ(color_input_
, sender
);
107 if (new_contents
.size() != 8u)
110 strtoul(base::UTF16ToASCII(new_contents
).c_str(), nullptr, 16);
111 if (new_color
<= 0xffffffff) {
118 void ButtonPressed(Button
* sender
, const ui::Event
& event
) override
{
119 DCHECK_EQ(file_go_button_
, sender
);
120 std::string contents
;
121 #if defined(OS_POSIX)
122 base::FilePath
path(base::UTF16ToUTF8(file_chooser_
->text()));
123 #elif defined(OS_WIN)
124 base::FilePath
path(file_chooser_
->text());
126 base::ReadFileToString(path
, &contents
);
127 image_view_
->SetImage(
128 gfx::CreateVectorIconFromSource(contents
, size_
, color_
));
132 image_view_
->SetImage(gfx::CreateVectorIcon(
133 static_cast<gfx::VectorIconId
>(vector_id_
), size_
, color_
));
138 ImageView
* image_view_
;
139 View
* image_view_container_
;
140 Textfield
* size_input_
;
141 Textfield
* color_input_
;
142 Textfield
* file_chooser_
;
143 Button
* file_go_button_
;
149 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery
);
154 VectorExample::VectorExample() : ExampleBase("Vector Icon") {}
156 VectorExample::~VectorExample() {}
158 void VectorExample::CreateExampleView(View
* container
) {
159 container
->SetLayoutManager(new FillLayout());
160 container
->AddChildView(new VectorIconGallery());
163 } // namespace examples