|
| 1 | +package com.jakewharton.mosaic.layout |
| 2 | + |
| 3 | +import androidx.compose.runtime.Stable |
| 4 | +import com.jakewharton.mosaic.modifier.Modifier |
| 5 | +import com.jakewharton.mosaic.ui.unit.Constraints |
| 6 | +import com.jakewharton.mosaic.ui.unit.IntOffset |
| 7 | +import com.jakewharton.mosaic.ui.unit.constrain |
| 8 | + |
| 9 | +/** |
| 10 | + * Declare the preferred width of the content to be the same as the min or max intrinsic width of |
| 11 | + * the content. The incoming measurement [Constraints] may override this value, forcing the content |
| 12 | + * to be either smaller or larger. |
| 13 | + * |
| 14 | + * See [height] for options of sizing to intrinsic height. Also see [width] and [widthIn] for other |
| 15 | + * options to set the preferred width. |
| 16 | + */ |
| 17 | +@Stable |
| 18 | +public fun Modifier.width(intrinsicSize: IntrinsicSize): Modifier = |
| 19 | + this then IntrinsicWidthModifier(width = intrinsicSize, enforceIncoming = true) |
| 20 | + |
| 21 | +/** |
| 22 | + * Declare the preferred height of the content to be the same as the min or max intrinsic height of |
| 23 | + * the content. The incoming measurement [Constraints] may override this value, forcing the content |
| 24 | + * to be either smaller or larger. |
| 25 | + * |
| 26 | + * See [width] for other options of sizing to intrinsic width. Also see [height] and [heightIn] for |
| 27 | + * other options to set the preferred height. |
| 28 | + */ |
| 29 | +@Stable |
| 30 | +public fun Modifier.height(intrinsicSize: IntrinsicSize): Modifier = |
| 31 | + this then IntrinsicHeightModifier(height = intrinsicSize, enforceIncoming = true) |
| 32 | + |
| 33 | +/** |
| 34 | + * Declare the width of the content to be exactly the same as the min or max intrinsic width of the |
| 35 | + * content. The incoming measurement [Constraints] will not override this value. If the content |
| 36 | + * intrinsic width does not satisfy the incoming [Constraints], the parent layout will be reported a |
| 37 | + * size coerced in the [Constraints], and the position of the content will be automatically offset |
| 38 | + * to be centered on the space assigned to the child by the parent layout under the assumption that |
| 39 | + * [Constraints] were respected. |
| 40 | + * |
| 41 | + * See [height] for options of sizing to intrinsic height. See [width] and [widthIn] for options to |
| 42 | + * set the preferred width. See [requiredWidth] and [requiredWidthIn] for other options to set the |
| 43 | + * required width. |
| 44 | + */ |
| 45 | +@Stable |
| 46 | +public fun Modifier.requiredWidth(intrinsicSize: IntrinsicSize): Modifier = |
| 47 | + this then IntrinsicWidthModifier(width = intrinsicSize, enforceIncoming = false) |
| 48 | + |
| 49 | +/** |
| 50 | + * Declare the height of the content to be exactly the same as the min or max intrinsic height of |
| 51 | + * the content. The incoming measurement [Constraints] will not override this value. If the content |
| 52 | + * intrinsic height does not satisfy the incoming [Constraints], the parent layout will be reported |
| 53 | + * a size coerced in the [Constraints], and the position of the content will be automatically offset |
| 54 | + * to be centered on the space assigned to the child by the parent layout under the assumption that |
| 55 | + * [Constraints] were respected. |
| 56 | + * |
| 57 | + * See [width] for options of sizing to intrinsic width. See [height] and [heightIn] for options to |
| 58 | + * set the preferred height. See [requiredHeight] and [requiredHeightIn] for other options to set |
| 59 | + * the required height. |
| 60 | + */ |
| 61 | +@Stable |
| 62 | +public fun Modifier.requiredHeight(intrinsicSize: IntrinsicSize): Modifier = |
| 63 | + this then IntrinsicHeightModifier(height = intrinsicSize, enforceIncoming = false) |
| 64 | + |
| 65 | +/** Intrinsic size used in [width] or [height] which can refer to width or height. */ |
| 66 | +public enum class IntrinsicSize { |
| 67 | + Min, |
| 68 | + Max, |
| 69 | +} |
| 70 | + |
| 71 | +private class IntrinsicWidthModifier( |
| 72 | + private val width: IntrinsicSize, |
| 73 | + override val enforceIncoming: Boolean, |
| 74 | +) : IntrinsicSizeModifier() { |
| 75 | + override fun MeasureScope.calculateContentConstraints( |
| 76 | + measurable: Measurable, |
| 77 | + constraints: Constraints, |
| 78 | + ): Constraints { |
| 79 | + var measuredWidth = |
| 80 | + if (width == IntrinsicSize.Min) { |
| 81 | + measurable.minIntrinsicWidth(constraints.maxHeight) |
| 82 | + } else { |
| 83 | + measurable.maxIntrinsicWidth(constraints.maxHeight) |
| 84 | + } |
| 85 | + if (measuredWidth < 0) { |
| 86 | + measuredWidth = 0 |
| 87 | + } |
| 88 | + return Constraints.fixedWidth(measuredWidth) |
| 89 | + } |
| 90 | + |
| 91 | + override fun minIntrinsicWidth( |
| 92 | + measurable: IntrinsicMeasurable, |
| 93 | + height: Int, |
| 94 | + ) = |
| 95 | + if (width == IntrinsicSize.Min) { |
| 96 | + measurable.minIntrinsicWidth(height) |
| 97 | + } else { |
| 98 | + measurable.maxIntrinsicWidth(height) |
| 99 | + } |
| 100 | + |
| 101 | + override fun maxIntrinsicWidth( |
| 102 | + measurable: IntrinsicMeasurable, |
| 103 | + height: Int, |
| 104 | + ) = |
| 105 | + if (width == IntrinsicSize.Min) { |
| 106 | + measurable.minIntrinsicWidth(height) |
| 107 | + } else { |
| 108 | + measurable.maxIntrinsicWidth(height) |
| 109 | + } |
| 110 | + |
| 111 | + override fun toString(): String { |
| 112 | + return "IntrinsicWidthModifier(width=$width, enforceIncoming=$enforceIncoming)" |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +private class IntrinsicHeightModifier( |
| 117 | + private val height: IntrinsicSize, |
| 118 | + override val enforceIncoming: Boolean, |
| 119 | +) : IntrinsicSizeModifier() { |
| 120 | + override fun MeasureScope.calculateContentConstraints( |
| 121 | + measurable: Measurable, |
| 122 | + constraints: Constraints, |
| 123 | + ): Constraints { |
| 124 | + var measuredHeight = |
| 125 | + if (height == IntrinsicSize.Min) { |
| 126 | + measurable.minIntrinsicHeight(constraints.maxWidth) |
| 127 | + } else { |
| 128 | + measurable.maxIntrinsicHeight(constraints.maxWidth) |
| 129 | + } |
| 130 | + if (measuredHeight < 0) { |
| 131 | + measuredHeight = 0 |
| 132 | + } |
| 133 | + return Constraints.fixedHeight(measuredHeight) |
| 134 | + } |
| 135 | + |
| 136 | + override fun minIntrinsicHeight( |
| 137 | + measurable: IntrinsicMeasurable, |
| 138 | + width: Int, |
| 139 | + ) = |
| 140 | + if (height == IntrinsicSize.Min) { |
| 141 | + measurable.minIntrinsicHeight(width) |
| 142 | + } else { |
| 143 | + measurable.maxIntrinsicHeight(width) |
| 144 | + } |
| 145 | + |
| 146 | + override fun maxIntrinsicHeight( |
| 147 | + measurable: IntrinsicMeasurable, |
| 148 | + width: Int, |
| 149 | + ) = |
| 150 | + if (height == IntrinsicSize.Min) { |
| 151 | + measurable.minIntrinsicHeight(width) |
| 152 | + } else { |
| 153 | + measurable.maxIntrinsicHeight(width) |
| 154 | + } |
| 155 | + |
| 156 | + override fun toString(): String { |
| 157 | + return "IntrinsicHeightModifier(height=$height, enforceIncoming=$enforceIncoming)" |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +private abstract class IntrinsicSizeModifier : LayoutModifier { |
| 162 | + |
| 163 | + abstract val enforceIncoming: Boolean |
| 164 | + |
| 165 | + abstract fun MeasureScope.calculateContentConstraints( |
| 166 | + measurable: Measurable, |
| 167 | + constraints: Constraints, |
| 168 | + ): Constraints |
| 169 | + |
| 170 | + final override fun MeasureScope.measure( |
| 171 | + measurable: Measurable, |
| 172 | + constraints: Constraints, |
| 173 | + ): MeasureResult { |
| 174 | + val contentConstraints = calculateContentConstraints(measurable, constraints) |
| 175 | + val placeable = |
| 176 | + measurable.measure( |
| 177 | + if (enforceIncoming) { |
| 178 | + constraints.constrain(contentConstraints) |
| 179 | + } else { |
| 180 | + contentConstraints |
| 181 | + }, |
| 182 | + ) |
| 183 | + return layout(placeable.width, placeable.height) { placeable.place(IntOffset.Zero) } |
| 184 | + } |
| 185 | + |
| 186 | + override fun minIntrinsicWidth( |
| 187 | + measurable: IntrinsicMeasurable, |
| 188 | + height: Int, |
| 189 | + ) = measurable.minIntrinsicWidth(height) |
| 190 | + |
| 191 | + override fun minIntrinsicHeight( |
| 192 | + measurable: IntrinsicMeasurable, |
| 193 | + width: Int, |
| 194 | + ) = measurable.minIntrinsicHeight(width) |
| 195 | + |
| 196 | + override fun maxIntrinsicWidth( |
| 197 | + measurable: IntrinsicMeasurable, |
| 198 | + height: Int, |
| 199 | + ) = measurable.maxIntrinsicWidth(height) |
| 200 | + |
| 201 | + override fun maxIntrinsicHeight( |
| 202 | + measurable: IntrinsicMeasurable, |
| 203 | + width: Int, |
| 204 | + ) = measurable.maxIntrinsicHeight(width) |
| 205 | +} |
0 commit comments