rounded_image_view.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2020 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. #ifndef ASH_PUBLIC_CPP_ROUNDED_IMAGE_VIEW_H_
  5. #define ASH_PUBLIC_CPP_ROUNDED_IMAGE_VIEW_H_
  6. #include "ash/public/cpp/ash_public_export.h"
  7. #include "ui/gfx/geometry/size.h"
  8. #include "ui/gfx/image/image_skia.h"
  9. #include "ui/views/metadata/view_factory.h"
  10. #include "ui/views/view.h"
  11. namespace ash {
  12. // A custom image view with rounded edges.
  13. class ASH_PUBLIC_EXPORT RoundedImageView : public views::View {
  14. public:
  15. enum class Alignment {
  16. // The image's drawn portion always contains the image's origin.
  17. kLeading,
  18. // If the image's size is greater than the view's, only the portion around
  19. // the image's center shows.
  20. kCenter
  21. };
  22. RoundedImageView();
  23. RoundedImageView(int corner_radius, Alignment alignment);
  24. RoundedImageView(const RoundedImageView&) = delete;
  25. RoundedImageView& operator=(const RoundedImageView&) = delete;
  26. ~RoundedImageView() override;
  27. // Set the image that should be displayed. The image contents is copied to the
  28. // receiver's image.
  29. void SetImage(const gfx::ImageSkia& image);
  30. // Similar with the method above but the preferred image size is `size`.
  31. void SetImage(const gfx::ImageSkia& image, const gfx::Size& size);
  32. // Set the radii of the corners independently.
  33. void SetCornerRadii(int top_left,
  34. int top_right,
  35. int bottom_right,
  36. int bottom_left);
  37. // Sets all radii of the corners collectively.
  38. void SetCornerRadius(int corner_radius);
  39. // views::View:
  40. gfx::Size CalculatePreferredSize() const override;
  41. void OnPaint(gfx::Canvas* canvas) override;
  42. const char* GetClassName() const override;
  43. const gfx::ImageSkia& original_image() const { return original_image_; }
  44. private:
  45. // Returns the preferred image size.
  46. gfx::Size GetImageSize() const;
  47. gfx::ImageSkia original_image_;
  48. gfx::ImageSkia resized_image_;
  49. int corner_radius_[4];
  50. const Alignment alignment_;
  51. };
  52. BEGIN_VIEW_BUILDER(ASH_PUBLIC_EXPORT, RoundedImageView, views::View)
  53. VIEW_BUILDER_PROPERTY(int, CornerRadius)
  54. END_VIEW_BUILDER
  55. } // namespace ash
  56. DEFINE_VIEW_BUILDER(ASH_PUBLIC_EXPORT, ash::RoundedImageView)
  57. #endif // ASH_PUBLIC_CPP_ROUNDED_IMAGE_VIEW_H_