scoop:scoop.ui.component:0.1.0
src/field/semantics.scoop
Selected production source · src/field/semantics.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.ui.*5
import scoop.window.accessibility.*7
internal enum FieldAnnotationKind {8
Label, Description, Error9
}11
/** A mounted annotation lends its real semantic node to the existing field connection. */12
internal class FieldAnnotation(private val scope: ComponentScope): Closeable {13
val node = createComponentSemantics(scope, SemanticRole.Group)14
private var currentKind = FieldAnnotationKind.Label15
private var target: FieldTarget? = None16
private var closed = false17
val kind: FieldAnnotationKind get() = currentKind19
init {20
when (node) {21
case Some(val handle) -> handle.mergeLabels = true22
case None -> {}23
}24
scope.routeSemanticsChildren(node)25
}27
fun update(28
target: FieldTarget?,29
kind: FieldAnnotationKind,30
options: ComponentSemantics,31
enabled: Boolean = true32
) {33
val handle = node ?: return34
handle.update(options.properties(enabled = enabled), relations = options.relations)35
val sameTarget = sameFieldTarget(this.target, target)36
val kindChanged = currentKind != kind37
if (!sameTarget)38
this.target?.removeAnnotation(this)39
this.target = target40
currentKind = kind41
if (!sameTarget)42
target?.addAnnotation(this)43
else44
if (kindChanged)45
target?.semanticsChanged()46
}48
fun nodeFor(source: SemanticsHandle): SemanticsHandle? {49
if (closed || !scope.isMounted)50
return None51
val handle = node ?: return None52
return if (!handle.isClosed && handle.id.window == source.id.window)53
Some(handle)54
else55
None56
}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
return71
closed = true72
target?.removeAnnotation(this)73
target = None74
}75
}77
private class FieldAnnotationProps(78
val target: FieldTarget?,79
val kind: FieldAnnotationKind,80
val semantics: ComponentSemantics81
)83
private class FieldAnnotationInk(84
private val props: Props<FieldAnnotationProps>,85
scope: ComponentScope86
): Element<Unit>(scope) {87
private val annotation = scope.resource { FieldAnnotation(scope) }89
public override fun update(): UiInvalidation {90
val input = props.value91
annotation.update(input.target, input.kind, input.semantics)92
return UiInvalidation.None93
}94
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<95
Unit96
> {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
}105
}107
internal 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
)