Scoop API

scoop:scoop.ui.component:0.1.0

src/calendar/layout.scoop

Selected production source · src/calendar/layout.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.Insets
7internal class CalendarMonthSizeInput(val width: Double)
9internal class CalendarMonthSize(
10 private val props: Props<CalendarMonthSizeInput>,
11 scope: ComponentScope
12): Element<Unit>(scope) {
13 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
14 Unit
15 > {
16 val width = constraints.constrain(Size(props.value.width, 0.0)).width
17 if (width == 0.0 || constraints.maxHeight == Some(0.0)) {
18 context.skipChild(0L)
19 return LayoutResult(LayoutMetrics(constraints.constrain(Size(width, 0.0))), Unit)
20 }
21 val child =
22 context.measureChild(
23 0L,
24 Constraints(
25 minWidth = width,
26 maxWidth = Some(width),
27 maxHeight = constraints.maxHeight
28 )
29 )
30 if (child.size.width > 0.0 && child.size.height > 0.0)
31 context.placeChild(0L, Point(0.0, 0.0))
32 return LayoutResult(LayoutMetrics(constraints.constrain(child.size)), Unit)
33 }
34 public override fun scene(context: SceneContext, measured: Unit) {
35 context.children()
36 }
39internal class CalendarMonthsInput(val count: Long, val width: Double, val gap: Double)
41/** The same month children move between one row and one column without remounting. */
42internal class CalendarMonths(
43 private val props: Props<CalendarMonthsInput>,
44 scope: ComponentScope
45): Element<Unit>(scope) {
46 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
47 Unit
48 > {
49 val input = props.value
50 val natural =
51 input.width * input.count.toDouble() + input.gap * (input.count - 1L).toDouble()
52 val horizontal = (constraints.maxWidth ?: natural) >= natural
53 val width =
54 if (horizontal)
55 input.width
56 else
57 upperBound(input.width, constraints.maxWidth ?: input.width)
58 val sizes = ArrayList<Size>()
59 var total = 0.0
60 var cross = 0.0
61 for (index in 0L ..< input.count) {
62 val result = context.measureChild(index, Constraints(maxWidth = Some(width)))
63 sizes.add(result.size)
64 if (index > 0L)
65 total += input.gap
66 total += if (horizontal)
67 result.size.width
68 else
69 result.size.height
70 cross =
71 lowerBound(
72 cross,
73 if (horizontal)
74 result.size.height
75 else
76 result.size.width
77 )
78 }
79 val size =
80 constraints.constrain(
81 if (horizontal)
82 Size(total, cross)
83 else
84 Size(cross, total)
85 )
86 var offset = 0.0
87 for (index in 0L ..< input.count) {
88 val child = sizes[index]
89 context.placeChild(
90 index,
91 Point(
92 logicalX(
93 if (horizontal)
94 offset
95 else
96 0.0,
97 child.width,
98 size.width,
99 context.layoutDirection
100 ),
101 if (horizontal)
102 0.0
103 else
104 offset
105 )
106 )
107 offset += (
108 if (horizontal)
109 child.width
110 else
111 child.height
112 ) +
113 input.gap
114 }
115 return LayoutResult(LayoutMetrics(size), Unit)
116 }
117 public override fun scene(context: SceneContext, measured: Unit) {
118 context.children()
119 }
122internal class CalendarDayFrameInput(
123 val owner: CalendarControl,
124 val day: CalendarDay,
125 val appearance: CalendarAppearance
128internal class CalendarDayFrame(
129 private val props: Props<CalendarDayFrameInput>,
130 scope: ComponentScope
131): Element<Unit>(scope) {
132 init {
133 scope.resource {
134 scope.input.observeActivity {
135 if (scope.isMounted)
136 props.value.owner.hover(props.value.day.data.date, scope.input.isHovered)
137 }
138 }
139 }
140 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
141 Unit
142 > {
143 val ring = props.value.appearance.ring
144 val child =
145 context.measureChild(0L, childConstraints(constraints, Size(ring * 2.0, ring * 2.0)))
146 context.placeChild(0L, Point(ring, ring))
147 return LayoutResult(
148 LayoutMetrics(
149 constraints.constrain(
150 Size(child.size.width + ring * 2.0, child.size.height + ring * 2.0)
151 )
152 ),
153 Unit
154 )
155 }
156 public override fun scene(context: SceneContext, measured: Unit) {
157 val input = props.value
158 val day = input.day
159 if (day.inRange && !(day.rangeStart && day.rangeEnd)) {
160 val width = context.size.width
161 val leftHalf = day.rangeEnd != (context.layoutDirection == LayoutDirection.Rtl)
162 val end = day.rangeStart || day.rangeEnd
163 val x =
164 if (end && !leftHalf)
165 width / 2.0
166 else
167 0.0
168 val shape =
169 Rect(
170 x,
171 input.appearance.ring,
172 if (end)
173 width / 2.0
174 else
175 width,
176 lowerBound(0.0, context.size.height - input.appearance.ring * 2.0)
177 )
178 val color = input.appearance.theme.tokens.palette.muted.background
179 val fill =
180 if (day.preview)
181 color.withAlpha(color.alpha * 0.65)
182 else
183 color
184 context.draw { target -> target.canvas.drawRect(shape, Paint(fill)) }
185 }
186 context.children()
187 }