Scoop API

scoop:scoop.ui.component:0.1.0

src/tabs/tabs.scoop

Selected production source · src/tabs/tabs.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9private class TabRelations(val key: ViewKey) {
10 val trigger = SemanticsReference()
11 val panel = SemanticsReference()
14public class Tabs<T: Equals<T>> public constructor(
15 private val props: Props<TabsProps<T>>,
16 scope: ComponentScope
17): Component(scope) {
18 private val handles = ChoiceHandles()
19 private val themes = ThemeAccess(scope)
20 private var links: Array<TabRelations> = []
21 public override fun onMount() {
22 themes.mount(props.value.theme)
23 }
24 private fun select(key: ViewKey) {
25 if (selectTab(props.value, key))
26 props.value.onValueChange(props.value.selected.value)
27 }
28 private fun selectSemantically(key: ViewKey): SemanticActionResult {
29 val input = props.value
30 if (!input.enabled)
31 return SemanticActionResult.Rejected
32 for (entry in input.entries) {
33 if (entry.choice.key != key)
34 continue
35 if (!entry.choice.enabled)
36 return SemanticActionResult.Rejected
37 select(key)
38 return if (props.value.selected.value == Some(entry.choice.value))
39 SemanticActionResult.Completed
40 else
41 SemanticActionResult.Rejected
42 }
43 return SemanticActionResult.Unavailable
44 }
45 public override fun render(): View {
46 val input = props.value
47 val theme = themes.read(input.theme)
48 val density = theme.tokens.typography.action.fontSize / 14.0
49 val keys = Array<ViewKey>(input.entries.size) { index -> input.entries[index].choice.key }
50 val previous = links
51 links =
52 Array<TabRelations>(keys.size) {
53 index ->
54 var found: TabRelations? = None
55 for (entry in previous)
56 if (entry.key == keys[index])
57 found = Some(entry)
58 found ?: TabRelations(keys[index])
59 }
60 val links = this.links
61 val focus =
62 handles.resolve(
63 keys,
64 Array<FocusOptions>(keys.size) { index -> input.entries[index].focus }
65 )
66 val selected = tabsIndex(input.entries, input.selected.value)
67 val headings =
68 Array<View>(keys.size) {
69 index ->
70 val entry = input.entries[index]
71 val key = entry.choice.key
72 val configured =
73 entry.trigger ?:
74 ButtonContent.Label(
75 LabelProps(
76 LabelText.Plain(entry.choice.label, None),
77 input.fonts,
78 Some(
79 paragraphStyle {
80 this.maxLines = Some(1L)
81 this.alignment =
82 if (input.axis == FlexAxis.Horizontal)
83 TextAlignment.Center
84 else
85 TextAlignment.Start
86 }
87 )
88 )
89 )
90 val content =
91 when (configured) {
92 case Label(val label) -> ButtonContent.Label(
93 LabelProps(
94 label.text,
95 when (label.fonts) {
96 case Some(_) -> label.fonts;
97 case None -> input.fonts
98 },
99 label.settings,
100 label.inlines,
101 label.theme,
102 label.baseStyle,
103 label.underline,
104 label.semantics
105 )
106 )
107 else -> configured
108 }
109 component(
110 TabControlProps(
111 content,
112 selected == Some(index),
113 input.enabled && entry.choice.enabled,
114 input.variant,
115 input.axis,
116 entry.triggerStyle ?: buttonStyle { this.kind = SurfaceKind.Plain },
117 choiceFocus(entry.focus, focus[index]),
118 theme,
119 { select(key) },
120 { selectSemantically(key) },
121 entry.semantics.copy {
122 if (name == None && relations.labelledBy.size == 0L)
123 name = Some(entry.choice.label)
124 reference = Some(entry.semantics.reference ?: links[index].trigger)
125 relations = semanticControls(relations, links[index].panel.id)
126 }
127 ),
128 { current, mount -> TabControl(current, scope = mount) }
129 )
130 }
131 val header =
132 panelFrame(
133 choiceLayout(
134 headings,
135 keys,
136 focus,
137 input.axis,
138 if (input.variant == TabsVariant.Line)
139 4.0 * density
140 else
141 0.0,
142 input.loop,
143 input.enabled,
144 styleOverride(),
145 when (selected) {
146 case Some(val index) -> Some(focus[index]);
147 case None -> None
148 },
149 {
150 handle -> if (props.value.activation == TabActivation.Auto)
151 for (index in 0L ..< focus.size)
152 if (focus[index] === handle)
153 select(keys[index])
154 }
155 ),
156 padding = Insets.all(3.0 * density),
157 visual = tabsListStyle(theme, input.variant).then(input.listStyle)
158 )
159 val panels =
160 Array<View>(keys.size) {
161 index ->
162 val entry = input.entries[index]
163 val active = selected == Some(index)
164 element(
165 DisclosureBodyProps(
166 None,
167 active,
168 active,
169 input.enabled && entry.choice.enabled
170 ),
171 { current, mount -> DisclosureBody(current, scope = mount) },
172 if (
173 active ||
174 (entry.retention ?: input.retention) ==
175 ContentRetention.KeepMounted
176 )
177 [
178 semanticContent(
179 panelFrame(entry.content, visual = entry.panelStyle),
180 SemanticRole.Group,
181 SemanticProperties(hidden = !active),
182 relations =
183 SemanticRelations(
184 labelledBy =
185 semanticIds(
186 (
187 entry.semantics.reference ?:
188 links[index].trigger
189 ).id
190 )
191 ),
192 mergeLabels = false,
193 reference = Some(links[index].panel)
194 )
195 ]
196 else
197 [],
198 keys[index]
199 )
200 }
201 return semanticContent(
202 panelFrame(
203 element(
204 TabsLayoutProps(input.axis, input.spacing),
205 { current, mount -> TabsLayout(current, scope = mount) },
206 [
207 header,
208 element(
209 selected,
210 { current, mount -> TabPanels(current, scope = mount) },
211 panels
212 )
213 ]
214 ),
215 visual = input.style
216 ),
217 SemanticRole.TabGroup,
218 input.semantics.properties(enabled = input.enabled, required = true).copy {
219 selectionMode = Some(SemanticSelectionMode.Single)
220 },
221 relations = input.semantics.relations,
222 mergeLabels = false,
223 reference = input.semantics.reference
224 )
225 }
228private fun tabsListStyle(theme: Theme, variant: TabsVariant): StyleOverride =
229 styleOverride {
230 this.background(
231 if (variant == TabsVariant.Default)
232 Some(Paint(theme.tokens.palette.muted.background))
233 else
234 None
235 )
236 this.corners(
237 Corners.all(
238 if (variant == TabsVariant.Default)
239 theme.tokens.corners.large
240 else
241 0.0
242 )
243 )
244 }