feat: PdfViewerParams.layout + SequentialPagesLayout + PdfFitMode#657
Open
enhancient wants to merge 1 commit into
Open
feat: PdfViewerParams.layout + SequentialPagesLayout + PdfFitMode#657enhancient wants to merge 1 commit into
enhancient wants to merge 1 commit into
Conversation
Additive, non-breaking declarative layout. An abstract value-type strategy `PdfLayout` with
`resolve({pages, viewport, params}) -> PdfPageLayout`, dispatched ahead of the existing layoutPages
closure: `params.layout?.resolve(...)` -> `layoutPages` -> built-in default. layoutPages is untouched.
Adds the first concrete strategy `SequentialPagesLayout` (continuous flow, cross-axis width
normalization for mixed page sizes, configurable spacing/margin/alignment) and `PdfFitMode
{none, fit, fill, cover}`, where each page is fitted by sizing its rect in the computed layout.
Why per-page fit lives in geometry rather than as a delegate scale: PdfViewerLayoutMetrics exposes
whole-document scalars (coverScale, alternativeFitScale). alternativeFitScale is a single scalar
derived from the current page, so it gives inconsistent fit across a mixed-size document. Fitting
each page individually needs a per-page scale a single viewer matrix can't express, so it must be
encoded per page in the rects. The size delegate keeps owning zoom bounds; layout owns geometry.
Margins and fit behave consistently on resize:
- The margin scales with the document (sized relative to the largest page, kept uniform - no
per-page normalization), so it shrinks/grows with the viewport.
- For fit/fill the column fills the viewport width, so the delegate's cover/fit zoom resolves to ~1.
- The layout reports its effectiveMargin on PdfPageLayout; the size delegate (additive fitMode arg,
default none), goToPage navigation, and the fit-zoom helpers all use it, so a declarative layout's
margin is the single source of truth. params.margin governs only the built-in layout
(effectiveMargin null) - no behavior change for existing users.
Equality invariant: PdfLayout is a value type (const base, abstract ==/hashCode forcing real value
equality on every subclass, no stored closures). The viewport enters only via resolve() and is never
stored, so resize relayouts without equality churn. `layout`, `fitMode`, and the previously-unwired
`scrollPhysicsScale` are folded into ==, hashCode, and doChangesRequireReload. Re-points the
useAlternativeFitScaleAsMinScale docs at fitMode.
Pure tests cover layout geometry per fit mode (incl. margin scaling), the delegate min-scale floor,
and params equality (incl. scrollPhysicsScale). The relayout widget test is PDFium-dependent.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
884a97d to
7f0b18b
Compare
Contributor
Author
|
@espresso3389 Can I help in any way to progress your review of this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat:
PdfViewerParams.layout— declarative page layoutSummary
Directly addressing #593, this is the first of a small series of PRs that re-propose the layout features from #589, split into small parts that are easy to review.
Adds
PdfViewerParams.layout: a declarative layout object (PdfLayout) that computes page positions viaresolve({pages, viewport, params}). The viewer uses it before the existinglayoutPagescallback:It is additive and non-breaking. With
layout: nullandfitMode: none, behavior is unchanged.This PR also adds the first layout,
SequentialPagesLayout(continuous scroll, handles mixed pagesizes), and
PdfFitMode {none, fit, fill, cover}. Each page's fit is applied by setting itsrectangle size in the layout, not by zooming the whole viewer. This per-page geometry is also what later enables
page-at-a-time (discrete) transitions as it requires each page pre-fitted in its rect, which a single
document-wide scale can't provide.
Example of PdfFitMode.fit with

SequentialPagesLayoutExample of non page fitted layout using

params.layoutPages(...)Why an object instead of
layoutPageslayoutPagesis a function:A function is a new instance on almost every build, so
PdfViewerParams ==cannot tell when itchanged. Today, changing
layoutPagesdoes nothing until you callinvalidate()by hand. Apps alsohave to rewrite the same layout math (spacing, centering, mixed page sizes) each time.
PdfLayoutis a value type, so:==/hashCode.PdfViewerParamsincludes it in equality and relayoutsautomatically when it changes.
resolve()at call time and is never stored, so it is not part of==.A window resize recomputes the layout but is not treated as a config change.
It can do anything
layoutPagescan do, so laterlayoutPagescan become a thin wrapper overPdfLayout(one code path, not two). For nowlayoutPagesis unchanged.Why not extend the size delegate (#582)
The size delegate produces single numbers for the whole document (
coverScale,alternativeFitScale).alternativeFitScaledoes fit the current page (width and height), but it isone number for the whole document: it changes depending on which page is current, and one number
cannot fit pages of different sizes at the same time.
To fit each page on its own, each page needs its own scale, stored in its rectangle. So per-page fit
must live in the layout. The size delegate keeps owning zoom limits; the layout owns page positions
and sizes. The new
fitModeis passed to the delegate as a normal argument (defaultnone) so itcan set the minimum zoom for each mode, with no change to the delegate's API.
Non-breaking
layoutdefaults tonull→ the existing path is unchanged.layoutPagesis not modified.fitModedefaults tonone→ native page sizes, no change.fitModeargument has a default, so existing customPdfViewerSizeDelegatecode still compiles.
scrollPhysicsScalefix: it was missing from==/hashCode/doChangesRequireReload, so a change to it alone was ignored. It is now wired likescrollPhysics.What's in this PR
PdfLayout(the layout object) and the dispatch in the viewer.SequentialPagesLayout(continuous scroll, mixed page sizes, spacing / margin / alignment).PdfFitMode {none, fit, fill, cover}and thefitModeargument on the size delegate.scrollPhysicsScaleequality fix;useAlternativeFitScaleAsMinScaledocs now point tofitMode.example/viewer.Notes
params.layoutandresolve(...)are open to change — please tell me your preference.The important parts are the value-type design and the equality.
FacingPagesLayout, thendiscrete page transitions, then a page-range API — each a small PR using the same
layoutslot.Later,
layoutPagesbecomes a thin wrapper overPdfLayout.