QmlMaterial 0.1.0
Loading...
Searching...
No Matches
viewport_controller.hpp
1#pragma once
2
3#include "linear_layout.hpp"
4#include "item_anchor_p.hpp"
5#include "qml_material/model/item_source.hpp"
6#include <optional>
7#include <QSet>
8
9namespace qml_material
10{
11
12class ViewportController {
13public:
14 struct Result {
15 bool accepted;
16 double offset;
17 };
18
19 const LinearLayout& layout() const { return m_layout; }
20 ItemSnapshotPtr snapshot() const { return m_snapshot; }
21 bool estimated() const { return m_cache.size() < m_layout.count(); }
22 bool measured(int index) const {
23 if (! m_snapshot || index < 0 || index >= m_snapshot->count()) return false;
24 const auto& row = m_snapshot->descriptorAt(index);
25 const auto found = m_cache.constFind(row.key);
26 return found != m_cache.cend() && found->type == row.reuseType &&
27 found->revision == row.contentRevision;
28 }
29
30 void clear() {
31 m_snapshot.reset();
32 m_cache.clear();
33 m_layout.reset(0, 0, 0);
34 }
35
36 Result apply(ItemSnapshotPtr next, double constraint, double estimate, double spacing,
37 double offset) {
38 if (! next || ! std::isfinite(constraint) || constraint < 0 || ! std::isfinite(offset))
39 return { false, offset };
40 LinearLayout layout;
41 if (! layout.reset(next->count(), estimate, spacing)) return { false, offset };
42
43 const auto anchor = ItemAnchor::capture(
44 m_snapshot, next, m_layout.indexAt(offset), offset, [this](int index) {
45 return m_layout.offset(index);
46 });
47
48 QHash<QString, Measurement> cache;
49 const bool reuse = m_snapshot && constraint == m_constraint &&
50 ((m_snapshot->stableKeys() && next->stableKeys()) || next == m_snapshot);
51 if (reuse) {
52 for (int i = 0; i < next->count(); ++i) {
53 const auto& row = next->descriptorAt(i);
54 const auto found = m_cache.constFind(row.key);
55 if (found == m_cache.cend() || found->type != row.reuseType ||
56 found->revision != row.contentRevision)
57 continue;
58 if (! layout.setExtent(i, found->extent)) return { false, offset };
59 cache.insert(row.key, *found);
60 }
61 }
62 double corrected = offset;
63 if (next->count() == 0)
64 corrected = 0;
65 else if (! anchor.key.isEmpty())
66 corrected = layout.offset(next->indexOfKey(anchor.key)) - anchor.screenOffset;
67 m_snapshot = std::move(next);
68 m_constraint = constraint;
69 m_estimate = estimate;
70 m_layout = std::move(layout);
71 m_cache = std::move(cache);
72 return { true, corrected };
73 }
74
75 Result applyValues(ItemSnapshotPtr next, const QVector<ItemChange>& changes, double constraint,
76 double estimate, double spacing, double offset) {
77 if (! m_snapshot || ! next || next->count() != m_layout.count() ||
78 constraint != m_constraint || estimate != m_estimate || spacing != m_layout.spacing())
79 return apply(std::move(next), constraint, estimate, spacing, offset);
80 if (! std::isfinite(offset)) return { false, offset };
81 QVector<int> dirty;
82 QSet<QString> keys;
83 for (const auto& change : changes) {
84 if (change.kind != ItemChange::Update)
85 return apply(std::move(next), constraint, estimate, spacing, offset);
86 if (change.index < 0 || change.count <= 0 ||
87 qint64(change.index) + change.count > next->count())
88 return { false, offset };
89 for (int index = change.index; index < change.index + change.count; ++index) {
90 const auto& row = next->descriptorAt(index);
91 if (m_snapshot->descriptorAt(index).key != row.key)
92 return apply(std::move(next), constraint, estimate, spacing, offset);
93 const auto found = m_cache.constFind(row.key);
94 if (found == m_cache.cend() ||
95 (found->type == row.reuseType && found->revision == row.contentRevision) ||
96 keys.contains(row.key))
97 continue;
98 keys.insert(row.key);
99 dirty.append(index);
100 }
101 }
102 // Keep the incremental mutation below the numeric limits checked by setExtent.
103 if (! std::isfinite(m_layout.offset(m_layout.count()) + dirty.size() * estimate))
104 return apply(std::move(next), constraint, estimate, spacing, offset);
105 const int anchor = m_layout.indexAt(offset);
106 const double oldStart = m_layout.offset(anchor);
107 QVector<double> oldExtents;
108 oldExtents.reserve(dirty.size());
109 for (int index : dirty) {
110 oldExtents.append(m_layout.extent(index));
111 if (! m_layout.setExtent(index, estimate)) {
112 for (int i = oldExtents.size() - 2; i >= 0; --i)
113 m_layout.setExtent(dirty[i], oldExtents[i]);
114 return { false, offset };
115 }
116 }
117 for (const auto& key : keys) m_cache.remove(key);
118 m_snapshot = std::move(next);
119 return { true, m_layout.count() ? offset + m_layout.offset(anchor) - oldStart : 0 };
120 }
121
122 Result measure(int index, double extent, double offset) {
123 if (! m_snapshot || index < 0 || index >= m_layout.count() || ! std::isfinite(offset))
124 return { false, offset };
125 const int anchor = m_layout.indexAt(offset);
126 const double oldStart = m_layout.offset(anchor);
127 if (! m_layout.setExtent(index, extent)) return { false, offset };
128 const auto& row = m_snapshot->descriptorAt(index);
129 m_cache.insert(row.key, { extent, row.reuseType, row.contentRevision });
130 return { true, offset + m_layout.offset(anchor) - oldStart };
131 }
132
133 Result invalidate(const QString& key, double offset) {
134 if (! m_snapshot) return { false, offset };
135 const int index = m_snapshot->indexOfKey(key);
136 if (index < 0 || ! std::isfinite(offset)) return { false, offset };
137 const int anchor = m_layout.indexAt(offset);
138 const double oldStart = m_layout.offset(anchor);
139 if (! m_layout.setExtent(index, m_estimate)) return { false, offset };
140 m_cache.remove(key);
141 return { true, offset + m_layout.offset(anchor) - oldStart };
142 }
143
144 std::optional<double> positionAt(const QString& key, double viewport, double alignment,
145 double extraOffset = 0) const {
146 if (! m_snapshot || ! std::isfinite(viewport) || viewport < 0 ||
147 ! std::isfinite(alignment) || alignment < 0 || alignment > 1 ||
148 ! std::isfinite(extraOffset))
149 return {};
150 const int index = m_snapshot->indexOfKey(key);
151 if (index < 0) return {};
152 return m_layout.offset(index) - (viewport - m_layout.extent(index)) * alignment +
153 extraOffset;
154 }
155
156private:
157 struct Measurement {
158 double extent;
159 QString type;
160 quint64 revision;
161 };
162 ItemSnapshotPtr m_snapshot;
163 LinearLayout m_layout;
164 QHash<QString, Measurement> m_cache;
165 double m_constraint { 0 };
166 double m_estimate { 0 };
167};
168
169} // namespace qml_material