Skip to content

fix(upscale): keep a page's ratio stable across the 2x swap - #100

Merged
dyphire merged 1 commit into
mainfrom
fix/upscale-swap-ratio
Nov 28, 2026
Merged

dyphire merged 1 commit into
mainfrom
fix/upscale-swap-ratio

Conversation

@dyphire

@dyphire dyphire commented Nov 28, 2026

Copy link
Copy Markdown
Member

Problem

With waifu2x enabled, a page's scroll position could jump at the moment the enhanced bitmap replaced the source in the webtoon (continuous vertical) reader.

PageImage derives its box ratio from the current painter state:

val decodedRatio = (imageState as? AsyncImagePainter.State.Success)
    ?.painter?.intrinsicSize ... width / height
val fullRatio = decodedRatio ?: metadataRatio ?: 0.7f

FitRatioBox measures a webtoon cell with an infinite height and derives it from that ratio (fillMaxWidth().aspectRatio(ratio)). When the enhanced request starts, Coil enters Loading, decodedRatio becomes null, and the ratio falls back to page.width/height (or 0.7 when the metadata is stale or missing). That resizes the cell for a frame, the LazyColumn re-anchors on visible item + pixel offset, and the view visibly moves.

Content itself was already handled (lastPainter keeps painting the previous bitmap while the new one decodes); only the geometry was unstable.

Fix

Remember the ratio the cell actually decoded at, per page and per split half, and reuse it across the source→2x model swap:

var lastRatio by remember(page.number, half) { mutableStateOf<Float?>(null) }
LaunchedEffect(imageState) {
    (imageState as? AsyncImagePainter.State.Success)?.let { state ->
        lastPainter = state.painter
        state.painter.intrinsicSize
            ?.takeIf { it.width > 0f && it.height > 0f }
            ?.let { it.width / it.height }
            ?.takeIf { it.isFinite() && it > 0f }
            ?.let { lastRatio = it }
    }
}
val fullRatio = lastRatio
    ?: metadataRatio.takeIf { it.isFinite() && it > 0f }
    ?: 0.7f
  • First load still guesses from metadata, so placeholder behaviour is unchanged.
  • Once a cell has decoded, its box no longer changes geometry during the swap (the 2x bitmap has the same ratio), so the strip's item height is stable and the scroll anchor cannot move.
  • Split halves keep their own ratio (half is part of the key), matching the existing per-half crop.

Verification

  • ./gradlew :app:compileDebugKotlin — passes.
  • ./gradlew :app:testDebugUnitTest — passes.
  • Not exercised on a device (no device attached during development). The manual check is: webtoon layout, upscale on, slow scroll, watch a page switch from source to 2x — the strip should not move and the old bitmap should stay on screen until the sharp one is ready.

Remaining (deliberately out of scope)

  • The pre-decode placeholder still uses page.width/height: if the server metadata ratio differs from the real bitmap, a cell's first settle still nudges the height. That is the pre-existing design trade-off, independent of upscaling.
  • lastRatio / lastPainter are composition-local; scrolling far enough to recycle the item resets them, so a revisit re-settles from metadata once. Hoisting a reader-level per-URL ratio cache would remove that, but it needs its own decision on what to render before the first decode.

PageImage derived its box ratio from the current AsyncImagePainter state, so while the enhanced request was in Loading the ratio fell back to page.width/height (or the 0.7 default when metadata is stale/missing). In a webtoon strip the LazyColumn item is measured with an infinite height and sized by that ratio, so the fallback resized the item for a frame and moved the scroll anchor — the view jumped as the 2x bitmap replaced the source.

The ratio the cell actually decoded at is now remembered per page/half and reused across model swaps; first loads still guess from metadata. The existing lastPainter fallback keeps the frame's content, so geometry and pixels are both unchanged during the swap.
@dyphire
dyphire merged commit 783ca10 into main Nov 28, 2026
1 check passed
@dyphire
dyphire deleted the fix/upscale-swap-ratio branch November 28, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant