summaryrefslogtreecommitdiffstats
path: root/chromium/cc/animation/scrollbar_animation_controller_thinning.cc
blob: 3de75989d0e47771f64c59f5bf6d4ac1b7a52564 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/animation/scrollbar_animation_controller_thinning.h"

#include <algorithm>

#include "base/time/time.h"
#include "cc/layers/layer_impl.h"
#include "cc/layers/scrollbar_layer_impl_base.h"

namespace {
const float kIdleThicknessScale = 0.4f;
const float kIdleOpacity = 0.7f;
const float kDefaultMouseMoveDistanceToTriggerAnimation = 25.f;
const int kDefaultAnimationDelay = 500;
const int kDefaultAnimationDuration = 300;
}

namespace cc {

scoped_ptr<ScrollbarAnimationControllerThinning>
ScrollbarAnimationControllerThinning::Create(LayerImpl* scroll_layer) {
  return make_scoped_ptr(new ScrollbarAnimationControllerThinning(
      scroll_layer,
      base::TimeDelta::FromMilliseconds(kDefaultAnimationDelay),
      base::TimeDelta::FromMilliseconds(kDefaultAnimationDuration)));
}

scoped_ptr<ScrollbarAnimationControllerThinning>
ScrollbarAnimationControllerThinning::CreateForTest(LayerImpl* scroll_layer,
    base::TimeDelta animation_delay, base::TimeDelta animation_duration) {
  return make_scoped_ptr(new ScrollbarAnimationControllerThinning(
      scroll_layer, animation_delay, animation_duration));
}

ScrollbarAnimationControllerThinning::ScrollbarAnimationControllerThinning(
    LayerImpl* scroll_layer,
    base::TimeDelta animation_delay,
    base::TimeDelta animation_duration)
    : ScrollbarAnimationController(),
      scroll_layer_(scroll_layer),
      mouse_is_over_scrollbar_(false),
      mouse_is_near_scrollbar_(false),
      thickness_change_(NONE),
      opacity_change_(NONE),
      should_delay_animation_(false),
      animation_delay_(animation_delay),
      animation_duration_(animation_duration),
      mouse_move_distance_to_trigger_animation_(
          kDefaultMouseMoveDistanceToTriggerAnimation) {
  ApplyOpacityAndThumbThicknessScale(kIdleOpacity, kIdleThicknessScale);
}

ScrollbarAnimationControllerThinning::~ScrollbarAnimationControllerThinning() {
}

bool ScrollbarAnimationControllerThinning::IsAnimating() const {
  return !last_awaken_time_.is_null();
}

base::TimeDelta ScrollbarAnimationControllerThinning::DelayBeforeStart(
    base::TimeTicks now) const {
  if (!should_delay_animation_)
    return base::TimeDelta();
  if (now > last_awaken_time_ + animation_delay_)
    return base::TimeDelta();
  return animation_delay_ - (now - last_awaken_time_);
}

bool ScrollbarAnimationControllerThinning::Animate(base::TimeTicks now) {
  float progress = AnimationProgressAtTime(now);
  float opacity = OpacityAtAnimationProgress(progress);
  float thumb_thickness_scale = ThumbThicknessScaleAtAnimationProgress(
      progress);
  ApplyOpacityAndThumbThicknessScale(opacity, thumb_thickness_scale);
  if (progress == 1.f) {
    opacity_change_ = NONE;
    thickness_change_ = NONE;
    last_awaken_time_ = base::TimeTicks();
  }
  return IsAnimating() && DelayBeforeStart(now) == base::TimeDelta();
}

void ScrollbarAnimationControllerThinning::DidScrollGestureBegin() {
}

void ScrollbarAnimationControllerThinning::DidScrollGestureEnd(
    base::TimeTicks now) {
}

void ScrollbarAnimationControllerThinning::DidMouseMoveOffScrollbar(
    base::TimeTicks now) {
  mouse_is_over_scrollbar_ = false;
  mouse_is_near_scrollbar_ = false;
  last_awaken_time_ = now;
  should_delay_animation_ = false;
  opacity_change_ = DECREASE;
  thickness_change_ = DECREASE;
}

bool ScrollbarAnimationControllerThinning::DidScrollUpdate(
    base::TimeTicks now) {
  ApplyOpacityAndThumbThicknessScale(
    1, mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale);

  last_awaken_time_ = now;
  should_delay_animation_ = true;
  if (!mouse_is_over_scrollbar_)
    opacity_change_ = DECREASE;
  return true;
}

bool ScrollbarAnimationControllerThinning::DidMouseMoveNear(
    base::TimeTicks now, float distance) {
  bool mouse_is_over_scrollbar = distance == 0.0;
  bool mouse_is_near_scrollbar =
      distance < mouse_move_distance_to_trigger_animation_;

  if (mouse_is_over_scrollbar == mouse_is_over_scrollbar_ &&
      mouse_is_near_scrollbar == mouse_is_near_scrollbar_)
    return false;

  if (mouse_is_over_scrollbar_ != mouse_is_over_scrollbar) {
    mouse_is_over_scrollbar_ = mouse_is_over_scrollbar;
    opacity_change_ = mouse_is_over_scrollbar_ ? INCREASE : DECREASE;
  }

  if (mouse_is_near_scrollbar_ != mouse_is_near_scrollbar) {
    mouse_is_near_scrollbar_ = mouse_is_near_scrollbar;
    thickness_change_ = mouse_is_near_scrollbar_ ? INCREASE : DECREASE;
  }

  last_awaken_time_ = now;
  should_delay_animation_ = false;
  return true;
}

float ScrollbarAnimationControllerThinning::AnimationProgressAtTime(
    base::TimeTicks now) {
  if (last_awaken_time_.is_null())
    return 1;

  base::TimeDelta delta = now - last_awaken_time_;
  if (should_delay_animation_)
    delta -= animation_delay_;
  float progress = delta.InSecondsF() / animation_duration_.InSecondsF();
  return std::max(std::min(progress, 1.f), 0.f);
}

float ScrollbarAnimationControllerThinning::OpacityAtAnimationProgress(
    float progress) {
  if (opacity_change_ == NONE)
    return mouse_is_over_scrollbar_ ? 1.f : kIdleOpacity;
  float factor = opacity_change_ == INCREASE ? progress : (1.f - progress);
  float ret = ((1.f - kIdleOpacity) * factor) + kIdleOpacity;
  return ret;
}

float
ScrollbarAnimationControllerThinning::ThumbThicknessScaleAtAnimationProgress(
    float progress) {
  if (thickness_change_ == NONE)
    return mouse_is_near_scrollbar_ ? 1.f : kIdleThicknessScale;
  float factor = thickness_change_ == INCREASE ? progress : (1.f - progress);
  return ((1.f - kIdleThicknessScale) * factor) + kIdleThicknessScale;
}

float ScrollbarAnimationControllerThinning::AdjustScale(
    float new_value,
    float current_value,
    AnimationChange animation_change) {
  if (animation_change == INCREASE && current_value > new_value)
    return current_value;
  if (animation_change == DECREASE && current_value < new_value)
    return current_value;
  return new_value;
}

void ScrollbarAnimationControllerThinning::ApplyOpacityAndThumbThicknessScale(
    float opacity, float thumb_thickness_scale) {
  ScrollbarLayerImplBase* horizontal_scrollbar =
      scroll_layer_->horizontal_scrollbar_layer();
  if (horizontal_scrollbar) {
    horizontal_scrollbar->SetOpacity(
        AdjustScale(opacity, horizontal_scrollbar->opacity(), opacity_change_));
    horizontal_scrollbar->SetThumbThicknessScaleFactor(
        AdjustScale(
          thumb_thickness_scale,
          horizontal_scrollbar->thumb_thickness_scale_factor(),
          thickness_change_));
  }

  ScrollbarLayerImplBase* vertical_scrollbar =
      scroll_layer_->vertical_scrollbar_layer();
  if (vertical_scrollbar) {
    vertical_scrollbar->SetOpacity(
        AdjustScale(opacity, vertical_scrollbar->opacity(), opacity_change_));
    vertical_scrollbar->SetThumbThicknessScaleFactor(
        AdjustScale(
          thumb_thickness_scale,
          vertical_scrollbar->thumb_thickness_scale_factor(),
          thickness_change_));
  }
}

}  // namespace cc