4#include <QElapsedTimer>
8#include <QQmlListProperty>
12#include "qml_material/scrollable/scroll_motion.hpp"
13#include "qml_material/export.hpp"
20class QML_MATERIAL_API FlickableVisibleArea :
public QObject {
24 Q_PROPERTY(qreal xPosition READ xPosition NOTIFY xPositionChanged FINAL)
25 Q_PROPERTY(qreal yPosition READ yPosition NOTIFY yPositionChanged FINAL)
26 Q_PROPERTY(qreal widthRatio READ widthRatio NOTIFY widthRatioChanged FINAL)
27 Q_PROPERTY(qreal heightRatio READ heightRatio NOTIFY heightRatioChanged FINAL)
30 explicit FlickableVisibleArea(Flickable* parent =
nullptr);
32 qreal xPosition()
const;
33 qreal yPosition()
const;
34 qreal widthRatio()
const;
35 qreal heightRatio()
const;
37 auto updateVisible() -> void;
39 Q_SIGNAL
void xPositionChanged(qreal value);
40 Q_SIGNAL
void yPositionChanged(qreal value);
41 Q_SIGNAL
void widthRatioChanged(qreal value);
42 Q_SIGNAL
void heightRatioChanged(qreal value);
45 QPointer<Flickable> m_flickable;
46 qreal m_xPosition { 0 };
47 qreal m_yPosition { 0 };
48 qreal m_widthRatio { 1 };
49 qreal m_heightRatio { 1 };
52class QML_MATERIAL_API Flickable :
public QQuickItem {
54 QML_NAMED_ELEMENT(Scrollable)
57 qreal contentWidth READ contentWidth WRITE setContentWidth NOTIFY contentWidthChanged)
59 qreal contentHeight READ contentHeight WRITE setContentHeight NOTIFY contentHeightChanged)
60 Q_PROPERTY(qreal contentX READ contentX WRITE setContentX NOTIFY contentXChanged)
61 Q_PROPERTY(qreal contentY READ contentY WRITE setContentY NOTIFY contentYChanged)
62 Q_PROPERTY(QQuickItem* contentItem READ contentItem CONSTANT)
64 Q_PROPERTY(qreal topMargin READ topMargin WRITE setTopMargin NOTIFY topMarginChanged)
66 qreal bottomMargin READ bottomMargin WRITE setBottomMargin NOTIFY bottomMarginChanged)
67 Q_PROPERTY(qreal originY READ originY NOTIFY originYChanged)
68 Q_PROPERTY(qreal leftMargin READ leftMargin WRITE setLeftMargin NOTIFY leftMarginChanged)
69 Q_PROPERTY(qreal rightMargin READ rightMargin WRITE setRightMargin NOTIFY rightMarginChanged)
70 Q_PROPERTY(qreal originX READ originX NOTIFY originXChanged)
72 Q_PROPERTY(qreal horizontalVelocity READ horizontalVelocity NOTIFY horizontalVelocityChanged)
73 Q_PROPERTY(qreal verticalVelocity READ verticalVelocity NOTIFY verticalVelocityChanged)
74 Q_PROPERTY(QPointF dragVelocity READ dragVelocity NOTIFY dragVelocityChanged)
75 Q_PROPERTY(qreal maximumFlickVelocity READ maximumFlickVelocity WRITE setMaximumFlickVelocity
76 NOTIFY maximumFlickVelocityChanged)
77 Q_PROPERTY(qreal flickDeceleration READ flickDeceleration WRITE setFlickDeceleration NOTIFY
78 flickDecelerationChanged)
79 Q_PROPERTY(
bool moving READ isMoving NOTIFY movingChanged)
80 Q_PROPERTY(
bool movingHorizontally READ isMovingHorizontally NOTIFY movingHorizontallyChanged)
81 Q_PROPERTY(
bool movingVertically READ isMovingVertically NOTIFY movingVerticallyChanged)
82 Q_PROPERTY(
bool flicking READ isFlicking NOTIFY flickingChanged)
84 bool flickingHorizontally READ isFlickingHorizontally NOTIFY flickingHorizontallyChanged)
85 Q_PROPERTY(
bool flickingVertically READ isFlickingVertically NOTIFY flickingVerticallyChanged)
86 Q_PROPERTY(
bool dragging READ isDragging NOTIFY draggingChanged)
88 bool draggingHorizontally READ isDraggingHorizontally NOTIFY draggingHorizontallyChanged)
89 Q_PROPERTY(
bool draggingVertically READ isDraggingVertically NOTIFY draggingVerticallyChanged)
90 Q_PROPERTY(FlickableDirection flickableDirection READ flickableDirection WRITE
91 setFlickableDirection NOTIFY flickableDirectionChanged)
93 Q_PROPERTY(
bool interactive READ isInteractive WRITE setInteractive NOTIFY interactiveChanged)
94 Q_PROPERTY(
bool atXEnd READ isAtXEnd NOTIFY atXEndChanged)
95 Q_PROPERTY(
bool atYEnd READ isAtYEnd NOTIFY atYEndChanged)
96 Q_PROPERTY(
bool atXBeginning READ isAtXBeginning NOTIFY atXBeginningChanged)
97 Q_PROPERTY(
bool atYBeginning READ isAtYBeginning NOTIFY atYBeginningChanged)
98 Q_PROPERTY(FlickableVisibleArea* visibleArea READ visibleArea CONSTANT)
99 Q_PROPERTY(
bool pixelAligned READ pixelAligned WRITE setPixelAligned NOTIFY pixelAlignedChanged)
100 Q_PROPERTY(
bool synchronousDrag READ synchronousDrag WRITE setSynchronousDrag NOTIFY
101 synchronousDragChanged)
102 Q_PROPERTY(Qt::MouseButtons acceptedButtons READ acceptedButtons WRITE setAcceptedButtons NOTIFY
103 acceptedButtonsChanged FINAL)
104 Q_PROPERTY(InputMaskMode inputMaskMode READ inputMaskMode WRITE setInputMaskMode NOTIFY
105 inputMaskModeChanged FINAL)
106 Q_PROPERTY(QQuickItem* interactionItem READ interactionItem WRITE setInteractionItem RESET
107 resetInteractionItem NOTIFY interactionItemChanged FINAL)
108 Q_PROPERTY(QQmlListProperty<QObject> flickableData READ flickableData)
109 Q_PROPERTY(QQmlListProperty<QQuickItem> flickableChildren READ flickableChildren)
110 Q_CLASSINFO(
"DefaultProperty",
"flickableData")
113 explicit Flickable(QQuickItem* parent =
nullptr);
114 ~Flickable() override;
116 QQmlListProperty<QObject> flickableData();
117 QQmlListProperty<QQuickItem> flickableChildren();
119 enum FlickableDirection
121 AutoFlickDirection = 0x0,
122 HorizontalFlick = 0x1,
124 HorizontalAndVerticalFlick = 0x3,
125 AutoFlickIfNeeded = 0xc,
127 Q_ENUM(FlickableDirection)
135 Q_ENUM(InputMaskMode)
137 qreal contentWidth()
const;
138 void setContentWidth(qreal value);
140 qreal contentHeight()
const;
141 void setContentHeight(qreal value);
143 qreal contentX()
const;
144 virtual void setContentX(qreal position);
146 qreal contentY()
const;
147 virtual void setContentY(qreal position);
149 qreal topMargin()
const;
150 void setTopMargin(qreal value);
152 qreal bottomMargin()
const;
153 void setBottomMargin(qreal value);
155 qreal leftMargin()
const;
156 void setLeftMargin(qreal value);
158 qreal rightMargin()
const;
159 void setRightMargin(qreal value);
161 virtual qreal originY()
const;
162 virtual qreal originX()
const;
164 bool isMoving()
const;
165 bool isMovingHorizontally()
const;
166 bool isMovingVertically()
const;
167 bool isFlicking()
const;
168 bool isFlickingHorizontally()
const;
169 bool isFlickingVertically()
const;
170 bool isDragging()
const;
171 bool isDraggingHorizontally()
const;
172 bool isDraggingVertically()
const;
174 qreal maximumFlickVelocity()
const;
175 void setMaximumFlickVelocity(qreal value);
177 qreal flickDeceleration()
const;
178 void setFlickDeceleration(qreal value);
180 bool isInteractive()
const;
181 void setInteractive(
bool value);
183 qreal horizontalVelocity()
const;
184 qreal verticalVelocity()
const;
186 QPointF dragVelocity()
const {
return m_dragVelocity; }
188 bool isAtXEnd()
const;
189 bool isAtXBeginning()
const;
190 bool isAtYEnd()
const;
191 bool isAtYBeginning()
const;
193 QQuickItem* contentItem()
const;
195 FlickableDirection flickableDirection()
const;
196 void setFlickableDirection(FlickableDirection direction);
198 bool pixelAligned()
const;
199 void setPixelAligned(
bool align);
201 bool synchronousDrag()
const;
202 void setSynchronousDrag(
bool value);
204 Qt::MouseButtons acceptedButtons()
const;
205 void setAcceptedButtons(Qt::MouseButtons buttons);
207 InputMaskMode inputMaskMode()
const;
208 void setInputMaskMode(InputMaskMode mode);
210 QQuickItem* interactionItem()
const;
211 void setInteractionItem(QQuickItem* item);
212 void resetInteractionItem();
214 FlickableVisibleArea* visibleArea();
216 Q_INVOKABLE
void resizeContent(qreal width, qreal height, QPointF center);
217 Q_INVOKABLE
void flick(qreal xVelocity, qreal yVelocity);
218 Q_INVOKABLE
void cancelFlick();
220 enum class ScrollSource
231 Qt::Orientations scrollAxes()
const;
232 QPointF boundedScrollPosition(QPointF position)
const;
233 bool acceptsScrollPoint(QPointF position)
const;
234 void beginScroll(ScrollSource source);
235 void cancelScrollActivity();
237 void releaseScroll(QPointF velocity);
238 void updateScrollActivity(ScrollSource source, Qt::Orientations axes, QPointF velocity = {});
239 void endScroll(ScrollEnd reason);
241 enum class ScrollInput
246 struct ScrollConsumption {
251 ScrollConsumption applyScrollDelta(QPointF delta, ScrollSource source, QPointF velocity = {});
253 ScrollConsumption consumeScroll(QPointF delta, ScrollInput input,
254 Qt::ScrollPhase phase = Qt::NoScrollPhase);
256 Q_SIGNAL
void scrollInterrupted();
257 Q_SIGNAL
void scrollGeometryAdjusted(QPointF delta);
258 Q_SIGNAL
void contentWidthChanged();
259 Q_SIGNAL
void contentHeightChanged();
260 Q_SIGNAL
void contentXChanged();
261 Q_SIGNAL
void contentYChanged();
262 Q_SIGNAL
void topMarginChanged();
263 Q_SIGNAL
void bottomMarginChanged();
264 Q_SIGNAL
void leftMarginChanged();
265 Q_SIGNAL
void rightMarginChanged();
266 Q_SIGNAL
void originYChanged();
267 Q_SIGNAL
void originXChanged();
268 Q_SIGNAL
void movingChanged();
269 Q_SIGNAL
void movingHorizontallyChanged();
270 Q_SIGNAL
void movingVerticallyChanged();
271 Q_SIGNAL
void flickingChanged();
272 Q_SIGNAL
void flickingHorizontallyChanged();
273 Q_SIGNAL
void flickingVerticallyChanged();
274 Q_SIGNAL
void draggingChanged();
275 Q_SIGNAL
void draggingHorizontallyChanged();
276 Q_SIGNAL
void draggingVerticallyChanged();
277 Q_SIGNAL
void horizontalVelocityChanged();
278 Q_SIGNAL
void verticalVelocityChanged();
279 Q_SIGNAL
void isAtBoundaryChanged();
280 Q_SIGNAL
void flickableDirectionChanged();
281 Q_SIGNAL
void interactiveChanged();
282 Q_SIGNAL
void maximumFlickVelocityChanged();
283 Q_SIGNAL
void flickDecelerationChanged();
284 Q_SIGNAL
void movementStarted();
285 Q_SIGNAL
void movementEnded();
286 Q_SIGNAL
void flickStarted();
287 Q_SIGNAL
void flickEnded();
288 Q_SIGNAL
void dragStarted();
289 Q_SIGNAL
void dragEnded();
290 Q_SIGNAL
void dragVelocityChanged();
291 Q_SIGNAL
void dragReleased(QPointF velocity);
292 Q_SIGNAL
void pixelAlignedChanged();
293 Q_SIGNAL
void synchronousDragChanged();
294 Q_SIGNAL
void atXEndChanged();
295 Q_SIGNAL
void atYEndChanged();
296 Q_SIGNAL
void atXBeginningChanged();
297 Q_SIGNAL
void atYBeginningChanged();
298 Q_SIGNAL
void acceptedButtonsChanged();
299 Q_SIGNAL
void inputMaskModeChanged();
300 Q_SIGNAL
void interactionItemChanged();
304 void updateContentGeometry(QSizeF extent, QPointF anchorDelta = {});
305 virtual void scrollInputStarted();
306 virtual void scrollActivityCancelled();
307 void setVerticalScrollTarget(qreal position);
308 void setHorizontalScrollTarget(qreal position);
309 auto timerEvent(QTimerEvent* event) ->
void override;
310 auto updatePolish() ->
void override;
311 auto geometryChange(
const QRectF& newGeometry,
const QRectF& oldGeometry) ->
void override;
312 auto componentComplete() ->
void override;
315 virtual auto minXExtent() const -> qreal;
316 virtual auto minYExtent() const -> qreal;
317 virtual auto maxXExtent() const -> qreal;
318 virtual auto maxYExtent() const -> qreal;
319 auto vWidth() const -> qreal;
320 auto vHeight() const -> qreal;
321 virtual auto viewportMoved(Qt::Orientations orientation) ->
void;
322 auto xflick() const ->
bool;
323 auto yflick() const ->
bool;
326 friend class FlickableVisibleArea;
335 qreal position { 0 };
336 qreal viewSize { -1 };
337 qreal startMargin { 0 };
338 qreal endMargin { 0 };
339 qreal smoothVelocity { 0 };
341 bool atEnd {
false };
342 bool atBeginning {
true };
343 bool moving {
false };
344 bool flicking {
false };
345 bool dragging {
false };
346 bool platformScrolling {
false };
349 static auto dataAppend(QQmlListProperty<QObject>* property, QObject*
object) -> void;
350 static auto dataCount(QQmlListProperty<QObject>* property) -> qsizetype;
351 static auto dataAt(QQmlListProperty<QObject>* property, qsizetype index) -> QObject*;
352 static auto dataClear(QQmlListProperty<QObject>* property) -> void;
353 static auto childrenAppend(QQmlListProperty<QQuickItem>* property, QQuickItem* item) -> void;
354 static auto childrenCount(QQmlListProperty<QQuickItem>* property) -> qsizetype;
355 static auto childrenAt(QQmlListProperty<QQuickItem>* property, qsizetype index) -> QQuickItem*;
356 static auto childrenClear(QQmlListProperty<QQuickItem>* property) -> void;
358 auto axisData(Axis axis) -> AxisData&;
359 auto axisData(Axis axis)
const ->
const AxisData&;
360 auto axisPosition(Axis axis)
const -> qreal;
361 auto setAxisPosition(Axis axis, qreal position) -> void;
362 void setScrollTarget(Axis axis, qreal position);
363 void setContentPosition(Axis axis, qreal position);
364 auto minExtent(Axis axis)
const -> qreal;
365 auto maxExtent(Axis axis)
const -> qreal;
366 auto viewportSize(Axis axis)
const -> qreal;
367 auto axisCanFlick(Axis axis)
const -> bool;
368 auto aligned(qreal value)
const -> qreal;
369 auto boundedPosition(Axis axis, qreal position)
const -> qreal;
370 auto updateContentSize(Axis axis) -> void;
371 auto updateBeginningEnd() -> void;
372 auto updateVisibleArea() -> void;
373 void flushContentGeometry();
375 auto setAxisMoving(Axis axis,
bool value) -> void;
376 auto setAxisDragging(Axis axis,
bool value) -> void;
377 auto setAxisFlicking(Axis axis,
bool value) -> void;
378 auto setAxisVelocity(Axis axis, qreal value) -> void;
379 auto movementStarting() -> void;
380 auto movementEnding() -> void;
381 auto flickingStarted(
bool horizontal,
bool vertical) -> void;
382 auto draggingStarting(
bool horizontal,
bool vertical) -> void;
383 auto setDragVelocity(QPointF velocity) -> void;
384 auto draggingEnding(std::optional<QPointF> releaseVelocity = std::nullopt) -> void;
386 auto startAxisFlick(Axis axis, qreal velocity) -> void;
387 auto stopAxisMotion(Axis axis) -> void;
388 auto requestMotionFrame() -> void;
389 auto attachMotionWindow(QQuickWindow* window) -> void;
390 auto advanceAxis(Axis axis, qreal now) -> void;
391 auto fixup(Axis axis) -> void;
392 auto isAxisAnimating(Axis axis)
const -> bool;
395 QQuickItem* m_contentItem {
nullptr };
396 quint64 m_geometryRevision { 0 };
397 unsigned m_geometryNotifications { 0 };
398 bool m_syncingGeometry {
false };
399 QList<QObject*> m_data;
402 QPointF m_dragVelocity;
403 FlickableVisibleArea* m_visibleArea {
nullptr };
404 QPointer<QQuickItem> m_interactionItem;
405 QMetaObject::Connection m_frameConnection;
406 QMetaObject::Connection m_windowVisibilityConnection;
407 bool m_motionWindowAttached =
false;
408 QElapsedTimer m_motionClock;
409 QBasicTimer m_dragVelocityTimer;
410 FlickableDirection m_flickableDirection { AutoFlickDirection };
411 InputMaskMode m_inputMaskMode { Viewport };
412 qreal m_deceleration { 5000 };
413 qreal m_maxVelocity { 2500 };
414 bool m_scrollActive {
false };
415 quint64 m_activityRevision { 0 };
416 bool m_interactive {
true };
417 bool m_pixelAligned {
false };
418 bool m_syncDrag {
false };
419 Qt::MouseButtons m_acceptedButtons { Qt::LeftButton };