scoop:scoop.ui.component:0.1.0
src/content/aspect_ratio.scoop
Selected production source · src/content/aspect_ratio.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*6
public class AspectRatioProps public constructor(7
public val content: View,8
public val ratio: Double,9
public val clip: Boolean = true10
) {11
init {12
requireComponent(13
ratio.isFinite() && ratio > 0.0,14
"aspect ratio must be positive and finite"15
)16
}17
}19
internal fun aspectSize(20
ratio: Double,21
constraints: Constraints,22
natural: Size = Size(0.0, 0.0)23
): Size {24
var width = 0.025
var height = 0.026
when (constraints.maxWidth) {27
case Some(val value) -> {28
width = value;29
height = value / ratio30
}31
case None -> when (constraints.maxHeight) {32
case Some(val value) -> {33
height = value;34
width = value * ratio35
}36
case None -> {37
width = lowerBound(natural.width, natural.height * ratio);38
height = width / ratio39
}40
}41
}42
when (constraints.maxHeight) {43
case Some(val limit) -> if (height > limit) {44
height = limit;45
width = height * ratio46
};47
case None -> {}48
}49
when (constraints.maxWidth) {50
case Some(val limit) -> if (width > limit) {51
width = limit;52
height = width / ratio53
};54
case None -> {}55
}56
val minimum = lowerBound(width, lowerBound(constraints.minWidth, constraints.minHeight * ratio))57
val candidateHeight = minimum / ratio58
if (59
minimum.isFinite() &&60
candidateHeight.isFinite() &&61
(constraints.maxWidth == None || minimum <= constraints.maxWidth!!) &&62
(constraints.maxHeight == None || candidateHeight <= constraints.maxHeight!!)63
) {64
width = minimum65
height = candidateHeight66
}67
val result = constraints.constrain(Size(width, height))68
validSize(result)69
return result70
}72
public class AspectRatio public constructor(73
private val props: Props<AspectRatioProps>,74
scope: ComponentScope75
): Element<Unit>(scope) {76
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<77
Unit78
> {79
val natural =80
if (constraints.maxWidth == None && constraints.maxHeight == None)81
context.measureChild(0L, Constraints()).size82
else83
Size(0.0, 0.0)84
val size = aspectSize(props.value.ratio, constraints, natural)85
val child = context.measureChild(0L, Constraints.tight(size))86
context.placeChild(0L, Point(0.0, 0.0))87
return LayoutResult(offsetMetrics(child, size, 0.0), Unit)88
}89
public override fun scene(context: SceneContext, measured: Unit) {90
containerScene(context, props.value.clip)91
}92
}94
public class AspectRatioBuilder internal constructor() {95
public var clip: Boolean = true96
public var key: ViewKey = ViewKey.Position97
}99
public fun aspectRatio(100
content: View,101
ratio: Double,102
configure: AspectRatioBuilder.() -> Unit = {}103
): View = makeAspectRatio(content, ratio, configure)105
internal fun makeAspectRatio(106
content: View,107
ratio: Double,108
configure: AspectRatioBuilder.() -> Unit109
): View {110
val builder = AspectRatioBuilder()111
builder.configure()112
return element(113
AspectRatioProps(content, ratio, builder.clip),114
{ current, mount -> AspectRatio(current, scope = mount) },115
[content],116
builder.key117
)118
}