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

Side by Side Diff: ui/views/controls/button/image_button.h

Issue 2744463002: Add VectorIconButton functionality to ImageButton. (Closed)
Patch Set: fix cros build 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_ 5 #ifndef UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_
6 #define UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_ 6 #define UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_
7 7
8 #include <memory> 8 #include <memory>
9 9
10 #include "base/gtest_prod_util.h" 10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "ui/base/layout.h" 12 #include "ui/base/layout.h"
13 #include "ui/gfx/image/image_skia.h" 13 #include "ui/gfx/image/image_skia.h"
14 #include "ui/views/controls/button/custom_button.h" 14 #include "ui/views/controls/button/custom_button.h"
15 15
16 namespace gfx {
17 struct VectorIcon;
18 }
19
16 namespace views { 20 namespace views {
17 21
18 class Painter; 22 class Painter;
19 23
24 // A ButtonListener that also provides padding around the image and a color if
25 // the image is a vector icon.
26 class VIEWS_EXPORT ImageButtonDelegate : public ButtonListener {
bruthig 2017/03/09 05:04:42 IMO I don't think coupling the click listener to t
Bret 2017/03/09 22:32:37 In practice I haven't seen any cases where it seem
bruthig 2017/03/10 14:35:25 As noted below, this is partly because we have man
27 public:
28 virtual SkColor GetVectorIconColor() const;
sky 2017/03/09 04:05:02 Specifying the color should not be a requirement f
Bret 2017/03/09 22:32:37 Subclasses aren't required to override GetVectorIc
bruthig 2017/03/10 14:35:25 It's different because it decouples the button sta
29
30 protected:
31 ~ImageButtonDelegate() override {}
32 };
33
20 // An image button. 34 // An image button.
21 // Note that this type of button is not focusable by default and will not be 35 // Note that this type of button is not focusable by default and will not be
22 // part of the focus chain, unless in accessibility mode. Call 36 // part of the focus chain, unless in accessibility mode. Call
23 // SetFocusForPlatform() to make it part of the focus chain. 37 // SetFocusForPlatform() to make it part of the focus chain.
24 class VIEWS_EXPORT ImageButton : public CustomButton { 38 class VIEWS_EXPORT ImageButton : public CustomButton {
25 public: 39 public:
26 static const char kViewClassName[]; 40 static const char kViewClassName[];
27 41
28 enum HorizontalAlignment { 42 enum HorizontalAlignment {
29 ALIGN_LEFT = 0, 43 ALIGN_LEFT = 0,
30 ALIGN_CENTER, 44 ALIGN_CENTER,
31 ALIGN_RIGHT 45 ALIGN_RIGHT
32 }; 46 };
33 47
34 enum VerticalAlignment { 48 enum VerticalAlignment {
35 ALIGN_TOP = 0, 49 ALIGN_TOP = 0,
36 ALIGN_MIDDLE, 50 ALIGN_MIDDLE,
37 ALIGN_BOTTOM 51 ALIGN_BOTTOM
38 }; 52 };
39 53
40 explicit ImageButton(ButtonListener* listener); 54 static ImageButton* CreateDefaultVectorIconButton(
55 const gfx::VectorIcon& icon,
56 ImageButtonDelegate* delegate);
57
58 explicit ImageButton(ImageButtonDelegate* delegate);
41 ~ImageButton() override; 59 ~ImageButton() override;
42 60
43 // Returns the image for a given |state|. 61 // Returns the image for a given |state|.
44 virtual const gfx::ImageSkia& GetImage(ButtonState state) const; 62 virtual const gfx::ImageSkia& GetImage(ButtonState state) const;
45 63
46 // Set the image the button should use for the provided state. 64 // Set the image the button should use for the provided state.
47 void SetImage(ButtonState state, const gfx::ImageSkia* image); 65 void SetImage(ButtonState state, const gfx::ImageSkia* image);
48 66
49 // As above, but takes a const ref. TODO(estade): all callers should be 67 // As above, but takes a const ref. TODO(estade): all callers should be
50 // updated to use this version, and then the implementations can be 68 // updated to use this version, and then the implementations can be
51 // consolidated. 69 // consolidated.
52 virtual void SetImage(ButtonState state, const gfx::ImageSkia& image); 70 virtual void SetImage(ButtonState state, const gfx::ImageSkia& image);
53 71
72 // Sets a vector icon to use for the button images. Generates images for
73 // STATE_NORMAL and STATE_DISALBED based on
74 // ImageButtonDelegate::GetVectorIconColor.
75 void SetVectorIcon(const gfx::VectorIcon& icon);
76
54 // Set the background details. 77 // Set the background details.
55 void SetBackground(SkColor color, 78 void SetBackground(SkColor color,
56 const gfx::ImageSkia* image, 79 const gfx::ImageSkia* image,
57 const gfx::ImageSkia* mask); 80 const gfx::ImageSkia* mask);
58 81
59 // Sets how the image is laid out within the button's bounds. 82 // Sets how the image is laid out within the button's bounds.
60 void SetImageAlignment(HorizontalAlignment h_align, 83 void SetImageAlignment(HorizontalAlignment h_align,
61 VerticalAlignment v_align); 84 VerticalAlignment v_align);
62 85
63 void SetFocusPainter(std::unique_ptr<Painter> focus_painter); 86 void SetFocusPainter(std::unique_ptr<Painter> focus_painter);
64 87
65 // The minimum size of the contents (not including the border). The contents 88 // The minimum size of the contents (not including the border). The contents
66 // will be at least this size, but may be larger if the image itself is 89 // will be at least this size, but may be larger if the image itself is
67 // larger. 90 // larger.
68 const gfx::Size& minimum_image_size() const { return minimum_image_size_; } 91 const gfx::Size& minimum_image_size() const { return minimum_image_size_; }
69 void SetMinimumImageSize(const gfx::Size& size); 92 void SetMinimumImageSize(const gfx::Size& size);
70 93
71 // Whether we should draw our images resources horizontally flipped. 94 // Whether we should draw our images resources horizontally flipped.
72 void SetDrawImageMirrored(bool mirrored) { 95 void SetDrawImageMirrored(bool mirrored) {
73 draw_image_mirrored_ = mirrored; 96 draw_image_mirrored_ = mirrored;
74 } 97 }
75 98
99 void SetPadding(const gfx::Insets& padding);
100
76 // Overridden from View: 101 // Overridden from View:
77 gfx::Size GetPreferredSize() const override; 102 gfx::Size GetPreferredSize() const override;
78 const char* GetClassName() const override; 103 const char* GetClassName() const override;
79 void OnPaint(gfx::Canvas* canvas) override; 104 void OnPaint(gfx::Canvas* canvas) override;
105 void OnThemeChanged() override;
106 void OnNativeThemeChanged(const ui::NativeTheme* theme) override;
80 107
81 protected: 108 protected:
82 // Overridden from View: 109 // Overridden from View:
83 void OnFocus() override; 110 void OnFocus() override;
84 void OnBlur() override; 111 void OnBlur() override;
85 112
86 // Returns the image to paint. This is invoked from paint and returns a value 113 // Returns the image to paint. This is invoked from paint and returns a value
87 // from images. 114 // from images.
88 virtual gfx::ImageSkia GetImageToPaint(); 115 virtual gfx::ImageSkia GetImageToPaint();
89 116
90 // Updates button background for |scale_factor|. 117 // Updates button background for |scale_factor|.
91 void UpdateButtonBackground(ui::ScaleFactor scale_factor); 118 void UpdateButtonBackground(ui::ScaleFactor scale_factor);
92 119
93 Painter* focus_painter() { return focus_painter_.get(); } 120 Painter* focus_painter() { return focus_painter_.get(); }
94 121
95 // The images used to render the different states of this button. 122 // The images used to render the different states of this button.
96 gfx::ImageSkia images_[STATE_COUNT]; 123 gfx::ImageSkia images_[STATE_COUNT];
97 124
98 gfx::ImageSkia background_image_; 125 gfx::ImageSkia background_image_;
99 126
100 private: 127 private:
101 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, Basics); 128 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, Basics);
102 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, ImagePositionWithBorder); 129 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, ImagePositionWithBorder);
103 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, LeftAlignedMirrored); 130 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, LeftAlignedMirrored);
104 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, RightAlignedMirrored); 131 FRIEND_TEST_ALL_PREFIXES(ImageButtonTest, RightAlignedMirrored);
105 132
106 // Returns the correct position of the image for painting. 133 // Returns the correct position of the image for painting.
107 gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image); 134 gfx::Point ComputeImagePaintPosition(const gfx::ImageSkia& image);
108 135
136 void UpdateImagesFromVectorIcon();
137
138 ImageButtonDelegate* delegate_;
139
140 const gfx::VectorIcon* vector_icon_;
141
109 // Image alignment. 142 // Image alignment.
110 HorizontalAlignment h_alignment_; 143 HorizontalAlignment h_alignment_;
111 VerticalAlignment v_alignment_; 144 VerticalAlignment v_alignment_;
112 gfx::Size minimum_image_size_; 145 gfx::Size minimum_image_size_;
113 146
114 // Whether we draw our resources horizontally flipped. This can happen in the 147 // Whether we draw our resources horizontally flipped. This can happen in the
115 // linux titlebar, where image resources were designed to be flipped so a 148 // linux titlebar, where image resources were designed to be flipped so a
116 // small curved corner in the close button designed to fit into the frame 149 // small curved corner in the close button designed to fit into the frame
117 // resources. 150 // resources.
118 bool draw_image_mirrored_; 151 bool draw_image_mirrored_;
119 152
120 std::unique_ptr<Painter> focus_painter_; 153 std::unique_ptr<Painter> focus_painter_;
121 154
122 DISALLOW_COPY_AND_ASSIGN(ImageButton); 155 DISALLOW_COPY_AND_ASSIGN(ImageButton);
123 }; 156 };
124 157
125 //////////////////////////////////////////////////////////////////////////////// 158 ////////////////////////////////////////////////////////////////////////////////
126 // 159 //
127 // ToggleImageButton 160 // ToggleImageButton
128 // 161 //
129 // A toggle-able ImageButton. It swaps out its graphics when toggled. 162 // A toggle-able ImageButton. It swaps out its graphics when toggled.
130 // 163 //
131 //////////////////////////////////////////////////////////////////////////////// 164 ////////////////////////////////////////////////////////////////////////////////
132 class VIEWS_EXPORT ToggleImageButton : public ImageButton { 165 class VIEWS_EXPORT ToggleImageButton : public ImageButton {
133 public: 166 public:
134 explicit ToggleImageButton(ButtonListener* listener); 167 explicit ToggleImageButton(ImageButtonDelegate* delegate);
135 ~ToggleImageButton() override; 168 ~ToggleImageButton() override;
136 169
137 // Change the toggled state. 170 // Change the toggled state.
138 void SetToggled(bool toggled); 171 void SetToggled(bool toggled);
139 172
140 // Like ImageButton::SetImage(), but to set the graphics used for the 173 // Like ImageButton::SetImage(), but to set the graphics used for the
141 // "has been toggled" state. Must be called for each button state 174 // "has been toggled" state. Must be called for each button state
142 // before the button is toggled. 175 // before the button is toggled.
143 void SetToggledImage(ButtonState state, const gfx::ImageSkia* image); 176 void SetToggledImage(ButtonState state, const gfx::ImageSkia* image);
144 177
145 // Set the tooltip text displayed when the button is toggled. 178 // Set the tooltip text displayed when the button is toggled.
146 void SetToggledTooltipText(const base::string16& tooltip); 179 void SetToggledTooltipText(const base::string16& tooltip);
147 180
148 // Overridden from ImageButton: 181 // ImageButton:
149 const gfx::ImageSkia& GetImage(ButtonState state) const override; 182 const gfx::ImageSkia& GetImage(ButtonState state) const override;
150 void SetImage(ButtonState state, const gfx::ImageSkia& image) override; 183 void SetImage(ButtonState state, const gfx::ImageSkia& image) override;
151 184
152 // Overridden from View: 185 // View:
153 bool GetTooltipText(const gfx::Point& p, 186 bool GetTooltipText(const gfx::Point& p,
154 base::string16* tooltip) const override; 187 base::string16* tooltip) const override;
155 void GetAccessibleNodeData(ui::AXNodeData* node_data) override; 188 void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
156 189
157 private: 190 private:
158 // The parent class's images_ member is used for the current images, 191 // The parent class's images_ member is used for the current images,
159 // and this array is used to hold the alternative images. 192 // and this array is used to hold the alternative images.
160 // We swap between the two when toggling. 193 // We swap between the two when toggling.
161 gfx::ImageSkia alternate_images_[STATE_COUNT]; 194 gfx::ImageSkia alternate_images_[STATE_COUNT];
162 195
163 // True if the button is currently toggled. 196 // True if the button is currently toggled.
164 bool toggled_; 197 bool toggled_;
165 198
166 // The parent class's tooltip_text_ is displayed when not toggled, and 199 // The parent class's tooltip_text_ is displayed when not toggled, and
167 // this one is shown when toggled. 200 // this one is shown when toggled.
168 base::string16 toggled_tooltip_text_; 201 base::string16 toggled_tooltip_text_;
169 202
170 DISALLOW_COPY_AND_ASSIGN(ToggleImageButton); 203 DISALLOW_COPY_AND_ASSIGN(ToggleImageButton);
171 }; 204 };
172 205
173 } // namespace views 206 } // namespace views
174 207
175 #endif // UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_ 208 #endif // UI_VIEWS_CONTROLS_BUTTON_IMAGE_BUTTON_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698