hopefully true virtual list layout
This commit is contained in:
10 files changed
+522
-37
No files matched your search
@@ -38,13 +38,41 @@ QVariant ReadingModel::data(const QModelIndex &index, int role) const
|
||||
return {};
|
||||
if (role == ContentRole)
|
||||
return m_chunks.at(index.row());
|
||||
if (role == LayoutRole && index.row() < m_chunkLayouts.size())
|
||||
if (role == LayoutRole && index.row() < m_chunks.size()) {
|
||||
ensureChunkLayout(index.row());
|
||||
return QVariant::fromValue(m_chunkLayouts.at(index.row()));
|
||||
if (role == PixelHeightRole && index.row() < m_chunkLayouts.size())
|
||||
}
|
||||
if (role == PixelHeightRole && index.row() < m_chunks.size()) {
|
||||
if (index.row() < m_chunkLayouts.size() && m_chunkLayouts.at(index.row()).totalHeight > 0 && !m_chunkLayouts.at(index.row()).lines.isEmpty())
|
||||
return m_chunkLayouts.at(index.row()).totalHeight;
|
||||
ensureChunkLayout(index.row());
|
||||
return m_chunkLayouts.at(index.row()).totalHeight;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void ReadingModel::ensureChunkLayout(int row) const
|
||||
{
|
||||
if (row < 0 || row >= m_chunks.size())
|
||||
return;
|
||||
if (row < m_chunkLayouts.size() && m_chunkLayouts[row].totalHeight > 0 && !m_chunkLayouts[row].lines.isEmpty())
|
||||
return;
|
||||
if (row >= m_preparedChunks.size())
|
||||
m_preparedChunks.resize(m_chunks.size());
|
||||
if (row >= m_chunkLayouts.size())
|
||||
m_chunkLayouts.resize(m_chunks.size());
|
||||
|
||||
LayoutParams params;
|
||||
params.contentWidth = m_layoutContentWidth;
|
||||
params.fontSize = m_layoutFontSize;
|
||||
params.lineSpacing = m_layoutLineSpacing;
|
||||
|
||||
if (m_preparedChunks[row].blocks.isEmpty() && !m_chunks[row].isEmpty()) {
|
||||
m_preparedChunks[row] = ChunkLayoutEngine::prepareChunk(m_chunks[row], params.fontSize, m_imageResolver);
|
||||
}
|
||||
m_chunkLayouts[row] = ChunkLayoutEngine::layoutPreparedChunk(m_preparedChunks[row], params);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> ReadingModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
@@ -107,8 +135,11 @@ void ReadingModel::updateLayoutMetrics(int contentWidth, int fontSize, qreal lin
|
||||
params.fontSize = m_layoutFontSize;
|
||||
params.lineSpacing = m_layoutLineSpacing;
|
||||
|
||||
const int startIdx = std::max(0, targetChunk - 2);
|
||||
const int endIdx = std::min(int(m_preparedChunks.size() - 1), targetChunk + 4);
|
||||
const int startIdx = std::max(0, targetChunk - 5);
|
||||
const int endIdx = std::min(int(m_preparedChunks.size() - 1), targetChunk + 25);
|
||||
|
||||
m_lastSyncStart = startIdx;
|
||||
m_lastSyncEnd = endIdx;
|
||||
|
||||
if (m_chunkLayouts.size() != m_preparedChunks.size()) {
|
||||
m_chunkLayouts.resize(m_preparedChunks.size());
|
||||
@@ -141,9 +172,11 @@ void ReadingModel::runRemainingLayout()
|
||||
params.contentWidth = m_layoutContentWidth;
|
||||
params.fontSize = m_layoutFontSize;
|
||||
params.lineSpacing = m_layoutLineSpacing;
|
||||
const int syncStart = m_lastSyncStart;
|
||||
const int syncEnd = m_lastSyncEnd;
|
||||
|
||||
QPointer<ReadingModel> self(this);
|
||||
m_layoutFuture = QtConcurrent::run([self, generation, prepared, params]() {
|
||||
m_layoutFuture = QtConcurrent::run([self, generation, prepared, params, syncStart, syncEnd]() {
|
||||
QVector<ChunkLayout> newLayouts = QtConcurrent::blockingMapped(
|
||||
prepared,
|
||||
[params](const ChunkLayoutEngine::PreparedChunk &prep) {
|
||||
@@ -153,12 +186,20 @@ void ReadingModel::runRemainingLayout()
|
||||
if (!self || self->m_layoutGeneration != generation)
|
||||
return;
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, generation, newLayouts = std::move(newLayouts)]() mutable {
|
||||
QMetaObject::invokeMethod(self.data(), [self, generation, newLayouts = std::move(newLayouts), syncStart, syncEnd]() mutable {
|
||||
if (!self || generation != self->m_layoutGeneration)
|
||||
return;
|
||||
self->m_chunkLayouts = std::move(newLayouts);
|
||||
if (!self->m_chunkLayouts.isEmpty()) {
|
||||
emit self->dataChanged(self->index(0, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
if (syncStart > 0) {
|
||||
emit self->dataChanged(self->index(0, 0), self->index(syncStart - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
}
|
||||
if (syncEnd >= 0 && syncEnd + 1 < self->m_chunkLayouts.size()) {
|
||||
emit self->dataChanged(self->index(syncEnd + 1, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
}
|
||||
if (syncStart < 0 && syncEnd < 0) {
|
||||
emit self->dataChanged(self->index(0, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
}
|
||||
}
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
@@ -235,8 +276,40 @@ void ReadingModel::startLayoutMeasurement(int restoreChunk, qreal restoreIntraOf
|
||||
params.lineSpacing = m_layoutLineSpacing;
|
||||
auto *resolver = m_imageResolver;
|
||||
|
||||
// Phase 1: Immediately prepare and layout the visible window synchronously (<1ms)
|
||||
const int startIdx = std::max(0, restoreChunk - 5);
|
||||
const int endIdx = std::min(int(chunks.size() - 1), restoreChunk + 25);
|
||||
|
||||
m_preparedChunks.resize(chunks.size());
|
||||
m_chunkLayouts.resize(chunks.size());
|
||||
|
||||
for (int i = startIdx; i <= endIdx; ++i) {
|
||||
m_preparedChunks[i] = ChunkLayoutEngine::prepareChunk(chunks[i], params.fontSize, resolver);
|
||||
m_chunkLayouts[i] = ChunkLayoutEngine::layoutPreparedChunk(m_preparedChunks[i], params);
|
||||
}
|
||||
for (int i = 0; i < chunks.size(); ++i) {
|
||||
if (i < startIdx || i > endIdx) {
|
||||
m_chunkLayouts[i].totalHeight = 60.0;
|
||||
}
|
||||
}
|
||||
|
||||
m_lastSyncStart = startIdx;
|
||||
m_lastSyncEnd = endIdx;
|
||||
|
||||
if (startIdx <= endIdx) {
|
||||
emit dataChanged(index(startIdx, 0), index(endIdx, 0), {LayoutRole, PixelHeightRole});
|
||||
}
|
||||
emit restoreReady(restoreChunk, restoreIntraOffset);
|
||||
|
||||
if (startIdx == 0 && endIdx == chunks.size() - 1) {
|
||||
m_measuring = false;
|
||||
emit measuringChanged();
|
||||
return;
|
||||
}
|
||||
|
||||
// Phase 2: Compute remaining chunks in the background
|
||||
QPointer<ReadingModel> self(this);
|
||||
m_layoutFuture = QtConcurrent::run([self, generation, chunks, params, resolver, restoreChunk, restoreIntraOffset]() {
|
||||
m_layoutFuture = QtConcurrent::run([self, generation, chunks, params, resolver, startIdx, endIdx]() {
|
||||
auto prepared = QtConcurrent::blockingMapped(chunks, [params, resolver](const QString &chunkHtml) {
|
||||
return ChunkLayoutEngine::prepareChunk(chunkHtml, params.fontSize, resolver);
|
||||
});
|
||||
@@ -251,17 +324,19 @@ void ReadingModel::startLayoutMeasurement(int restoreChunk, qreal restoreIntraOf
|
||||
if (!self || self->m_layoutGeneration != generation)
|
||||
return;
|
||||
|
||||
QMetaObject::invokeMethod(self.data(), [self, generation, prepared = std::move(prepared), layouts = std::move(layouts), restoreChunk, restoreIntraOffset]() mutable {
|
||||
QMetaObject::invokeMethod(self.data(), [self, generation, prepared = std::move(prepared), layouts = std::move(layouts), startIdx, endIdx]() mutable {
|
||||
if (!self || generation != self->m_layoutGeneration)
|
||||
return;
|
||||
self->m_preparedChunks = std::move(prepared);
|
||||
self->m_chunkLayouts = std::move(layouts);
|
||||
self->m_measuring = false;
|
||||
emit self->measuringChanged();
|
||||
if (!self->m_chunkLayouts.isEmpty()) {
|
||||
emit self->dataChanged(self->index(0, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
if (startIdx > 0) {
|
||||
emit self->dataChanged(self->index(0, 0), self->index(startIdx - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
}
|
||||
if (endIdx + 1 < self->m_chunkLayouts.size()) {
|
||||
emit self->dataChanged(self->index(endIdx + 1, 0), self->index(self->m_chunkLayouts.size() - 1, 0), {LayoutRole, PixelHeightRole});
|
||||
}
|
||||
emit self->restoreReady(restoreChunk, restoreIntraOffset);
|
||||
}, Qt::QueuedConnection);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user