Skip to content

Commit

Permalink
Limit max Dialog and Popup size by safe area on iOS (#732)
Browse files Browse the repository at this point in the history
* Limit max dialog size by safe area

* Add TODO

* Add applyPlatformConstrains

* Extract shared calculations to RootMeasurePolicy

* Add usePlatformDefaultWidth parameter to PopupProperties

* Add unrestricted Dialog size sample

* Fix formatting

* Add doc and equals/hashCode for usePlatformDefaultWidth
  • Loading branch information
MatkovIvan committed Aug 8, 2023
1 parent a4bc29d commit bdb02dc
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.IntOffset
Expand All @@ -28,6 +29,7 @@ import androidx.compose.ui.window.DialogProperties
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun PopupAndDialog() {
Column(Modifier.padding(5.dp)) {
Expand All @@ -40,6 +42,13 @@ fun PopupAndDialog() {
modifier = Modifier.fillMaxSize(),
text = "Dialog: max size"
)
DialogSample(
modifier = Modifier.fillMaxSize(),
text = "Dialog: max size (unrestricted)",
properties = DialogProperties(
usePlatformDefaultWidth = false
)
)
}
}

Expand Down Expand Up @@ -134,15 +143,16 @@ private fun MyPopup(
}

@Composable
private fun DialogSample(modifier: Modifier = Modifier, text: String = "Dialog") {
private fun DialogSample(
modifier: Modifier = Modifier,
text: String = "Dialog",
properties: DialogProperties = DialogProperties()
) {
var showDialog by remember { mutableStateOf(false) }
if (showDialog) {
Dialog(
onDismissRequest = { showDialog = false },
properties = DialogProperties(
dismissOnBackPress = true,
dismissOnClickOutside = true
)
properties = properties
) {
Surface(
modifier = modifier,
Expand Down
5 changes: 4 additions & 1 deletion compose/ui/ui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
api(libs.skikoCommon)
}
}
notMobileMain.dependsOn(skikoMain)
desktopMain {
dependsOn(skikoMain)
dependsOn(notMobileMain)
dependencies {
implementation(libs.kotlinStdlibJdk8)
}
Expand All @@ -211,6 +212,8 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) {
jsNativeMain.dependsOn(skikoMain)
nativeMain.dependsOn(jsNativeMain)
jsWasmMain.dependsOn(jsNativeMain)
jsWasmMain.dependsOn(notMobileMain)
macosMain.dependsOn(notMobileMain)

jsMain {
dependsOn(jsWasmMain)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.ui.window

import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.IntOffset

@Composable
internal actual fun Density.platformOffset(): IntOffset =
IntOffset.Zero
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,12 @@ import androidx.compose.ui.input.pointer.PointerButton
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.PointerInputEvent
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.MeasureScope
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.dialog
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.center
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastMap
import androidx.compose.ui.util.fastMaxBy
import kotlin.math.min

/**
* The default scrim opacity.
Expand Down Expand Up @@ -157,7 +150,11 @@ private fun DialogLayout(
focusable = true,
onOutsidePointerEvent = onOutsidePointerEvent
) { owner ->
val measurePolicy = rememberDialogMeasurePolicy(properties) {
val density = LocalDensity.current
val measurePolicy = rememberDialogMeasurePolicy(
properties = properties,
platformOffset = with(density) { platformOffset() }
) {
owner.bounds = it
}
Layout(
Expand All @@ -170,45 +167,19 @@ private fun DialogLayout(
@Composable
private fun rememberDialogMeasurePolicy(
properties: DialogProperties,
platformOffset: IntOffset,
onBoundsChanged: (IntRect) -> Unit
) = remember(properties, onBoundsChanged) {
MeasurePolicy { measurables, constraints ->
val dialogConstraints = if (properties.usePlatformDefaultWidth) {
platformDefaultConstrains(constraints)
} else constraints
val placeables = measurables.fastMap { it.measure(dialogConstraints) }
val width = placeables.fastMaxBy { it.width }?.width ?: constraints.minWidth
val height = placeables.fastMaxBy { it.height }?.height ?: constraints.minHeight

val placeableSize = IntSize(width, height)
val windowSize = IntSize(constraints.maxWidth, constraints.maxHeight)
val position = windowSize.center - placeableSize.center
onBoundsChanged(IntRect(position, placeableSize))

layout(windowSize.width, windowSize.height) {
placeables.fastForEach {
it.place(position.x, position.y)
}
}
) = remember(properties, platformOffset, onBoundsChanged) {
RootMeasurePolicy(
platformOffset = platformOffset,
usePlatformDefaultWidth = properties.usePlatformDefaultWidth
) { windowSize, contentSize ->
val position = windowSize.center - contentSize.center
onBoundsChanged(IntRect(position, contentSize))
position
}
}

private fun MeasureScope.platformDefaultConstrains(
constraints: Constraints
): Constraints = constraints.copy(
maxWidth = min(preferredDialogWidth(constraints), constraints.maxWidth)
)

// Ported from Android. See https://cs.android.com/search?q=abc_config_prefDialogWidth
private fun MeasureScope.preferredDialogWidth(constraints: Constraints): Int {
val smallestWidth = min(constraints.maxWidth, constraints.maxHeight).toDp()
return when {
smallestWidth >= 600.dp -> 580.dp
smallestWidth >= 480.dp -> 440.dp
else -> 320.dp
}.roundToPx()
}

private fun PointerInputEvent.isMainAction() =
button == PointerButton.Primary ||
button == null && pointers.size == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEvent
Expand All @@ -33,34 +34,58 @@ import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.pointer.PointerInputEvent
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.MeasurePolicy
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInWindow
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.semantics.popup
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.IntRect
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.round
import androidx.compose.ui.util.fastForEach
import androidx.compose.ui.util.fastMap
import androidx.compose.ui.util.fastMaxBy

/**
* Properties used to customize the behavior of a [Popup].
*
* @property focusable Whether the popup is focusable. When true, the popup will receive IME
* events and key presses, such as when the back button is pressed.
* @property dismissOnBackPress Whether the popup can be dismissed by pressing the back button
* on Android or escape key on desktop.
* If true, pressing the back button will call onDismissRequest. Note that [focusable] must be
* set to true in order to receive key events such as the back button - if the popup is not
* focusable then this property does nothing.
* @property dismissOnClickOutside Whether the popup can be dismissed by clicking outside the
* popup's bounds. If true, clicking outside the popup will call onDismissRequest.
* @property usePlatformDefaultWidth Whether the width of the dialog's content should be limited to
* the platform default, which is smaller than the screen width.
*/
@Immutable
actual class PopupProperties actual constructor(
actual class PopupProperties @ExperimentalComposeUiApi constructor(
actual val focusable: Boolean,
actual val dismissOnBackPress: Boolean,
actual val dismissOnClickOutside: Boolean
actual val dismissOnClickOutside: Boolean,
val usePlatformDefaultWidth: Boolean = false,
) {
actual constructor(
focusable: Boolean,
dismissOnBackPress: Boolean,
dismissOnClickOutside: Boolean
) : this(
focusable = focusable,
dismissOnBackPress = dismissOnBackPress,
dismissOnClickOutside = dismissOnClickOutside,
usePlatformDefaultWidth = false,
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is PopupProperties) return false

if (focusable != other.focusable) return false
if (dismissOnBackPress != other.dismissOnBackPress) return false
if (dismissOnClickOutside != other.dismissOnClickOutside) return false
if (usePlatformDefaultWidth != other.usePlatformDefaultWidth) return false

return true
}
Expand All @@ -69,6 +94,7 @@ actual class PopupProperties actual constructor(
var result = focusable.hashCode()
result = 31 * result + dismissOnBackPress.hashCode()
result = 31 * result + dismissOnClickOutside.hashCode()
result = 31 * result + usePlatformDefaultWidth.hashCode()
return result
}
}
Expand Down Expand Up @@ -362,16 +388,19 @@ private fun PopupLayout(
onOutsidePointerEvent: ((PointerInputEvent) -> Unit)? = null,
content: @Composable () -> Unit
) {
val layoutDirection = LocalLayoutDirection.current
var parentBounds by remember { mutableStateOf(IntRect.Zero) }
EmptyLayout(Modifier.parentBoundsInWindow { parentBounds = it })
RootLayout(
modifier = modifier,
focusable = properties.focusable,
onOutsidePointerEvent = onOutsidePointerEvent
) { owner ->
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
val measurePolicy = rememberPopupMeasurePolicy(
popupPositionProvider = popupPositionProvider,
properties = properties,
platformOffset = with(density) { platformOffset() },
layoutDirection = layoutDirection,
parentBounds = parentBounds
) {
Expand All @@ -397,30 +426,21 @@ private fun Modifier.parentBoundsInWindow(
@Composable
private fun rememberPopupMeasurePolicy(
popupPositionProvider: PopupPositionProvider,
properties: PopupProperties,
platformOffset: IntOffset,
layoutDirection: LayoutDirection,
parentBounds: IntRect,
onBoundsChanged: (IntRect) -> Unit
) = remember(popupPositionProvider, layoutDirection, parentBounds, onBoundsChanged) {
MeasurePolicy { measurables, constraints ->
val placeables = measurables.fastMap { it.measure(constraints) }
val width = placeables.fastMaxBy { it.width }?.width ?: constraints.minWidth
val height = placeables.fastMaxBy { it.height }?.height ?: constraints.minHeight

val placeableSize = IntSize(width, height)
val windowSize = IntSize(constraints.maxWidth, constraints.maxHeight)
) = remember(popupPositionProvider, properties, platformOffset, layoutDirection, parentBounds, onBoundsChanged) {
RootMeasurePolicy(
platformOffset = platformOffset,
usePlatformDefaultWidth = properties.usePlatformDefaultWidth
) { windowSize, contentSize ->
val position = popupPositionProvider.calculatePosition(
anchorBounds = parentBounds,
windowSize = windowSize,
layoutDirection = layoutDirection,
popupContentSize = placeableSize
parentBounds, windowSize, layoutDirection, contentSize
)
onBoundsChanged(IntRect(position, placeableSize))

layout(windowSize.width, windowSize.height) {
placeables.fastForEach {
it.place(position.x, position.y)
}
}
onBoundsChanged(IntRect(position, contentSize))
position
}
}

Expand Down

0 comments on commit bdb02dc

Please sign in to comment.