scoop:scoop.ui.component:0.1.0
src/tabs/tabs.scoop
Selected production source · src/tabs/tabs.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
private class TabRelations(val key: ViewKey) {10
val trigger = SemanticsReference()11
val panel = SemanticsReference()12
}14
public class Tabs<T: Equals<T>> public constructor(15
private val props: Props<TabsProps<T>>,16
scope: ComponentScope17
): 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.value30
if (!input.enabled)31
return SemanticActionResult.Rejected32
for (entry in input.entries) {33
if (entry.choice.key != key)34
continue35
if (!entry.choice.enabled)36
return SemanticActionResult.Rejected37
select(key)38
return if (props.value.selected.value == Some(entry.choice.value))39
SemanticActionResult.Completed40
else41
SemanticActionResult.Rejected42
}43
return SemanticActionResult.Unavailable44
}45
public override fun render(): View {46
val input = props.value47
val theme = themes.read(input.theme)48
val density = theme.tokens.typography.action.fontSize / 14.049
val keys = Array<ViewKey>(input.entries.size) { index -> input.entries[index].choice.key }50
val previous = links51
links =52
Array<TabRelations>(keys.size) {53
index ->54
var found: TabRelations? = None55
for (entry in previous)56
if (entry.key == keys[index])57
found = Some(entry)58
found ?: TabRelations(keys[index])59
}60
val links = this.links61
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.key72
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.Center84
else85
TextAlignment.Start86
}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.fonts98
},99
label.settings,100
label.inlines,101
label.theme,102
label.baseStyle,103
label.underline,104
label.semantics105
)106
)107
else -> configured108
}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 * density140
else141
0.0,142
input.loop,143
input.enabled,144
styleOverride(),145
when (selected) {146
case Some(val index) -> Some(focus[index]);147
case None -> None148
},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.enabled170
),171
{ current, mount -> DisclosureBody(current, scope = mount) },172
if (173
active ||174
(entry.retention ?: input.retention) ==175
ContentRetention.KeepMounted176
)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].trigger189
).id190
)191
),192
mergeLabels = false,193
reference = Some(links[index].panel)194
)195
]196
else197
[],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
panels212
)213
]214
),215
visual = input.style216
),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.reference224
)225
}226
}228
private 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
else234
None235
)236
this.corners(237
Corners.all(238
if (variant == TabsVariant.Default)239
theme.tokens.corners.large240
else241
0.0242
)243
)244
}