Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: ui/views/controls/button/vector_icon_button.cc

Issue 2744463002: Add VectorIconButton functionality to ImageButton. (Closed)
Patch Set: WIP: use observer Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/controls/button/vector_icon_button.h" 5 #include "ui/views/controls/button/vector_icon_button.h"
6 6
7 #include "ui/gfx/color_palette.h" 7 #include "ui/gfx/color_palette.h"
8 #include "ui/gfx/color_utils.h" 8 #include "ui/gfx/color_utils.h"
9 #include "ui/gfx/paint_vector_icon.h" 9 #include "ui/gfx/paint_vector_icon.h"
10 #include "ui/gfx/vector_icons_public.h" 10 #include "ui/gfx/vector_icons_public.h"
11 #include "ui/views/border.h"
12 #include "ui/views/controls/button/vector_icon_button_delegate.h" 11 #include "ui/views/controls/button/vector_icon_button_delegate.h"
13 #include "ui/views/painter.h" 12 #include "ui/views/painter.h"
13 #include "ui/views/views_delegate.h"
14 14
15 namespace views { 15 namespace views {
16 16
17 namespace {
18
19 // Extra space around the buttons to increase their event target size.
20 const int kButtonExtraTouchSize = 4;
21
22 } // namespace
23
24 VectorIconButton::VectorIconButton(VectorIconButtonDelegate* delegate) 17 VectorIconButton::VectorIconButton(VectorIconButtonDelegate* delegate)
25 : ImageButton(delegate), 18 : ImageButton(delegate), delegate_(delegate) {
26 delegate_(delegate),
27 id_(gfx::VectorIconId::VECTOR_ICON_NONE) {
28 SetInkDropMode(InkDropMode::ON); 19 SetInkDropMode(InkDropMode::ON);
29 set_has_ink_drop_action_on_click(true); 20 set_has_ink_drop_action_on_click(true);
30 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE); 21 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_MIDDLE);
31 SetFocusPainter(nullptr); 22 SetFocusPainter(nullptr);
23 SetPadding(ViewsDelegate::GetInstance()->GetButtonMargins());
32 } 24 }
33 25
34 VectorIconButton::~VectorIconButton() {} 26 VectorIconButton::~VectorIconButton() {}
35 27
36 void VectorIconButton::SetIcon(gfx::VectorIconId id) {
37 id_ = id;
38 icon_ = nullptr;
39
40 OnSetIcon();
41 }
42
43 void VectorIconButton::SetIcon(const gfx::VectorIcon& icon) { 28 void VectorIconButton::SetIcon(const gfx::VectorIcon& icon) {
44 id_ = gfx::VectorIconId::VECTOR_ICON_NONE;
45 icon_ = &icon; 29 icon_ = &icon;
46 30 UpdateImagesAndColors();
47 OnSetIcon();
48 } 31 }
49 32
50 void VectorIconButton::OnThemeChanged() { 33 void VectorIconButton::OnThemeChanged() {
51 UpdateImagesAndColors(); 34 UpdateImagesAndColors();
52 } 35 }
53 36
54 void VectorIconButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { 37 void VectorIconButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
55 UpdateImagesAndColors(); 38 UpdateImagesAndColors();
56 } 39 }
57 40
58 void VectorIconButton::OnSetIcon() {
59 if (!border())
60 SetBorder(CreateEmptyBorder(gfx::Insets(kButtonExtraTouchSize)));
61
62 UpdateImagesAndColors();
63 }
64
65 void VectorIconButton::UpdateImagesAndColors() { 41 void VectorIconButton::UpdateImagesAndColors() {
42 if (!icon_)
43 return;
44
66 SkColor icon_color = 45 SkColor icon_color =
67 color_utils::DeriveDefaultIconColor(delegate_->GetVectorIconBaseColor()); 46 color_utils::DeriveDefaultIconColor(delegate_->GetVectorIconBaseColor());
68 SkColor disabled_color = SkColorSetA(icon_color, 0xff / 2); 47 SkColor disabled_color = SkColorSetA(icon_color, 0xff / 2);
69 if (icon_) { 48 SetImage(CustomButton::STATE_NORMAL,
70 SetImage(CustomButton::STATE_NORMAL, 49 gfx::CreateVectorIcon(*icon_, icon_color));
71 gfx::CreateVectorIcon(*icon_, icon_color)); 50 SetImage(CustomButton::STATE_DISABLED,
72 SetImage(CustomButton::STATE_DISABLED, 51 gfx::CreateVectorIcon(*icon_, disabled_color));
73 gfx::CreateVectorIcon(*icon_, disabled_color));
74 } else {
75 SetImage(CustomButton::STATE_NORMAL,
76 gfx::CreateVectorIcon(id_, icon_color));
77 SetImage(CustomButton::STATE_DISABLED,
78 gfx::CreateVectorIcon(id_, disabled_color));
79 }
80 set_ink_drop_base_color(icon_color); 52 set_ink_drop_base_color(icon_color);
81 } 53 }
82 54
83 } // namespace views 55 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698