blob: 5b867beaf309c45dfab8b28b7f77b36c71db1878 [file] [log] [blame]
Andrey Kulikovfeb3ee02019-01-23 14:25:29 +00001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Louis Pullen-Freilicha7eeb102020-07-22 17:54:24 +010017package androidx.compose.ui.unit
Andrey Kulikovfeb3ee02019-01-23 14:25:29 +000018
Louis Pullen-Freilich1f10a592020-07-24 16:35:14 +010019import androidx.compose.runtime.Immutable
20import androidx.compose.runtime.Stable
Louis Pullen-Freilichf434a132020-07-22 14:19:24 +010021import androidx.compose.ui.geometry.Rect
George Mountdae1d492021-07-23 22:52:24 +000022import androidx.compose.ui.geometry.Size
Zach Klippensteinfc183192021-10-04 23:31:05 -070023import androidx.compose.ui.geometry.isSpecified
Oleksandr Karpovich6f986482022-06-28 11:13:19 +020024import androidx.compose.ui.unit.internal.JvmDefaultWithCompatibility
Jeff Gastonbcaacc82023-05-09 22:44:28 -040025import kotlin.math.roundToInt
Andrey Kulikovfeb3ee02019-01-23 14:25:29 +000026
27/**
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000028 * A density of the screen. Used for convert [Dp] to pixels.
29 *
30 * @param density The logical density of the display. This is a scaling factor for the [Dp] unit.
31 * @param fontScale Current user preference for the scaling factor for fonts.
32 */
Leland Richardson47df77f2020-05-21 09:15:40 -070033@Stable
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000034fun Density(density: Float, fontScale: Float = 1f): Density =
35 DensityImpl(density, fontScale)
36
37private data class DensityImpl(
38 override val density: Float,
39 override val fontScale: Float
40) : Density
41
42/**
George Mountdae1d492021-07-23 22:52:24 +000043 * A density of the screen. Used for the conversions between pixels, [Dp], [Int] and [TextUnit].
Andrey Kulikov254d58c2019-03-06 12:57:26 +000044 *
Louis Pullen-Freilicha7eeb102020-07-22 17:54:24 +010045 * @sample androidx.compose.ui.unit.samples.WithDensitySample
Andrey Kulikovfeb3ee02019-01-23 14:25:29 +000046 */
Leland Richardson47df77f2020-05-21 09:15:40 -070047@Immutable
Adam Powell289a76f2022-06-01 14:53:23 -070048@JvmDefaultWithCompatibility
Tyler Freemand1b43ca2023-08-10 17:28:22 +010049interface Density : FontScalable {
Andrey Kulikov254d58c2019-03-06 12:57:26 +000050
51 /**
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000052 * The logical density of the display. This is a scaling factor for the [Dp] unit.
Andrey Kulikov254d58c2019-03-06 12:57:26 +000053 */
Leland Richardson47df77f2020-05-21 09:15:40 -070054 @Stable
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000055 val density: Float
Andrey Kulikov254d58c2019-03-06 12:57:26 +000056
57 /**
Nader Jawadfeb99f82020-05-21 13:07:36 -070058 * Convert [Dp] to pixels. Pixels are used to paint to Canvas.
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000059 */
Leland Richardson47df77f2020-05-21 09:15:40 -070060 @Stable
Nader Jawadfeb99f82020-05-21 13:07:36 -070061 fun Dp.toPx(): Float = value * density
Andrey Kulikov254d58c2019-03-06 12:57:26 +000062
63 /**
George Mount8f237572020-04-30 12:08:30 -070064 * Convert [Dp] to [Int] by rounding
Andrey Kulikov254d58c2019-03-06 12:57:26 +000065 */
Leland Richardson47df77f2020-05-21 09:15:40 -070066 @Stable
Mihai Popa217bf492021-01-25 23:55:14 +000067 fun Dp.roundToPx(): Int {
George Mount8f237572020-04-30 12:08:30 -070068 val px = toPx()
69 return if (px.isInfinite()) Constraints.Infinity else px.roundToInt()
70 }
Andrey Kulikov254d58c2019-03-06 12:57:26 +000071
72 /**
Nader Jawadfeb99f82020-05-21 13:07:36 -070073 * Convert Sp to pixels. Pixels are used to paint to Canvas.
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000074 * @throws IllegalStateException if TextUnit other than SP unit is specified.
Haoyu Zhang88398d42019-03-29 11:31:46 -070075 */
Leland Richardson47df77f2020-05-21 09:15:40 -070076 @Stable
Nader Jawadfeb99f82020-05-21 13:07:36 -070077 fun TextUnit.toPx(): Float {
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +000078 check(type == TextUnitType.Sp) { "Only Sp can convert to Px" }
Tyler Freemand1b43ca2023-08-10 17:28:22 +010079 return toDp().toPx()
Seigo Nonakad9638202019-11-19 16:37:07 -080080 }
Haoyu Zhang88398d42019-03-29 11:31:46 -070081
82 /**
George Mount8f237572020-04-30 12:08:30 -070083 * Convert Sp to [Int] by rounding
Haoyu Zhang88398d42019-03-29 11:31:46 -070084 */
Leland Richardson47df77f2020-05-21 09:15:40 -070085 @Stable
Mihai Popa217bf492021-01-25 23:55:14 +000086 fun TextUnit.roundToPx(): Int = toPx().roundToInt()
Haoyu Zhang88398d42019-03-29 11:31:46 -070087
88 /**
George Mount8f237572020-04-30 12:08:30 -070089 * Convert an [Int] pixel value to [Dp].
Calin Tataru6fd39e12019-07-04 10:59:07 +010090 */
Leland Richardson47df77f2020-05-21 09:15:40 -070091 @Stable
George Mount8f237572020-04-30 12:08:30 -070092 fun Int.toDp(): Dp = (this / density).dp
Calin Tataru6fd39e12019-07-04 10:59:07 +010093
94 /**
George Mount8f237572020-04-30 12:08:30 -070095 * Convert an [Int] pixel value to Sp.
Calin Tataru6fd39e12019-07-04 10:59:07 +010096 */
Leland Richardson47df77f2020-05-21 09:15:40 -070097 @Stable
Tyler Freemand1b43ca2023-08-10 17:28:22 +010098 fun Int.toSp(): TextUnit = toDp().toSp()
Calin Tataru6fd39e12019-07-04 10:59:07 +010099
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000100 /** Convert a [Float] pixel value to a Dp */
Leland Richardson47df77f2020-05-21 09:15:40 -0700101 @Stable
Andrey Kulikov47f0e1a2020-01-28 13:52:46 +0000102 fun Float.toDp(): Dp = (this / density).dp
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000103
Haoyu Zhang88398d42019-03-29 11:31:46 -0700104 /** Convert a [Float] pixel value to a Sp */
Leland Richardson47df77f2020-05-21 09:15:40 -0700105 @Stable
Tyler Freemand1b43ca2023-08-10 17:28:22 +0100106 fun Float.toSp(): TextUnit = toDp().toSp()
Haoyu Zhang88398d42019-03-29 11:31:46 -0700107
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000108 /**
Doris Liu619be1f2021-02-02 21:45:17 -0800109 * Convert a [DpRect] to a [Rect].
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000110 */
Leland Richardson47df77f2020-05-21 09:15:40 -0700111 @Stable
Doris Liu619be1f2021-02-02 21:45:17 -0800112 fun DpRect.toRect(): Rect {
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000113 return Rect(
Nader Jawadfeb99f82020-05-21 13:07:36 -0700114 left.toPx(),
115 top.toPx(),
116 right.toPx(),
117 bottom.toPx()
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000118 )
119 }
George Mountdae1d492021-07-23 22:52:24 +0000120
121 /**
122 * Convert a [DpSize] to a [Size].
123 */
124 @Stable
Zach Klippensteinfc183192021-10-04 23:31:05 -0700125 fun DpSize.toSize(): Size = if (isSpecified) {
126 Size(width.toPx(), height.toPx())
127 } else {
128 Size.Unspecified
129 }
George Mountdae1d492021-07-23 22:52:24 +0000130
131 /**
132 * Convert a [Size] to a [DpSize].
133 */
134 @Stable
Zach Klippensteinfc183192021-10-04 23:31:05 -0700135 fun Size.toDpSize(): DpSize = if (isSpecified) {
136 DpSize(width.toDp(), height.toDp())
137 } else {
138 DpSize.Unspecified
139 }
Andrey Kulikov254d58c2019-03-06 12:57:26 +0000140}