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/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/gfx/paint_vector_icon.h"
10 #include "ui/gfx/vector_icons_public2.h"
11 #include "ui/views/controls/image_view.h"
12 #include "ui/views/controls/textfield/textfield.h"
13 #include "ui/views/controls/textfield/textfield_controller.h"
14 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/layout/fill_layout.h"
16 #include "ui/views/view.h"
23 class VectorIconGallery
: public View
, public TextfieldController
{
26 : image_view_(new ImageView()),
27 size_input_(new Textfield()),
28 color_input_(new Textfield()),
32 AddChildView(size_input_
);
33 AddChildView(color_input_
);
34 AddChildView(image_view_
);
36 size_input_
->set_placeholder_text(base::ASCIIToUTF16("Size in dip"));
37 size_input_
->set_controller(this);
38 color_input_
->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)"));
39 color_input_
->set_controller(this);
41 BoxLayout
* box
= new BoxLayout(BoxLayout::kVertical
, 10, 10, 10);
42 SetLayoutManager(box
);
43 box
->SetFlexForView(image_view_
, 1);
47 ~VectorIconGallery() override
{}
49 // View implementation.
50 bool OnMousePressed(const ui::MouseEvent
& event
) override
{
51 if (GetEventHandlerForPoint(event
.location()) == image_view_
) {
52 vector_id_
= ((vector_id_
+ 1) %
53 static_cast<int>(gfx::VectorIconId::VECTOR_ICON_NONE
));
60 // TextfieldController implementation.
61 void ContentsChanged(Textfield
* sender
,
62 const base::string16
& new_contents
) override
{
63 if (sender
== size_input_
) {
64 if (base::StringToSizeT(new_contents
, &size_
))
67 size_input_
->SetText(base::string16());
72 DCHECK_EQ(color_input_
, sender
);
73 if (new_contents
.size() != 8u)
76 strtoul(base::UTF16ToASCII(new_contents
).c_str(), nullptr, 16);
77 if (new_color
<= 0xffffffff) {
84 image_view_
->SetImage(gfx::CreateVectorIcon(
85 static_cast<gfx::VectorIconId
>(vector_id_
), size_
, color_
));
89 ImageView
* image_view_
;
90 Textfield
* size_input_
;
91 Textfield
* color_input_
;
97 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery
);
102 VectorExample::VectorExample() : ExampleBase("Vector Icon") {}
104 VectorExample::~VectorExample() {}
106 void VectorExample::CreateExampleView(View
* container
) {
107 container
->SetLayoutManager(new FillLayout());
108 container
->AddChildView(new VectorIconGallery());
111 } // namespace examples