scoop:scoop.ui.component:0.1.0
src/chart/ink.scoop
Selected production source · src/chart/ink.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.*8
import scoop.window.accessibility.*10
internal class ChartInkProps(11
val input: ChartProps,12
val plan: ChartPlan,13
val active: Binding<ChartSelection?>14
)16
private class ChartAccepted(val plan: ChartPlan, val geometry: ChartGeometry)18
internal class ChartInk(private val props: Props<ChartInkProps>, scope: ComponentScope): Element<19
ChartGeometry20
>(scope) {21
private val layer = PaintKey()22
private var revision = 0L23
private var drawn: ChartGeometry? = None24
private var accepted: ChartAccepted? = None25
private var pointer = false26
private var activationPoint: Point? = None27
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 None52
return if (value.plan === props.value.plan && props.value.input.enabled)53
Some(value.geometry)54
else55
None56
}57
private fun notify(context: RoutedEventContext, event: InputEvent) {58
if (!scope.isMounted)59
return60
if (context.phase == RoutedPhase.Target) {61
when (event) {62
case Contact(InputContactEvent.Began(_)) -> activationPoint = context.localPosition63
case Contact(InputContactEvent.Ended(_)) -> activationPoint = context.localPosition64
case Pointer(65
InputPointerEvent.Button(_, MouseButton.Left, _, _)66
) -> activationPoint = context.localPosition67
case Keyboard(_) -> activationPoint = None68
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
return99
val geometry = valid() ?: return100
val point = context.localPosition ?: return101
pointer = true102
select(103
chartHit(104
geometry,105
point,106
props.value.input.orientation == Orientation.Horizontal,107
props.value.active.value108
)109
)110
}111
private fun activate() {112
val geometry = valid() ?: return113
val point = activationPoint114
pointer = false115
focus.handle.requestFocus(116
if (point != None)117
FocusOrigin.Pointer118
else119
FocusOrigin.Keyboard120
)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.value128
)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 = value142
}143
private fun keyboard(context: RoutedEventContext, key: KeyEvent) {144
if (!area.isFocusActive || !sliderKeyAllowed(key))145
return146
val geometry = valid() ?: return147
val current = props.value.active.value148
val direction =149
if (scope.layoutDirection == LayoutDirection.Rtl)150
-1L151
else152
1L153
val selected =154
when (key.logicalKey) {155
case Named(NamedKey.ArrowRight) -> chartNavigate(156
geometry,157
current,158
datumStep = direction159
)160
case Named(NamedKey.ArrowLeft) -> chartNavigate(161
geometry,162
current,163
datumStep = -direction164
)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) -> None170
else -> return171
}172
pointer = false;173
select(selected);174
context.consume()175
}176
public override fun update(): UiInvalidation {177
area.scope.enabled = props.value.input.enabled178
press.update(PressOptions(props.value.input.enabled)) { activate() }179
focus.options = props.value.input.focus180
val input = props.value.input181
val snapshot = input.model.snapshot182
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.relations193
)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.Layout205
}206
private fun selectSemantically(key: SemanticItemKey, selected: Boolean): SemanticActionResult {207
val input = props.value208
if (!input.input.enabled)209
return SemanticActionResult.Rejected210
val datum =211
when (key) {212
case Text(val text) -> ChartDatumKey(text);213
case Number(_) -> return SemanticActionResult.Unavailable214
}215
val snapshot = input.input.model.snapshot216
if (snapshot.data.indexOfDatum(datum) == None || snapshot.visible.size == 0L)217
return SemanticActionResult.Unavailable218
val current = input.active.value219
if (!selected) {220
if (current?.datum == Some(datum))221
select(None)222
return if (input.active.value?.datum != Some(datum))223
SemanticActionResult.Completed224
else225
SemanticActionResult.Rejected226
}227
var series = snapshot.data.series[snapshot.visible[0L]].key228
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.Completed237
else238
SemanticActionResult.Rejected239
}240
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<241
ChartGeometry242
> {243
val input = props.value.input244
val d = props.value.plan.appearance.density245
val width =246
when (input.width) {247
case Some(val value) -> value * d;248
case None -> constraints.maxWidth ?: 480.0 * d249
}250
val height =251
when (input.height) {252
case Some(val value) -> value * d;253
case None -> width * 9.0 / 16.0254
}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 -> None261
}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.0270
) {271
val child =272
context.measureChild(273
0L,274
Constraints(maxWidth = Some(size.width), maxHeight = Some(size.height))275
)276
val point = active!!.point277
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
} else292
context.skipChild(0L)293
val content = geometry.contentBounds294
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.0305
)306
)307
} else308
context.skipChild(1L)309
return LayoutResult(LayoutMetrics(size), geometry)310
}311
public override fun scene(context: SceneContext, geometry: ChartGeometry) {312
val plan = props.value.plan313
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.0319
context.onFrameComplete {320
frame -> if (frame.submission == FrameSubmission.Accepted && scope.isMounted) {321
accepted =322
if (presented && geometry.interactive)323
Some(ChartAccepted(plan, geometry))324
else325
None326
area.scope.enabled = props.value.input.enabled && presented && geometry.interactive327
}328
}329
val same =330
when (drawn) {331
case Some(val value) -> value === geometry;332
case None -> false333
}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.0366
)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 = None377
}378
}