scoop:scoop.ui.component:0.1.0
src/carousel/viewport.scoop
Selected production source · src/carousel/viewport.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
internal class CarouselLayoutResult(val geometry: CarouselGeometry, val visible: Array<Boolean>)11
internal class CarouselViewport(12
private val props: Props<CarouselInput>,13
scope: ComponentScope14
): Element<CarouselLayoutResult>(scope) {15
private var control: CarouselControl? = None16
private var painter: StylePainter? = None17
private var ring: ThemePainterCache? = None18
private val semantics = createComponentSemantics(scope, SemanticRole.List)19
public override fun onMount() {20
val value = CarouselControl(scope, { props.value });21
control = Some(value)22
semantics?.bindFocus(Some(value.focus))23
scope.routeSemanticsChildren(semantics)24
props.value.owner.control = Some(value)25
painter = Some(scope.resource { StylePainter() });26
ring = Some(scope.resource { ThemePainterCache() })27
}28
public override fun update(): UiInvalidation {29
control!!.update();30
val input = props.value.props31
semantics?.bindReference(input.semantics.reference)32
semantics?.update(33
input.semantics.properties(enabled = input.enabled).copy {34
orientation =35
Some(36
if (input.orientation == Orientation.Horizontal)37
SemanticOrientation.Horizontal38
else39
SemanticOrientation.Vertical40
)41
},42
relations = input.semantics.relations43
)44
ring!!.get(props.value.theme)45
return UiInvalidation.Layout46
}47
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<48
CarouselLayoutResult49
> {50
val input = props.value51
val horizontal = input.props.orientation == Orientation.Horizontal52
val density = input.theme.tokens.typography.body.fontSize / 14.053
val padding = input.props.viewport.padding * density54
val main =55
(56
if (horizontal)57
constraints.maxWidth58
else59
constraints.maxHeight60
) ?:61
0.062
val innerMain = lowerBound(0.0, main - padding * 2.0)63
val maxCross =64
if (horizontal)65
constraints.maxHeight66
else67
constraints.maxWidth68
val innerMaxCross =69
when (maxCross) {70
case Some(val value) -> Some(lowerBound(0.0, value - padding * 2.0));71
case None -> None72
}73
val spacing = input.props.spacing * density74
var naturalCross = 0.075
val sizes = ArrayList<Double>()76
for (index in 0L ..< context.childCount) {77
val extent = carouselExtent(input.props.itemSize(index), innerMain, spacing)78
val measured =79
context.measureChild(80
index,81
if (horizontal)82
Constraints(extent ?: 0.0, extent, 0.0, innerMaxCross)83
else84
Constraints(0.0, innerMaxCross, extent ?: 0.0, extent)85
)86
naturalCross =87
lowerBound(88
naturalCross,89
if (horizontal)90
measured.size.height91
else92
measured.size.width93
)94
sizes.add(95
if (horizontal)96
measured.size.width97
else98
measured.size.height99
)100
}101
val extents = sizes.toArray()102
val size =103
constraints.constrain(resizableSize(main, naturalCross + padding * 2.0, horizontal))104
val inner =105
Rect(106
upperBound(padding, size.width),107
upperBound(padding, size.height),108
lowerBound(0.0, size.width - padding * 2.0),109
lowerBound(0.0, size.height - padding * 2.0)110
)111
val cross =112
if (horizontal)113
inner.height114
else115
inner.width116
val shape =117
CarouselGeometry(118
size,119
inner,120
carouselTrack(extents, innerMain, spacing, input.props.loop),121
input.keys,122
input.props.orientation,123
context.layoutDirection124
)125
val offset = control!!.visualPosition(shape)126
val visible = MutableArray<Boolean>(context.childCount) { _ -> false }127
for (index in 0L ..< context.childCount) {128
val child =129
context.measureChild(130
index,131
Constraints.tight(resizableSize(extents[index], cross, horizontal))132
)133
val position = carouselItemPosition(shape.track, index, offset)134
if (135
position <136
shape.track.viewport &&137
position +138
extents[index] >139
0.0 &&140
child.size.width >141
0.0 &&142
child.size.height >143
0.0144
) {145
visible[index] = true146
val along =147
if (horizontal)148
logicalX(position, child.size.width, inner.width, shape.direction)149
else150
position151
context.placeChild(152
index,153
Point(154
inner.x +155
if (horizontal)156
along157
else158
0.0,159
inner.y +160
if (horizontal)161
0.0162
else163
along164
)165
)166
}167
}168
return LayoutResult(169
LayoutMetrics(size),170
CarouselLayoutResult(shape, Array<Boolean>(visible.size) { index -> visible[index] })171
)172
}173
public override fun scene(context: SceneContext, layout: CarouselLayoutResult) {174
val input = props.value175
val shape = layout.geometry176
val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)177
when (semantics) {178
case Some(val handle) -> context.semantics(handle, bounds);179
case None -> {}180
}181
if (bounds.width > 0.0 && bounds.height > 0.0)182
context.place(control!!.area, bounds)183
painter!!.scene(context, input.props.viewport.style.resolve(style())) {184
local ->185
local.clipRect(bounds);186
local.children()187
}188
if (control!!.area.isFocusVisible) {189
val visual = SurfaceVisual(SurfaceKind.Plain, InteractionVisual(focusVisible = true))190
val geometry =191
input.theme.surfaceGeometry(192
bounds,193
visual,194
surfaceOptions {195
style {196
background(None);197
border(None);198
corners(Corners.all(0.0))199
}200
focus = Some(OutlineStyle(input.theme.tokens.palette.ring, 1.0, 1.0))201
},202
context.layoutDirection203
)204
context.draw {205
paint -> ring!!.get(input.theme).drawSurface(paint.canvas, geometry, visual)206
}207
}208
context.onFrameComplete {209
frame -> if (frame.submission == FrameSubmission.Accepted) {210
for (index in 0L ..< input.items.size)211
when (input.items[index].scope) {212
case Some(val scope) -> if (scope.isMounted)213
scope.input.enabled = layout.visible[index]214
case None -> {}215
}216
control!!.accept(shape)217
}218
}219
}220
public override fun onUnmount() {221
control!!.retire()222
}223
}225
internal class CarouselItemOwner {226
var scope: ComponentScope? = None227
}229
/** The keyed scope follows only the final viewport placement. */230
internal class CarouselItemLayout(231
private val props: Props<CarouselItemOwner>,232
scope: ComponentScope233
): Element<Unit>(scope) {234
private var owner: CarouselItemOwner? = None235
public override fun onMount() {236
scope.input.enabled = false237
}238
public override fun update(): UiInvalidation {239
when (owner) {240
case Some(val previous) -> previous.scope = None241
case None -> {}242
}243
owner = Some(props.value)244
props.value.scope = Some(scope)245
return UiInvalidation.Layout246
}247
public override fun onUnmount() {248
when (owner) {249
case Some(val previous) -> previous.scope = None250
case None -> {}251
}252
}253
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<254
Unit255
> {256
val child = context.measureChild(0L, constraints)257
context.placeChild(0L, Point(0.0, 0.0))258
return LayoutResult(child, Unit)259
}260
public override fun scene(context: SceneContext, measured: Unit) {261
context.children()262
}263
}