Scoop API

scoop:scoop.ui.component:0.1.0

src/content/item_media.scoop

Selected production source · src/content/item_media.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal fun itemMediaSide(variant: ItemMediaVariant, size: ItemSize, density: Double): Double? =
8 when (variant) {
9 case Default -> None
10 case Icon -> Some(16.0 * density)
11 case Image -> Some(
12 (
13 when (size) {
14 case Default -> 40.0
15 case Sm -> 32.0
16 case Xs -> 24.0
17 }
18 ) *
19 density
20 )
21 }
23private struct ItemMediaProps(val side: Double?, val offset: Double, val radius: Double?)
25internal fun itemMedia(
26 content: View,
27 variant: ItemMediaVariant,
28 size: ItemSize,
29 radius: Double,
30 density: Double,
31 description: Boolean
32): View =
33 element(
34 ItemMediaProps(
35 itemMediaSide(variant, size, density),
36 if (description)
37 2.0 * density
38 else
39 0.0,
40 if (variant == ItemMediaVariant.Image)
41 Some(radius)
42 else
43 None
44 ),
45 { current, mount -> ItemMedia(current, scope = mount) },
46 [content]
47 )
49/** Translation changes painting and hit geometry without adding a layout gap. */
50private class ItemMedia(private val props: Props<ItemMediaProps>, scope: ComponentScope): Element<
51 Unit
52>(scope) {
53 private var painter: StylePainter? = None
54 public override fun onMount() {
55 painter = Some(scope.resource { StylePainter() })
56 }
57 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
58 Unit
59 > {
60 val input = props.value
61 val child =
62 context.measureChild(
63 0L,
64 when (input.side) {
65 case Some(val side) -> Constraints.tight(constraints.constrain(Size(side, side)))
66 case None -> constraints
67 }
68 )
69 context.placeChild(0L, Point(0.0, input.offset))
70 return LayoutResult(offsetMetrics(child, child.size, input.offset), Unit)
71 }
72 public override fun scene(context: SceneContext, measured: Unit) {
73 val input = props.value
74 when (input.radius) {
75 case Some(val radius) -> painter!!.scene(
76 context,
77 Rect(0.0, input.offset, context.size.width, context.size.height),
78 style {
79 this.corners = Corners.all(radius);
80 this.clipContent = true
81 }
82 ) { scene -> scene.children() }
83 case None -> context.children()
84 }
85 }