Scoop API

scoop:scoop.ui.component:0.1.0

src/chart/ink.scoop

Selected production source · src/chart/ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.*
8import scoop.window.accessibility.*
10internal class ChartInkProps(
11 val input: ChartProps,
12 val plan: ChartPlan,
13 val active: Binding<ChartSelection?>
16private class ChartAccepted(val plan: ChartPlan, val geometry: ChartGeometry)
18internal class ChartInk(private val props: Props<ChartInkProps>, scope: ComponentScope): Element<
19 ChartGeometry
20>(scope) {
21 private val layer = PaintKey()
22 private var revision = 0L
23 private var drawn: ChartGeometry? = None
24 private var accepted: ChartAccepted? = None
25 private var pointer = false
26 private var activationPoint: Point? = None
27 private val area =
28 scope.createArea(focusable = true) { context, event -> notify(context, event) }
29 private val focus = scope.focusTarget(area, props.value.input.focus)
30 private val press = scope.press(area, PressOptions(props.value.input.enabled)) { activate() }
31 private val semantics = createComponentSemantics(scope, SemanticRole.Table)
32 init {
33 scope.routeChildren(area)
34 semantics?.bindFocus(Some(focus))
35 area.scope.defaultAction =
36 {
37 context,
38 event -> when (event) {
39 case Keyboard(val key) -> keyboard(context, key)
40 case Pointer(
41 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
42 ) -> {
43 if (context.target === area.scope && props.value.input.enabled)
44 focus.handle.requestFocus(FocusOrigin.Pointer)
45 }
46 else -> {}
47 }
48 }
49 }
50 private fun valid(): ChartGeometry? {
51 val value = accepted ?: return None
52 return if (value.plan === props.value.plan && props.value.input.enabled)
53 Some(value.geometry)
54 else
55 None
56 }
57 private fun notify(context: RoutedEventContext, event: InputEvent) {
58 if (!scope.isMounted)
59 return
60 if (context.phase == RoutedPhase.Target) {
61 when (event) {
62 case Contact(InputContactEvent.Began(_)) -> activationPoint = context.localPosition
63 case Contact(InputContactEvent.Ended(_)) -> activationPoint = context.localPosition
64 case Pointer(
65 InputPointerEvent.Button(_, MouseButton.Left, _, _)
66 ) -> activationPoint = context.localPosition
67 case Keyboard(_) -> activationPoint = None
68 else -> {}
69 }
70 }
71 when (event) {
72 case Pointer(InputPointerEvent.Moved(_)) -> hover(context)
73 case Pointer(InputPointerEvent.Entered(_)) -> hover(context)
74 case Pointer(InputPointerEvent.Left(_)) -> {
75 if (pointer && context.phase == RoutedPhase.Notification) {
76 pointer = false;
77 select(None)
78 }
79 }
80 case FocusChanged(val focused) -> {
81 if (context.phase == RoutedPhase.Notification) {
82 if (!focused) {
83 pointer = false;
84 select(None)
85 };
86 scope.requestFrame()
87 }
88 }
89 case FocusVisibilityChanged(_) -> {
90 if (context.phase == RoutedPhase.Notification)
91 scope.requestFrame()
92 }
93 else -> {}
94 }
95 }
96 private fun hover(context: RoutedEventContext) {
97 if (context.phase != RoutedPhase.Target && context.phase != RoutedPhase.Notification)
98 return
99 val geometry = valid() ?: return
100 val point = context.localPosition ?: return
101 pointer = true
102 select(
103 chartHit(
104 geometry,
105 point,
106 props.value.input.orientation == Orientation.Horizontal,
107 props.value.active.value
108 )
109 )
110 }
111 private fun activate() {
112 val geometry = valid() ?: return
113 val point = activationPoint
114 pointer = false
115 focus.handle.requestFocus(
116 if (point != None)
117 FocusOrigin.Pointer
118 else
119 FocusOrigin.Keyboard
120 )
121 select(
122 when (point) {
123 case Some(val value) -> chartHit(
124 geometry,
125 value,
126 props.value.input.orientation == Orientation.Horizontal,
127 props.value.active.value
128 )
129 case None -> when (props.value.active.value) {
130 case Some(val current) -> when (geometry.find(current)) {
131 case Some(_) -> Some(current)
132 case None -> chartNavigate(geometry, None, edge = -1)
133 }
134 case None -> chartNavigate(geometry, None, edge = -1)
135 }
136 }
137 )
138 }
139 private fun select(value: ChartSelection?) {
140 if (props.value.active.value != value)
141 props.value.active.value = value
142 }
143 private fun keyboard(context: RoutedEventContext, key: KeyEvent) {
144 if (!area.isFocusActive || !sliderKeyAllowed(key))
145 return
146 val geometry = valid() ?: return
147 val current = props.value.active.value
148 val direction =
149 if (scope.layoutDirection == LayoutDirection.Rtl)
150 -1L
151 else
152 1L
153 val selected =
154 when (key.logicalKey) {
155 case Named(NamedKey.ArrowRight) -> chartNavigate(
156 geometry,
157 current,
158 datumStep = direction
159 )
160 case Named(NamedKey.ArrowLeft) -> chartNavigate(
161 geometry,
162 current,
163 datumStep = -direction
164 )
165 case Named(NamedKey.ArrowUp) -> chartNavigate(geometry, current, seriesStep = -1L)
166 case Named(NamedKey.ArrowDown) -> chartNavigate(geometry, current, seriesStep = 1L)
167 case Named(NamedKey.Home) -> chartNavigate(geometry, current, edge = -1)
168 case Named(NamedKey.End) -> chartNavigate(geometry, current, edge = 1)
169 case Named(NamedKey.Escape) -> None
170 else -> return
171 }
172 pointer = false;
173 select(selected);
174 context.consume()
175 }
176 public override fun update(): UiInvalidation {
177 area.scope.enabled = props.value.input.enabled
178 press.update(PressOptions(props.value.input.enabled)) { activate() }
179 focus.options = props.value.input.focus
180 val input = props.value.input
181 val snapshot = input.model.snapshot
182 val source = ChartSemanticSource(snapshot, input.enabled)
183 val selected = ArrayList<SemanticItemKey>()
184 when (props.value.active.value) {
185 case Some(val value) -> if (snapshot.data.indexOfDatum(value.datum) != None)
186 selected.add(SemanticItemKey.Text(value.datum.value))
187 case None -> {}
188 }
189 semantics?.bindReference(input.semantics.reference)
190 semantics?.update(
191 input.semantics.properties(enabled = input.enabled),
192 relations = input.semantics.relations
193 )
194 semantics?.updateDataCollection(
195 SemanticCollectionSnapshot(
196 source,
197 Some(SemanticCollectionSelection(selected.toArray())),
198 Some(source.grid())
199 ),
200 SemanticCollectionActions(
201 setSelected = Some { key, value -> selectSemantically(key, value) }
202 )
203 )
204 return UiInvalidation.Layout
205 }
206 private fun selectSemantically(key: SemanticItemKey, selected: Boolean): SemanticActionResult {
207 val input = props.value
208 if (!input.input.enabled)
209 return SemanticActionResult.Rejected
210 val datum =
211 when (key) {
212 case Text(val text) -> ChartDatumKey(text);
213 case Number(_) -> return SemanticActionResult.Unavailable
214 }
215 val snapshot = input.input.model.snapshot
216 if (snapshot.data.indexOfDatum(datum) == None || snapshot.visible.size == 0L)
217 return SemanticActionResult.Unavailable
218 val current = input.active.value
219 if (!selected) {
220 if (current?.datum == Some(datum))
221 select(None)
222 return if (input.active.value?.datum != Some(datum))
223 SemanticActionResult.Completed
224 else
225 SemanticActionResult.Rejected
226 }
227 var series = snapshot.data.series[snapshot.visible[0L]].key
228 when (current) {
229 case Some(val value) -> if (input.input.model.isVisible(value.series))
230 series = value.series;
231 case None -> {}
232 }
233 val next = ChartSelection(series, datum)
234 select(Some(next))
235 return if (input.active.value == Some(next))
236 SemanticActionResult.Completed
237 else
238 SemanticActionResult.Rejected
239 }
240 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
241 ChartGeometry
242 > {
243 val input = props.value.input
244 val d = props.value.plan.appearance.density
245 val width =
246 when (input.width) {
247 case Some(val value) -> value * d;
248 case None -> constraints.maxWidth ?: 480.0 * d
249 }
250 val height =
251 when (input.height) {
252 case Some(val value) -> value * d;
253 case None -> width * 9.0 / 16.0
254 }
255 val size = constraints.constrain(Size(width, height))
256 val geometry = props.value.plan.cache.resolve(size)
257 val active =
258 when (props.value.active.value) {
259 case Some(val key) -> geometry.find(key);
260 case None -> None
261 }
262 if (
263 (active?.visible ?: false) &&
264 input.tooltip &&
265 input.enabled &&
266 geometry.plot.width >
267 0.0 &&
268 geometry.plot.height >
269 0.0
270 ) {
271 val child =
272 context.measureChild(
273 0L,
274 Constraints(maxWidth = Some(size.width), maxHeight = Some(size.height))
275 )
276 val point = active!!.point
277 val origin =
278 Point(
279 chartClamp(
280 point.x + 12.0 * d,
281 0.0,
282 lowerBound(size.width - child.size.width, 0.0)
283 ),
284 chartClamp(
285 point.y - child.size.height - 12.0 * d,
286 0.0,
287 lowerBound(size.height - child.size.height, 0.0)
288 )
289 )
290 context.placeChild(0L, origin)
291 } else
292 context.skipChild(0L)
293 val content = geometry.contentBounds
294 if (content.width > 0.0 && content.height > 0.0) {
295 val child =
296 context.measureChild(
297 1L,
298 Constraints(maxWidth = Some(content.width), maxHeight = Some(content.height))
299 )
300 context.placeChild(
301 1L,
302 Point(
303 content.x + (content.width - child.size.width) / 2.0,
304 content.y + (content.height - child.size.height) / 2.0
305 )
306 )
307 } else
308 context.skipChild(1L)
309 return LayoutResult(LayoutMetrics(size), geometry)
310 }
311 public override fun scene(context: SceneContext, geometry: ChartGeometry) {
312 val plan = props.value.plan
313 val bounds = Rect(0.0, 0.0, context.size.width, context.size.height)
314 when (semantics) {
315 case Some(val handle) -> context.semantics(handle, bounds);
316 case None -> {}
317 }
318 val presented = geometry.plot.width > 0.0 && geometry.plot.height > 0.0
319 context.onFrameComplete {
320 frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {
321 accepted =
322 if (presented && geometry.interactive)
323 Some(ChartAccepted(plan, geometry))
324 else
325 None
326 area.scope.enabled = props.value.input.enabled && presented && geometry.interactive
327 }
328 }
329 val same =
330 when (drawn) {
331 case Some(val value) -> value === geometry;
332 case None -> false
333 }
334 if (!same) {
335 revision += 1L;
336 drawn = Some(geometry)
337 }
338 if (presented) {
339 context.drawLayer(layer, bounds, revision) {
340 paint -> chartDraw(paint.canvas, plan, geometry)
341 }
342 context.draw {
343 paint ->
344 chartDrawActive(paint.canvas, plan, geometry, props.value.active.value)
345 if (area.isFocusVisible)
346 paint.canvas.drawRoundRect(
347 Rect(
348 2.0,
349 2.0,
350 lowerBound(bounds.width - 4.0, 0.0),
351 lowerBound(bounds.height - 4.0, 0.0)
352 ),
353 8.0,
354 8.0,
355 Paint(plan.appearance.theme.tokens.palette.ring, PaintingStyle.Stroke, 2.0)
356 )
357 if (plan.prepared.series.size == 0L || plan.snapshot.data.datums.size == 0L) {
358 val text = "No data"
359 val size = plan.appearance.measure(text)
360 plan.appearance.draw(
361 paint.canvas,
362 text,
363 Point(
364 (bounds.width - size.width) / 2.0,
365 (bounds.height - size.height) / 2.0
366 )
367 )
368 }
369 }
370 if (geometry.interactive)
371 context.place(area, bounds)
372 }
373 context.children([1L, 0L])
374 }
375 public override fun onUnmount() {
376 accepted = None
377 }