media_artwork_view.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2021 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. #include "components/media_message_center/media_artwork_view.h"
  5. #include "ui/gfx/canvas.h"
  6. #include "ui/gfx/geometry/skia_conversions.h"
  7. namespace media_message_center {
  8. namespace {
  9. // Get target bounds of the image fitting into |canvas_size|.
  10. // If |image_size| is greater than |expect_size| in any dimension,
  11. // we shrink it down to fit, keep the size otherwise.
  12. gfx::Rect GetTargetBound(const gfx::Size image_size,
  13. const gfx::Size expect_size,
  14. const gfx::Size canvas_size) {
  15. gfx::Size target_size = image_size;
  16. if (image_size.width() > expect_size.width() ||
  17. image_size.height() > expect_size.height()) {
  18. const float scale = std::min(
  19. expect_size.width() / static_cast<float>(image_size.width()),
  20. expect_size.height() / static_cast<float>(image_size.height()));
  21. target_size = gfx::ScaleToFlooredSize(image_size, scale);
  22. }
  23. int offset_x = (canvas_size.width() - target_size.width()) / 2;
  24. int offset_y = (canvas_size.height() - target_size.height()) / 2;
  25. return gfx::Rect(offset_x, offset_y, target_size.width(),
  26. target_size.height());
  27. }
  28. } // anonymous namespace
  29. MediaArtworkView::MediaArtworkView(float corner_radius,
  30. const gfx::Size& artwork_size,
  31. const gfx::Size& favicon_size)
  32. : corner_radius_(corner_radius),
  33. artwork_size_(artwork_size),
  34. favicon_size_(favicon_size) {}
  35. void MediaArtworkView::SetVignetteColor(const SkColor& vignette_color) {
  36. if (vignette_color_ == vignette_color)
  37. return;
  38. vignette_color_ = vignette_color;
  39. OnPropertyChanged(&vignette_color_, views::kPropertyEffectsPaint);
  40. }
  41. SkColor MediaArtworkView::GetVignetteColor() const {
  42. return vignette_color_;
  43. }
  44. void MediaArtworkView::SetBackgroundColor(const SkColor& background_color) {
  45. background_color_ = background_color;
  46. }
  47. void MediaArtworkView::SetImage(const gfx::ImageSkia& image) {
  48. image_ = image;
  49. }
  50. void MediaArtworkView::SetFavicon(const gfx::ImageSkia& favicon) {
  51. favicon_ = favicon;
  52. }
  53. void MediaArtworkView::OnPaint(gfx::Canvas* canvas) {
  54. views::View::OnPaint(canvas);
  55. {
  56. // Paint background.
  57. cc::PaintFlags paint_flags;
  58. paint_flags.setStyle(cc::PaintFlags::kFill_Style);
  59. paint_flags.setAntiAlias(true);
  60. paint_flags.setColor(background_color_);
  61. canvas->DrawRect(gfx::Rect(artwork_size_), paint_flags);
  62. }
  63. // Draw image if we have artwork; fallback to favicon if we don't.
  64. if (!image_.isNull()) {
  65. gfx::Rect target =
  66. GetTargetBound(image_.size(), artwork_size_, artwork_size_);
  67. canvas->DrawImageInt(image_, 0, 0, image_.width(), image_.height(),
  68. target.x(), target.y(), target.width(),
  69. target.height(), false);
  70. } else if (!favicon_.isNull()) {
  71. gfx::Rect target =
  72. GetTargetBound(favicon_.size(), favicon_size_, artwork_size_);
  73. canvas->DrawImageInt(favicon_, 0, 0, favicon_.width(), favicon_.height(),
  74. target.x(), target.y(), target.width(),
  75. target.height(), false);
  76. }
  77. {
  78. auto path = SkPath().addRoundRect(RectToSkRect(GetLocalBounds()),
  79. corner_radius_, corner_radius_);
  80. path.toggleInverseFillType();
  81. cc::PaintFlags paint_flags;
  82. paint_flags.setStyle(cc::PaintFlags::kFill_Style);
  83. paint_flags.setAntiAlias(true);
  84. paint_flags.setColor(vignette_color_);
  85. canvas->DrawPath(path, paint_flags);
  86. }
  87. }
  88. BEGIN_METADATA(MediaArtworkView, views::View)
  89. ADD_PROPERTY_METADATA(SkColor, VignetteColor)
  90. END_METADATA
  91. } // namespace media_message_center