/
list.go
398 lines (361 loc) · 10.5 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// SPDX-License-Identifier: Unlicense OR MIT
// This is a vendored copy of upstream's layout.List, hacked to support bottom-aligning elements, barely.
package layout
import (
"context"
"image"
"math"
rtrace "runtime/trace"
_ "unsafe"
"gioui.org/gesture"
"gioui.org/op"
"gioui.org/op/clip"
)
type scrollChild struct {
size image.Point
call op.CallOp
}
// List displays a subsection of a potentially infinitely
// large underlying list. List accepts user input to scroll
// the subsection.
type List struct {
Axis Axis
// ScrollToEnd instructs the list to stay scrolled to the far end position
// once reached. A List with ScrollToEnd == true and Position.BeforeEnd ==
// false draws its content with the last item at the bottom of the list
// area.
ScrollToEnd bool
// Alignment is the cross axis alignment of list elements.
Alignment Alignment
cs Constraints
scroll gesture.Scroll
scrollDelta int
// Position is updated during Layout. To save the list scroll position,
// just save Position after Layout finishes. To scroll the list
// programmatically, update Position (e.g. restore it from a saved value)
// before calling Layout.
Position Position
len int
// maxSize is the total size of visible children.
maxSize int
children []scrollChild
dir iterationDir
}
type iterationDir uint8
// Position is a List scroll offset represented as an offset from the top edge
// of a child element.
type Position struct {
// BeforeEnd tracks whether the List position is before the very end. We
// use "before end" instead of "at end" so that the zero value of a
// Position struct is useful.
//
// When laying out a list, if ScrollToEnd is true and BeforeEnd is false,
// then First and Offset are ignored, and the list is drawn with the last
// item at the bottom. If ScrollToEnd is false then BeforeEnd is ignored.
BeforeEnd bool
// First is the index of the first visible child.
First int
// Offset is the distance in pixels from the leading edge to the child at index
// First.
Offset int
// OffsetLast is the signed distance in pixels from the trailing edge to the
// bottom edge of the child at index First+Count.
OffsetLast int
// Count is the number of visible children.
Count int
// Length is the estimated total size of all children, measured in pixels.
Length int
ForceEndAligned bool
}
const (
iterateNone iterationDir = iota
iterateForward
iterateBackward
)
const inf = 1e6
// init prepares the list for iterating through its children with next.
func (l *List) init(gtx Context, len int) {
if l.more() {
panic("unfinished child")
}
l.cs = gtx.Constraints
l.maxSize = 0
l.children = l.children[:0]
l.len = len
l.update(gtx)
if l.Position.First < 0 {
l.Position.Offset = 0
l.Position.First = 0
}
if l.scrollToEnd() || l.Position.First > len {
l.Position.Offset = 0
l.Position.First = len
}
}
// Layout a List of len items, where each item is implicitly defined
// by the callback w. Layout can handle very large lists because it only calls
// w to fill its viewport and the distance scrolled, if any.
func (l *List) Layout(gtx Context, len int, w ListElement) Dimensions {
defer rtrace.StartRegion(context.Background(), "layout.List.Layout").End()
l.init(gtx, len)
crossMin, crossMax := axisCrossConstraint(l.Axis, gtx.Constraints)
gtx.Constraints = axisConstraints(l.Axis, 0, inf, crossMin, crossMax)
macro := op.Record(gtx.Ops)
laidOutTotalLength := 0
numLaidOut := 0
for l.next(); l.more(); l.next() {
child := op.Record(gtx.Ops)
dims := w(gtx, l.index())
call := child.Stop()
l.end(dims, call)
laidOutTotalLength += l.Axis.Convert(dims.Size).X
numLaidOut++
}
if numLaidOut > 0 {
l.Position.Length = laidOutTotalLength * len / numLaidOut
} else {
l.Position.Length = 0
}
return l.layout(gtx.Ops, macro)
}
func (l *List) scrollToEnd() bool {
return l.ScrollToEnd && !l.Position.BeforeEnd
}
// Dragging reports whether the List is being dragged.
func (l *List) Dragging() bool {
return l.scroll.State() == gesture.StateDragging
}
func (l *List) update(gtx Context) {
d := l.scroll.Update(gtx.Metric, gtx, gtx.Now, gesture.Axis(l.Axis))
l.scrollDelta = d
l.Position.Offset += d
}
// next advances to the next child.
func (l *List) next() {
l.dir = l.nextDir()
// The user scroll offset is applied after scrolling to
// list end.
if l.scrollToEnd() && !l.more() && l.scrollDelta < 0 {
l.Position.BeforeEnd = true
l.Position.Offset += l.scrollDelta
l.dir = l.nextDir()
}
}
// index is current child's position in the underlying list.
func (l *List) index() int {
switch l.dir {
case iterateBackward:
return l.Position.First - 1
case iterateForward:
return l.Position.First + len(l.children)
default:
panic("Index called before Next")
}
}
// more reports whether more children are needed.
func (l *List) more() bool {
return l.dir != iterateNone
}
func (l *List) nextDir() iterationDir {
_, vsize := axisMainConstraint(l.Axis, l.cs)
last := l.Position.First + len(l.children)
// Clamp offset.
if l.maxSize-l.Position.Offset < vsize && last == l.len {
l.Position.Offset = l.maxSize - vsize
}
if l.Position.Offset < 0 && l.Position.First == 0 {
l.Position.Offset = 0
}
// Lay out an extra (invisible) child at each end to enable focus to
// move to them, triggering automatic scroll.
firstSize, lastSize := 0, 0
if len(l.children) > 0 {
if l.Position.First > 0 {
firstChild := l.children[0]
firstSize = l.Axis.Convert(firstChild.size).X
}
if last < l.len {
lastChild := l.children[len(l.children)-1]
lastSize = l.Axis.Convert(lastChild.size).X
}
}
switch {
case len(l.children) == l.len:
return iterateNone
case l.maxSize-l.Position.Offset-lastSize < vsize:
return iterateForward
case l.Position.Offset-firstSize < 0:
return iterateBackward
}
return iterateNone
}
// End the current child by specifying its dimensions.
func (l *List) end(dims Dimensions, call op.CallOp) {
child := scrollChild{dims.Size, call}
mainSize := l.Axis.Convert(child.size).X
l.maxSize += mainSize
switch l.dir {
case iterateForward:
l.children = append(l.children, child)
case iterateBackward:
l.children = append(l.children, scrollChild{})
copy(l.children[1:], l.children)
l.children[0] = child
l.Position.First--
l.Position.Offset += mainSize
default:
panic("call Next before End")
}
l.dir = iterateNone
}
// Layout the List and return its dimensions.
func (l *List) layout(ops *op.Ops, macro op.MacroOp) Dimensions {
if l.more() {
panic("unfinished child")
}
mainMin, mainMax := axisMainConstraint(l.Axis, l.cs)
children := l.children
var first scrollChild
// Skip invisible children.
for len(children) > 0 {
child := children[0]
sz := child.size
mainSize := l.Axis.Convert(sz).X
if l.Position.Offset < mainSize {
// First child is partially visible.
break
}
l.Position.First++
l.Position.Offset -= mainSize
first = child
children = children[1:]
}
size := -l.Position.Offset
var maxCross int
var last scrollChild
for i, child := range children {
sz := l.Axis.Convert(child.size)
if c := sz.Y; c > maxCross {
maxCross = c
}
size += sz.X
if size >= mainMax {
if i < len(children)-1 {
last = children[i+1]
}
children = children[:i+1]
break
}
}
l.Position.Count = len(children)
l.Position.OffsetLast = mainMax - size
// ScrollToEnd lists are end aligned.
if space := l.Position.OffsetLast; l.Position.ForceEndAligned || l.ScrollToEnd && space > 0 {
l.Position.ForceEndAligned = false
l.Position.Offset -= space
if l.Position.Offset < 0 {
l.Position.Offset = 0
}
}
pos := -l.Position.Offset
layout := func(child scrollChild) {
sz := l.Axis.Convert(child.size)
var cross int
switch l.Alignment {
case End:
cross = maxCross - sz.Y
case Middle:
cross = (maxCross - sz.Y) / 2
}
childSize := sz.X
min := pos
if min < 0 {
min = 0
}
pt := l.Axis.Convert(image.Pt(pos, cross))
trans := op.Offset(pt).Push(ops)
child.call.Add(ops)
trans.Pop()
pos += childSize
}
// Lay out leading invisible child.
if first != (scrollChild{}) {
sz := l.Axis.Convert(first.size)
pos -= sz.X
layout(first)
}
for _, child := range children {
layout(child)
}
// Lay out trailing invisible child.
if last != (scrollChild{}) {
layout(last)
}
atStart := l.Position.First == 0 && l.Position.Offset <= 0
atEnd := l.Position.First+len(children) == l.len && mainMax >= pos
if atStart && l.scrollDelta < 0 || atEnd && l.scrollDelta > 0 {
l.scroll.Stop()
}
l.Position.BeforeEnd = !atEnd
if pos < mainMin {
pos = mainMin
}
if pos > mainMax {
pos = mainMax
}
if crossMin, crossMax := axisCrossConstraint(l.Axis, l.cs); maxCross < crossMin {
maxCross = crossMin
} else if maxCross > crossMax {
maxCross = crossMax
}
dims := l.Axis.Convert(image.Pt(pos, maxCross))
call := macro.Stop()
defer clip.Rect(image.Rectangle{Max: dims}).Push(ops).Pop()
min, max := int(-inf), int(inf)
if l.Position.First == 0 {
// Use the size of the invisible part as scroll boundary.
min = -l.Position.Offset
if min > 0 {
min = 0
}
}
if l.Position.First+l.Position.Count == l.len {
max = -l.Position.OffsetLast
if max < 0 {
max = 0
}
}
scrollRange := image.Rectangle{
Min: l.Axis.Convert(image.Pt(min, 0)),
Max: l.Axis.Convert(image.Pt(max, 0)),
}
l.scroll.Add(ops, scrollRange)
call.Add(ops)
return Dimensions{Size: dims}
}
// ScrollBy scrolls the list by a relative amount of items.
//
// Fractional scrolling may be inaccurate for items of differing
// dimensions. This includes scrolling by integer amounts if the current
// l.Position.Offset is non-zero.
func (l *List) ScrollBy(num float32) {
// Split number of items into integer and fractional parts
i, f := math.Modf(float64(num))
// Scroll by integer amount of items
l.Position.First += int(i)
// Adjust Offset to account for fractional items. If Offset gets so large that it amounts to an entire item, then
// the layout code will handle that for us and adjust First and Offset accordingly.
itemHeight := float64(l.Position.Length) / float64(l.len)
l.Position.Offset += int(math.Round(itemHeight * f))
// First and Offset can go out of bounds, but the layout code knows how to handle that.
// Ensure that the list pays attention to the Offset field when the scrollbar drag
// is started while the bar is at the end of the list. Without this, the scrollbar
// cannot be dragged away from the end.
l.Position.BeforeEnd = true
}
// ScrollTo scrolls to the specified item.
func (l *List) ScrollTo(n int) {
l.Position.First = n
l.Position.Offset = 0
l.Position.BeforeEnd = true
}