scoop:scoop.ui.component:0.1.0
src/calendar/layout.scoop
Selected production source · src/calendar/layout.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.ui.style.Insets7
internal class CalendarMonthSizeInput(val width: Double)9
internal class CalendarMonthSize(10
private val props: Props<CalendarMonthSizeInput>,11
scope: ComponentScope12
): Element<Unit>(scope) {13
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<14
Unit15
> {16
val width = constraints.constrain(Size(props.value.width, 0.0)).width17
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.maxHeight28
)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
}37
}39
internal 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. */42
internal class CalendarMonths(43
private val props: Props<CalendarMonthsInput>,44
scope: ComponentScope45
): Element<Unit>(scope) {46
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<47
Unit48
> {49
val input = props.value50
val natural =51
input.width * input.count.toDouble() + input.gap * (input.count - 1L).toDouble()52
val horizontal = (constraints.maxWidth ?: natural) >= natural53
val width =54
if (horizontal)55
input.width56
else57
upperBound(input.width, constraints.maxWidth ?: input.width)58
val sizes = ArrayList<Size>()59
var total = 0.060
var cross = 0.061
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.gap66
total += if (horizontal)67
result.size.width68
else69
result.size.height70
cross =71
lowerBound(72
cross,73
if (horizontal)74
result.size.height75
else76
result.size.width77
)78
}79
val size =80
constraints.constrain(81
if (horizontal)82
Size(total, cross)83
else84
Size(cross, total)85
)86
var offset = 0.087
for (index in 0L ..< input.count) {88
val child = sizes[index]89
context.placeChild(90
index,91
Point(92
logicalX(93
if (horizontal)94
offset95
else96
0.0,97
child.width,98
size.width,99
context.layoutDirection100
),101
if (horizontal)102
0.0103
else104
offset105
)106
)107
offset += (108
if (horizontal)109
child.width110
else111
child.height112
) +113
input.gap114
}115
return LayoutResult(LayoutMetrics(size), Unit)116
}117
public override fun scene(context: SceneContext, measured: Unit) {118
context.children()119
}120
}122
internal class CalendarDayFrameInput(123
val owner: CalendarControl,124
val day: CalendarDay,125
val appearance: CalendarAppearance126
)128
internal class CalendarDayFrame(129
private val props: Props<CalendarDayFrameInput>,130
scope: ComponentScope131
): 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
Unit142
> {143
val ring = props.value.appearance.ring144
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
Unit154
)155
}156
public override fun scene(context: SceneContext, measured: Unit) {157
val input = props.value158
val day = input.day159
if (day.inRange && !(day.rangeStart && day.rangeEnd)) {160
val width = context.size.width161
val leftHalf = day.rangeEnd != (context.layoutDirection == LayoutDirection.Rtl)162
val end = day.rangeStart || day.rangeEnd163
val x =164
if (end && !leftHalf)165
width / 2.0166
else167
0.0168
val shape =169
Rect(170
x,171
input.appearance.ring,172
if (end)173
width / 2.0174
else175
width,176
lowerBound(0.0, context.size.height - input.appearance.ring * 2.0)177
)178
val color = input.appearance.theme.tokens.palette.muted.background179
val fill =180
if (day.preview)181
color.withAlpha(color.alpha * 0.65)182
else183
color184
context.draw { target -> target.canvas.drawRect(shape, Paint(fill)) }185
}186
context.children()187
}188
}