Skip to content

Fix missed implementation for DrawerPredictiveBackHandler #1763

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

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,81 @@

package androidx.compose.material3

import androidx.compose.animation.core.animate
import androidx.compose.material3.internal.BackEventCompat
import androidx.compose.material3.internal.PredictiveBack
import androidx.compose.material3.internal.PredictiveBackHandler
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.launch

// TODO https://youtrack.jetbrains.com/issue/COMPOSE-1301/Implement-DrawerPredictiveBackHandler

// This is a copy-paste from NavigationDrawer.android.kt
// TODO: Remove expect/actual and move this code to common
@Composable
internal actual fun DrawerPredictiveBackHandler(
drawerState: DrawerState,
content: @Composable (DrawerPredictiveBackState) -> Unit
) {
}
val drawerPredictiveBackState = remember { DrawerPredictiveBackState() }
val scope = rememberCoroutineScope()
val isRtl = LocalLayoutDirection.current == LayoutDirection.Rtl
val maxScaleXDistanceGrow: Float
val maxScaleXDistanceShrink: Float
val maxScaleYDistance: Float
with(LocalDensity.current) {
maxScaleXDistanceGrow = PredictiveBackDrawerMaxScaleXDistanceGrow.toPx()
maxScaleXDistanceShrink = PredictiveBackDrawerMaxScaleXDistanceShrink.toPx()
maxScaleYDistance = PredictiveBackDrawerMaxScaleYDistance.toPx()
}

PredictiveBackHandler(enabled = drawerState.isOpen) { progress ->
try {
progress.collect { backEvent ->
drawerPredictiveBackState.update(
PredictiveBack.transform(backEvent.progress),
backEvent.swipeEdge == BackEventCompat.EDGE_LEFT,
isRtl,
maxScaleXDistanceGrow,
maxScaleXDistanceShrink,
maxScaleYDistance
)
}
} catch (e: CancellationException) {
drawerPredictiveBackState.clear()
} finally {
if (drawerPredictiveBackState.swipeEdgeMatchesDrawer) {
// If swipe edge matches drawer gravity and we've stretched the drawer horizontally,
// un-stretch it smoothly so that it hides completely during the drawer close.
scope.launch {
animate(
initialValue = drawerPredictiveBackState.scaleXDistance,
targetValue = 0f
) { value, _ ->
drawerPredictiveBackState.scaleXDistance = value
}
drawerPredictiveBackState.clear()
}
}
drawerState.close()
}
}

LaunchedEffect(drawerState.isClosed) {
if (drawerState.isClosed) {
drawerPredictiveBackState.clear()
}
}

content(drawerPredictiveBackState)
}

internal val PredictiveBackDrawerMaxScaleXDistanceGrow = 12.dp
internal val PredictiveBackDrawerMaxScaleXDistanceShrink = 24.dp
internal val PredictiveBackDrawerMaxScaleYDistance = 48.dp