| OLD | NEW |
| 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 #include "ui/views/controls/button/image_button.h" | 5 #include "ui/views/controls/button/image_button.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "ui/accessibility/ax_node_data.h" | 10 #include "ui/accessibility/ax_node_data.h" |
| 11 #include "ui/gfx/animation/throb_animation.h" | 11 #include "ui/gfx/animation/throb_animation.h" |
| 12 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/image/image_skia_operations.h" | 13 #include "ui/gfx/image/image_skia_operations.h" |
| 14 #include "ui/gfx/paint_vector_icon.h" |
| 14 #include "ui/gfx/scoped_canvas.h" | 15 #include "ui/gfx/scoped_canvas.h" |
| 16 #include "ui/gfx/vector_icon_types.h" |
| 17 #include "ui/views/animation/ink_drop_host_view.h" |
| 18 #include "ui/views/border.h" |
| 15 #include "ui/views/painter.h" | 19 #include "ui/views/painter.h" |
| 20 #include "ui/views/views_delegate.h" |
| 16 #include "ui/views/widget/widget.h" | 21 #include "ui/views/widget/widget.h" |
| 17 | 22 |
| 18 namespace views { | 23 namespace views { |
| 19 | 24 |
| 20 // Default button size if no image is set. This is ignored if there is an image, | 25 // Default button size if no image is set. This is ignored if there is an image, |
| 21 // and exists for historical reasons (any number of clients could depend on this | 26 // and exists for historical reasons (any number of clients could depend on this |
| 22 // behaviour). | 27 // behaviour). |
| 23 static const int kDefaultWidth = 16; | 28 static const int kDefaultWidth = 16; |
| 24 static const int kDefaultHeight = 14; | 29 static const int kDefaultHeight = 14; |
| 25 | 30 |
| 26 const char ImageButton::kViewClassName[] = "ImageButton"; | 31 const char ImageButton::kViewClassName[] = "ImageButton"; |
| 27 | 32 |
| 33 SkColor ImageButtonDelegate::GetVectorIconColor() const { |
| 34 return SK_ColorBLACK; |
| 35 } |
| 36 |
| 37 // static |
| 38 ImageButton* ImageButton::CreateDefaultVectorIconButton( |
| 39 const gfx::VectorIcon& icon, |
| 40 ImageButtonDelegate* delegate) { |
| 41 ImageButton* button = new ImageButton(delegate); |
| 42 button->SetVectorIcon(icon); |
| 43 button->SetInkDropMode(InkDropHostView::InkDropMode::ON); |
| 44 button->set_has_ink_drop_action_on_click(true); |
| 45 button->SetImageAlignment(ImageButton::ALIGN_CENTER, |
| 46 ImageButton::ALIGN_MIDDLE); |
| 47 button->SetFocusPainter(nullptr); |
| 48 button->SetPadding(ViewsDelegate::GetInstance()->GetButtonMargins()); |
| 49 return button; |
| 50 } |
| 51 |
| 28 //////////////////////////////////////////////////////////////////////////////// | 52 //////////////////////////////////////////////////////////////////////////////// |
| 29 // ImageButton, public: | 53 // ImageButton, public: |
| 30 | 54 |
| 31 ImageButton::ImageButton(ButtonListener* listener) | 55 ImageButton::ImageButton(ImageButtonDelegate* delegate) |
| 32 : CustomButton(listener), | 56 : CustomButton(delegate), |
| 57 vector_icon_(nullptr), |
| 33 h_alignment_(ALIGN_LEFT), | 58 h_alignment_(ALIGN_LEFT), |
| 34 v_alignment_(ALIGN_TOP), | 59 v_alignment_(ALIGN_TOP), |
| 35 draw_image_mirrored_(false), | 60 draw_image_mirrored_(false), |
| 36 focus_painter_(Painter::CreateDashedFocusPainter()) { | 61 focus_painter_(Painter::CreateDashedFocusPainter()) { |
| 62 delegate_ = delegate; |
| 37 // By default, we request that the gfx::Canvas passed to our View::OnPaint() | 63 // By default, we request that the gfx::Canvas passed to our View::OnPaint() |
| 38 // implementation is flipped horizontally so that the button's images are | 64 // implementation is flipped horizontally so that the button's images are |
| 39 // mirrored when the UI directionality is right-to-left. | 65 // mirrored when the UI directionality is right-to-left. |
| 40 EnableCanvasFlippingForRTLUI(true); | 66 EnableCanvasFlippingForRTLUI(true); |
| 41 } | 67 } |
| 42 | 68 |
| 43 ImageButton::~ImageButton() { | 69 ImageButton::~ImageButton() { |
| 44 } | 70 } |
| 45 | 71 |
| 46 const gfx::ImageSkia& ImageButton::GetImage(ButtonState state) const { | 72 const gfx::ImageSkia& ImageButton::GetImage(ButtonState state) const { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 57 const gfx::Size old_preferred_size = GetPreferredSize(); | 83 const gfx::Size old_preferred_size = GetPreferredSize(); |
| 58 images_[for_state] = image; | 84 images_[for_state] = image; |
| 59 | 85 |
| 60 if (old_preferred_size != GetPreferredSize()) | 86 if (old_preferred_size != GetPreferredSize()) |
| 61 PreferredSizeChanged(); | 87 PreferredSizeChanged(); |
| 62 | 88 |
| 63 if (state() == for_state) | 89 if (state() == for_state) |
| 64 SchedulePaint(); | 90 SchedulePaint(); |
| 65 } | 91 } |
| 66 | 92 |
| 93 void ImageButton::SetVectorIcon(const gfx::VectorIcon& icon) { |
| 94 vector_icon_ = &icon; |
| 95 UpdateImagesFromVectorIcon(); |
| 96 } |
| 97 |
| 67 void ImageButton::SetBackground(SkColor color, | 98 void ImageButton::SetBackground(SkColor color, |
| 68 const gfx::ImageSkia* image, | 99 const gfx::ImageSkia* image, |
| 69 const gfx::ImageSkia* mask) { | 100 const gfx::ImageSkia* mask) { |
| 70 if (image == NULL || mask == NULL) { | 101 if (image == NULL || mask == NULL) { |
| 71 background_image_ = gfx::ImageSkia(); | 102 background_image_ = gfx::ImageSkia(); |
| 72 return; | 103 return; |
| 73 } | 104 } |
| 74 | 105 |
| 75 background_image_ = gfx::ImageSkiaOperations::CreateButtonBackground(color, | 106 background_image_ = gfx::ImageSkiaOperations::CreateButtonBackground(color, |
| 76 *image, *mask); | 107 *image, *mask); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 88 } | 119 } |
| 89 | 120 |
| 90 void ImageButton::SetMinimumImageSize(const gfx::Size& size) { | 121 void ImageButton::SetMinimumImageSize(const gfx::Size& size) { |
| 91 if (minimum_image_size_ == size) | 122 if (minimum_image_size_ == size) |
| 92 return; | 123 return; |
| 93 | 124 |
| 94 minimum_image_size_ = size; | 125 minimum_image_size_ = size; |
| 95 PreferredSizeChanged(); | 126 PreferredSizeChanged(); |
| 96 } | 127 } |
| 97 | 128 |
| 129 void ImageButton::SetPadding(const gfx::Insets& padding) { |
| 130 if (padding.IsEmpty()) |
| 131 SetBorder(nullptr); |
| 132 else |
| 133 SetBorder(CreateEmptyBorder(padding)); |
| 134 } |
| 135 |
| 98 //////////////////////////////////////////////////////////////////////////////// | 136 //////////////////////////////////////////////////////////////////////////////// |
| 99 // ImageButton, View overrides: | 137 // ImageButton, View overrides: |
| 100 | 138 |
| 101 gfx::Size ImageButton::GetPreferredSize() const { | 139 gfx::Size ImageButton::GetPreferredSize() const { |
| 102 gfx::Size size(kDefaultWidth, kDefaultHeight); | 140 gfx::Size size(kDefaultWidth, kDefaultHeight); |
| 103 if (!images_[STATE_NORMAL].isNull()) { | 141 if (!images_[STATE_NORMAL].isNull()) { |
| 104 size = gfx::Size(images_[STATE_NORMAL].width(), | 142 size = gfx::Size(images_[STATE_NORMAL].width(), |
| 105 images_[STATE_NORMAL].height()); | 143 images_[STATE_NORMAL].height()); |
| 106 } | 144 } |
| 107 | 145 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 135 gfx::Point position = ComputeImagePaintPosition(img); | 173 gfx::Point position = ComputeImagePaintPosition(img); |
| 136 if (!background_image_.isNull()) | 174 if (!background_image_.isNull()) |
| 137 canvas->DrawImageInt(background_image_, position.x(), position.y()); | 175 canvas->DrawImageInt(background_image_, position.x(), position.y()); |
| 138 | 176 |
| 139 canvas->DrawImageInt(img, position.x(), position.y()); | 177 canvas->DrawImageInt(img, position.x(), position.y()); |
| 140 } | 178 } |
| 141 | 179 |
| 142 Painter::PaintFocusPainter(this, canvas, focus_painter()); | 180 Painter::PaintFocusPainter(this, canvas, focus_painter()); |
| 143 } | 181 } |
| 144 | 182 |
| 183 void ImageButton::OnThemeChanged() { |
| 184 UpdateImagesFromVectorIcon(); |
| 185 } |
| 186 |
| 187 void ImageButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
| 188 UpdateImagesFromVectorIcon(); |
| 189 } |
| 190 |
| 145 //////////////////////////////////////////////////////////////////////////////// | 191 //////////////////////////////////////////////////////////////////////////////// |
| 146 // ImageButton, protected: | 192 // ImageButton, protected: |
| 147 | 193 |
| 148 void ImageButton::OnFocus() { | 194 void ImageButton::OnFocus() { |
| 149 CustomButton::OnFocus(); | 195 CustomButton::OnFocus(); |
| 150 if (focus_painter_.get()) | 196 if (focus_painter_.get()) |
| 151 SchedulePaint(); | 197 SchedulePaint(); |
| 152 } | 198 } |
| 153 | 199 |
| 154 void ImageButton::OnBlur() { | 200 void ImageButton::OnBlur() { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 y = (rect.height() - image.height()) / 2; | 241 y = (rect.height() - image.height()) / 2; |
| 196 else if (v_alignment_ == ALIGN_BOTTOM) | 242 else if (v_alignment_ == ALIGN_BOTTOM) |
| 197 y = rect.height() - image.height(); | 243 y = rect.height() - image.height(); |
| 198 | 244 |
| 199 x += rect.x(); | 245 x += rect.x(); |
| 200 y += rect.y(); | 246 y += rect.y(); |
| 201 | 247 |
| 202 return gfx::Point(x, y); | 248 return gfx::Point(x, y); |
| 203 } | 249 } |
| 204 | 250 |
| 251 void ImageButton::UpdateImagesFromVectorIcon() { |
| 252 if (!vector_icon_) |
| 253 return; |
| 254 |
| 255 SkColor icon_color = |
| 256 color_utils::DeriveDefaultIconColor(delegate_->GetVectorIconColor()); |
| 257 SkColor disabled_color = SkColorSetA(icon_color, 0xff / 2); |
| 258 SetImage(CustomButton::STATE_NORMAL, |
| 259 gfx::CreateVectorIcon(*vector_icon_, icon_color)); |
| 260 SetImage(CustomButton::STATE_DISABLED, |
| 261 gfx::CreateVectorIcon(*vector_icon_, disabled_color)); |
| 262 set_ink_drop_base_color(icon_color); |
| 263 } |
| 264 |
| 205 //////////////////////////////////////////////////////////////////////////////// | 265 //////////////////////////////////////////////////////////////////////////////// |
| 206 // ToggleImageButton, public: | 266 // ToggleImageButton, public: |
| 207 | 267 |
| 208 ToggleImageButton::ToggleImageButton(ButtonListener* listener) | 268 ToggleImageButton::ToggleImageButton(ImageButtonDelegate* delegate) |
| 209 : ImageButton(listener), | 269 : ImageButton(delegate), toggled_(false) {} |
| 210 toggled_(false) { | |
| 211 } | |
| 212 | 270 |
| 213 ToggleImageButton::~ToggleImageButton() { | 271 ToggleImageButton::~ToggleImageButton() { |
| 214 } | 272 } |
| 215 | 273 |
| 216 void ToggleImageButton::SetToggled(bool toggled) { | 274 void ToggleImageButton::SetToggled(bool toggled) { |
| 217 if (toggled == toggled_) | 275 if (toggled == toggled_) |
| 218 return; | 276 return; |
| 219 | 277 |
| 220 for (int i = 0; i < STATE_COUNT; ++i) { | 278 for (int i = 0; i < STATE_COUNT; ++i) { |
| 221 gfx::ImageSkia temp = images_[i]; | 279 gfx::ImageSkia temp = images_[i]; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 // accessible toggle button. | 345 // accessible toggle button. |
| 288 if ((toggled_ && !images_[ButtonState::STATE_NORMAL].isNull()) || | 346 if ((toggled_ && !images_[ButtonState::STATE_NORMAL].isNull()) || |
| 289 (!toggled_ && !alternate_images_[ButtonState::STATE_NORMAL].isNull())) { | 347 (!toggled_ && !alternate_images_[ButtonState::STATE_NORMAL].isNull())) { |
| 290 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON; | 348 node_data->role = ui::AX_ROLE_TOGGLE_BUTTON; |
| 291 if (toggled_) | 349 if (toggled_) |
| 292 node_data->AddStateFlag(ui::AX_STATE_PRESSED); | 350 node_data->AddStateFlag(ui::AX_STATE_PRESSED); |
| 293 } | 351 } |
| 294 } | 352 } |
| 295 | 353 |
| 296 } // namespace views | 354 } // namespace views |
| OLD | NEW |