scoop:scoop.ui.component:0.1.0
src/disclosure/body.scoop
Selected production source · src/disclosure/body.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.FrameSubmission5
import scoop.ui.*6
import scoop.window.accessibility.*8
internal class DisclosureBodyProps(9
val presence: Presence?,10
val open: Boolean,11
val shown: Boolean,12
val enabled: Boolean,13
val reference: SemanticsReference? = None,14
val labelledBy: SemanticNodeId? = None15
)17
internal struct DisclosureBodyLayout(val clipped: Boolean, val placed: Boolean)19
internal class DisclosureBody(20
private val props: Props<DisclosureBodyProps>,21
scope: ComponentScope22
): Element<DisclosureBodyLayout>(scope) {23
private var revision = 0UL24
private val semantics = createComponentSemantics(scope, SemanticRole.Group)25
public override fun onMount() {26
scope.input.enabled = false27
scope.routeSemanticsChildren(semantics)28
}29
public override fun update(): UiInvalidation {30
revision += 1UL31
val input = props.value32
if (!input.open || !input.enabled)33
scope.input.enabled = false34
scope.isFrameEnabled = input.open35
when (semantics) {36
case Some(val handle) -> {37
handle.bindReference(input.reference)38
handle.update(39
SemanticProperties(hidden = !input.open, enabled = input.enabled),40
relations = SemanticRelations(labelledBy = semanticIds(input.labelledBy))41
)42
}43
case None -> {}44
}45
return UiInvalidation.Layout46
}47
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<48
DisclosureBodyLayout49
> {50
val input = props.value51
if (!input.shown) {52
for (index in 0L ..< context.childCount)53
context.skipChild(index)54
return LayoutResult(55
LayoutMetrics(constraints.constrain(Size(constraints.maxWidth ?: 0.0, 0.0))),56
DisclosureBodyLayout(false, false)57
)58
}59
val child =60
context.measureChild(61
0L,62
Constraints(minWidth = constraints.minWidth, maxWidth = constraints.maxWidth)63
)64
val progress = disclosureProgress(input.presence, input.open)65
val size = constraints.constrain(Size(child.size.width, child.size.height * progress))66
val clipped =67
progress < 1.0 || size.width < child.size.width || size.height < child.size.height68
val placed = size.width > 0.0 && size.height > 0.069
if (placed)70
context.placeChild(0L, Point(0.0, 0.0))71
return LayoutResult(LayoutMetrics(size), DisclosureBodyLayout(clipped, placed))72
}73
public override fun scene(context: SceneContext, layout: DisclosureBodyLayout) {74
when (semantics) {75
case Some(val handle) -> context.semantics(76
handle,77
Rect(0.0, 0.0, context.size.width, context.size.height)78
)79
case None -> {}80
}81
if (layout.clipped)82
context.clipRect(Rect(0.0, 0.0, context.size.width, context.size.height))83
context.children()84
val current = revision85
val visible = layout.placed86
context.onFrameComplete {87
frame -> if (88
frame.submission ==89
FrameSubmission.Accepted &&90
scope.isMounted &&91
revision ==92
current93
)94
scope.input.enabled = visible && props.value.open && props.value.enabled95
}96
}97
}