Scoop API

scoop:scoop.ui.component:0.1.0

src/navigation/pagination.scoop

Selected production source · src/navigation/pagination.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.window.accessibility.*
7import scoop.ui.component.row as makeRow
9public class Pagination public constructor(
10 private val props: Props<PaginationProps>,
11 scope: ComponentScope
12): 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.Unavailable
24 if (!props.value.enabled)
25 return SemanticActionResult.Rejected
26 select(page)
27 return if (props.value.page.value == page)
28 SemanticActionResult.Completed
29 else
30 SemanticActionResult.Rejected
31 }
32 private fun move(forward: Boolean) {
33 val input = props.value
34 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.value
41 val theme = themes.read(input.theme)
42 val density = theme.tokens.typography.action.fontSize / 14.0
43 val selected = input.page.value
44 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 None
67 )
68 )
69 ),
70 input.fonts,
71 TextAlignment.Center
72 )
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.Outline
82 else
83 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 else
103 if (input.page.value == number)
104 SemanticActionResult.Rejected
105 else
106 SemanticActionResult.Completed
107 }
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.foreground
120 ),
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 * density
129 this.alignment = CrossAlignment.Center
130 }
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.Center
138 },
139 visual = input.style
140 ),
141 SemanticRole.Group,
142 input.semantics.properties(
143 enabled = input.enabled,
144 required = input.pageCount > 0L
145 ).copy { selectionMode = Some(SemanticSelectionMode.Single) },
146 relations = input.semantics.relations,
147 mergeLabels = false,
148 reference = input.semantics.reference
149 )
150 }
151 private fun edge(input: PaginationProps, forward: Boolean): View {
152 val available = paginationNeighbor(input.page.value, input.pageCount, forward) != None
153 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.color
163 )
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.nextText
174 else
175 input.previousText,
176 None
177 )
178 )
179 ),
180 input.fonts
181 ),
182 appearance
183 )
184 )
185 if (forward)
186 this.add(
187 navigationIcon(
188 NavigationGlyph.End,
189 appearance.iconSize,
190 appearance.text.color
191 )
192 )
193 this.spacing = appearance.spacing
194 this.alignment = CrossAlignment.Center
195 }
196 },
197 { move(forward) },
198 input.enabled && available,
199 input.buttonStyle,
200 input.theme,
201 if (forward)
202 input.nextFocus
203 else
204 input.previousFocus,
205 variant = ButtonVariant.Ghost,
206 size =
207 if (input.compact)
208 ButtonSize.Icon
209 else
210 ButtonSize.Default,
211 semantics =
212 ComponentSemantics(
213 name =
214 Some(
215 if (forward)
216 input.nextText
217 else
218 input.previousText
219 )
220 )
221 ),
222 { current, mount -> Button(current, scope = mount) },
223 ViewKey.Text(
224 if (forward)
225 "next"
226 else
227 "previous"
228 )
229 )
230 }