1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125

cc / input / single_scrollbar_animation_controller_thinning.h [blame]

// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_INPUT_SINGLE_SCROLLBAR_ANIMATION_CONTROLLER_THINNING_H_
#define CC_INPUT_SINGLE_SCROLLBAR_ANIMATION_CONTROLLER_THINNING_H_

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "cc/cc_export.h"
#include "cc/input/scrollbar.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/scrollbar_layer_impl_base.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace cc {

class ScrollbarAnimationControllerClient;

// ScrollbarAnimationControllerThinning for one scrollbar
class CC_EXPORT SingleScrollbarAnimationControllerThinning {
 public:
  static std::unique_ptr<SingleScrollbarAnimationControllerThinning> Create(
      ElementId scroll_element_id,
      ScrollbarOrientation orientation,
      ScrollbarAnimationControllerClient* client,
      base::TimeDelta thinning_duration,
      float idle_thickness_scale);

  SingleScrollbarAnimationControllerThinning(
      const SingleScrollbarAnimationControllerThinning&) = delete;
  ~SingleScrollbarAnimationControllerThinning() = default;

  SingleScrollbarAnimationControllerThinning& operator=(
      const SingleScrollbarAnimationControllerThinning&) = delete;

  bool mouse_is_over_scrollbar_thumb() const {
    return mouse_is_over_scrollbar_thumb_;
  }
  bool mouse_is_near_scrollbar_thumb() const {
    return mouse_is_near_scrollbar_thumb_;
  }
  bool mouse_is_near_scrollbar() const { return mouse_is_near_scrollbar_; }

  bool captured() const { return captured_; }
  gfx::PointF device_viewport_last_pointer_location() const {
    return device_viewport_last_pointer_location_;
  }

  bool Animate(base::TimeTicks now);
  void StartAnimation();
  void StopAnimation();

  void DidScrollUpdate();
  void DidRequestShow();

  void DidMouseDown();
  void DidMouseUp();
  void DidMouseLeave();
  void DidMouseMove(const gfx::PointF& device_viewport_point);

  float MouseMoveDistanceToTriggerExpand();
  float MouseMoveDistanceToTriggerFadeIn();

  void UpdateTickmarksVisibility(bool show);

 private:
  SingleScrollbarAnimationControllerThinning(
      ElementId scroll_element_id,
      ScrollbarOrientation orientation,
      ScrollbarAnimationControllerClient* client,
      base::TimeDelta thinning_duration,
      float idle_thickness_scale);

  ScrollbarLayerImplBase* GetScrollbar() const;
  float AnimationProgressAtTime(base::TimeTicks now);
  void RunAnimationFrame(float progress);

  // Describes whether the current animation should kIncrease (thicken)
  // a bar or kDecrease it (thin).
  enum class AnimationChange { kNone, kIncrease, kDecrease };
  float ThumbThicknessScaleAt(float progress) const;
  float CurrentForcedThumbThicknessScale() const;
  void CalculateThicknessShouldChange(const gfx::PointF& device_viewport_point);

  float AdjustScale(float new_value,
                    float current_value,
                    AnimationChange animation_change,
                    float min_value,
                    float max_value);
  void UpdateThumbThicknessScale();
  void ApplyThumbThicknessScale(float thumb_thickness_scale);

  raw_ptr<ScrollbarAnimationControllerClient> client_;

  base::TimeTicks last_awaken_time_;
  bool is_animating_ = false;

  const ElementId scroll_element_id_;

  const ScrollbarOrientation orientation_;
  bool captured_ = false;
  bool mouse_is_over_scrollbar_thumb_ = false;
  bool mouse_is_near_scrollbar_thumb_ = false;
  // For Fluent scrollbars the near distance to the scrollbar is 0 which is
  // equivalent to the mouse being over the scrollbar.
  bool mouse_is_near_scrollbar_ = false;
  // Are we narrowing or thickening the bars.
  AnimationChange thickness_change_ = AnimationChange::kNone;

  const base::TimeDelta thinning_duration_;

  bool tickmarks_showing_ = false;
  // Save last known pointer location in the device viewport for use in
  // DidScrollUpdate() to check the pointers proximity to the thumb in case of a
  // scroll.
  gfx::PointF device_viewport_last_pointer_location_{-1, -1};
  const float idle_thickness_scale_;
};

}  // namespace cc

#endif  // CC_INPUT_SINGLE_SCROLLBAR_ANIMATION_CONTROLLER_THINNING_H_