3#include "linear_layout.hpp"
4#include "item_anchor_p.hpp"
5#include "qml_material/model/item_source.hpp"
12class ViewportController {
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;
33 m_layout.reset(0, 0, 0);
36 Result apply(ItemSnapshotPtr next,
double constraint,
double estimate,
double spacing,
38 if (! next || ! std::isfinite(constraint) || constraint < 0 || ! std::isfinite(offset))
39 return {
false, offset };
41 if (! layout.reset(next->count(), estimate, spacing))
return {
false, offset };
43 const auto anchor = ItemAnchor::capture(
44 m_snapshot, next, m_layout.indexAt(offset), offset, [
this](
int index) {
45 return m_layout.offset(index);
48 QHash<QString, Measurement> cache;
49 const bool reuse = m_snapshot && constraint == m_constraint &&
50 ((m_snapshot->stableKeys() && next->stableKeys()) || next == m_snapshot);
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)
58 if (! layout.setExtent(i, found->extent))
return {
false, offset };
59 cache.insert(row.key, *found);
62 double corrected = offset;
63 if (next->count() == 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 };
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 };
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))
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 };
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 };
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 };
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 };
141 return {
true, offset + m_layout.offset(anchor) - oldStart };
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))
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 +
162 ItemSnapshotPtr m_snapshot;
163 LinearLayout m_layout;
164 QHash<QString, Measurement> m_cache;
165 double m_constraint { 0 };
166 double m_estimate { 0 };