Scoop API

scoop:scoop.ui.component:0.1.0

src/carousel/viewport.scoop

Selected production source · src/carousel/viewport.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9internal class CarouselLayoutResult(val geometry: CarouselGeometry, val visible: Array<Boolean>)
11internal class CarouselViewport(
12 private val props: Props<CarouselInput>,
13 scope: ComponentScope
14): Element<CarouselLayoutResult>(scope) {
15 private var control: CarouselControl? = None
16 private var painter: StylePainter? = None
17 private var ring: ThemePainterCache? = None
18 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.props
31 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.Horizontal
38 else
39 SemanticOrientation.Vertical
40 )
41 },
42 relations = input.semantics.relations
43 )
44 ring!!.get(props.value.theme)
45 return UiInvalidation.Layout
46 }
47 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
48 CarouselLayoutResult
49 > {
50 val input = props.value
51 val horizontal = input.props.orientation == Orientation.Horizontal
52 val density = input.theme.tokens.typography.body.fontSize / 14.0
53 val padding = input.props.viewport.padding * density
54 val main =
55 (
56 if (horizontal)
57 constraints.maxWidth
58 else
59 constraints.maxHeight
60 ) ?:
61 0.0
62 val innerMain = lowerBound(0.0, main - padding * 2.0)
63 val maxCross =
64 if (horizontal)
65 constraints.maxHeight
66 else
67 constraints.maxWidth
68 val innerMaxCross =
69 when (maxCross) {
70 case Some(val value) -> Some(lowerBound(0.0, value - padding * 2.0));
71 case None -> None
72 }
73 val spacing = input.props.spacing * density
74 var naturalCross = 0.0
75 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 else
84 Constraints(0.0, innerMaxCross, extent ?: 0.0, extent)
85 )
86 naturalCross =
87 lowerBound(
88 naturalCross,
89 if (horizontal)
90 measured.size.height
91 else
92 measured.size.width
93 )
94 sizes.add(
95 if (horizontal)
96 measured.size.width
97 else
98 measured.size.height
99 )
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.height
114 else
115 inner.width
116 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.layoutDirection
124 )
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.0
144 ) {
145 visible[index] = true
146 val along =
147 if (horizontal)
148 logicalX(position, child.size.width, inner.width, shape.direction)
149 else
150 position
151 context.placeChild(
152 index,
153 Point(
154 inner.x +
155 if (horizontal)
156 along
157 else
158 0.0,
159 inner.y +
160 if (horizontal)
161 0.0
162 else
163 along
164 )
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.value
175 val shape = layout.geometry
176 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.layoutDirection
203 )
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 }
225internal class CarouselItemOwner {
226 var scope: ComponentScope? = None
229/** The keyed scope follows only the final viewport placement. */
230internal class CarouselItemLayout(
231 private val props: Props<CarouselItemOwner>,
232 scope: ComponentScope
233): Element<Unit>(scope) {
234 private var owner: CarouselItemOwner? = None
235 public override fun onMount() {
236 scope.input.enabled = false
237 }
238 public override fun update(): UiInvalidation {
239 when (owner) {
240 case Some(val previous) -> previous.scope = None
241 case None -> {}
242 }
243 owner = Some(props.value)
244 props.value.scope = Some(scope)
245 return UiInvalidation.Layout
246 }
247 public override fun onUnmount() {
248 when (owner) {
249 case Some(val previous) -> previous.scope = None
250 case None -> {}
251 }
252 }
253 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
254 Unit
255 > {
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 }