scroll_view_gradient_helper.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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_SCROLL_VIEW_GRADIENT_HELPER_H_
  5. #define ASH_CONTROLS_SCROLL_VIEW_GRADIENT_HELPER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "base/callback_list.h"
  9. #include "ui/views/controls/scroll_view.h"
  10. namespace ash {
  11. class GradientLayerDelegate;
  12. // Draws fade in / fade out gradients at the top and bottom of a ScrollView.
  13. // Uses layer masks to draw the gradient. Each gradient only shows if the view
  14. // can be scrolled in that direction. For efficiency, does not create a layer
  15. // mask if no gradient is showing (i.e. if the scroll view contents fit in the
  16. // viewport and hence the view cannot be scrolled).
  17. //
  18. // The gradient is created on the first call to UpdateGradientZone(), not in the
  19. // constructor. This allows the helper to be created any time during view tree
  20. // construction, even before the scroll view contents are known.
  21. //
  22. // Views using this helper should call UpdateGradientZone() whenever the scroll
  23. // view bounds or contents bounds change (e.g. from Layout()).
  24. //
  25. // The gradient is destroyed when this object is destroyed. This does not add
  26. // extra work in the common views::View teardown case (the layer would be
  27. // destroyed anyway).
  28. class ASH_EXPORT ScrollViewGradientHelper {
  29. public:
  30. // `scroll_view` must have a layer.
  31. ScrollViewGradientHelper(views::ScrollView* scroll_view, int gradient_height);
  32. ~ScrollViewGradientHelper();
  33. // Updates the gradients based on `scroll_view_` bounds and scroll position.
  34. void UpdateGradientZone();
  35. GradientLayerDelegate* gradient_layer_for_test() {
  36. return gradient_layer_.get();
  37. }
  38. private:
  39. friend class ScopedScrollViewGradientDisabler;
  40. // Removes the scroll view mask layer.
  41. void RemoveMaskLayer();
  42. // The scroll view being decorated.
  43. views::ScrollView* const scroll_view_;
  44. // The height of the gradient in DIPs.
  45. const int gradient_height_;
  46. // Draws the fade in/out gradients via a `scroll_view_` mask layer.
  47. std::unique_ptr<GradientLayerDelegate> gradient_layer_;
  48. // Callback subscriptions.
  49. base::CallbackListSubscription on_contents_scrolled_subscription_;
  50. base::CallbackListSubscription on_contents_scroll_ended_subscription_;
  51. };
  52. } // namespace ash
  53. #endif // ASH_CONTROLS_SCROLL_VIEW_GRADIENT_HELPER_H_