Extract the layout logic to a view model to simplify the update logic in continuous scroll mode

This commit is contained in:
luisangelsm
2026-02-26 16:19:58 +01:00
committed by Luis Ángel San Martín
parent f61c5280c9
commit 0bd291ba98
7 changed files with 506 additions and 375 deletions

View File

@ -8,6 +8,7 @@
#include "themable.h"
class Render;
class ContinuousViewModel;
class ContinuousPageWidget : public QWidget, protected Themable
{
@ -16,27 +17,13 @@ public:
explicit ContinuousPageWidget(QWidget *parent = nullptr);
void setRender(Render *r);
void setNumPages(int count);
void setZoomFactor(int zoom);
void probeBufferedPages();
void setViewModel(ContinuousViewModel *viewModel);
void reset();
int centerPage(int scrollY, int viewportHeight) const;
int yPositionForPage(int pageIndex) const;
int totalHeight() const;
bool hasHeightForWidth() const override;
int heightForWidth(int w) const override;
QSize sizeHint() const override;
void setAnchorPage(int page);
void setViewportState(int scrollY, int viewportHeight);
signals:
// emitted after layout recomputation when the preserved viewport anchor
// resolves to an absolute scroll position
void layoutScrollPositionRequested(int scrollY);
public slots:
void onPageAvailable(int absolutePageIndex);
@ -46,41 +33,8 @@ protected:
void applyTheme(const Theme &theme) override;
private:
struct LayoutSnapshot {
QVector<int> yPositions;
QVector<QSize> scaledSizes;
int totalHeight = 0;
};
struct ViewportAnchor {
int pageIndex = -1;
float offsetRatio = 0.0f;
bool valid = false;
};
void updateLayout();
void updateLayoutWithAnchor();
void relayout(bool preserveAnchor);
LayoutSnapshot buildLayoutSnapshot(int w) const;
int pageAtY(const LayoutSnapshot &snapshot, int y) const;
ViewportAnchor anchorFromViewport(const LayoutSnapshot &snapshot, int scrollY, int viewportHeight) const;
int resolveAnchorToScrollY(const LayoutSnapshot &snapshot, const ViewportAnchor &anchor, int viewportHeight) const;
int pageAtY(int y) const;
QSize scaledPageSize(int pageIndex, int forWidth) const;
float scaleForPage(int pageIndex, int forWidth) const;
Render *render = nullptr;
int numPages = 0;
QVector<QSize> pageSizes;
QVector<int> yPositions;
int currentTotalHeight = 0;
LayoutSnapshot layoutSnapshot;
QSize defaultPageSize { 800, 1200 };
int zoomFactor = 100;
int anchorPage = -1;
int viewportScrollY = 0;
int currentViewportHeight = 0;
bool hasViewportState = false;
ContinuousViewModel *continuousViewModel = nullptr;
};
#endif // CONTINUOUS_PAGE_WIDGET_H