Mihai Popa | ea553e8 | 2019-05-23 18:19:22 +0100 | [diff] [blame] | 1 | /* |
| 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-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 17 | package androidx.compose.foundation.layout |
Mihai Popa | ea553e8 | 2019-05-23 18:19:22 +0100 | [diff] [blame] | 18 | |
Leland Richardson | 47df77f | 2020-05-21 09:15:40 -0700 | [diff] [blame] | 19 | import androidx.compose.Stable |
Mihai Popa | ea553e8 | 2019-05-23 18:19:22 +0100 | [diff] [blame] | 20 | import androidx.ui.core.Constraints |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 21 | import androidx.ui.core.IntrinsicMeasurable |
| 22 | import androidx.ui.core.IntrinsicMeasureScope |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 23 | import androidx.ui.core.LayoutDirection |
Mihai Popa | 5f9e753 | 2020-04-21 01:19:23 +0100 | [diff] [blame] | 24 | import androidx.ui.core.LayoutModifier |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 25 | import androidx.ui.core.Measurable |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 26 | import androidx.ui.core.MeasureScope |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 27 | import androidx.ui.core.Modifier |
Mihai Popa | ea553e8 | 2019-05-23 18:19:22 +0100 | [diff] [blame] | 28 | import androidx.ui.core.satisfiedBy |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 29 | import androidx.ui.unit.IntSize |
Nikolay Igotti | 63c6e12 | 2020-07-06 20:21:59 +0300 | [diff] [blame] | 30 | import androidx.ui.util.annotation.FloatRange |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 31 | import kotlin.math.roundToInt |
Mihai Popa | ea553e8 | 2019-05-23 18:19:22 +0100 | [diff] [blame] | 32 | |
| 33 | /** |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 34 | * Attempts to size the content to match a specified aspect ratio by trying to match one of the |
| 35 | * incoming constraints in the following order: |
| 36 | * [Constraints.maxWidth], [Constraints.maxHeight], [Constraints.minWidth], [Constraints.minHeight]. |
| 37 | * The size in the other dimension is determined by the aspect ratio. |
| 38 | * |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 39 | * Example usage: |
Louis Pullen-Freilich | 623e405 | 2020-07-19 20:24:03 +0100 | [diff] [blame] | 40 | * @sample androidx.compose.foundation.layout.samples.SimpleAspectRatio |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 41 | * |
| 42 | * @param ratio the desired width/height positive ratio |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 43 | */ |
Leland Richardson | 47df77f | 2020-05-21 09:15:40 -0700 | [diff] [blame] | 44 | @Stable |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 45 | fun Modifier.aspectRatio( |
Nikolay Igotti | 63c6e12 | 2020-07-06 20:21:59 +0300 | [diff] [blame] | 46 | @FloatRange(from = 0.0, to = 3.4e38 /* POSITIVE_INFINITY */, fromInclusive = false) |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 47 | ratio: Float |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 48 | ) = this + AspectRatioModifier(ratio) |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 49 | |
Mihai Popa | 5f9e753 | 2020-04-21 01:19:23 +0100 | [diff] [blame] | 50 | private data class AspectRatioModifier(val aspectRatio: Float) : LayoutModifier { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 51 | init { |
Adam Powell | 712dc99 | 2019-12-04 12:48:30 -0800 | [diff] [blame] | 52 | require(aspectRatio > 0) { "aspectRatio $aspectRatio must be > 0" } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 55 | override fun MeasureScope.measure( |
| 56 | measurable: Measurable, |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 57 | constraints: Constraints, |
| 58 | layoutDirection: LayoutDirection |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 59 | ): MeasureScope.MeasureResult { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 60 | val size = constraints.findSizeWith(aspectRatio) |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 61 | val wrappedConstraints = if (size != null) { |
Mihai Popa | d9d5350 | 2020-01-15 12:58:15 +0000 | [diff] [blame] | 62 | Constraints.fixed(size.width, size.height) |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 63 | } else { |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 64 | constraints |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 65 | } |
| 66 | val placeable = measurable.measure(wrappedConstraints) |
| 67 | return layout(placeable.width, placeable.height) { |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 68 | placeable.place(0, 0) |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 69 | } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 72 | override fun IntrinsicMeasureScope.minIntrinsicWidth( |
| 73 | measurable: IntrinsicMeasurable, |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 74 | height: Int, |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 75 | layoutDirection: LayoutDirection |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 76 | ) = if (height != Constraints.Infinity) { |
| 77 | (height * aspectRatio).roundToInt() |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 78 | } else { |
| 79 | measurable.minIntrinsicWidth(height) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 80 | } |
| 81 | |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 82 | override fun IntrinsicMeasureScope.maxIntrinsicWidth( |
| 83 | measurable: IntrinsicMeasurable, |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 84 | height: Int, |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 85 | layoutDirection: LayoutDirection |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 86 | ) = if (height != Constraints.Infinity) { |
| 87 | (height * aspectRatio).roundToInt() |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 88 | } else { |
| 89 | measurable.maxIntrinsicWidth(height) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 92 | override fun IntrinsicMeasureScope.minIntrinsicHeight( |
| 93 | measurable: IntrinsicMeasurable, |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 94 | width: Int, |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 95 | layoutDirection: LayoutDirection |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 96 | ) = if (width != Constraints.Infinity) { |
| 97 | (width / aspectRatio).roundToInt() |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 98 | } else { |
| 99 | measurable.minIntrinsicHeight(width) |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 102 | override fun IntrinsicMeasureScope.maxIntrinsicHeight( |
| 103 | measurable: IntrinsicMeasurable, |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 104 | width: Int, |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 105 | layoutDirection: LayoutDirection |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 106 | ) = if (width != Constraints.Infinity) { |
| 107 | (width / aspectRatio).roundToInt() |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 108 | } else { |
| 109 | measurable.maxIntrinsicHeight(width) |
| 110 | } |
| 111 | |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 112 | private fun Constraints.findSizeWith(aspectRatio: Float): IntSize? { |
| 113 | val maxWidth = this.maxWidth |
| 114 | if (maxWidth != Constraints.Infinity) { |
| 115 | val height = (maxWidth / aspectRatio).roundToInt() |
| 116 | if (height > 0) { |
| 117 | val size = IntSize(maxWidth, height) |
| 118 | if (satisfiedBy(size)) { |
| 119 | return size |
| 120 | } |
| 121 | } |
Mihai Popa | 832df7d | 2020-04-20 21:05:40 +0100 | [diff] [blame] | 122 | } |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 123 | val maxHeight = this.maxHeight |
| 124 | if (maxHeight != Constraints.Infinity) { |
| 125 | val width = (maxHeight * aspectRatio).roundToInt() |
| 126 | if (width > 0) { |
| 127 | val size = IntSize(width, maxHeight) |
| 128 | if (satisfiedBy(size)) { |
| 129 | return size |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | val minWidth = this.minWidth |
| 134 | val height = (minWidth / aspectRatio).roundToInt() |
| 135 | if (height > 0) { |
| 136 | val size = IntSize(minWidth, height) |
| 137 | if (satisfiedBy(size)) { |
| 138 | return size |
| 139 | } |
| 140 | } |
| 141 | val minHeight = this.minHeight |
| 142 | val width = (minHeight * aspectRatio).roundToInt() |
| 143 | if (width > 0) { |
| 144 | val size = IntSize(width, minHeight) |
| 145 | if (satisfiedBy(size)) { |
| 146 | return size |
| 147 | } |
| 148 | } |
| 149 | return null |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 150 | } |
Anastasia Soboleva | 1de1d10 | 2019-09-30 16:48:31 +0100 | [diff] [blame] | 151 | } |