File: ScrollLatchingController.cpp

package info (click to toggle)
webkit2gtk 2.42.2-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 362,452 kB
  • sloc: cpp: 2,881,971; javascript: 282,447; ansic: 134,088; python: 43,789; ruby: 18,308; perl: 15,872; asm: 14,389; xml: 4,395; yacc: 2,350; sh: 2,074; java: 1,734; lex: 1,323; makefile: 288; pascal: 60
file content (272 lines) | stat: -rw-r--r-- 10,263 bytes parent folder | download | duplicates (4)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2020-2022 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "ScrollLatchingController.h"

#if ENABLE(WHEEL_EVENT_LATCHING)

#include "Element.h"
#include "LocalFrame.h"
#include "LocalFrameView.h"
#include "Logging.h"
#include "PlatformWheelEvent.h"
#include "ScrollableArea.h"
#include <wtf/text/TextStream.h>

namespace WebCore {

// See also ScrollTreeLatchingController.cpp
static const Seconds resetLatchedStateTimeout { 100_ms };

ScrollLatchingController::ScrollLatchingController()
    : m_clearLatchingStateTimer(*this, &ScrollLatchingController::clearTimerFired)
{
}

ScrollLatchingController::~ScrollLatchingController() = default;

void ScrollLatchingController::clear()
{
    LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::clear()");
    m_cumulativeEventDelta = { };
    m_frameStateStack.clear();
}

// FIXME: This logic is different from ScrollingTreeLatchingController, which simply lets the latching state elapse after 100ms.
void ScrollLatchingController::clearOrScheduleClearIfNeeded(const PlatformWheelEvent& wheelEvent)
{
    if (wheelEvent.shouldResetLatching() || wheelEvent.isNonGestureEvent()) {
        clear();
        LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::clearOrScheduleClearingLatchedStateIfNeeded() - event" << wheelEvent << ", resetting latching");
        return;
    }

    if (m_clearLatchingStateTimer.isActive()) {
        // If another wheel event scrolling starts, stop the timer manually, and reset the latched state immediately.
        if (wheelEvent.isGestureStart()) {
            LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::clearOrScheduleClearingLatchedStateIfNeeded() - event" << wheelEvent << ", timer pending, another scroll starting");
            clear();
            m_clearLatchingStateTimer.stop();
        } else if (wheelEvent.isTransitioningToMomentumScroll()) {
            // Wheel events machinery is transitioning to momentum scrolling, so no need to reset latched state. Stop the timer.
            m_clearLatchingStateTimer.stop();
        }
        return;
    }

    if (wheelEvent.isEndOfNonMomentumScroll()) {
        LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::clearOrScheduleClearingLatchedStateIfNeeded() - event" << wheelEvent << ", scheduling clear timer");
        m_clearLatchingStateTimer.startOneShot(resetLatchedStateTimeout);
    }
}

void ScrollLatchingController::clearTimerFired()
{
    LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::clearTimerFired() - clearing state");
    clear();
}

void ScrollLatchingController::receivedWheelEvent(const PlatformWheelEvent& wheelEvent)
{
    clearOrScheduleClearIfNeeded(wheelEvent);

    if (wheelEvent.isGestureStart() || wheelEvent.isNonGestureEvent())
        m_cumulativeEventDelta = wheelEvent.delta();
    else
        m_cumulativeEventDelta += wheelEvent.delta();
}

bool ScrollLatchingController::latchingAllowsScrollingInFrame(const LocalFrame& frame, WeakPtr<ScrollableArea>& latchedScroller) const
{
    if (m_frameStateStack.isEmpty())
        return true;

    if (auto* frameState = stateForFrame(frame)) {
        latchedScroller = frameState->scrollableArea;
        return !!frameState->scrollableArea;
    }
    return false;
}

void ScrollLatchingController::updateAndFetchLatchingStateForFrame(LocalFrame& frame, const PlatformWheelEvent& wheelEvent, RefPtr<Element>& latchedElement, WeakPtr<ScrollableArea>& scrollableArea, bool& isOverWidget)
{
    if (wheelEvent.isGestureStart()) {
        // We can have existing state here because state is cleared on a timer.
        if (!hasStateForFrame(frame)) {
            FrameState state;
            state.frame = &frame;
            state.wheelEventElement = latchedElement;
            if (shouldLatchToScrollableArea(frame, scrollableArea.get(), m_cumulativeEventDelta))
                state.scrollableArea = scrollableArea;
            state.isOverWidget = isOverWidget;

            LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::updateAndFetchLatchingStateForFrame() - pushing state for Frame " << &frame << " element " << state.wheelEventElement.get() << " scrollableArea " << state.scrollableArea);
            m_frameStateStack.append(WTFMove(state));
            return;
        }
    }

    if (wheelEvent.isGestureContinuation()) {
        auto* state = stateForFrame(frame);
        if (!state)
            return;

        // We may not have latched at gesture start because of small deltas. Re-evaluate latching based on accumulated delta.
        if (!state->scrollableArea && shouldLatchToScrollableArea(frame, scrollableArea.get(), m_cumulativeEventDelta))
            state->scrollableArea = scrollableArea;
    }

    if (!wheelEvent.useLatchedEventElement())
        return;

    for (const auto& state : m_frameStateStack) {
        if (state.frame == &frame) {
            latchedElement = state.wheelEventElement.get();
            scrollableArea = state.scrollableArea;
            isOverWidget = state.isOverWidget;

            LOG_WITH_STREAM(ScrollLatching, stream << "ScrollLatchingController::updateAndFetchLatchingStateForFrame() - using state for Frame " << &frame << " element " << latchedElement.get() << " scrollableArea " << scrollableArea);
        }
    }
}

void ScrollLatchingController::removeLatchingStateForTarget(const Element& element)
{
    if (m_frameStateStack.isEmpty())
        return;

    auto findResult = m_frameStateStack.findIf([&element] (const auto& state) {
        auto* wheelElement = state.wheelEventElement.get();
        return wheelElement && element.isEqualNode(wheelElement);
    });
    
    // If this element is found in the latching stack, just clear the whole stack. We can't just remove one entry,
    // since the stack has to match the frame hierarchy.
    if (findResult != notFound)
        m_frameStateStack.clear();
}

void ScrollLatchingController::removeLatchingStateForFrame(const LocalFrame& frame)
{
    if (m_frameStateStack.isEmpty())
        return;

    // If the frame was in the latching stack, just clear state.
    if (auto* frameState = stateForFrame(frame))
        clear();
}

static bool deltaIsPredominantlyVertical(FloatSize delta)
{
    return std::abs(delta.height()) > std::abs(delta.width());
}

bool ScrollLatchingController::shouldLatchToScrollableArea(const LocalFrame& frame, ScrollableArea* scrollableArea, FloatSize scrollDelta) const
{
    if (!scrollableArea)
        return false;

    // We always allow the main frame to receive wheel events to permit rubber-banding.
    if (frame.isMainFrame() && scrollableArea == frame.view())
        return true;

    if (!scrollableArea->canHaveScrollbars())
        return false;

    if (scrollDelta.isZero())
        return false;

    if (!deltaIsPredominantlyVertical(scrollDelta) && scrollDelta.width()) {
        if (!scrollableArea->horizontalScrollbar())
            return false;

        if (scrollDelta.width() < 0)
            return !scrollableArea->scrolledToRight();
        
        return !scrollableArea->scrolledToLeft();
    }

    if (!scrollableArea->verticalScrollbar())
        return false;

    if (scrollDelta.height() < 0)
        return !scrollableArea->scrolledToBottom();

    return !scrollableArea->scrolledToTop();
}

bool ScrollLatchingController::hasStateForFrame(const LocalFrame& frame) const
{
    for (const auto& state : m_frameStateStack) {
        if (state.frame == &frame)
            return true;
    }
    return false;
}

ScrollLatchingController::FrameState* ScrollLatchingController::stateForFrame(const LocalFrame& frame)
{
    for (auto& state : m_frameStateStack) {
        if (state.frame == &frame)
            return &state;
    }
    return nullptr;
}

const ScrollLatchingController::FrameState* ScrollLatchingController::stateForFrame(const LocalFrame& frame) const
{
    for (const auto& state : m_frameStateStack) {
        if (state.frame == &frame)
            return &state;
    }
    return nullptr;
}

void ScrollLatchingController::dump(WTF::TextStream& ts) const
{
    TextStream multilineStream;
    multilineStream.setIndent(ts.indent() + 2);

    for (const auto& state : m_frameStateStack) {
        TextStream::GroupScope groupScope(multilineStream);
        multilineStream.dumpProperty("frame", ValueOrNull(state.frame));
        multilineStream.dumpProperty("element", ValueOrNull(state.wheelEventElement.get()));
        multilineStream.dumpProperty("scrollable area", ValueOrNull(state.scrollableArea.get()));
        multilineStream.dumpProperty("is over widget", state.isOverWidget);
    }

    ts << "ScrollLatchingController state " << multilineStream.release();
}

TextStream& operator<<(TextStream& ts, const ScrollLatchingController& controller)
{
    controller.dump(ts);
    return ts;
}

} // namespace WebCore

#endif // ENABLE(WHEEL_EVENT_LATCHING)