| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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. |
| 4 |
| 5 #include "ui/views/controls/button/vector_icon_image_updater.h" |
| 6 |
| 7 #include "ui/gfx/color_utils.h" |
| 8 #include "ui/gfx/paint_vector_icon.h" |
| 9 #include "ui/views/animation/ink_drop_host_view.h" |
| 10 #include "ui/views/controls/button/button.h" |
| 11 #include "ui/views/controls/button/custom_button.h" |
| 12 #include "ui/views/controls/button/image_button.h" |
| 13 #include "ui/views/painter.h" |
| 14 #include "ui/views/view.h" |
| 15 #include "ui/views/views_delegate.h" |
| 16 |
| 17 namespace views { |
| 18 |
| 19 ImageButton* CreateImageButtonWithVectorIcon(const gfx::VectorIcon& icon, |
| 20 ButtonListener* listener) { |
| 21 return CreateImageButtonWithImageUpdater(new VectorIconImageUpdater(icon), |
| 22 listener); |
| 23 }; |
| 24 |
| 25 ImageButton* CreateImageButtonWithImageUpdater(VectorIconImageUpdater* updater, |
| 26 ButtonListener* listener) { |
| 27 ImageButton* button = new ImageButton(listener); |
| 28 updater->Attach(button); |
| 29 button->SetInkDropMode(InkDropHostView::InkDropMode::ON); |
| 30 button->set_has_ink_drop_action_on_click(true); |
| 31 button->SetImageAlignment(ImageButton::ALIGN_CENTER, |
| 32 ImageButton::ALIGN_MIDDLE); |
| 33 button->SetFocusPainter(nullptr); |
| 34 button->SetPadding(ViewsDelegate::GetInstance()->GetButtonMargins()); |
| 35 return button; |
| 36 }; |
| 37 |
| 38 VectorIconImageUpdater::VectorIconImageUpdater(const gfx::VectorIcon& icon) |
| 39 : owner_button_(nullptr), icon_(&icon) {} |
| 40 |
| 41 void VectorIconImageUpdater::Attach(ImageButton* owner_button) { |
| 42 DCHECK(owner_button); |
| 43 owner_button_ = owner_button; |
| 44 owner_button_->AddObserver(this); |
| 45 UpdateButtonImages(); |
| 46 } |
| 47 |
| 48 void VectorIconImageUpdater::SetIcon(const gfx::VectorIcon& icon) { |
| 49 icon_ = &icon; |
| 50 UpdateButtonImages(); |
| 51 } |
| 52 |
| 53 void VectorIconImageUpdater::OnThemeChanged(View* observed_view) { |
| 54 UpdateButtonImages(); |
| 55 } |
| 56 |
| 57 void VectorIconImageUpdater::OnNativeThemeChanged( |
| 58 View* observed_view, |
| 59 const ui::NativeTheme* theme) { |
| 60 UpdateButtonImages(); |
| 61 } |
| 62 |
| 63 void VectorIconImageUpdater::OnViewIsDeleting(View* observed_view) { |
| 64 delete this; |
| 65 } |
| 66 |
| 67 void VectorIconImageUpdater::UpdateButtonImages() { |
| 68 DCHECK(icon_); |
| 69 if (!owner_button_) |
| 70 return; |
| 71 |
| 72 SkColor icon_color = |
| 73 color_utils::DeriveDefaultIconColor(GetVectorIconColor()); |
| 74 SkColor disabled_color = SkColorSetA(icon_color, 0xff / 2); |
| 75 owner_button_->SetImage(CustomButton::STATE_NORMAL, |
| 76 gfx::CreateVectorIcon(*icon_, icon_color)); |
| 77 owner_button_->SetImage(CustomButton::STATE_DISABLED, |
| 78 gfx::CreateVectorIcon(*icon_, disabled_color)); |
| 79 owner_button_->set_ink_drop_base_color(icon_color); |
| 80 } |
| 81 |
| 82 SkColor VectorIconImageUpdater::GetVectorIconColor() const { |
| 83 return SK_ColorBLACK; |
| 84 } |
| 85 |
| 86 } // namespace views |
| OLD | NEW |