Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / views / examples / vector_example.cc
blob4c72147f2b987c3a4515ec4c2d9b94c2b9aa1e5a
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"
18 namespace views {
19 namespace examples {
21 namespace {
23 class VectorIconGallery : public View, public TextfieldController {
24 public:
25 VectorIconGallery()
26 : image_view_(new ImageView()),
27 size_input_(new Textfield()),
28 color_input_(new Textfield()),
29 vector_id_(0),
30 size_(32),
31 color_(SK_ColorRED) {
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);
44 UpdateImage();
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));
54 UpdateImage();
55 return true;
57 return false;
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_))
65 UpdateImage();
66 else
67 size_input_->SetText(base::string16());
69 return;
72 DCHECK_EQ(color_input_, sender);
73 if (new_contents.size() != 8u)
74 return;
75 unsigned new_color =
76 strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16);
77 if (new_color <= 0xffffffff) {
78 color_ = new_color;
79 UpdateImage();
83 void UpdateImage() {
84 image_view_->SetImage(gfx::CreateVectorIcon(
85 static_cast<gfx::VectorIconId>(vector_id_), size_, color_));
88 private:
89 ImageView* image_view_;
90 Textfield* size_input_;
91 Textfield* color_input_;
93 int vector_id_;
94 size_t size_;
95 SkColor color_;
97 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery);
100 } // namespace
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
112 } // namespace views