summaryrefslogtreecommitdiffstats
path: root/cc/layers/viewport.cc
blob: 35db561e28a2ec442417fa85f0508c3726a54bac (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
// Copyright 2015 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/layers/viewport.h"

#include "base/logging.h"
#include "cc/input/top_controls_manager.h"
#include "cc/trees/layer_tree_host_impl.h"
#include "cc/trees/layer_tree_impl.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace cc {

// static
scoped_ptr<Viewport> Viewport::Create(
    LayerTreeHostImpl* host_impl) {
  return make_scoped_ptr(new Viewport(host_impl));
}

Viewport::Viewport(LayerTreeHostImpl* host_impl)
    : host_impl_(host_impl) {
  DCHECK(host_impl_);
}

void Viewport::Pan(const gfx::Vector2dF& delta) {
  gfx::Vector2dF pending_delta = delta;

  pending_delta -= host_impl_->ScrollLayer(InnerScrollLayer(),
                                           pending_delta,
                                           gfx::Point(),
                                           false);
}

Viewport::ScrollResult Viewport::ScrollBy(const gfx::Vector2dF& delta,
                                          const gfx::Point& viewport_point,
                                          bool is_wheel_scroll,
                                          bool affect_top_controls) {
  gfx::Vector2dF content_delta = delta;
  ScrollResult result;

  if (affect_top_controls && ShouldTopControlsConsumeScroll(delta)) {
    result.top_controls_applied_delta = ScrollTopControls(delta);
    content_delta -= result.top_controls_applied_delta;
  }

  gfx::Vector2dF pending_content_delta = content_delta;

  if (OuterScrollLayer()) {
    pending_content_delta -= host_impl_->ScrollLayer(OuterScrollLayer(),
                                                     pending_content_delta,
                                                     viewport_point,
                                                     is_wheel_scroll);
  }

  // TODO(bokan): This shouldn't be needed but removing it causes subtle
  // viewport movement during top controls manipulation.
  if (!gfx::ToRoundedVector2d(pending_content_delta).IsZero()) {
    pending_content_delta -= host_impl_->ScrollLayer(InnerScrollLayer(),
                                                     pending_content_delta,
                                                     viewport_point,
                                                     is_wheel_scroll);
    result.unused_scroll_delta = AdjustOverscroll(pending_content_delta);
  }


  result.applied_delta = content_delta - pending_content_delta;
  return result;
}

gfx::Vector2dF Viewport::ScrollTopControls(const gfx::Vector2dF& delta) {
  gfx::Vector2dF excess_delta =
      host_impl_->top_controls_manager()->ScrollBy(delta);

  return delta - excess_delta;
}

bool Viewport::ShouldTopControlsConsumeScroll(
    const gfx::Vector2dF& scroll_delta) const {
  // Always consume if it's in the direction to show the top controls.
  if (scroll_delta.y() < 0)
    return true;

  if (TotalScrollOffset().y() < MaxTotalScrollOffset().y())
    return true;

  return false;
}

gfx::Vector2dF Viewport::AdjustOverscroll(const gfx::Vector2dF& delta) const {
  const float kEpsilon = 0.1f;
  gfx::Vector2dF adjusted = delta;

  if (std::abs(adjusted.x()) < kEpsilon)
    adjusted.set_x(0.0f);
  if (std::abs(adjusted.y()) < kEpsilon)
    adjusted.set_y(0.0f);

  // Disable overscroll on axes which are impossible to scroll.
  if (host_impl_->settings().report_overscroll_only_for_scrollable_axes) {
    if (std::abs(MaxTotalScrollOffset().x()) <= kEpsilon ||
        !InnerScrollLayer()->user_scrollable_horizontal())
      adjusted.set_x(0.0f);
    if (std::abs(MaxTotalScrollOffset().y()) <= kEpsilon ||
        !InnerScrollLayer()->user_scrollable_vertical())
      adjusted.set_y(0.0f);
  }

  return adjusted;
}

gfx::ScrollOffset Viewport::MaxTotalScrollOffset() const {
  gfx::ScrollOffset offset;

  offset += InnerScrollLayer()->MaxScrollOffset();

  if (OuterScrollLayer())
    offset += OuterScrollLayer()->MaxScrollOffset();

  return offset;
}

gfx::ScrollOffset Viewport::TotalScrollOffset() const {
  gfx::ScrollOffset offset;

  offset += InnerScrollLayer()->CurrentScrollOffset();

  if (OuterScrollLayer())
    offset += OuterScrollLayer()->CurrentScrollOffset();

  return offset;
}

LayerImpl* Viewport::InnerScrollLayer() const {
  return host_impl_->InnerViewportScrollLayer();
}

LayerImpl* Viewport::OuterScrollLayer() const {
  return host_impl_->OuterViewportScrollLayer();
}

}  // namespace cc