Skip to content

Commit

Permalink
fix: optimized WindowInsets
Browse files Browse the repository at this point in the history
  • Loading branch information
ch4rl3x committed Jun 9, 2024
1 parent 3e58192 commit 8a5535f
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bottom-drawer-scaffold/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ apply from: '../buildCompose.gradle'

ext {
PUBLISH_GROUP_ID = 'de.charlex.compose'
PUBLISH_VERSION = '2.0.0-beta03'
PUBLISH_VERSION = '2.0.0-rc01'
PUBLISH_ARTIFACT_ID = 'bottom-drawer-scaffold'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.exclude
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeightIn
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.systemBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.BottomSheetScaffoldState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FabPosition
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SheetState
import androidx.compose.material3.SheetValue
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.contentColorFor
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.material3.rememberStandardBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -36,17 +39,39 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch


@Composable
@ExperimentalMaterial3Api
fun rememberBottomSheetScaffoldState(
bottomSheetState: SheetState = rememberStandardBottomSheetState(),
snackbarHostState: SnackbarHostState = remember { SnackbarHostState() },
onSheetStateChanged: (SheetValue) -> Unit
): BottomSheetScaffoldState {
val state = remember(bottomSheetState, snackbarHostState) {
BottomSheetScaffoldState(
bottomSheetState = bottomSheetState,
snackbarHostState = snackbarHostState
)
}

LaunchedEffect(state.bottomSheetState.currentValue) {
onSheetStateChanged(state.bottomSheetState.currentValue)
}

return state
}

@Composable
@ExperimentalMaterial3Api
fun BottomDrawerScaffold(
modifier: Modifier = Modifier,
contentWindowInsets: WindowInsets = WindowInsets.systemBars.exclude(WindowInsets.statusBars).exclude(WindowInsets.navigationBars),
bottomSheetScaffoldState: BottomSheetScaffoldState = rememberBottomSheetScaffoldState(),
topBar: @Composable (() -> Unit)? = null,
topBar: (@Composable () -> Unit)? = null,
bottomBar: @Composable (() -> Unit)? = null,
gesturesEnabled: Boolean = true,
drawerModifier: Modifier = Modifier,
Expand All @@ -69,7 +94,7 @@ fun BottomDrawerScaffold(
) {
Scaffold(
modifier = modifier,
contentWindowInsets = WindowInsets.navigationBars.exclude(WindowInsets.statusBars),
contentWindowInsets = contentWindowInsets,
topBar = {
topBar?.invoke()
},
Expand All @@ -88,78 +113,78 @@ fun BottomDrawerScaffold(
.padding(scaffoldPaddingValues)
) {
val fullHeight = constraints.maxHeight.toFloat()
val peekHeightPx = with(LocalDensity.current) { drawerPeekHeight.toPx() }
var bottomDrawerHeight by remember { mutableStateOf(fullHeight) }
var initialOffset by remember { mutableStateOf(-1f) }

Surface(
color = backgroundColor,
contentColor = contentColor
) {
Box(Modifier.fillMaxSize()) {
content(PaddingValues(bottom = drawerPeekHeight))

BottomSheetScaffold(
modifier = Modifier,
scaffoldState = bottomSheetScaffoldState,
sheetDragHandle = null,
sheetSwipeEnabled = drawerGesturesEnabled ?: gesturesEnabled,
sheetContainerColor = Color.Transparent,
sheetPeekHeight = drawerPeekHeight + 30.dp,
sheetTonalElevation = 0.dp,
sheetShadowElevation = 0.dp,
snackbarHost = snackbarHost,
sheetContent = {
val topPadding = if (topBar == null) {
WindowInsets.statusBars.asPaddingValues(LocalDensity.current).calculateTopPadding()
} else {
0.dp
}
Surface(
Modifier
.fillMaxWidth()
.requiredHeightIn(
min = drawerPeekHeight
)
.padding(
top = drawerPadding + topPadding,
start = drawerPadding,
end = drawerPadding,
)
.onGloballyPositioned {
bottomDrawerHeight = it.size.height.toFloat()
Scrim(
open = bottomSheetScaffoldState.isExpanded(),
onClose = {
if (gesturesEnabled) {
scope.launch { bottomSheetScaffoldState.collapse() }
}
.then(
drawerModifier
),
shape = drawerShape,
shadowElevation = drawerShadowElevation,
tonalElevation = drawerTonalElevation,
color = drawerBackgroundColor,
contentColor = drawerContentColor,
content = {
Column(
content = {
drawerContent()
}
)
}
},
fraction = {
calculateFraction(initialOffset, 0f, bottomSheetScaffoldState.bottomSheetState.requireOffset())
},
color = drawerScrimColor
)
}
}

Box(
modifier = if(topBar == null) Modifier.windowInsetsPadding(WindowInsets.statusBars) else Modifier
) {
Surface(
color = backgroundColor,
contentColor = contentColor
) {
Box(Modifier.fillMaxSize()) {
content(PaddingValues(bottom = drawerPeekHeight))

Scrim(
open = bottomSheetScaffoldState.isExpanded(),
onClose = {
if (gesturesEnabled) {
scope.launch { bottomSheetScaffoldState.collapse() }
BottomSheetScaffold(
modifier = Modifier,
scaffoldState = bottomSheetScaffoldState,
sheetDragHandle = null,
sheetSwipeEnabled = drawerGesturesEnabled ?: gesturesEnabled,
sheetContainerColor = Color.Transparent,
sheetPeekHeight = drawerPeekHeight,
sheetTonalElevation = 0.dp,
sheetShadowElevation = 0.dp,
snackbarHost = snackbarHost,
sheetContent = {
Surface(
Modifier
.fillMaxWidth()
.padding(
top = drawerPadding,
start = drawerPadding,
end = drawerPadding
)
.onGloballyPositioned {
if(initialOffset == -1f) {
initialOffset = bottomSheetScaffoldState.bottomSheetState.requireOffset()
}
bottomDrawerHeight = it.size.height.toFloat()
}
},
fraction = {
calculateFraction(fullHeight - peekHeightPx, fullHeight - bottomDrawerHeight, bottomSheetScaffoldState.bottomSheetState.requireOffset())
},
color = drawerScrimColor
.then(
drawerModifier
),
shape = drawerShape,
shadowElevation = drawerShadowElevation,
tonalElevation = drawerTonalElevation,
color = drawerBackgroundColor,
contentColor = drawerContentColor,
content = {
Column(
content = {
drawerContent()
}
)
}
)
}
}
},
content = {}
)
}
}
}
Expand Down
Loading

0 comments on commit 8a5535f

Please sign in to comment.