Skip to content

Commit

Permalink
Merge pull request #728 from microsoft/joypal/scrimFixes
Browse files Browse the repository at this point in the history
[Bottom Sheet v2] Scrim fixes
  • Loading branch information
joyeeta26 authored Nov 14, 2024
2 parents 5f54500 + e72f5c9 commit 95a1fec
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class V2BottomSheetActivity : V2DemoActivity() {

@Composable
private fun CreateActivityUI() {
var scrimVisible by rememberSaveable { mutableStateOf(true) }
var scrimVisible by rememberSaveable { mutableStateOf(false) }

var enableSwipeDismiss by remember { mutableStateOf(true) }

Expand Down Expand Up @@ -429,14 +429,13 @@ private fun CreateActivityUI() {
style = ButtonStyle.Button,
size = ButtonSize.Medium,
text = "+ 8 dp",
enabled = hidden,
onClick = { peekHeightState += 8.dp })

Button(
style = ButtonStyle.Button,
size = ButtonSize.Medium,
text = "- 8 dp",
enabled = hidden && (peekHeightState > 0.dp),
enabled = peekHeightState > 0.dp,
onClick = { peekHeightState -= 8.dp })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fun BottomSheet(
sheetState: BottomSheetState = rememberBottomSheetState(BottomSheetValue.Hidden),
expandable: Boolean = true,
peekHeight: Dp = 110.dp,
scrimVisible: Boolean = true,
scrimVisible: Boolean = false,
showHandle: Boolean = true,
slideOver: Boolean = true,
enableSwipeDismiss: Boolean = false,
Expand Down Expand Up @@ -277,39 +277,42 @@ fun BottomSheet(
}
}) {
content()
Scrim(color = if (scrimVisible) scrimColor else Color.Transparent,
onClose = {
if (sheetState.confirmStateChange(BottomSheetValue.Hidden)) {
scope.launch { sheetState.hide() }
}
},
fraction = {
if (sheetState.anchors.isEmpty() || (sheetHeightState.value != null && sheetHeightState.value == 0f)) {
0.toFloat()
} else {
val targetValue: BottomSheetValue = if (slideOver) {
if (sheetState.anchors.entries.firstOrNull { it.value == BottomSheetValue.Expanded } != null) {
BottomSheetValue.Expanded
} else if (sheetState.anchors.entries.firstOrNull { it.value == BottomSheetValue.Shown } != null) {
BottomSheetValue.Shown
if (scrimVisible) {
Scrim(
color = scrimColor,
onClose = {
if (sheetState.confirmStateChange(BottomSheetValue.Hidden)) {
scope.launch { sheetState.hide() }
}
},
fraction = {
if (sheetState.anchors.isEmpty() || (sheetHeightState.value != null && sheetHeightState.value == 0f)) {
0.toFloat()
} else {
val targetValue: BottomSheetValue = if (slideOver) {
if (sheetState.anchors.entries.firstOrNull { it.value == BottomSheetValue.Expanded } != null) {
BottomSheetValue.Expanded
} else if (sheetState.anchors.entries.firstOrNull { it.value == BottomSheetValue.Shown } != null) {
BottomSheetValue.Shown
} else {
BottomSheetValue.Hidden
}
} else {
BottomSheetValue.Hidden
BottomSheetValue.Shown
}
} else {
BottomSheetValue.Shown
calculateFraction(
sheetState.anchors.entries.firstOrNull { it.value == BottomSheetValue.Hidden }?.key!!,
sheetState.anchors.entries.firstOrNull { it.value == targetValue }?.key!!,
sheetState.offset.value
)
}
calculateFraction(
sheetState.anchors.entries.firstOrNull { it.value == BottomSheetValue.Hidden }?.key!!,
sheetState.anchors.entries.firstOrNull { it.value == targetValue }?.key!!,
sheetState.offset.value
)
}
},
open = sheetState.isVisible,
onScrimClick = onDismiss,
preventDismissalOnScrimClick = preventDismissalOnScrimClick,
tag = BOTTOMSHEET_SCRIM_TAG
)
},
open = sheetState.isVisible,
onScrimClick = onDismiss,
preventDismissalOnScrimClick = preventDismissalOnScrimClick,
tag = BOTTOMSHEET_SCRIM_TAG
)
}
}
val configuration = LocalConfiguration.current

Expand Down Expand Up @@ -444,46 +447,46 @@ fun BottomSheet(
},
tint = sheetHandleColor,
modifier = Modifier.clickable(
enabled = sheetState.hasExpandedState,
role = Role.Button,
onClickLabel = if (sheetState.currentValue == BottomSheetValue.Expanded) {
LocalContext.current.resources.getString(R.string.collapse)
} else {
if (sheetState.hasExpandedState && sheetState.isVisible) LocalContext.current.resources.getString(
R.string.expand
) else null
}
) {
if (sheetState.currentValue == BottomSheetValue.Expanded) {
if (sheetState.confirmStateChange(BottomSheetValue.Shown)) {
scope.launch { sheetState.show() }
accessibilityManager?.let { manager ->
if (manager.isEnabled) {
val event =
AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT)
.apply {
text.add(collapsed)
}
manager.sendAccessibilityEvent(event)
}
enabled = sheetState.hasExpandedState,
role = Role.Button,
onClickLabel = if (sheetState.currentValue == BottomSheetValue.Expanded) {
LocalContext.current.resources.getString(R.string.collapse)
} else {
if (sheetState.hasExpandedState && sheetState.isVisible) LocalContext.current.resources.getString(
R.string.expand
) else null
}
) {
if (sheetState.currentValue == BottomSheetValue.Expanded) {
if (sheetState.confirmStateChange(BottomSheetValue.Shown)) {
scope.launch { sheetState.show() }
accessibilityManager?.let { manager ->
if (manager.isEnabled) {
val event =
AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT)
.apply {
text.add(collapsed)
}
manager.sendAccessibilityEvent(event)
}
}
} else if (sheetState.hasExpandedState) {
if (sheetState.confirmStateChange(BottomSheetValue.Expanded)) {
scope.launch { sheetState.expand() }
accessibilityManager?.let { manager ->
if (manager.isEnabled) {
val event =
AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT)
.apply {
text.add(expanded)
}
manager.sendAccessibilityEvent(event)
}
}
} else if (sheetState.hasExpandedState) {
if (sheetState.confirmStateChange(BottomSheetValue.Expanded)) {
scope.launch { sheetState.expand() }
accessibilityManager?.let { manager ->
if (manager.isEnabled) {
val event =
AccessibilityEvent.obtain(AccessibilityEvent.TYPE_ANNOUNCEMENT)
.apply {
text.add(expanded)
}
manager.sendAccessibilityEvent(event)
}
}
}
})
}
})
}
}
Column(modifier = Modifier
Expand Down

0 comments on commit 95a1fec

Please sign in to comment.