Scoop API

scoop:scoop.ui.component:0.1.0

src/slider/thumb.scoop

Selected production source · src/slider/thumb.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9internal class SliderThumbProps(
10 val input: SliderInput,
11 val index: Long,
12 val pressed: Boolean,
13 val control: SliderControl
16internal class SliderThumb(
17 private val props: Props<SliderThumbProps>,
18 scope: ComponentScope
19): Element<Unit>(scope) {
20 private var previousSide: Double? = None
21 private var area: InputArea? = None
22 private var focus: FocusTarget? = None
23 private var field: FieldLink? = None
24 private var painters: ThemePainterCache? = None
25 private var motion: InteractionMotion? = None
26 private val semantics = createComponentSemantics(scope, SemanticRole.Slider)
27 private fun options(): SliderThumbOptions {
28 val input = props.value
29 return if (input.input.props.thumbs.size == 0L)
30 SliderThumbOptions()
31 else
32 input.input.props.thumbs[input.index]
33 }
34 public override fun onMount() {
35 area = Some(scope.createArea(focusable = true) { context, event -> notify(context, event) })
36 focus = Some(scope.focusTarget(area!!, options().focus))
37 semantics?.bindFocus(focus)
38 field =
39 Some(
40 scope.resource {
41 FieldLink(scope, area!!, focus!!, semantics) { updateSemantics() }
42 }
43 )
44 painters = Some(scope.resource { ThemePainterCache() })
45 motion = Some(InteractionMotion(scope, visual()))
46 scope.resource {
47 area!!.scope.addDefaultAction {
48 context,
49 event -> when (event) {
50 case Keyboard(val key) -> {
51 if (area!!.isFocusActive)
52 props.value.control.key(props.value.index, context, key)
53 }
54 else -> {}
55 }
56 }
57 }
58 }
59 private fun visual(): InteractionVisual =
60 InteractionVisual(
61 props.value.input.props.enabled,
62 area!!.scope.isHovered,
63 props.value.pressed,
64 area!!.isFocusVisible,
65 invalid = props.value.input.props.invalid
66 )
67 private fun refresh() {
68 val input = props.value.input
69 val specs = input.theme.tokens.motion
70 motion!!.update(
71 visual(),
72 specs.resolve(input.props.style.transition.resolve(specs.hover)),
73 specs.resolve(input.props.style.transition.resolve(specs.press))
74 )
75 if (scope.isMounted)
76 scope.requestFrame()
77 }
78 private fun notify(context: RoutedEventContext, event: InputEvent) {
79 if (context.phase != RoutedPhase.Notification || !scope.isMounted)
80 return
81 when (event) {
82 case FocusChanged(false) -> props.value.control.lostFocus(props.value.index)
83 else -> {}
84 }
85 when (motion) {
86 case Some(_) -> refresh();
87 case None -> {}
88 }
89 }
90 public override fun update(): UiInvalidation {
91 val input = props.value.input
92 val local = options()
93 val slot = input.slots[props.value.index]
94 slot.area = area
95 slot.focus = focus
96 focus!!.options = local.focus
97 area!!.scope.enabled = input.props.enabled
98 field!!.update(
99 when (local.labelTarget) {
100 case Some(_) -> local.labelTarget
101 case None -> if (props.value.index == 0L)
102 input.props.labelTarget
103 else
104 None
105 },
106 input.props.enabled
107 )
108 updateSemantics()
109 refresh()
110 painters!!.get(input.theme)
111 val side =
112 input.props.style.thumbSize ?: 12.0 * input.theme.tokens.typography.body.fontSize / 14.0
113 val changed = previousSide != Some(side)
114 previousSide = Some(side)
115 return if (changed)
116 UiInvalidation.Layout
117 else
118 UiInvalidation.Scene
119 }
120 private fun updateSemantics() {
121 val handle = semantics ?: return
122 val current = props.value
123 val input = current.input
124 val local = options().semantics ?: input.props.semantics
125 handle.bindReference(
126 if (panelPresent(options().semantics) || current.index == 0L)
127 local.reference
128 else
129 None
130 )
131 val bounds = input.props.range.thumbLimits(input.values, current.index)
132 handle.update(
133 local.properties(
134 enabled = input.props.enabled,
135 invalid = input.props.invalid,
136 required = field!!.isRequired(handle),
137 range =
138 Some(
139 SemanticValueRange(
140 bounds.minimum,
141 bounds.maximum,
142 input.values[current.index],
143 input.props.range.step ?: input.props.keyboardStep!!
144 )
145 )
146 ).copy {
147 orientation =
148 Some(
149 if (input.props.orientation == Orientation.Horizontal)
150 SemanticOrientation.Horizontal
151 else
152 SemanticOrientation.Vertical
153 )
154 },
155 SemanticActions(
156 setNumber =
157 Some { value -> props.value.control.setNumber(props.value.index, value) },
158 increment =
159 Some { props.value.control.adjust(props.value.index, SliderKey.Step(1.0)) },
160 decrement =
161 Some { props.value.control.adjust(props.value.index, SliderKey.Step(-1.0)) }
162 ),
163 field!!.relations(handle, local.relations)
164 )
165 }
166 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
167 Unit
168 > {
169 val input = props.value.input
170 val side =
171 input.props.style.thumbSize ?: 12.0 * input.theme.tokens.typography.body.fontSize / 14.0
172 val size = constraints.constrain(Size(side, side))
173 return LayoutResult(LayoutMetrics(size), Unit)
174 }
175 public override fun scene(context: SceneContext, measured: Unit) {
176 val input = props.value.input
177 val size = context.size
178 when (semantics) {
179 case Some(val handle) -> context.semantics(
180 handle,
181 Rect(0.0, 0.0, size.width, size.height)
182 )
183 case None -> {}
184 }
185 val base = motion!!.sample(visual())
186 val ring =
187 base.focusVisible ||
188 (base.hoverProgress ?: 0.0) >
189 0.0 ||
190 (base.pressProgress ?: 0.0) >
191 0.0
192 val sampled =
193 SurfaceVisual(
194 SurfaceKind.Plain,
195 InteractionVisual(
196 base.enabled,
197 base.hovered,
198 base.pressed,
199 ring,
200 base.hoverProgress,
201 base.pressProgress,
202 base.invalid
203 )
204 )
205 val shape =
206 input.theme.surfaceGeometry(
207 Rect(0.0, 0.0, size.width, size.height),
208 sampled,
209 sliderThumbOptions(input.theme, input.props.style, base),
210 context.layoutDirection
211 )
212 val geometry =
213 SurfaceGeometry(
214 shape.bounds,
215 shape.outline,
216 shape.borderInner,
217 shape.content,
218 shape.ring,
219 shape.style,
220 1.0
221 )
223 context.draw {
224 paint -> painters!!.get(input.theme).drawSurface(paint.canvas, geometry, sampled)
225 }
226 if (context.size.width > 0.0 && context.size.height > 0.0)
227 context.place(
228 area!!,
229 switchHitBounds(
230 shape.bounds,
231 input.props.style.hitInsets ?:
232 Insets.all(8.0 * input.theme.tokens.typography.body.fontSize / 14.0)
233 )
234 )
235 }
236 public override fun onUnmount() {
237 val slot = props.value.input.slots[props.value.index]
238 slot.area = None
239 slot.focus = None
240 }