summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/page/scrolling/overscroll_controller.cc
blob: 15aa9a9dbccea454b9542e54ee965dbe8d5f3afc (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
// Copyright 2016 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 "third_party/blink/renderer/core/page/scrolling/overscroll_controller.h"

#include "third_party/blink/renderer/core/frame/visual_viewport.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/scroll/scroll_types.h"
#include "third_party/blink/renderer/core/style/computed_style_base_constants.h"
#include "third_party/blink/renderer/platform/geometry/float_point.h"
#include "third_party/blink/renderer/platform/geometry/float_size.h"
#include "third_party/blink/renderer/platform/graphics/paint/scroll_paint_property_node.h"

namespace blink {

namespace {

// Report Overscroll if OverscrollDelta is greater than minimumOverscrollDelta
// to maintain consistency as done in the compositor.
const float kMinimumOverscrollDelta = 0.1;

void AdjustOverscroll(gfx::Vector2dF* unused_delta) {
  DCHECK(unused_delta);
  if (std::abs(unused_delta->x()) < kMinimumOverscrollDelta)
    unused_delta->set_x(0);
  if (std::abs(unused_delta->y()) < kMinimumOverscrollDelta)
    unused_delta->set_y(0);
}

}  // namespace

OverscrollController::OverscrollController(
    const VisualViewport& visual_viewport,
    ChromeClient& chrome_client)
    : visual_viewport_(&visual_viewport), chrome_client_(&chrome_client) {}

void OverscrollController::Trace(Visitor* visitor) const {
  visitor->Trace(visual_viewport_);
  visitor->Trace(chrome_client_);
}

void OverscrollController::ResetAccumulated(bool reset_x, bool reset_y) {
  if (reset_x)
    accumulated_root_overscroll_.set_x(0);
  if (reset_y)
    accumulated_root_overscroll_.set_y(0);
}

void OverscrollController::HandleOverscroll(
    const ScrollResult& scroll_result,
    const FloatPoint& position_in_root_frame,
    const FloatSize& velocity_in_root_frame) {
  DCHECK(visual_viewport_);
  DCHECK(chrome_client_);

  gfx::Vector2dF unused_delta(scroll_result.unused_scroll_delta_x,
                              scroll_result.unused_scroll_delta_y);
  AdjustOverscroll(&unused_delta);

  gfx::Vector2dF delta_in_viewport =
      gfx::ScaleVector2d(unused_delta, visual_viewport_->Scale());
  gfx::Vector2dF velocity_in_viewport = gfx::ScaleVector2d(
      gfx::Vector2dF(velocity_in_root_frame), visual_viewport_->Scale());
  gfx::PointF position_in_viewport =
      visual_viewport_->RootFrameToViewport(position_in_root_frame);

  ResetAccumulated(scroll_result.did_scroll_x, scroll_result.did_scroll_y);

  if (!delta_in_viewport.IsZero()) {
    accumulated_root_overscroll_ += delta_in_viewport;
    chrome_client_->DidOverscroll(delta_in_viewport,
                                  accumulated_root_overscroll_,
                                  position_in_viewport, velocity_in_viewport);
  }
}

}  // namespace blink