summaryrefslogtreecommitdiffstats
path: root/cc/top_controls_manager.cc
blob: eb4b6b255592e999ab3e38c4a056143cb2dde0ad (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
// 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/top_controls_manager.h"

#include <algorithm>

#include "base/logging.h"
#include "base/time.h"
#include "cc/keyframed_animation_curve.h"
#include "cc/layer_tree_impl.h"
#include "cc/timing_function.h"
#include "cc/top_controls_manager_client.h"
#include "ui/gfx/transform.h"
#include "ui/gfx/vector2d_f.h"

namespace cc {
namespace {
// These constants were chosen empirically for their visually pleasant behavior.
// Contact tedchoc@chromium.org for questions about changing these values.
const float kShowHideThreshold = 0.5f;
const int64 kShowHideMaxDurationMs = 175;
}

// static
scoped_ptr<TopControlsManager> TopControlsManager::Create(
    TopControlsManagerClient* client, float top_controls_height) {
  return make_scoped_ptr(new TopControlsManager(client, top_controls_height));
}

TopControlsManager::TopControlsManager(TopControlsManagerClient* client,
                                       float top_controls_height)
    : client_(client),
      animation_direction_(NO_ANIMATION),
      in_scroll_gesture_(false),
      top_controls_height_(top_controls_height),
      controls_top_offset_(0),
      content_top_offset_(top_controls_height),
      previous_root_scroll_offset_(0.f),
      scroll_start_offset_(0.f) {
  CHECK(client_);
}

TopControlsManager::~TopControlsManager() {
}

void TopControlsManager::ScrollBegin() {
  ResetAnimations();
  in_scroll_gesture_ = true;
  scroll_start_offset_ = RootScrollLayerTotalScrollY() + controls_top_offset_;
}

gfx::Vector2dF TopControlsManager::ScrollBy(
    const gfx::Vector2dF pending_delta) {
  if (pending_delta.y() == 0)
    return pending_delta;

  float scroll_total_y = RootScrollLayerTotalScrollY();

  if (in_scroll_gesture_ && pending_delta.y() > 0 && controls_top_offset_ == 0)
    scroll_start_offset_ = scroll_total_y;
  else if (in_scroll_gesture_ &&
      ((pending_delta.y() > 0 && scroll_total_y < scroll_start_offset_) ||
       (pending_delta.y() < 0 &&
           scroll_total_y > scroll_start_offset_ + top_controls_height_))) {
    return pending_delta;
  }

  ResetAnimations();
  return ScrollInternal(pending_delta);
}

gfx::Vector2dF TopControlsManager::ScrollInternal(
    const gfx::Vector2dF pending_delta) {
  float scroll_total_y = RootScrollLayerTotalScrollY();
  float scroll_delta_y = pending_delta.y();

  float previous_controls_offset = controls_top_offset_;
  float previous_content_offset = content_top_offset_;

  controls_top_offset_ -= scroll_delta_y;
  controls_top_offset_ = std::min(
      std::max(controls_top_offset_, -top_controls_height_), 0.f);

  // To determine if the scroll delta should be applied to the content position
  // as well, we check the following:
  // 1.) Scrolling down the page and the content position isn't already at 0.
  //     This case handles corrections where the page content shifts outside of
  //     the knowledge of the top controls manager.
  // 2.) Scrolling either direction while the root scroll layer is scrolled to
  //     the very top.
  if ((scroll_delta_y > 0 && content_top_offset_ > 0) || scroll_total_y <= 0) {
    float content_scroll_delta_y = scroll_delta_y;
    if (content_scroll_delta_y > 0)
      content_scroll_delta_y -= previous_controls_offset - controls_top_offset_;
    content_top_offset_ -= content_scroll_delta_y;
  }

  content_top_offset_ = std::max(
      std::min(content_top_offset_,
               controls_top_offset_ + top_controls_height_), 0.f);

  gfx::Vector2dF applied_delta(
      0.f, previous_content_offset - content_top_offset_);

  if (previous_controls_offset != controls_top_offset_ ||
      previous_content_offset != content_top_offset_) {
    client_->setNeedsRedraw();
    client_->setActiveTreeNeedsUpdateDrawProperties();
  }

  return pending_delta - applied_delta;
}

void TopControlsManager::ScrollEnd() {
  StartAnimationIfNecessary();
  previous_root_scroll_offset_ = RootScrollLayerTotalScrollY();
  in_scroll_gesture_ = false;
}

void TopControlsManager::Animate(base::TimeTicks monotonic_time) {
  if (!top_controls_animation_ || !client_->haveRootScrollLayer())
    return;

  double time = (monotonic_time - base::TimeTicks()).InMillisecondsF();
  float new_offset = top_controls_animation_->getValue(time);
  gfx::Vector2dF scroll_vector(0.f, -(new_offset - controls_top_offset_));
  ScrollInternal(scroll_vector);
  client_->setNeedsRedraw();

  if (IsAnimationCompleteAtTime(monotonic_time))
    ResetAnimations();
}

void TopControlsManager::ResetAnimations() {
  if (top_controls_animation_)
    top_controls_animation_.reset();

  animation_direction_ = NO_ANIMATION;
}

float TopControlsManager::RootScrollLayerTotalScrollY() {
  return client_->rootScrollLayerTotalScrollY();
}

void TopControlsManager::SetupAnimation(AnimationDirection direction) {
  top_controls_animation_ = KeyframedFloatAnimationCurve::create();
  double start_time =
      (base::TimeTicks::Now() - base::TimeTicks()).InMillisecondsF();
  top_controls_animation_->addKeyframe(
      FloatKeyframe::create(start_time, controls_top_offset_,
                            scoped_ptr<TimingFunction>()));
  float max_ending_offset =
      (direction == SHOWING_CONTROLS ? 1 : -1) * top_controls_height_;
  top_controls_animation_->addKeyframe(
      FloatKeyframe::create(start_time + kShowHideMaxDurationMs,
                            controls_top_offset_ + max_ending_offset,
                            EaseTimingFunction::create()));
  animation_direction_ = direction;
}

void TopControlsManager::StartAnimationIfNecessary() {
  float scroll_total_y = RootScrollLayerTotalScrollY();

  if (controls_top_offset_ != 0
      && controls_top_offset_ != -top_controls_height_) {
    AnimationDirection show_controls =
        controls_top_offset_ >= -(top_controls_height_ * kShowHideThreshold) ?
            SHOWING_CONTROLS : HIDING_CONTROLS;
    if (!top_controls_animation_ || animation_direction_ != show_controls) {
      SetupAnimation(show_controls);
      client_->setNeedsRedraw();
    }
  }
}

bool TopControlsManager::IsAnimationCompleteAtTime(base::TimeTicks time) {
  if (!top_controls_animation_)
    return true;

  double time_ms = (time - base::TimeTicks()).InMillisecondsF();
  float new_offset = top_controls_animation_->getValue(time_ms);

  if ((animation_direction_ == SHOWING_CONTROLS && new_offset >= 0) ||
      (animation_direction_ == HIDING_CONTROLS
          && new_offset <= -top_controls_height_)) {
    return true;
  }
  return false;
}

}  // namespace cc