Move all density unit calculation to the same place.
This commit is contained in:
parent
d3d26094bd
commit
8cdcaba9e3
5 changed files with 39 additions and 34 deletions
|
|
@ -37,7 +37,7 @@ import coil.compose.AsyncImage
|
|||
import io.element.android.libraries.designsystem.preview.ElementThemedPreview
|
||||
import io.element.android.libraries.designsystem.preview.PreviewGroup
|
||||
import io.element.android.libraries.designsystem.preview.debugPlaceholderAvatar
|
||||
import io.element.android.libraries.designsystem.text.textDp
|
||||
import io.element.android.libraries.designsystem.text.toSp
|
||||
import io.element.android.libraries.designsystem.theme.components.Text
|
||||
import io.element.android.libraries.theme.AvatarGradientEnd
|
||||
import io.element.android.libraries.theme.AvatarGradientStart
|
||||
|
|
@ -103,7 +103,7 @@ private fun InitialsAvatar(
|
|||
Text(
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
text = avatarData.initial,
|
||||
fontSize = ((avatarData.size.dp / 2).value).toInt().textDp,
|
||||
fontSize = avatarData.size.dp.toSp() / 2,
|
||||
color = Color.White,
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,13 +18,30 @@ package io.element.android.libraries.designsystem.text
|
|||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.unit.Density
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.TextUnit
|
||||
import androidx.compose.ui.unit.dp
|
||||
|
||||
private fun Int.textDp(density: Density): TextUnit = with(density) {
|
||||
this@textDp.dp.toSp()
|
||||
}
|
||||
/**
|
||||
* Convert Dp to Sp, regarding current density.
|
||||
* Can be used for instance to use Dp unit for text.
|
||||
*/
|
||||
@Composable
|
||||
fun Dp.toSp(): TextUnit = with(LocalDensity.current) { toSp() }
|
||||
|
||||
val Int.textDp: TextUnit
|
||||
@Composable get() = this.textDp(density = LocalDensity.current)
|
||||
/**
|
||||
* Convert Px value to Dp, regarding current density.
|
||||
*/
|
||||
@Composable
|
||||
fun Int.toDp(): Dp = with(LocalDensity.current) { toDp() }
|
||||
|
||||
/**
|
||||
* Convert Dp value to pixels, regarding current density.
|
||||
*/
|
||||
@Composable
|
||||
fun Dp.toPx(): Float = with(LocalDensity.current) { toPx() }
|
||||
|
||||
/**
|
||||
* Convert Dp value to pixels, regarding current density.
|
||||
*/
|
||||
@Composable
|
||||
fun Dp.roundToPx(): Int = with(LocalDensity.current) { roundToPx() }
|
||||
|
|
@ -36,11 +36,11 @@ import androidx.compose.runtime.setValue
|
|||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.layout.Layout
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import io.element.android.libraries.designsystem.preview.ElementPreviewDark
|
||||
import io.element.android.libraries.designsystem.preview.ElementPreviewLight
|
||||
import io.element.android.libraries.designsystem.text.toPx
|
||||
import io.element.android.libraries.matrix.api.user.MatrixUser
|
||||
import kotlinx.collections.immutable.ImmutableList
|
||||
import kotlinx.collections.immutable.toImmutableList
|
||||
|
|
@ -76,8 +76,8 @@ fun SelectedUsersList(
|
|||
// users, the last visible user will be precisely half visible. This gives an obvious affordance that there are more entries and the list can be scrolled.
|
||||
// For efficiency, we assume that all the children are the same width. If they needed to be different sizes we'd have to do this calculation each time
|
||||
// they needed to be measured.
|
||||
val minimumSpacing = with(LocalDensity.current) { 24.dp.toPx() }
|
||||
val userWidth = with(LocalDensity.current) { 56.dp.toPx() }
|
||||
val minimumSpacing = 24.dp.toPx()
|
||||
val userWidth = 56.dp.toPx()
|
||||
val userSpacing by remember {
|
||||
derivedStateOf {
|
||||
if (rowWidth == 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue