File: ScrollState.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (112 lines) | stat: -rw-r--r-- 3,817 bytes parent folder | download
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
// 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 "core/page/scrolling/ScrollState.h"

#include "core/dom/DOMNodeIds.h"
#include "core/dom/Element.h"
#include "core/dom/ExceptionCode.h"
#include "platform/graphics/CompositorElementId.h"
#include "wtf/PtrUtil.h"
#include <memory>

namespace blink {

namespace {
Element* elementForId(int elementId) {
  Node* node = DOMNodeIds::nodeForId(elementId);
  ASSERT(node);
  if (!node)
    return nullptr;
  ASSERT(node->isElementNode());
  if (!node->isElementNode())
    return nullptr;
  return static_cast<Element*>(node);
}
}  // namespace

ScrollState* ScrollState::create(ScrollStateInit init) {
  std::unique_ptr<ScrollStateData> scrollStateData =
      WTF::makeUnique<ScrollStateData>();
  scrollStateData->delta_x = init.deltaX();
  scrollStateData->delta_y = init.deltaY();
  scrollStateData->position_x = init.positionX();
  scrollStateData->position_y = init.positionY();
  scrollStateData->velocity_x = init.velocityX();
  scrollStateData->velocity_y = init.velocityY();
  scrollStateData->is_beginning = init.isBeginning();
  scrollStateData->is_in_inertial_phase = init.isInInertialPhase();
  scrollStateData->is_ending = init.isEnding();
  scrollStateData->should_propagate = init.shouldPropagate();
  scrollStateData->from_user_input = init.fromUserInput();
  scrollStateData->is_direct_manipulation = init.isDirectManipulation();
  scrollStateData->delta_granularity = init.deltaGranularity();
  ScrollState* scrollState = new ScrollState(std::move(scrollStateData));
  return scrollState;
}

ScrollState* ScrollState::create(std::unique_ptr<ScrollStateData> data) {
  ScrollState* scrollState = new ScrollState(std::move(data));
  return scrollState;
}

ScrollState::ScrollState(std::unique_ptr<ScrollStateData> data)
    : m_data(std::move(data)) {}

void ScrollState::consumeDelta(double x,
                               double y,
                               ExceptionState& exceptionState) {
  if ((m_data->delta_x > 0 && 0 > x) || (m_data->delta_x < 0 && 0 < x) ||
      (m_data->delta_y > 0 && 0 > y) || (m_data->delta_y < 0 && 0 < y)) {
    exceptionState.throwDOMException(InvalidModificationError,
                                     "Can't increase delta using consumeDelta");
    return;
  }
  if (fabs(x) > fabs(m_data->delta_x) || fabs(y) > fabs(m_data->delta_y)) {
    exceptionState.throwDOMException(
        InvalidModificationError,
        "Can't change direction of delta using consumeDelta");
    return;
  }
  consumeDeltaNative(x, y);
}

void ScrollState::distributeToScrollChainDescendant() {
  if (!m_scrollChain.empty()) {
    int descendantId = m_scrollChain.front();
    m_scrollChain.pop_front();
    elementForId(descendantId)->callDistributeScroll(*this);
  }
}

void ScrollState::consumeDeltaNative(double x, double y) {
  m_data->delta_x -= x;
  m_data->delta_y -= y;

  if (x)
    m_data->caused_scroll_x = true;
  if (y)
    m_data->caused_scroll_y = true;
  if (x || y)
    m_data->delta_consumed_for_scroll_sequence = true;
}

Element* ScrollState::currentNativeScrollingElement() const {
  uint64_t elementId = m_data->current_native_scrolling_element().primaryId;
  if (elementId == 0)
    return nullptr;
  return elementForId(elementId);
}

void ScrollState::setCurrentNativeScrollingElement(Element* element) {
  m_data->set_current_native_scrolling_element(createCompositorElementId(
      DOMNodeIds::idForNode(element), CompositorSubElementId::Scroll));
}

void ScrollState::setCurrentNativeScrollingElementById(int elementId) {
  m_data->set_current_native_scrolling_element(
      createCompositorElementId(elementId, CompositorSubElementId::Scroll));
}

}  // namespace blink