oleksandrbalan / minabox

Lazy box library for Jetpack Compose, which allows to display lazy loaded items on the 2D plane.
Apache License 2.0
292 stars 18 forks source link

Please expose viewport's size in a MinaBoxState #18

Closed snakemr closed 6 months ago

snakemr commented 6 months ago

Desktop Platform of Compose Multiplatform has buil-in VerticalScrollbar and HorizontalScrollbar with their ScrollbarAdapter. I adopted it for minabox: https://gist.github.com/snakemr/fe054fc4ac1b27c3f7738638ce5ef859 usage:

    val state = rememberLazyTableState()
    BoxWithConstraints {
        LazyTable(state = state) {
            //...
        }
        VerticalScrollbar(
            rememberScrollbarAdapter(state, maxHeight, Orientation.Vertical),
            Modifier.align(Alignment.TopEnd).fillMaxHeight()
        )
        HorizontalScrollbar(
            rememberScrollbarAdapter(state, maxWidth, Orientation.Horizontal),
            Modifier.align(Alignment.BottomStart).fillMaxWidth()
        )
    }

Please, expose viewport's size in a MinaBoxState, so one do not have to set maxWidth/maxHeight manually.

oleksandrbalan commented 6 months ago

I have added viewport's size to the Translate class in this PR #21.

It is available in 1.7.1 release.

Also I have checked your snippet and this looked strange to me 🤔

delay(10)
cancel()

I guess this causes de-sync when scrollbar is dragged quickly, but it is needed because without it everything get "stuck" when scrollbar is dragged during translation animation from fling. I have played around with it a little bit and looks like with NonCancelable context it works correctly and does not get "stuck" when dragged during fling animation.

coroutineScope {
    launch(NonCancellable) {
        if (orientation == Orientation.Vertical)
            boxState.snapTo(translate.x, scrollOffset.toFloat())
        else
            boxState.snapTo(scrollOffset.toFloat(), translate.y)
    }
}

Just my two cents 😸