-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snapping #16
base: master
Are you sure you want to change the base?
Snapping #16
Changes from all commits
ab42e1b
6a8db15
3bad446
68f16b8
14acae5
ef7cbdf
f2842b7
5ba771d
241ccab
a9472a6
3d27659
2a534d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,22 +30,11 @@ import androidx.compose.foundation.MutatePriority | |
import androidx.compose.foundation.gestures.FlingBehavior | ||
import androidx.compose.foundation.gestures.ScrollScope | ||
import androidx.compose.foundation.gestures.ScrollableState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Stable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clipToBounds | ||
import androidx.compose.ui.layout.Layout | ||
import androidx.compose.ui.layout.Measurable | ||
import androidx.compose.ui.layout.MeasurePolicy | ||
import androidx.compose.ui.layout.MeasureResult | ||
import androidx.compose.ui.layout.MeasureScope | ||
import androidx.compose.ui.layout.ParentDataModifier | ||
import androidx.compose.ui.layout.Placeable | ||
import androidx.compose.ui.layout.* | ||
import androidx.compose.ui.unit.Constraints | ||
import androidx.compose.ui.unit.Density | ||
import androidx.compose.ui.unit.IntSize | ||
|
@@ -57,7 +46,7 @@ import kotlin.math.roundToInt | |
|
||
@Stable | ||
class CollapsingToolbarState( | ||
initial: Int = Int.MAX_VALUE | ||
initial: Int = CollapsingToolbarDefaults.InitialHeight | ||
): ScrollableState { | ||
/** | ||
* [height] indicates current height of the toolbar. | ||
|
@@ -136,8 +125,23 @@ class CollapsingToolbarState( | |
) | ||
fun feedScroll(value: Float): Float = dispatchRawDelta(value) | ||
|
||
/** | ||
* @return Remaining velocity after fling | ||
*/ | ||
suspend fun fling(flingBehavior: FlingBehavior, velocity: Float): Float { | ||
var left = velocity | ||
scroll { | ||
with(flingBehavior) { | ||
left = performFling(left) | ||
} | ||
} | ||
|
||
return left | ||
} | ||
|
||
// TODO: A strange jump in snap speed is often observed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain this issue? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bug I mentioned. You can see a strange expanding before the snap will applied. This sometimes works even in wrong direction. Look at the demonstration GIF: When I release the finger you can see that snapping is moving in wrong direction (to top) before it finally snap to full size. I tried to debug it and I suppose that a bug is in implementation of |
||
@ExperimentalToolbarApi | ||
suspend fun expand(duration: Int = 200) { | ||
suspend fun expand(duration: Int = CollapsingToolbarDefaults.ExpandDuration) { | ||
val anim = AnimationState(height.toFloat()) | ||
|
||
scroll { | ||
|
@@ -149,8 +153,9 @@ class CollapsingToolbarState( | |
} | ||
} | ||
|
||
// TODO: A strange jump in snap speed is often observed | ||
@ExperimentalToolbarApi | ||
suspend fun collapse(duration: Int = 200) { | ||
suspend fun collapse(duration: Int = CollapsingToolbarDefaults.CollapseDuration) { | ||
val anim = AnimationState(height.toFloat()) | ||
|
||
scroll { | ||
|
@@ -162,20 +167,6 @@ class CollapsingToolbarState( | |
} | ||
} | ||
|
||
/** | ||
* @return Remaining velocity after fling | ||
*/ | ||
suspend fun fling(flingBehavior: FlingBehavior, velocity: Float): Float { | ||
var left = velocity | ||
scroll { | ||
with(flingBehavior) { | ||
left = performFling(left) | ||
} | ||
} | ||
|
||
return left | ||
} | ||
|
||
override val isScrollInProgress: Boolean | ||
get() = scrollableState.isScrollInProgress | ||
|
||
|
@@ -189,7 +180,7 @@ class CollapsingToolbarState( | |
|
||
@Composable | ||
fun rememberCollapsingToolbarState( | ||
initial: Int = Int.MAX_VALUE | ||
initial: Int = CollapsingToolbarDefaults.InitialHeight | ||
): CollapsingToolbarState { | ||
return remember { | ||
CollapsingToolbarState( | ||
|
@@ -216,6 +207,13 @@ fun CollapsingToolbar( | |
) | ||
} | ||
|
||
object CollapsingToolbarDefaults { | ||
const val InitialHeight = Int.MAX_VALUE | ||
const val Edge = 0.5f | ||
const val ExpandDuration = 200 | ||
const val CollapseDuration = 200 | ||
} | ||
|
||
private class CollapsingToolbarMeasurePolicy( | ||
private val collapsingToolbarState: CollapsingToolbarState | ||
): MeasurePolicy { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert contract(i.e. parameter or behavior) changes in
AppBarContainer.kt
as it is in maintenance mode and thus should not be changed until it is removed!