| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/examples/vector_example.h" | 5 #include "ui/views/examples/vector_example.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 7 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 9 #include "ui/gfx/paint_vector_icon.h" | 11 #include "ui/gfx/paint_vector_icon.h" |
| 10 #include "ui/gfx/vector_icons_public2.h" | 12 #include "ui/gfx/vector_icons_public2.h" |
| 13 #include "ui/views/controls/button/blue_button.h" |
| 14 #include "ui/views/controls/button/button.h" |
| 11 #include "ui/views/controls/image_view.h" | 15 #include "ui/views/controls/image_view.h" |
| 12 #include "ui/views/controls/textfield/textfield.h" | 16 #include "ui/views/controls/textfield/textfield.h" |
| 13 #include "ui/views/controls/textfield/textfield_controller.h" | 17 #include "ui/views/controls/textfield/textfield_controller.h" |
| 14 #include "ui/views/layout/box_layout.h" | 18 #include "ui/views/layout/box_layout.h" |
| 15 #include "ui/views/layout/fill_layout.h" | 19 #include "ui/views/layout/fill_layout.h" |
| 16 #include "ui/views/view.h" | 20 #include "ui/views/view.h" |
| 17 | 21 |
| 18 namespace views { | 22 namespace views { |
| 19 namespace examples { | 23 namespace examples { |
| 20 | 24 |
| 21 namespace { | 25 namespace { |
| 22 | 26 |
| 23 class VectorIconGallery : public View, public TextfieldController { | 27 class VectorIconGallery : public View, |
| 28 public TextfieldController, |
| 29 public ButtonListener { |
| 24 public: | 30 public: |
| 25 VectorIconGallery() | 31 VectorIconGallery() |
| 26 : image_view_(new ImageView()), | 32 : image_view_(new ImageView()), |
| 27 size_input_(new Textfield()), | 33 size_input_(new Textfield()), |
| 28 color_input_(new Textfield()), | 34 color_input_(new Textfield()), |
| 35 file_chooser_(new Textfield()), |
| 36 file_go_button_(new BlueButton(this, base::ASCIIToUTF16("Render"))), |
| 29 vector_id_(0), | 37 vector_id_(0), |
| 30 size_(32), | 38 size_(32), |
| 31 color_(SK_ColorRED) { | 39 color_(SK_ColorRED) { |
| 32 AddChildView(size_input_); | 40 AddChildView(size_input_); |
| 33 AddChildView(color_input_); | 41 AddChildView(color_input_); |
| 34 AddChildView(image_view_); | 42 AddChildView(image_view_); |
| 35 | 43 |
| 44 file_chooser_->set_placeholder_text( |
| 45 base::ASCIIToUTF16("Or enter a file to read")); |
| 46 View* file_container = new View(); |
| 47 BoxLayout* file_box = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 48 file_container->SetLayoutManager(file_box); |
| 49 file_container->AddChildView(file_chooser_); |
| 50 file_container->AddChildView(file_go_button_); |
| 51 file_box->SetFlexForView(file_chooser_, 1); |
| 52 AddChildView(file_container); |
| 53 |
| 36 size_input_->set_placeholder_text(base::ASCIIToUTF16("Size in dip")); | 54 size_input_->set_placeholder_text(base::ASCIIToUTF16("Size in dip")); |
| 37 size_input_->set_controller(this); | 55 size_input_->set_controller(this); |
| 38 color_input_->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)")); | 56 color_input_->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)")); |
| 39 color_input_->set_controller(this); | 57 color_input_->set_controller(this); |
| 40 | 58 |
| 41 BoxLayout* box = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); | 59 BoxLayout* box = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); |
| 42 SetLayoutManager(box); | 60 SetLayoutManager(box); |
| 43 box->SetFlexForView(image_view_, 1); | 61 box->SetFlexForView(image_view_, 1); |
| 44 UpdateImage(); | 62 UpdateImage(); |
| 45 } | 63 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 73 if (new_contents.size() != 8u) | 91 if (new_contents.size() != 8u) |
| 74 return; | 92 return; |
| 75 unsigned new_color = | 93 unsigned new_color = |
| 76 strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16); | 94 strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16); |
| 77 if (new_color <= 0xffffffff) { | 95 if (new_color <= 0xffffffff) { |
| 78 color_ = new_color; | 96 color_ = new_color; |
| 79 UpdateImage(); | 97 UpdateImage(); |
| 80 } | 98 } |
| 81 } | 99 } |
| 82 | 100 |
| 101 // ButtonListener |
| 102 void ButtonPressed(Button* sender, const ui::Event& event) override { |
| 103 DCHECK_EQ(file_go_button_, sender); |
| 104 std::string contents; |
| 105 base::ReadFileToString(base::FilePath(UTF16ToUTF8(file_chooser_->text())), |
| 106 &contents); |
| 107 image_view_->SetImage( |
| 108 gfx::CreateVectorIconFromSource(contents, size_, color_)); |
| 109 } |
| 110 |
| 83 void UpdateImage() { | 111 void UpdateImage() { |
| 84 image_view_->SetImage(gfx::CreateVectorIcon( | 112 image_view_->SetImage(gfx::CreateVectorIcon( |
| 85 static_cast<gfx::VectorIconId>(vector_id_), size_, color_)); | 113 static_cast<gfx::VectorIconId>(vector_id_), size_, color_)); |
| 86 } | 114 } |
| 87 | 115 |
| 88 private: | 116 private: |
| 89 ImageView* image_view_; | 117 ImageView* image_view_; |
| 90 Textfield* size_input_; | 118 Textfield* size_input_; |
| 91 Textfield* color_input_; | 119 Textfield* color_input_; |
| 120 Textfield* file_chooser_; |
| 121 Button* file_go_button_; |
| 92 | 122 |
| 93 int vector_id_; | 123 int vector_id_; |
| 94 size_t size_; | 124 size_t size_; |
| 95 SkColor color_; | 125 SkColor color_; |
| 96 | 126 |
| 97 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery); | 127 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery); |
| 98 }; | 128 }; |
| 99 | 129 |
| 100 } // namespace | 130 } // namespace |
| 101 | 131 |
| 102 VectorExample::VectorExample() : ExampleBase("Vector Icon") {} | 132 VectorExample::VectorExample() : ExampleBase("Vector Icon") {} |
| 103 | 133 |
| 104 VectorExample::~VectorExample() {} | 134 VectorExample::~VectorExample() {} |
| 105 | 135 |
| 106 void VectorExample::CreateExampleView(View* container) { | 136 void VectorExample::CreateExampleView(View* container) { |
| 107 container->SetLayoutManager(new FillLayout()); | 137 container->SetLayoutManager(new FillLayout()); |
| 108 container->AddChildView(new VectorIconGallery()); | 138 container->AddChildView(new VectorIconGallery()); |
| 109 } | 139 } |
| 110 | 140 |
| 111 } // namespace examples | 141 } // namespace examples |
| 112 } // namespace views | 142 } // namespace views |
| OLD | NEW |