Scoop API

scoop:scoop.ui.component:0.1.0

src/navigation/pagination_values.scoop

Selected production source · src/navigation/pagination_values.scoop

1package scoop.ui.component
3import scoop.graphics.text.FontCollection
4import scoop.ui.*
5import scoop.ui.style.*
7public enum PaginationItem {
8 Page(Long), Ellipsis
9}
11public 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 }
61internal fun validatePagination(count: Long, siblings: Long, boundaries: Long) {
62 requireComponent(
63 count >= 0L && siblings >= 0L && boundaries >= 0L,
64 "pagination counts must be nonnegative"
65 )
68public fun paginationItems(
69 page: Long,
70 pageCount: Long,
71 siblings: Long = 1L,
72 boundaries: Long = 1L
73): Array<PaginationItem> {
74 validatePagination(pageCount, siblings, boundaries)
75 if (pageCount == 0L)
76 return []
77 val anchor =
78 if (page < 1L)
79 1L
80 else
81 if (page > pageCount)
82 pageCount
83 else
84 page
85 val edge =
86 if (boundaries > pageCount)
87 pageCount
88 else
89 boundaries
90 val start =
91 if (siblings >= anchor - 1L)
92 1L
93 else
94 anchor - siblings
95 val end =
96 if (siblings >= pageCount - anchor)
97 pageCount
98 else
99 anchor + siblings
100 val result = ArrayList<PaginationItem>()
101 var last = 0L
102 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 - last
108 if (tail == 1L)
109 result.add(PaginationItem.Page(pageCount))
110 else
111 if (tail > 1L)
112 result.add(PaginationItem.Ellipsis)
113 return result.toArray()
116private fun appendPaginationRange(
117 result: ArrayList<PaginationItem>,
118 start: Long,
119 end: Long,
120 previous: Long
121): Long {
122 if (end <= previous)
123 return previous
124 var next =
125 if (start <= previous)
126 previous + 1L
127 else
128 start
129 val gap = next - previous - 1L
130 if (gap == 1L)
131 result.add(PaginationItem.Page(previous + 1L))
132 else
133 if (gap > 1L)
134 result.add(PaginationItem.Ellipsis)
135 while (true) {
136 result.add(PaginationItem.Page(next))
137 if (next == end)
138 return end
139 next += 1L
140 }
143internal fun paginationNeighbor(page: Long, count: Long, forward: Boolean): Long? {
144 if (count == 0L)
145 return None
146 if (forward)
147 return if (page >= count)
148 None
149 else
150 Some(
151 if (page < 1L)
152 1L
153 else
154 page + 1L
155 )
156 return if (page <= 1L)
157 None
158 else
159 Some(
160 if (page > count)
161 count
162 else
163 page - 1L
164 )
167internal fun selectPage(input: PaginationProps, desired: Long): Boolean {
168 if (!input.enabled || desired < 1L || desired > input.pageCount)
169 return false
170 val previous = input.page.value
171 if (previous == desired)
172 return false
173 input.page.value = desired
174 return input.page.value != previous