Scoop API

scoop:scoop.ui.component:0.1.0

src/field/semantics.scoop

Selected production source · src/field/semantics.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.ui.*
5import scoop.window.accessibility.*
7internal enum FieldAnnotationKind {
8 Label, Description, Error
9}
11/** A mounted annotation lends its real semantic node to the existing field connection. */
12internal class FieldAnnotation(private val scope: ComponentScope): Closeable {
13 val node = createComponentSemantics(scope, SemanticRole.Group)
14 private var currentKind = FieldAnnotationKind.Label
15 private var target: FieldTarget? = None
16 private var closed = false
17 val kind: FieldAnnotationKind get() = currentKind
19 init {
20 when (node) {
21 case Some(val handle) -> handle.mergeLabels = true
22 case None -> {}
23 }
24 scope.routeSemanticsChildren(node)
25 }
27 fun update(
28 target: FieldTarget?,
29 kind: FieldAnnotationKind,
30 options: ComponentSemantics,
31 enabled: Boolean = true
32 ) {
33 val handle = node ?: return
34 handle.update(options.properties(enabled = enabled), relations = options.relations)
35 val sameTarget = sameFieldTarget(this.target, target)
36 val kindChanged = currentKind != kind
37 if (!sameTarget)
38 this.target?.removeAnnotation(this)
39 this.target = target
40 currentKind = kind
41 if (!sameTarget)
42 target?.addAnnotation(this)
43 else
44 if (kindChanged)
45 target?.semanticsChanged()
46 }
48 fun nodeFor(source: SemanticsHandle): SemanticsHandle? {
49 if (closed || !scope.isMounted)
50 return None
51 val handle = node ?: return None
52 return if (!handle.isClosed && handle.id.window == source.id.window)
53 Some(handle)
54 else
55 None
56 }
58 fun scene(context: SceneContext) {
59 when (node) {
60 case Some(val handle) -> context.semantics(
61 handle,
62 Rect(0.0, 0.0, context.size.width, context.size.height)
63 )
64 case None -> {}
65 }
66 }
68 public override fun close() {
69 if (closed)
70 return
71 closed = true
72 target?.removeAnnotation(this)
73 target = None
74 }
77private class FieldAnnotationProps(
78 val target: FieldTarget?,
79 val kind: FieldAnnotationKind,
80 val semantics: ComponentSemantics
83private class FieldAnnotationInk(
84 private val props: Props<FieldAnnotationProps>,
85 scope: ComponentScope
86): Element<Unit>(scope) {
87 private val annotation = scope.resource { FieldAnnotation(scope) }
89 public override fun update(): UiInvalidation {
90 val input = props.value
91 annotation.update(input.target, input.kind, input.semantics)
92 return UiInvalidation.None
93 }
94 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
95 Unit
96 > {
97 val child = context.measureChild(0L, constraints)
98 context.placeChild(0L, Point(0.0, 0.0))
99 return LayoutResult(child, Unit)
100 }
101 public override fun scene(context: SceneContext, measured: Unit) {
102 annotation.scene(context)
103 context.children()
104 }
107internal fun fieldAnnotated(
108 content: View,
109 target: FieldTarget?,
110 kind: FieldAnnotationKind,
111 semantics: ComponentSemantics = ComponentSemantics()
112): View =
113 element(
114 FieldAnnotationProps(target, kind, semantics),
115 { current, mount -> FieldAnnotationInk(current, scope = mount) },
116 [content]
117 )