playback: partially reduce pager choppiness

This commit is contained in:
Alexander Capehart
2026-07-05 22:59:00 -06:00
parent ab5341f81c
commit d466c934be
2 changed files with 82 additions and 4 deletions
@@ -296,6 +296,46 @@ class PlaybackPanelFragment :
}
private fun updatePager(queue: PagerQueue) {
// Right now there's easily 140ms of frame skipping when going next/prev. This is primarily
// the fault of specifically the nested bottom sheet UI setup, which is intractable to
// optimize. If I don't do multiple remeasures/relayouts on every slightest state instability
// I will suddenly encounter insane issues where the sheet fails to measure, appears below
// the sidebar, flies away, not changing with ui scale, etc, often only on third-party OEM
// ROMs that randomly mangle SDK APIs and the SystemUI chrome for no reason.
//
// Historically this was not an issue, as I did not animate next/prev. Now I do, and it's
// highly noticeable. So at least for plain next/prev I have to hack around it, do not
// execute any transition until the state has fully adjudicated and laid out the UI. It's
// not effective for swiping but there's nothing I can do there.
//
// Eventually one day Claude Fable 6.7 will probably be able to figure out that you need to
// reflect into System.FoobaCrumbo::beegieConnector(GoolaUtils.PlubBud) and call it
// specifically with 0x189B31FA alongside disabling the AndroidX Helpo SuperCharge by
// manually clobbering `BottomSheetM2InternalBoogieCompat::scrimbloManager` to null for it
// to not actually randomly mangle the sheets and do it in 1 clean layout, but for now I
// must do this to keep my sanity.
//
// Actual snippet here was codex, just cleaned & adapted it / cognitive ownership
requireBinding().playbackPager.apply {
if (!isAttachedToWindow) {
post { updatePagerImpl(queue) }
return
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && isHardwareAccelerated) {
// New version using post-Q frame hooks
viewTreeObserver.registerFrameCommitCallback {
post { postOnAnimation { updatePagerImpl(queue) } }
}
postInvalidateOnAnimation()
} else {
// Let current layout happen, then wait for the next to conclude
postOnAnimation { postOnAnimation { updatePagerImpl(queue) } }
}
}
}
private fun updatePagerImpl(queue: PagerQueue) {
val binding = requireBinding()
val command = playbackModel.pagerCommand.consume()
@@ -325,10 +365,14 @@ class PlaybackPanelFragment :
// user scroll, carry on
return
}
binding.playbackPager.setCurrentItem(
command.scroll,
command.update == null && abs(delta) == 1,
)
if (command.update == null && abs(delta) == 1) {
binding.playbackPager.smoothScrollByPageTo(command.scroll)
} else {
binding.playbackPager.setCurrentItem(
command.scroll,
false,
)
}
}
}
@@ -156,8 +156,42 @@ fun ViewPager2.dampen() {
RV_TOUCH_SLOP_FIELD.set(recycler, slop * 3)
}
/**
* Reflect into a [ViewPager2]'s internal [RecyclerView].
*/
fun ViewPager2.recycler() = (VP_RECYCLER_FIELD.get(this) as RecyclerView)
/**
* Move to [item] using smooth drag gestures instead of the RecyclerView's scroll interpolator,
* which is far faster/choppier/blunt and doesn't look as good at all.
*
* Proposed by Codex, cleaned up / cognitive ownership by me
*/
fun ViewPager2.smoothScrollByPageTo(item: Int, durationMs: Int = 300) {
// Nothing to actually do if there's no data.
val adapter = adapter ?: return
if (adapter.itemCount <= 0) {
return
}
// Actually figure out how long we have to
// coerceIn is codex defensiveness but honestly I'd rather just do nothing
// if we go OOB, makes index logic easier.
val target = item.coerceIn(0, adapter.itemCount - 1)
val delta = target - currentItem
if (delta == 0) {
return
}
val recycler = recycler()
recycler.stopScroll()
// Note: Assumption is horizonal ViewPager, less logic to manage.
// TODO: Fix ViewPager2 RTL Support
val direction = if (isRtl) -1 else 1
recycler.smoothScrollBy(width * delta * direction, 0, null, durationMs)
}
/**
* Crash-safe wrapped around [NavController.navigate] that will not crash if multiple destinations
* are selected at once.