gradient_layer_delegate.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef ASH_CONTROLS_GRADIENT_LAYER_DELEGATE_H_
  5. #define ASH_CONTROLS_GRADIENT_LAYER_DELEGATE_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ui/compositor/layer.h"
  9. #include "ui/compositor/layer_delegate.h"
  10. #include "ui/gfx/animation/animation_delegate.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. namespace gfx {
  13. class Canvas;
  14. class LinearAnimation;
  15. } // namespace gfx
  16. namespace ash {
  17. // A layer delegate that paints optional fade-in/out gradient zones at the ends
  18. // of its layer.
  19. class ASH_EXPORT GradientLayerDelegate : public ui::LayerDelegate,
  20. public gfx::AnimationDelegate {
  21. public:
  22. struct FadeZone {
  23. // Bounds of the fade in/out zone.
  24. gfx::Rect zone_rect;
  25. // Specifies the type of FadeZone: fade in or fade out.
  26. bool fade_in = false;
  27. // Indicates the drawing direction.
  28. bool is_horizontal = false;
  29. };
  30. // If `animate_in` is true, the gradient will quickly animate in when it is
  31. // first created. See https://crbug.com/1293100
  32. explicit GradientLayerDelegate(bool animate_in);
  33. GradientLayerDelegate(const GradientLayerDelegate&) = delete;
  34. GradientLayerDelegate& operator=(const GradientLayerDelegate&) = delete;
  35. ~GradientLayerDelegate() override;
  36. void set_start_fade_zone(const FadeZone& fade_zone) {
  37. start_fade_zone_ = fade_zone;
  38. }
  39. void set_end_fade_zone(const FadeZone& fade_zone) {
  40. end_fade_zone_ = fade_zone;
  41. }
  42. const gfx::Rect& start_fade_zone_bounds() const {
  43. return start_fade_zone_.zone_rect;
  44. }
  45. const gfx::Rect& end_fade_zone_bounds() const {
  46. return end_fade_zone_.zone_rect;
  47. }
  48. ui::Layer* layer() { return &layer_; }
  49. private:
  50. // ui::LayerDelegate:
  51. void OnPaintLayer(const ui::PaintContext& context) override;
  52. void OnDeviceScaleFactorChanged(float old_device_scale_factor,
  53. float new_device_scale_factor) override {}
  54. // gfx::AnimationDelegate:
  55. void AnimationProgressed(const gfx::Animation* animation) override;
  56. void DrawFadeZone(const FadeZone& fade_zone, gfx::Canvas* canvas);
  57. ui::Layer layer_;
  58. FadeZone start_fade_zone_;
  59. FadeZone end_fade_zone_;
  60. // Optional animation to quickly animate in the gradient on creation.
  61. std::unique_ptr<gfx::LinearAnimation> animation_;
  62. };
  63. } // namespace ash
  64. #endif // ASH_CONTROLS_GRADIENT_LAYER_DELEGATE_H_