browser_controls_offset_manager.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2016 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 CC_INPUT_BROWSER_CONTROLS_OFFSET_MANAGER_H_
  5. #define CC_INPUT_BROWSER_CONTROLS_OFFSET_MANAGER_H_
  6. #include <memory>
  7. #include <utility>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/time/time.h"
  10. #include "cc/input/browser_controls_state.h"
  11. #include "cc/layers/layer_impl.h"
  12. #include "cc/trees/browser_controls_params.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "ui/gfx/geometry/size.h"
  15. #include "ui/gfx/geometry/vector2d_f.h"
  16. namespace cc {
  17. class BrowserControlsOffsetManagerClient;
  18. // Manages the position of the browser controls.
  19. class CC_EXPORT BrowserControlsOffsetManager {
  20. public:
  21. enum class AnimationDirection {
  22. NO_ANIMATION,
  23. SHOWING_CONTROLS,
  24. HIDING_CONTROLS
  25. };
  26. static std::unique_ptr<BrowserControlsOffsetManager> Create(
  27. BrowserControlsOffsetManagerClient* client,
  28. float controls_show_threshold,
  29. float controls_hide_threshold);
  30. BrowserControlsOffsetManager(const BrowserControlsOffsetManager&) = delete;
  31. virtual ~BrowserControlsOffsetManager();
  32. BrowserControlsOffsetManager& operator=(const BrowserControlsOffsetManager&) =
  33. delete;
  34. // The offset from the window top to the top edge of the controls. Runs from 0
  35. // (controls fully shown) to negative values (down is positive).
  36. float ControlsTopOffset() const;
  37. // The amount of offset of the web content area. Same as the current shown
  38. // height of the browser controls.
  39. float ContentTopOffset() const;
  40. float TopControlsShownRatio() const;
  41. float TopControlsHeight() const;
  42. float TopControlsMinHeight() const;
  43. // The minimum shown ratio top controls can have.
  44. float TopControlsMinShownRatio() const;
  45. // The current top controls min-height. If the min-height is changing with an
  46. // animation, this will return a value between the old min-height and the new
  47. // min-height, which is equal to the current visible min-height. Otherwise,
  48. // this will return the same value as |TopControlsMinHeight()|.
  49. float TopControlsMinHeightOffset() const;
  50. // The amount of offset of the web content area, calculating from the bottom.
  51. // Same as the current shown height of the bottom controls.
  52. float ContentBottomOffset() const;
  53. // Similar to TopControlsHeight(), this method should return a static value.
  54. // The current animated height should be acquired from ContentBottomOffset().
  55. float BottomControlsHeight() const;
  56. float BottomControlsMinHeight() const;
  57. float BottomControlsShownRatio() const;
  58. // The minimum shown ratio bottom controls can have.
  59. float BottomControlsMinShownRatio() const;
  60. // The current bottom controls min-height. If the min-height is changing with
  61. // an animation, this will return a value between the old min-height and the
  62. // new min-height, which is equal to the current visible min-height.
  63. // Otherwise, this will return the same value as |BottomControlsMinHeight()|.
  64. float BottomControlsMinHeightOffset() const;
  65. // Valid shown ratio range for the top controls. The values will be (0, 1) if
  66. // there is no animation running.
  67. std::pair<float, float> TopControlsShownRatioRange();
  68. // Valid shown ratio range for the bottom controls. The values will be (0, 1)
  69. // if there is no animation running.
  70. std::pair<float, float> BottomControlsShownRatioRange();
  71. bool HasAnimation();
  72. void UpdateBrowserControlsState(BrowserControlsState constraints,
  73. BrowserControlsState current,
  74. bool animate);
  75. // Return the browser control constraint that must be synced to the
  76. // main renderer thread (to trigger viewport and related changes).
  77. BrowserControlsState PullConstraintForMainThread(
  78. bool* out_changed_since_commit);
  79. // Called to notify this object that the control constraint has
  80. // been pushed to the main thread. When a compositor commit does not
  81. // happen the value pulled by the method above may not be synced;
  82. // a call to this method notifies us that it has.
  83. void NotifyConstraintSyncedToMainThread();
  84. void OnBrowserControlsParamsChanged(bool animate_changes);
  85. void ScrollBegin();
  86. gfx::Vector2dF ScrollBy(const gfx::Vector2dF& pending_delta);
  87. void ScrollEnd();
  88. // The caller should ensure that |Pinch{Begin,End}| are called within
  89. // the scope of |Scroll{Begin,End}|.
  90. void PinchBegin();
  91. void PinchEnd();
  92. gfx::Vector2dF Animate(base::TimeTicks monotonic_time);
  93. protected:
  94. BrowserControlsOffsetManager(BrowserControlsOffsetManagerClient* client,
  95. float controls_show_threshold,
  96. float controls_hide_threshold);
  97. private:
  98. class Animation;
  99. void ResetAnimations();
  100. void SetupAnimation(AnimationDirection direction);
  101. void StartAnimationIfNecessary();
  102. void ResetBaseline();
  103. float OldTopControlsMinShownRatio();
  104. float OldBottomControlsMinShownRatio();
  105. void UpdateOldBrowserControlsParams();
  106. void InitAnimationForHeightChange(Animation* animation,
  107. float start_ratio,
  108. float stop_ratio);
  109. void SetTopMinHeightOffsetAnimationRange(float from, float to);
  110. void SetBottomMinHeightOffsetAnimationRange(float from, float to);
  111. // The client manages the lifecycle of this.
  112. raw_ptr<BrowserControlsOffsetManagerClient> client_;
  113. BrowserControlsState permitted_state_;
  114. // Accumulated scroll delta since last baseline reset
  115. float accumulated_scroll_delta_;
  116. // Content offset when last baseline reset occurred.
  117. float baseline_top_content_offset_;
  118. float baseline_bottom_content_offset_;
  119. // The percent height of the visible control such that it must be shown
  120. // when the user stops the scroll.
  121. float controls_show_threshold_;
  122. // The percent height of the visible control such that it must be hidden
  123. // when the user stops the scroll.
  124. float controls_hide_threshold_;
  125. bool pinch_gesture_active_;
  126. // Used to track whether the constraint has changed and we need up reflect
  127. // the changes to Blink.
  128. bool constraint_changed_since_commit_;
  129. // The old browser controls params that are used to figure out how to animate
  130. // the height and min-height changes.
  131. BrowserControlsParams old_browser_controls_params_;
  132. // Whether a min-height change animation is in progress.
  133. bool top_min_height_change_in_progress_;
  134. bool bottom_min_height_change_in_progress_;
  135. // Current top/bottom controls min-height.
  136. float top_controls_min_height_offset_;
  137. float bottom_controls_min_height_offset_;
  138. // Minimum and maximum values |top_controls_min_height_offset_| can take
  139. // during the current min-height change animation.
  140. absl::optional<std::pair<float, float>>
  141. top_min_height_offset_animation_range_;
  142. // Minimum and maximum values |bottom_controls_min_height_offset_| can take
  143. // during the current min-height change animation.
  144. absl::optional<std::pair<float, float>>
  145. bottom_min_height_offset_animation_range_;
  146. // Class that holds and manages the state of the controls animations.
  147. class Animation {
  148. public:
  149. Animation();
  150. // Whether the animation is initialized with a direction and start and stop
  151. // values.
  152. bool IsInitialized() { return initialized_; }
  153. AnimationDirection Direction() { return direction_; }
  154. void Initialize(AnimationDirection direction,
  155. float start_value,
  156. float stop_value,
  157. int64_t duration,
  158. bool jump_to_end_on_reset);
  159. // Returns the animated value for the given monotonic time tick if the
  160. // animation is initialized. Otherwise, returns |absl::nullopt|.
  161. absl::optional<float> Tick(base::TimeTicks monotonic_time);
  162. // Set the minimum and maximum values the animation can have.
  163. void SetBounds(float min, float max);
  164. // Reset the properties. If |skip_to_end_on_reset_| is false, this function
  165. // will return |absl::nullopt|. Otherwise, it will return the end value
  166. // (clamped to min-max).
  167. absl::optional<float> Reset();
  168. // Returns the value the animation will end on. This will be the stop_value
  169. // passed to the constructor clamped by the currently configured bounds.
  170. float FinalValue();
  171. // Return the bounds.
  172. float min_value() { return min_value_; }
  173. float max_value() { return max_value_; }
  174. private:
  175. bool IsComplete(float value);
  176. // Whether the animation is running.
  177. bool started_ = false;
  178. // Whether the animation is initialized by setting start and stop time and
  179. // values.
  180. bool initialized_ = false;
  181. AnimationDirection direction_ = AnimationDirection::NO_ANIMATION;
  182. // Monotonic start and stop times.
  183. base::TimeTicks start_time_;
  184. base::TimeTicks stop_time_;
  185. // Animation duration.
  186. base::TimeDelta duration_;
  187. // Start and stop values.
  188. float start_value_ = 0.f;
  189. float stop_value_ = 0.f;
  190. // Minimum and maximum values the animation can have, used to decide if the
  191. // animation is complete.
  192. float min_value_ = 0.f;
  193. float max_value_ = 1.f;
  194. // Whether to fast-forward to end when reset. It is still BCOM's
  195. // responsibility to actually set the shown ratios using the value returned
  196. // by ::Reset().
  197. bool jump_to_end_on_reset_ = false;
  198. };
  199. Animation top_controls_animation_;
  200. Animation bottom_controls_animation_;
  201. };
  202. } // namespace cc
  203. #endif // CC_INPUT_BROWSER_CONTROLS_OFFSET_MANAGER_H_