Scoop API

scoop:scoop.ui.component:0.1.0

src/tabs/indicator.scoop

Selected production source · src/tabs/indicator.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.ui.style.*
7internal class TabIndicatorProps(
8 val selected: Boolean,
9 val axis: FlexAxis,
10 val theme: Theme,
11 val enabled: Boolean
14internal class TabIndicator(
15 private val props: Props<TabIndicatorProps>,
16 scope: ComponentScope
17): Element<Unit>(scope) {
18 private var presence: Presence? = None
19 public override fun onMount() {
20 presence = Some(scope.presence(props.value.selected))
21 }
22 public override fun update(): UiInvalidation {
23 val input = props.value
24 val motion = input.theme.tokens.motion
25 val transition = motion.resolve(motion.selection)
26 presence!!.update(input.selected, Some(PresenceOptions(transition, transition)))
27 return UiInvalidation.Layout
28 }
29 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
30 Unit
31 > {
32 val measured = context.measureChild(0L, constraints)
33 context.placeChild(0L, Point(0.0, 0.0))
34 return LayoutResult(measured, Unit)
35 }
36 public override fun scene(context: SceneContext, measured: Unit) {
37 context.children()
38 val input = props.value
39 val progress = presence!!.snapshot.progress
40 if (progress <= 0.0)
41 return
42 val density = input.theme.tokens.typography.action.fontSize / 14.0
43 val color = input.theme.tokens.palette.background.foreground
44 val opacity =
45 if (input.enabled)
46 1.0
47 else
48 input.theme.tokens.disabledOpacity
49 val bounds =
50 if (input.axis == FlexAxis.Horizontal)
51 Rect(0.0, context.size.height + 3.0 * density, context.size.width, 2.0 * density)
52 else
53 Rect(
54 if (context.layoutDirection == LayoutDirection.Ltr)
55 context.size.width + 3.0 * density
56 else
57 -5.0 * density,
58 0.0,
59 2.0 * density,
60 context.size.height
61 )
62 context.draw {
63 paint -> paint.canvas.drawRect(
64 bounds,
65 Paint(color.withAlpha(color.alpha * progress * opacity))
66 )
67 }
68 }