Skip to content

Commit

Permalink
add align modifier to scaffold body
Browse files Browse the repository at this point in the history
  • Loading branch information
onebone committed Jan 23, 2022
1 parent 76e19ed commit f55d946
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/src/main/java/me/onebone/toolbar/CollapsingToolbarScaffold.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.ParentDataModifier
import androidx.compose.ui.unit.Density
import kotlin.math.max

@Stable
Expand Down Expand Up @@ -70,6 +73,11 @@ fun rememberCollapsingToolbarScaffoldState(
}
}

interface CollapsingToolbarScaffoldScope {
@ExperimentalToolbarApi
fun Modifier.align(alignment: Alignment): Modifier
}

@Composable
fun CollapsingToolbarScaffold(
modifier: Modifier,
Expand All @@ -78,7 +86,7 @@ fun CollapsingToolbarScaffold(
enabled: Boolean = true,
toolbarModifier: Modifier = Modifier,
toolbar: @Composable CollapsingToolbarScope.() -> Unit,
body: @Composable () -> Unit
body: @Composable CollapsingToolbarScaffoldScope.() -> Unit
) {
val flingBehavior = ScrollableDefaults.flingBehavior()

Expand All @@ -96,7 +104,7 @@ fun CollapsingToolbarScaffold(
) {
toolbar()
}
body()
CollapsingToolbarScaffoldScopeInstance.body()
},
modifier = modifier
.then(
Expand Down Expand Up @@ -146,3 +154,21 @@ fun CollapsingToolbarScaffold(
}
}
}

internal object CollapsingToolbarScaffoldScopeInstance: CollapsingToolbarScaffoldScope {
@ExperimentalToolbarApi
override fun Modifier.align(alignment: Alignment): Modifier =
this.then(ScaffoldChildAlignmentModifier(alignment))
}

private class ScaffoldChildAlignmentModifier(
private val alignment: Alignment
) : ParentDataModifier {
override fun Density.modifyParentData(parentData: Any?): Any {
return (parentData as? ScaffoldParentData) ?: ScaffoldParentData(alignment)
}
}

data class ScaffoldParentData(
var alignment: Alignment? = null
)

0 comments on commit f55d946

Please sign in to comment.