Scoop API

scoop:scoop.ui.component:0.1.0

src/content/aspect_ratio.scoop

Selected production source · src/content/aspect_ratio.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
6public class AspectRatioProps public constructor(
7 public val content: View,
8 public val ratio: Double,
9 public val clip: Boolean = true
10) {
11 init {
12 requireComponent(
13 ratio.isFinite() && ratio > 0.0,
14 "aspect ratio must be positive and finite"
15 )
16 }
19internal fun aspectSize(
20 ratio: Double,
21 constraints: Constraints,
22 natural: Size = Size(0.0, 0.0)
23): Size {
24 var width = 0.0
25 var height = 0.0
26 when (constraints.maxWidth) {
27 case Some(val value) -> {
28 width = value;
29 height = value / ratio
30 }
31 case None -> when (constraints.maxHeight) {
32 case Some(val value) -> {
33 height = value;
34 width = value * ratio
35 }
36 case None -> {
37 width = lowerBound(natural.width, natural.height * ratio);
38 height = width / ratio
39 }
40 }
41 }
42 when (constraints.maxHeight) {
43 case Some(val limit) -> if (height > limit) {
44 height = limit;
45 width = height * ratio
46 };
47 case None -> {}
48 }
49 when (constraints.maxWidth) {
50 case Some(val limit) -> if (width > limit) {
51 width = limit;
52 height = width / ratio
53 };
54 case None -> {}
55 }
56 val minimum = lowerBound(width, lowerBound(constraints.minWidth, constraints.minHeight * ratio))
57 val candidateHeight = minimum / ratio
58 if (
59 minimum.isFinite() &&
60 candidateHeight.isFinite() &&
61 (constraints.maxWidth == None || minimum <= constraints.maxWidth!!) &&
62 (constraints.maxHeight == None || candidateHeight <= constraints.maxHeight!!)
63 ) {
64 width = minimum
65 height = candidateHeight
66 }
67 val result = constraints.constrain(Size(width, height))
68 validSize(result)
69 return result
72public class AspectRatio public constructor(
73 private val props: Props<AspectRatioProps>,
74 scope: ComponentScope
75): Element<Unit>(scope) {
76 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
77 Unit
78 > {
79 val natural =
80 if (constraints.maxWidth == None && constraints.maxHeight == None)
81 context.measureChild(0L, Constraints()).size
82 else
83 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 }
94public class AspectRatioBuilder internal constructor() {
95 public var clip: Boolean = true
96 public var key: ViewKey = ViewKey.Position
99public fun aspectRatio(
100 content: View,
101 ratio: Double,
102 configure: AspectRatioBuilder.() -> Unit = {}
103): View = makeAspectRatio(content, ratio, configure)
105internal fun makeAspectRatio(
106 content: View,
107 ratio: Double,
108 configure: AspectRatioBuilder.() -> Unit
109): 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.key
117 )