| 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" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "ui/gfx/paint_vector_icon.h" | 13 #include "ui/gfx/paint_vector_icon.h" |
| 14 #include "ui/gfx/vector_icons_public.h" | |
| 15 #include "ui/views/border.h" | 14 #include "ui/views/border.h" |
| 16 #include "ui/views/controls/button/button.h" | 15 #include "ui/views/controls/button/button.h" |
| 17 #include "ui/views/controls/button/md_text_button.h" | 16 #include "ui/views/controls/button/md_text_button.h" |
| 18 #include "ui/views/controls/image_view.h" | 17 #include "ui/views/controls/image_view.h" |
| 19 #include "ui/views/controls/textfield/textfield.h" | 18 #include "ui/views/controls/textfield/textfield.h" |
| 20 #include "ui/views/controls/textfield/textfield_controller.h" | 19 #include "ui/views/controls/textfield/textfield_controller.h" |
| 21 #include "ui/views/layout/box_layout.h" | 20 #include "ui/views/layout/box_layout.h" |
| 22 #include "ui/views/layout/fill_layout.h" | 21 #include "ui/views/layout/fill_layout.h" |
| 23 #include "ui/views/view.h" | 22 #include "ui/views/view.h" |
| 24 | 23 |
| 25 namespace views { | 24 namespace views { |
| 26 namespace examples { | 25 namespace examples { |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 class VectorIconGallery : public View, | 29 class VectorIconGallery : public View, |
| 31 public TextfieldController, | 30 public TextfieldController, |
| 32 public ButtonListener { | 31 public ButtonListener { |
| 33 public: | 32 public: |
| 34 VectorIconGallery() | 33 VectorIconGallery() |
| 35 : image_view_(new ImageView()), | 34 : image_view_(new ImageView()), |
| 36 image_view_container_(new views::View()), | 35 image_view_container_(new views::View()), |
| 37 size_input_(new Textfield()), | 36 size_input_(new Textfield()), |
| 38 color_input_(new Textfield()), | 37 color_input_(new Textfield()), |
| 39 file_chooser_(new Textfield()), | 38 file_chooser_(new Textfield()), |
| 40 file_go_button_( | 39 file_go_button_( |
| 41 MdTextButton::Create(this, base::ASCIIToUTF16("Render"))), | 40 MdTextButton::Create(this, base::ASCIIToUTF16("Render"))), |
| 42 vector_id_(0), | |
| 43 // 36dp is one of the natural sizes for MD icons, and corresponds | 41 // 36dp is one of the natural sizes for MD icons, and corresponds |
| 44 // roughly to a 32dp usable area. | 42 // roughly to a 32dp usable area. |
| 45 size_(36), | 43 size_(36), |
| 46 color_(SK_ColorRED) { | 44 color_(SK_ColorRED) { |
| 47 AddChildView(size_input_); | 45 AddChildView(size_input_); |
| 48 AddChildView(color_input_); | 46 AddChildView(color_input_); |
| 49 | 47 |
| 50 image_view_container_->AddChildView(image_view_); | 48 image_view_container_->AddChildView(image_view_); |
| 51 BoxLayout* image_layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); | 49 BoxLayout* image_layout = new BoxLayout(BoxLayout::kHorizontal, 0, 0, 0); |
| 52 image_layout->set_cross_axis_alignment( | 50 image_layout->set_cross_axis_alignment( |
| 53 BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | 51 BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 54 image_layout->set_main_axis_alignment( | 52 image_layout->set_main_axis_alignment( |
| 55 BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | 53 BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 56 image_view_container_->SetLayoutManager(image_layout); | 54 image_view_container_->SetLayoutManager(image_layout); |
| 57 image_view_->SetBorder(CreateSolidSidedBorder(1, 1, 1, 1, SK_ColorBLACK)); | 55 image_view_->SetBorder(CreateSolidSidedBorder(1, 1, 1, 1, SK_ColorBLACK)); |
| 58 AddChildView(image_view_container_); | 56 AddChildView(image_view_container_); |
| 59 | 57 |
| 60 BoxLayout* box = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); | 58 BoxLayout* box = new BoxLayout(BoxLayout::kVertical, 10, 10, 10); |
| 61 SetLayoutManager(box); | 59 SetLayoutManager(box); |
| 62 box->SetFlexForView(image_view_container_, 1); | 60 box->SetFlexForView(image_view_container_, 1); |
| 63 | 61 |
| 64 file_chooser_->set_placeholder_text( | 62 file_chooser_->set_placeholder_text( |
| 65 base::ASCIIToUTF16("Or enter a file to read")); | 63 base::ASCIIToUTF16("Enter a file to read")); |
| 66 View* file_container = new View(); | 64 View* file_container = new View(); |
| 67 BoxLayout* file_box = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); | 65 BoxLayout* file_box = new BoxLayout(BoxLayout::kHorizontal, 10, 10, 10); |
| 68 file_container->SetLayoutManager(file_box); | 66 file_container->SetLayoutManager(file_box); |
| 69 file_container->AddChildView(file_chooser_); | 67 file_container->AddChildView(file_chooser_); |
| 70 file_container->AddChildView(file_go_button_); | 68 file_container->AddChildView(file_go_button_); |
| 71 file_box->SetFlexForView(file_chooser_, 1); | 69 file_box->SetFlexForView(file_chooser_, 1); |
| 72 AddChildView(file_container); | 70 AddChildView(file_container); |
| 73 | 71 |
| 74 size_input_->set_placeholder_text(base::ASCIIToUTF16("Size in dip")); | 72 size_input_->set_placeholder_text(base::ASCIIToUTF16("Size in dip")); |
| 75 size_input_->set_controller(this); | 73 size_input_->set_controller(this); |
| 76 color_input_->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)")); | 74 color_input_->set_placeholder_text(base::ASCIIToUTF16("Color (AARRGGBB)")); |
| 77 color_input_->set_controller(this); | 75 color_input_->set_controller(this); |
| 78 | |
| 79 UpdateImage(); | |
| 80 } | 76 } |
| 81 | 77 |
| 82 ~VectorIconGallery() override {} | 78 ~VectorIconGallery() override {} |
| 83 | 79 |
| 84 // View implementation. | |
| 85 bool OnMousePressed(const ui::MouseEvent& event) override { | |
| 86 if (GetEventHandlerForPoint(event.location()) == image_view_container_) { | |
| 87 int increment = event.IsOnlyRightMouseButton() ? -1 : 1; | |
| 88 int icon_count = static_cast<int>(gfx::VectorIconId::VECTOR_ICON_NONE); | |
| 89 vector_id_ = (icon_count + vector_id_ + increment) % icon_count; | |
| 90 UpdateImage(); | |
| 91 return true; | |
| 92 } | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 // TextfieldController implementation. | 80 // TextfieldController implementation. |
| 97 void ContentsChanged(Textfield* sender, | 81 void ContentsChanged(Textfield* sender, |
| 98 const base::string16& new_contents) override { | 82 const base::string16& new_contents) override { |
| 99 if (sender == size_input_) { | 83 if (sender == size_input_) { |
| 100 if (base::StringToInt(new_contents, &size_) && (size_ > 0)) | 84 if (base::StringToInt(new_contents, &size_) && (size_ > 0)) |
| 101 UpdateImage(); | 85 Update(); |
| 102 else | 86 else |
| 103 size_input_->SetText(base::string16()); | 87 size_input_->SetText(base::string16()); |
| 104 | 88 |
| 105 return; | 89 return; |
| 106 } | 90 } |
| 107 | 91 |
| 108 DCHECK_EQ(color_input_, sender); | 92 DCHECK_EQ(color_input_, sender); |
| 109 if (new_contents.size() != 8u) | 93 if (new_contents.size() != 8u) |
| 110 return; | 94 return; |
| 111 unsigned new_color = | 95 unsigned new_color = |
| 112 strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16); | 96 strtoul(base::UTF16ToASCII(new_contents).c_str(), nullptr, 16); |
| 113 if (new_color <= 0xffffffff) { | 97 if (new_color <= 0xffffffff) { |
| 114 color_ = new_color; | 98 color_ = new_color; |
| 115 UpdateImage(); | 99 Update(); |
| 116 } | 100 } |
| 117 } | 101 } |
| 118 | 102 |
| 119 // ButtonListener | 103 // ButtonListener |
| 120 void ButtonPressed(Button* sender, const ui::Event& event) override { | 104 void ButtonPressed(Button* sender, const ui::Event& event) override { |
| 121 DCHECK_EQ(file_go_button_, sender); | 105 DCHECK_EQ(file_go_button_, sender); |
| 122 std::string contents; | |
| 123 #if defined(OS_POSIX) | 106 #if defined(OS_POSIX) |
| 124 base::FilePath path(base::UTF16ToUTF8(file_chooser_->text())); | 107 base::FilePath path(base::UTF16ToUTF8(file_chooser_->text())); |
| 125 #elif defined(OS_WIN) | 108 #elif defined(OS_WIN) |
| 126 base::FilePath path(file_chooser_->text()); | 109 base::FilePath path(file_chooser_->text()); |
| 127 #endif | 110 #endif |
| 128 base::ReadFileToString(path, &contents); | 111 base::ReadFileToString(path, &contents_); |
| 129 // Skip over comments. | 112 // Skip over comments. |
| 130 for (size_t slashes = contents.find("//"); slashes != std::string::npos; | 113 for (size_t slashes = contents_.find("//"); slashes != std::string::npos; |
| 131 slashes = contents.find("//")) { | 114 slashes = contents_.find("//")) { |
| 132 size_t eol = contents.find("\n", slashes); | 115 size_t eol = contents_.find("\n", slashes); |
| 133 contents.erase(slashes, eol - slashes); | 116 contents_.erase(slashes, eol - slashes); |
| 134 } | 117 } |
| 135 image_view_->SetImage( | 118 Update(); |
| 136 gfx::CreateVectorIconFromSource(contents, size_, color_)); | |
| 137 } | 119 } |
| 138 | 120 |
| 139 void UpdateImage() { | 121 private: |
| 140 image_view_->SetImage(gfx::CreateVectorIcon( | 122 void Update() { |
| 141 static_cast<gfx::VectorIconId>(vector_id_), size_, color_)); | 123 if (!contents_.empty()) { |
| 124 image_view_->SetImage( |
| 125 gfx::CreateVectorIconFromSource(contents_, size_, color_)); |
| 126 } |
| 142 Layout(); | 127 Layout(); |
| 143 } | 128 } |
| 144 | 129 |
| 145 private: | |
| 146 ImageView* image_view_; | 130 ImageView* image_view_; |
| 147 View* image_view_container_; | 131 View* image_view_container_; |
| 148 Textfield* size_input_; | 132 Textfield* size_input_; |
| 149 Textfield* color_input_; | 133 Textfield* color_input_; |
| 150 Textfield* file_chooser_; | 134 Textfield* file_chooser_; |
| 151 Button* file_go_button_; | 135 Button* file_go_button_; |
| 136 std::string contents_; |
| 152 | 137 |
| 153 int vector_id_; | |
| 154 int size_; | 138 int size_; |
| 155 SkColor color_; | 139 SkColor color_; |
| 156 | 140 |
| 157 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery); | 141 DISALLOW_COPY_AND_ASSIGN(VectorIconGallery); |
| 158 }; | 142 }; |
| 159 | 143 |
| 160 } // namespace | 144 } // namespace |
| 161 | 145 |
| 162 VectorExample::VectorExample() : ExampleBase("Vector Icon") {} | 146 VectorExample::VectorExample() : ExampleBase("Vector Icon") {} |
| 163 | 147 |
| 164 VectorExample::~VectorExample() {} | 148 VectorExample::~VectorExample() {} |
| 165 | 149 |
| 166 void VectorExample::CreateExampleView(View* container) { | 150 void VectorExample::CreateExampleView(View* container) { |
| 167 container->SetLayoutManager(new FillLayout()); | 151 container->SetLayoutManager(new FillLayout()); |
| 168 container->AddChildView(new VectorIconGallery()); | 152 container->AddChildView(new VectorIconGallery()); |
| 169 } | 153 } |
| 170 | 154 |
| 171 } // namespace examples | 155 } // namespace examples |
| 172 } // namespace views | 156 } // namespace views |
| OLD | NEW |