Skip to content

Commit

Permalink
Set systemGestureExclusionRects when PopupProperties#excludeFromSyste…
Browse files Browse the repository at this point in the history
…mGesture is enabled
  • Loading branch information
saket committed Oct 4, 2023
1 parent 4084362 commit 5772605
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cascade-compose/src/main/java/me/saket/cascade/Cascade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ fun CascadeDropdownMenu(
clickableWithoutRipple(onClick = onDismissRequest)
},
positionProvider = popupPositionProvider,
anchorBounds = anchorBounds
anchorBounds = anchorBounds,
properties = properties,
) {
PopupContent(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package me.saket.cascade.internal

import android.os.Build
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.absoluteOffset
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 @@ -19,19 +21,33 @@ import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.round
import androidx.compose.ui.unit.toOffset
import androidx.compose.ui.window.PopupPositionProvider
import androidx.compose.ui.window.PopupProperties
import kotlin.math.roundToInt
import android.graphics.Rect as AndroidRect

@Composable
@Suppress("NAME_SHADOWING")
internal fun PositionPopupContent(
modifier: Modifier = Modifier,
positionProvider: PopupPositionProvider,
anchorBounds: ScreenRelativeBounds?,
properties: PopupProperties,
content: @Composable () -> Unit
) {
val popupView = LocalView.current
val layoutDirection = LocalLayoutDirection.current
var contentPosition: IntOffset? by remember { mutableStateOf(null) }

if (Build.VERSION.SDK_INT >= 29 && properties.excludeFromSystemGesture) {
contentPosition?.let { contentPosition ->
LaunchedEffect(contentPosition) {
popupView.systemGestureExclusionRects = mutableListOf(
AndroidRect(0, 0, contentPosition.x, contentPosition.y)
)
}
}
}

Box(modifier) {
Box(
Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.runtime.Immutable
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInRoot
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.findRootCoordinates
import androidx.compose.ui.layout.positionInRoot
Expand Down Expand Up @@ -37,6 +38,10 @@ internal fun ScreenRelativeBounds(coordinates: LayoutCoordinates, owner: View):
size = coordinates.size.toSize()
),
root = RootLayoutCoordinatesInfo(
// material3 uses View#getWindowVisibleDisplayFrame() for calculating window size,
// but that produces infinite-like values for windows that have FLAG_LAYOUT_NO_LIMITS
// set (source: WindowLayout.java). material3 ends up looking okay because WindowManager
// sanitizes bad values.
layoutBoundsInWindow = coordinates.findRootCoordinates().boundsInWindow(),
windowPositionOnScreen = run {
owner.rootView.getLocationOnScreen(intArrayBuffer)
Expand All @@ -46,7 +51,7 @@ internal fun ScreenRelativeBounds(coordinates: LayoutCoordinates, owner: View):
)
}

// I do not expect this to be shared across threads to need any synchronization.
// I don't expect this to be shared across threads to need any synchronization.
private val intArrayBuffer = IntArray(size = 2)

/**
Expand Down

0 comments on commit 5772605

Please sign in to comment.