Skip to content

Forms: DatePickerDialog scaling #198

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 5 commits into from
Nov 8, 2023
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 @@ -18,6 +18,7 @@

package com.arcgismaps.toolkit.featureforms.components.datetime.picker

import android.content.res.Configuration
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
Expand All @@ -26,6 +27,7 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -50,8 +52,11 @@ import androidx.compose.runtime.key
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.scale
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -325,9 +330,10 @@ private fun DateTimePickerDialog(
) {
Surface(
modifier = Modifier
.padding(start = 25.dp, end = 25.dp)
.widthIn(DateTimePickerDialogTokens.containerWidth)
.height(DateTimePickerDialogTokens.containerHeight),
.padding(horizontal = DateTimePickerDialogTokens.horizontalPadding)
.widthWithOrientation(DateTimePickerDialogTokens.containerWidth)
.height(DateTimePickerDialogTokens.containerHeight)
.scaleIfNarrow(DateTimePickerDialogTokens.containerWidth + DateTimePickerDialogTokens.horizontalPadding * 2),
shape = shape,
color = MaterialTheme.colorScheme.surface,
tonalElevation = tonalElevation,
Expand All @@ -349,6 +355,33 @@ private fun DateTimePickerDialog(
private object DateTimePickerDialogTokens {
val containerHeight = 600.0.dp
val containerWidth = 360.0.dp
val horizontalPadding = 25.dp
}

/**
* Constraints the width of the content based on the orientation and the [width]. If the
* current orientation is portrait, [Modifier.requiredWidth] used. If it is landscape then
* [Modifier.widthIn] is used. This is useful when different layouts are needed in portrait
* and landscape orientations.
*/
internal fun Modifier.widthWithOrientation(width: Dp) : Modifier = composed {
val configuration = LocalConfiguration.current
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
this.widthIn(width)
} else {
this.requiredWidth(width)
}
}

/**
* Scales the content appropriately if the current screen width is less than the [minWidth].
*/
internal fun Modifier.scaleIfNarrow(minWidth: Dp): Modifier = composed {
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val scale = if (screenWidth < minWidth)
screenWidth / minWidth
else 1f
this.scale(scale)
}

@Preview
Expand Down