Scoop API

scoop:scoop.ui.component:0.1.0

src/candidate/row.scoop

Selected production source · src/candidate/row.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
7import scoop.window.accessibility.*
9internal class CandidateRowInkProps(
10 val key: ViewKey,
11 val name: String,
12 val enabled: Boolean,
13 val highlight: (ViewKey) -> Unit,
14 val select: (ViewKey, Boolean) -> SemanticActionResult,
15 val role: SemanticRole = SemanticRole.Button,
16 val checked: SemanticChecked? = None
19internal class CandidateRowInk(
20 private val props: Props<CandidateRowInkProps>,
21 scope: ComponentScope
22): Element<Unit>(scope) {
23 private var area: InputArea? = None
24 private var press: Press? = None
25 private var semantics: SemanticsHandle? = None
26 public override fun onMount() {
27 val area = scope.createArea(focusable = false)
28 this.area = Some(area)
29 scope.routeChildren(area)
30 press =
31 Some(
32 scope.press(area, PressOptions(props.value.enabled)) {
33 props.value.select(props.value.key, true)
34 }
35 )
36 area.scope.addEventHandler {
37 context, event ->
38 if (!props.value.enabled || context.phase == RoutedPhase.Capture)
39 return@addEventHandler
40 when (event) {
41 case Pointer(InputPointerEvent.Moved(_)) -> props.value.highlight(props.value.key)
42 case Pointer(
43 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
44 ) -> props.value.highlight(props.value.key)
45 case Contact(InputContactEvent.Began(_)) -> props.value.highlight(props.value.key)
46 else -> {}
47 }
48 }
49 }
50 public override fun update(): UiInvalidation {
51 area!!.scope.enabled = props.value.enabled
52 press!!.update(PressOptions(props.value.enabled)) {
53 props.value.select(props.value.key, true)
54 }
55 updateSemantics()
56 return UiInvalidation.Layout
57 }
58 private fun updateSemantics() {
59 val input = props.value
60 when (semantics) {
61 case Some(val previous) -> if (previous.role != input.role) {
62 previous.close()
63 semantics = None
64 }
65 case None -> {}
66 }
67 if (!panelPresent(semantics)) {
68 semantics = createComponentSemantics(scope, input.role)
69 when (semantics) {
70 case Some(val handle) -> {
71 handle.mergeLabels = true
72 scope.routeSemanticsChildren(Some(handle))
73 }
74 case None -> {}
75 }
76 }
77 val action: () -> SemanticActionResult = { props.value.select(props.value.key, false) }
78 semantics?.update(
79 SemanticProperties(
80 name = Some(input.name),
81 enabled = input.enabled,
82 checked = input.checked
83 ),
84 if (input.role == SemanticRole.Checkbox)
85 SemanticActions(toggle = Some(action))
86 else
87 SemanticActions(invoke = Some(action))
88 )
89 }
90 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
91 Unit
92 > {
93 val measured = context.measureChild(0L, constraints)
94 context.placeChild(0L, Point(0.0, 0.0))
95 return LayoutResult(measured, Unit)
96 }
97 public override fun scene(context: SceneContext, measured: Unit) {
98 if (!props.value.enabled)
99 context.saveLayer(0.5)
100 context.place(area!!, Rect(0.0, 0.0, context.size.width, context.size.height))
101 when (semantics) {
102 case Some(val handle) -> context.semantics(
103 handle,
104 Rect(0.0, 0.0, context.size.width, context.size.height)
105 )
106 case None -> {}
107 }
108 context.children()
109 if (!props.value.enabled)
110 context.restore()
111 }