scoop:scoop.ui.component:0.1.0
src/navigation/pagination_values.scoop
Selected production source · src/navigation/pagination_values.scoop
1
package scoop.ui.component3
import scoop.graphics.text.FontCollection4
import scoop.ui.*5
import scoop.ui.style.*7
public enum PaginationItem {8
Page(Long), Ellipsis9
}11
public class PaginationProps public constructor(12
public val page: Binding<Long>,13
public val pageCount: Long,14
public val siblings: Long = 1L,15
public val boundaries: Long = 1L,16
public val items: Array<PaginationItem>? = None,17
public val enabled: Boolean = true,18
public val showPrevious: Boolean = true,19
public val showNext: Boolean = true,20
public val compact: Boolean = false,21
public val previousText: String = "Previous",22
public val nextText: String = "Next",23
public val pageLabel: (Long) -> String = { value -> f"${value}" },24
public val pageContent: ((Long, ActionContentStyle) -> View)? = None,25
public val ellipsis: View? = None,26
public val fonts: FontCollection? = None,27
public val theme: ThemeSource? = None,28
public val buttonStyle: ButtonStyle = buttonStyle(),29
public val style: StyleOverride = styleOverride(),30
public val previousFocus: FocusOptions = FocusOptions(),31
public val nextFocus: FocusOptions = FocusOptions(),32
public val pageFocus: (Long) -> FocusOptions = { _ -> FocusOptions() },33
public val onValueChange: (Long) -> Unit = {},34
public val semantics: ComponentSemantics = ComponentSemantics(),35
public val pageSemantics: (Long) -> ComponentSemantics = { _ -> ComponentSemantics() }36
) {37
init {38
validatePagination(pageCount, siblings, boundaries)39
when (items) {40
case Some(val entries) -> for (index in 0L ..< entries.size) {41
when (entries[index]) {42
case Page(val number) -> {43
requireComponent(44
number > 0L && number <= pageCount,45
"pagination item is outside the page range"46
)47
for (previous in 0L ..< index)48
requireComponent(49
entries[previous] != entries[index],50
"duplicate pagination page"51
)52
}53
case Ellipsis -> {}54
}55
}56
case None -> {}57
}58
}59
}61
internal fun validatePagination(count: Long, siblings: Long, boundaries: Long) {62
requireComponent(63
count >= 0L && siblings >= 0L && boundaries >= 0L,64
"pagination counts must be nonnegative"65
)66
}68
public fun paginationItems(69
page: Long,70
pageCount: Long,71
siblings: Long = 1L,72
boundaries: Long = 1L73
): Array<PaginationItem> {74
validatePagination(pageCount, siblings, boundaries)75
if (pageCount == 0L)76
return []77
val anchor =78
if (page < 1L)79
1L80
else81
if (page > pageCount)82
pageCount83
else84
page85
val edge =86
if (boundaries > pageCount)87
pageCount88
else89
boundaries90
val start =91
if (siblings >= anchor - 1L)92
1L93
else94
anchor - siblings95
val end =96
if (siblings >= pageCount - anchor)97
pageCount98
else99
anchor + siblings100
val result = ArrayList<PaginationItem>()101
var last = 0L102
if (edge > 0L)103
last = appendPaginationRange(result, 1L, edge, last)104
last = appendPaginationRange(result, start, end, last)105
if (edge > 0L)106
last = appendPaginationRange(result, pageCount - edge + 1L, pageCount, last)107
val tail = pageCount - last108
if (tail == 1L)109
result.add(PaginationItem.Page(pageCount))110
else111
if (tail > 1L)112
result.add(PaginationItem.Ellipsis)113
return result.toArray()114
}116
private fun appendPaginationRange(117
result: ArrayList<PaginationItem>,118
start: Long,119
end: Long,120
previous: Long121
): Long {122
if (end <= previous)123
return previous124
var next =125
if (start <= previous)126
previous + 1L127
else128
start129
val gap = next - previous - 1L130
if (gap == 1L)131
result.add(PaginationItem.Page(previous + 1L))132
else133
if (gap > 1L)134
result.add(PaginationItem.Ellipsis)135
while (true) {136
result.add(PaginationItem.Page(next))137
if (next == end)138
return end139
next += 1L140
}141
}143
internal fun paginationNeighbor(page: Long, count: Long, forward: Boolean): Long? {144
if (count == 0L)145
return None146
if (forward)147
return if (page >= count)148
None149
else150
Some(151
if (page < 1L)152
1L153
else154
page + 1L155
)156
return if (page <= 1L)157
None158
else159
Some(160
if (page > count)161
count162
else163
page - 1L164
)165
}167
internal fun selectPage(input: PaginationProps, desired: Long): Boolean {168
if (!input.enabled || desired < 1L || desired > input.pageCount)169
return false170
val previous = input.page.value171
if (previous == desired)172
return false173
input.page.value = desired174
return input.page.value != previous175
}