scoop:scoop.ui.component:0.1.0
src/navigation/pagination.scoop
Selected production source · src/navigation/pagination.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.window.accessibility.*7
import scoop.ui.component.row as makeRow9
public class Pagination public constructor(10
private val props: Props<PaginationProps>,11
scope: ComponentScope12
): Component(scope) {13
private val themes = ThemeAccess(scope)14
public override fun onMount() {15
themes.mount(props.value.theme)16
}17
private fun select(page: Long) {18
if (selectPage(props.value, page))19
props.value.onValueChange(props.value.page.value)20
}21
private fun selectSemantically(page: Long): SemanticActionResult {22
if (!scope.isMounted)23
return SemanticActionResult.Unavailable24
if (!props.value.enabled)25
return SemanticActionResult.Rejected26
select(page)27
return if (props.value.page.value == page)28
SemanticActionResult.Completed29
else30
SemanticActionResult.Rejected31
}32
private fun move(forward: Boolean) {33
val input = props.value34
when (paginationNeighbor(input.page.value, input.pageCount, forward)) {35
case Some(val page) -> select(page);36
case None -> {}37
}38
}39
public override fun render(): View {40
val input = props.value41
val theme = themes.read(input.theme)42
val density = theme.tokens.typography.action.fontSize / 14.043
val selected = input.page.value44
val entries =45
input.items ?:46
paginationItems(selected, input.pageCount, input.siblings, input.boundaries)47
val controls =48
makeRow {49
if (input.showPrevious)50
this.add(this @Pagination.edge(input, false))51
for (index in 0L ..< entries.size) {52
when (entries[index]) {53
case Page(val number) -> this.add(54
panelFrame(55
semanticButton(56
ButtonProps(57
when (input.pageContent) {58
case Some(val build) -> ButtonContent.Resolved {59
appearance -> build(number, appearance)60
}61
case None -> navigationLabel(62
ButtonContent.Label(63
LabelProps(64
LabelText.Plain(65
input.pageLabel(number),66
None67
)68
)69
),70
input.fonts,71
TextAlignment.Center72
)73
},74
{ this @Pagination.select(number) },75
input.enabled,76
input.buttonStyle,77
input.theme,78
input.pageFocus(number),79
variant =80
if (selected == number)81
ButtonVariant.Outline82
else83
ButtonVariant.Ghost,84
size = ButtonSize.Icon,85
semantics = input.pageSemantics(number)86
),87
SemanticRole.Button,88
input.pageSemantics(number).properties(89
enabled = input.enabled,90
selected = Some(selected == number)91
).copy {92
if (name == None)93
name = Some(input.pageLabel(number))94
},95
SemanticActions(96
invoke =97
Some { this @Pagination.selectSemantically(number) },98
setSelected =99
Some {100
value -> if (value)101
this @Pagination.selectSemantically(number)102
else103
if (input.page.value == number)104
SemanticActionResult.Rejected105
else106
SemanticActionResult.Completed107
}108
)109
),110
key = ViewKey.Number(number)111
)112
)113
case Ellipsis -> this.add(114
panelFrame(115
input.ellipsis ?:116
navigationEllipsis(117
32.0 * density,118
16.0 * density,119
theme.tokens.palette.background.foreground120
),121
key = ViewKey.Text(f"gap-${index}")122
)123
)124
}125
}126
if (input.showNext)127
this.add(this @Pagination.edge(input, true))128
this.spacing = 2.0 * density129
this.alignment = CrossAlignment.Center130
}131
return semanticContent(132
panelFrame(133
makeRow {134
this.item(empty()) { this.grow = 1.0 }135
this.add(controls)136
this.item(empty()) { this.grow = 1.0 }137
this.alignment = CrossAlignment.Center138
},139
visual = input.style140
),141
SemanticRole.Group,142
input.semantics.properties(143
enabled = input.enabled,144
required = input.pageCount > 0L145
).copy { selectionMode = Some(SemanticSelectionMode.Single) },146
relations = input.semantics.relations,147
mergeLabels = false,148
reference = input.semantics.reference149
)150
}151
private fun edge(input: PaginationProps, forward: Boolean): View {152
val available = paginationNeighbor(input.page.value, input.pageCount, forward) != None153
return component(154
ButtonProps(155
ButtonContent.Resolved {156
appearance -> makeRow {157
if (!forward)158
this.add(159
navigationIcon(160
NavigationGlyph.Start,161
appearance.iconSize,162
appearance.text.color163
)164
)165
if (!input.compact)166
this.add(167
actionContent(168
navigationLabel(169
ButtonContent.Label(170
LabelProps(171
LabelText.Plain(172
if (forward)173
input.nextText174
else175
input.previousText,176
None177
)178
)179
),180
input.fonts181
),182
appearance183
)184
)185
if (forward)186
this.add(187
navigationIcon(188
NavigationGlyph.End,189
appearance.iconSize,190
appearance.text.color191
)192
)193
this.spacing = appearance.spacing194
this.alignment = CrossAlignment.Center195
}196
},197
{ move(forward) },198
input.enabled && available,199
input.buttonStyle,200
input.theme,201
if (forward)202
input.nextFocus203
else204
input.previousFocus,205
variant = ButtonVariant.Ghost,206
size =207
if (input.compact)208
ButtonSize.Icon209
else210
ButtonSize.Default,211
semantics =212
ComponentSemantics(213
name =214
Some(215
if (forward)216
input.nextText217
else218
input.previousText219
)220
)221
),222
{ current, mount -> Button(current, scope = mount) },223
ViewKey.Text(224
if (forward)225
"next"226
else227
"previous"228
)229
)230
}231
}