Move all density unit calculation to the same place.
This commit is contained in:
parent
77432ef1a7
commit
ee3e2108a0
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() }
|
||||
Loading…
Add table
Add a link
Reference in a new issue