QmlMaterial 0.1.0
Loading...
Searching...
No Matches
input_host.hpp
1#pragma once
2
3#include <QElapsedTimer>
4#include <QHash>
5#include <QTimer>
6#include <memory>
7#include <optional>
8#include "qml_material/input/interaction_source.hpp"
9
10namespace qml_material
11{
12class InputHost;
13class QtInputAdapter;
14class QtDragInputAdapter;
15
16struct QML_MATERIAL_API RepeatClickInput {
17 int delay = 350, interval = 80;
18
19private:
20 friend class ClickInput;
21 std::unique_ptr<QTimer> timer;
22};
23
24struct PointerInput {
25 Interaction::Device device = Interaction::Unspecified;
26 int identity = -1;
27 QPointF position;
28 qint64 timestamp = 0;
29};
30
31class QML_MATERIAL_API CombinedClickInput {
32public:
33 bool longClickEnabled = false;
34 bool doubleClickEnabled = false;
35 int longClickInterval = 0;
36 std::function<void()> longClicked, doubleClicked;
37
38private:
39 friend class ClickInput;
40 struct Tap {
41 qint64 released, deadline;
42 };
43 QHash<quint64, Tap> taps;
44 std::unique_ptr<QTimer> holdTimer, tapTimer;
45};
46
47class QML_MATERIAL_API ClickInput {
48public:
49 explicit ClickInput(InputHost&);
50 ~ClickInput();
51 std::function<void()> clicked;
52 std::function<void(const PointerInput&)> pointerStarted;
53 std::function<void(QPointF)> pointerClicked, pointerMoved;
54 std::function<void()> pointerCancelled;
55 bool keyboardEnabled = true;
56 CombinedClickInput& combined();
57 RepeatClickInput& repeat();
58 void resetCombined();
59 void activate();
60
61private:
62 friend class InputHost;
63 friend class QtInputAdapter;
64 friend class QtDragInputAdapter;
65 struct Candidate {
66 PointerInput input;
67 quint64 token = 0;
68 qint64 deadline = -1;
69 qint64 repeatDeadline = -1;
70 bool held = false, second = false, ignored = false, dragging = false;
71 bool feedbackEligible = false;
72 };
73 void begin(const PointerInput&);
74 void move(const PointerInput&, bool notifyDrag = true);
75 bool startDrag();
76 void finish(const PointerInput&);
77 void cancelPointer(bool notify);
78 void cancelKeys();
79 void cancel(bool notify);
80 bool key(int, bool down, bool repeat, qint64 timestamp);
81 bool prepare(Candidate&);
82 void feedback(Candidate&);
83 void feedbackTimeout();
84 void release(Candidate&, bool cancelled);
85 void complete(const Candidate&);
86 void scheduleHold();
87 void holdTimeout();
88 void scheduleRepeat();
89 void repeatTimeout();
90 void scheduleTap();
91 void tapTimeout();
92 static quint64 tapIdentity(const Candidate&);
93 InputHost& m_host;
94 std::optional<Candidate> m_pointer;
95 QHash<int, Candidate> m_keys;
96 std::unique_ptr<QTimer> m_feedbackTimer;
97 std::unique_ptr<CombinedClickInput> m_combined;
98 std::unique_ptr<RepeatClickInput> m_repeat;
99};
100
101class QML_MATERIAL_API DragInput {
102public:
103 Qt::Orientations axes = Qt::Horizontal | Qt::Vertical;
104 qreal threshold = -1;
105 bool startImmediately = false;
106 std::function<bool(QPointF, QPointF)> canStart;
107 std::function<void(QPointF)> started, moved;
108 std::function<void(QPointF, bool)> finished;
109 QPointF velocity;
110
111private:
112 friend class ClickInput;
113 friend class QtDragInputAdapter;
114 quint64 token = 0;
115 std::function<bool()> requestGrab;
116};
117
118class HoverInput {
119 friend class InputHost;
120 quint64 token = 0;
121};
122class FocusInput {
123 friend class InputHost;
124 quint64 token = 0;
125};
126
127class QML_MATERIAL_API InputHost {
128public:
129 // Embed in the target item so input is detached before the item is destroyed.
130 explicit InputHost(QQuickItem*, std::function<void()> sourceChanged = {});
131 ~InputHost();
132 InputHost(const InputHost&) = delete;
133 InputHost& operator=(const InputHost&) = delete;
134 QQuickItem* item() const { return m_item; }
135 InteractionSource* source();
136 InteractionSource* existingSource() const { return m_source; }
137 void setSource(InteractionSource*);
138 bool event(QEvent*);
139 bool observeEvent(QEvent*);
140 void mouseUngrabbed();
141 void touchUngrabbed();
142 ClickInput& click();
143 void setHoverEnabled(bool);
144 void observeHover(bool);
145 void setFocusEnabled(bool);
146 DragInput& drag();
147 void resetDrag();
148 void observeChildPointerInput();
149 void cancelActivation();
150 quint64 activationRevision() const { return m_generation; }
151 void cancel();
152 bool available() const;
153 std::function<bool()> acceptsInput;
154 bool focusVisible() const { return m_focusVisible; }
155 std::function<void()> focusVisibleChanged;
156
157private:
158 friend class ClickInput;
159 friend class QtInputAdapter;
160 friend class QtDragInputAdapter;
161 quint64 begin(Interaction::Kind, const PointerInput& = {});
162 void end(quint64, bool cancelled = false);
163 void hover(bool);
164 void focus();
165 void blur();
166 void focusReason(Qt::FocusReason);
167 void observeWindow();
168 void reset(bool notify);
169 void attachSource(InteractionSource*);
170 QPointer<QQuickItem> m_item;
171 QPointer<InteractionSource> m_source;
172 std::unique_ptr<InteractionSource> m_ownedSource;
173 std::unique_ptr<InteractionEmitter> m_emitter;
174 QList<QMetaObject::Connection> m_connections;
175 QMetaObject::Connection m_sourceDestroyed, m_windowActive, m_windowVisibility;
176 std::function<void()> m_sourceChanged;
177 std::optional<ClickInput> m_click;
178 std::unique_ptr<DragInput> m_drag;
179 std::unique_ptr<QtDragInputAdapter> m_pointerAdapter;
180 std::shared_ptr<const bool> m_lifetime = std::make_shared<const bool>(true);
181 HoverInput m_hover;
182 FocusInput m_focus;
183 quint64 m_editorPress = 0;
184 QElapsedTimer m_elapsed;
185 bool m_focusVisible = false, m_detaching = false;
186 bool m_focusEnabled = true;
187 Qt::FocusReason m_focusReason = Qt::OtherFocusReason;
188 quint64 m_generation = 0;
189};
190
191class QML_MATERIAL_API QtInputAdapter {
192public:
193 static bool deliver(InputHost&, QEvent*);
194 // The editor owns acceptance, selection and grabs; this only observes its result.
195 static bool deliverToEditor(InputHost&, QEvent*, const std::function<bool()>& receiver);
196};
197} // namespace qml_material