scoop:scoop.ui.component:0.1.0
src/disclosure/collapsible.scoop
Selected production source · src/disclosure/collapsible.scoop
1
package scoop.ui.component3
import scoop.graphics.text.FontCollection4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.window.accessibility.*10
public enum ContentRetention {11
Dispose, KeepMounted12
}14
public class CollapsibleProps public constructor(15
public val open: Binding<Boolean>,16
public val content: View,17
public val trigger: TriggerSpec? = None,18
public val enabled: Boolean = true,19
public val retention: ContentRetention = ContentRetention.Dispose,20
public val spacing: Double = 8.0,21
public val transition: StyleValue<AnimationSpec?> = StyleValue.Inherit,22
public val onOpenChange: (Boolean) -> Unit = {},23
public val fonts: FontCollection? = None,24
public val theme: ThemeSource? = None,25
public val style: StyleOverride = styleOverride()26
) {27
init {28
nonnegative(spacing, "collapsible spacing must be finite and nonnegative")29
}30
}32
public class Collapsible public constructor(33
private val props: Props<CollapsibleProps>,34
scope: ComponentScope35
): Component(scope) {36
private val themes = ThemeAccess(scope)37
private val motion = scope.presence(props.value.open.value)38
private val triggerSemantics = SemanticsReference()39
private val bodySemantics = SemanticsReference()40
public override fun onMount() {41
themes.mount(props.value.theme)42
}43
private fun setOpen(value: Boolean): SemanticActionResult {44
val input = props.value45
if (!input.enabled || !(input.trigger?.enabled ?: false))46
return SemanticActionResult.Rejected47
val previous = input.open.value48
input.open.value = value49
val accepted = input.open.value50
if (accepted != previous)51
props.value.onOpenChange(accepted)52
return if (accepted == value)53
SemanticActionResult.Completed54
else55
SemanticActionResult.Rejected56
}57
public override fun render(): View {58
val input = props.value59
val theme = themes.read(input.theme)60
val open = input.open.value61
val presence = Some(motion)62
val shown = open || motion.isPresent63
val transition =64
theme.tokens.motion.resolve(65
input.transition.resolve(66
Some(67
AnimationSpec.Tween(68
Duration.milliseconds(200L),69
Easing.cubicBezier(0.4, 0.0, 0.2, 1.0)70
)71
)72
)73
)74
val trigger =75
when (input.trigger) {76
case Some(val value) -> disclosureTrigger(77
value,78
input.enabled,79
input.fonts,80
input.theme,81
presence,82
open,83
{ props.value.open.value },84
{ value -> setOpen(value) },85
value.semantics.copy {86
reference =87
Some(value.semantics.reference ?: this @Collapsible.triggerSemantics)88
relations = semanticControls(relations, this @Collapsible.bodySemantics.id)89
}90
)91
case None -> empty()92
}93
return panelFrame(94
element(95
DisclosureLayoutProps(96
presence,97
open,98
PresenceOptions(transition, transition),99
panelPresent(input.trigger),100
input.spacing101
),102
{ current, mount -> DisclosureLayout(current, scope = mount) },103
[104
trigger,105
element(106
DisclosureBodyProps(107
presence,108
open,109
shown,110
input.enabled,111
Some(bodySemantics),112
when (input.trigger) {113
case Some(val spec) -> (114
spec.semantics.reference ?: triggerSemantics115
).id116
case None -> None117
}118
),119
{ current, mount -> DisclosureBody(current, scope = mount) },120
if (shown || input.retention == ContentRetention.KeepMounted)121
[input.content]122
else123
[]124
)125
]126
),127
visual = input.style128
)129
}130
}132
public fun collapsible(133
open: Binding<Boolean>,134
content: View,135
configure: CollapsibleBuilder.() -> Unit = {}136
): View = makeCollapsible(open, content, configure)138
internal fun disclosureProgress(presence: Presence?, open: Boolean): Double =139
presence?.snapshot?.progress ?:140
(141
if (open)142
1.0143
else144
0.0145
)