Scoop API

scoop:scoop.ui.component:0.1.0

src/disclosure/collapsible.scoop

Selected production source · src/disclosure/collapsible.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.window.accessibility.*
10public enum ContentRetention {
11 Dispose, KeepMounted
14public 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 }
32public class Collapsible public constructor(
33 private val props: Props<CollapsibleProps>,
34 scope: ComponentScope
35): 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.value
45 if (!input.enabled || !(input.trigger?.enabled ?: false))
46 return SemanticActionResult.Rejected
47 val previous = input.open.value
48 input.open.value = value
49 val accepted = input.open.value
50 if (accepted != previous)
51 props.value.onOpenChange(accepted)
52 return if (accepted == value)
53 SemanticActionResult.Completed
54 else
55 SemanticActionResult.Rejected
56 }
57 public override fun render(): View {
58 val input = props.value
59 val theme = themes.read(input.theme)
60 val open = input.open.value
61 val presence = Some(motion)
62 val shown = open || motion.isPresent
63 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.spacing
101 ),
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 ?: triggerSemantics
115 ).id
116 case None -> None
117 }
118 ),
119 { current, mount -> DisclosureBody(current, scope = mount) },
120 if (shown || input.retention == ContentRetention.KeepMounted)
121 [input.content]
122 else
123 []
124 )
125 ]
126 ),
127 visual = input.style
128 )
129 }
132public fun collapsible(
133 open: Binding<Boolean>,
134 content: View,
135 configure: CollapsibleBuilder.() -> Unit = {}
136): View = makeCollapsible(open, content, configure)
138internal fun disclosureProgress(presence: Presence?, open: Boolean): Double =
139 presence?.snapshot?.progress ?:
140 (
141 if (open)
142 1.0
143 else
144 0.0
145 )