QmlMaterial 0.1.0
Loading...
Searching...
No Matches
page_stack.hpp
1#pragma once
2
3#include "qml_material/control/control.hpp"
4#include <memory>
5#include "qml_material/anim/size_transition.hpp"
6
7#include "qml_material/control/page_motion.hpp"
8#include "qml_material/input/input_host.hpp"
9#include <QQmlComponent>
10
11namespace qml_material
12{
13class PageStackAttached;
14class QML_MATERIAL_API PageStack : public Control {
15 Q_OBJECT
16 QML_NAMED_ELEMENT(PageStackBase)
17 QML_ATTACHED(PageStackAttached)
18 Q_PROPERTY(int depth READ depth NOTIFY depthChanged FINAL)
19 Q_PROPERTY(bool empty READ empty NOTIFY depthChanged FINAL)
20 Q_PROPERTY(bool busy READ busy NOTIFY busyChanged FINAL)
21 Q_PROPERTY(SizeTransition* sizeTransition READ sizeTransition WRITE setSizeTransition NOTIFY
22 sizeTransitionChanged FINAL)
23 Q_PROPERTY(
24 QQmlComponent* pushMotion READ pushMotion WRITE setPushMotion NOTIFY motionsChanged FINAL)
25 Q_PROPERTY(
26 QQmlComponent* popMotion READ popMotion WRITE setPopMotion NOTIFY motionsChanged FINAL)
27 Q_PROPERTY(QQmlComponent* replaceMotion READ replaceMotion WRITE setReplaceMotion NOTIFY
28 motionsChanged FINAL)
29 Q_PROPERTY(PageMotion* currentMotion READ currentMotion NOTIFY currentMotionChanged FINAL)
30 Q_PROPERTY(bool preview READ preview NOTIFY previewChanged FINAL)
31 Q_PROPERTY(bool settling READ settling NOTIFY previewChanged FINAL)
32 Q_PROPERTY(bool backGestureEnabled READ backGestureEnabled WRITE setBackGestureEnabled NOTIFY
33 backGestureEnabledChanged FINAL)
34 Q_PROPERTY(QSizeF destinationSize READ destinationSize NOTIFY destinationSizeChanged FINAL)
35 Q_PROPERTY(QQuickItem* currentItem READ currentItem NOTIFY currentItemChanged FINAL)
36 Q_PROPERTY(QQuickItem* initialItem READ initialItem WRITE setInitialItem NOTIFY
37 initialItemChanged FINAL)
38public:
39 enum Status
40 {
41 Inactive,
42 Deactivating,
43 Activating,
44 Active
45 };
46 Q_ENUM(Status)
47 enum TransitionMode
48 {
49 Immediate,
50 Animated
51 };
52 Q_ENUM(TransitionMode)
53 explicit PageStack(QQuickItem* parent = nullptr);
54 ~PageStack() override;
55 static PageStackAttached* qmlAttachedProperties(QObject*);
56 int depth() const;
57 bool empty() const { return depth() == 0; }
58 bool busy() const;
59 SizeTransition* sizeTransition() const;
60 void setSizeTransition(SizeTransition*);
61 Q_SIGNAL void sizeTransitionChanged();
62 QQmlComponent* pushMotion() const;
63 QQmlComponent* popMotion() const;
64 QQmlComponent* replaceMotion() const;
65 void setPushMotion(QQmlComponent*);
66 void setPopMotion(QQmlComponent*);
67 void setReplaceMotion(QQmlComponent*);
68 PageMotion* currentMotion() const;
69 bool preview() const;
70 bool settling() const;
71 bool backGestureEnabled() const;
72 void setBackGestureEnabled(bool);
73 QSizeF destinationSize() const;
74 Q_INVOKABLE QString beginPopPreview();
75 Q_INVOKABLE bool updatePopPreview(QString id, qreal progress);
76 Q_INVOKABLE bool commitPopPreview(QString id, qreal velocity = 0);
77 Q_INVOKABLE bool cancelPopPreview(QString id, qreal velocity = 0);
78 Q_INVOKABLE void abortPreview();
79 Q_INVOKABLE void completeTransition();
80 Q_SIGNAL void busyChanged();
81 Q_SIGNAL void motionsChanged();
82 Q_SIGNAL void currentMotionChanged();
83 Q_SIGNAL void previewChanged();
84 Q_SIGNAL void backGestureEnabledChanged();
85 Q_SIGNAL void destinationSizeChanged();
86 QQuickItem* currentItem() const;
87 QQuickItem* initialItem() const;
88 void setInitialItem(QQuickItem*);
89 Q_INVOKABLE bool pushItem(QQuickItem*, TransitionMode mode = Animated);
90 Q_INVOKABLE bool replaceCurrentItem(QQuickItem*, TransitionMode mode = Animated);
91 Q_INVOKABLE bool popCurrentItem(TransitionMode mode = Animated);
92 Q_INVOKABLE bool clear(TransitionMode mode = Animated);
93 Q_INVOKABLE QQuickItem* itemAt(int index) const;
94 Q_SIGNAL void depthChanged();
95 Q_SIGNAL void currentItemChanged();
96 Q_SIGNAL void initialItemChanged();
97 Q_SIGNAL void entryRemoved(quint64 id);
98 Q_SIGNAL void entryAdded(quint64 id, QQuickItem* item);
99
100protected:
101 void enableDefaultMotion();
102 void componentComplete() override;
103 void geometryChange(const QRectF&, const QRectF&) override;
104 bool childMouseEventFilter(QQuickItem*, QEvent*) override;
105 QSizeF measureImplicitContent() const override;
106
107private:
108 struct State;
109 std::shared_ptr<State> m_state;
110 bool navigate(int operation, QQuickItem*, TransitionMode);
111 void synchronize();
112 void layoutPages();
113 void removePage(quint64);
114 void startTransition(quint64);
115 void applyMotion();
116 bool settlePreview(QString, bool, qreal);
117 void tryFinishTransition();
118 void stopMotions();
119 InputHost m_input;
120 QString m_gesture;
121 QPointF m_gesturePoint;
122 qreal m_gestureFraction = 0;
123};
124
125class QML_MATERIAL_API MaterialPageStack : public PageStack {
126 Q_OBJECT
127 QML_NAMED_ELEMENT(PageStack)
128 QML_ATTACHED(PageStackAttached)
129public:
130 explicit MaterialPageStack(QQuickItem* parent = nullptr);
131};
132
133class QML_MATERIAL_API PageStackAttached : public QObject {
134 Q_OBJECT
135 Q_PROPERTY(PageStack* view READ view NOTIFY changed FINAL)
136 Q_PROPERTY(int index READ index NOTIFY changed FINAL)
137 Q_PROPERTY(PageStack::Status status READ status NOTIFY statusChanged FINAL)
138public:
139 using Status = PageStack::Status;
140 explicit PageStackAttached(QObject* parent): QObject(parent) {}
141 PageStack* view() const { return m_view; }
142 int index() const { return m_index; }
143 Status status() const { return m_status; }
144 Q_SIGNAL void changed();
145 Q_SIGNAL void statusChanged();
146 Q_SIGNAL void removed();
147
148private:
149 friend class PageStack;
150 void update(PageStack*, int, Status);
151 QPointer<PageStack> m_view;
152 int m_index = -1;
153 Status m_status = PageStack::Inactive;
154 bool m_removing = false;
155};
156} // namespace qml_material
157QML_DECLARE_TYPEINFO(qml_material::PageStack, QML_HAS_ATTACHED_PROPERTIES)
158QML_DECLARE_TYPEINFO(qml_material::MaterialPageStack, QML_HAS_ATTACHED_PROPERTIES)