-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
list.go
227 lines (198 loc) · 7.68 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
package theme
// This is copied almost verbatim from material, but uses our scrollbar.
import (
"context"
"image"
"math"
rtrace "runtime/trace"
"honnef.co/go/gotraceui/layout"
"honnef.co/go/gotraceui/widget"
"gioui.org/op"
"gioui.org/op/clip"
)
// FromListPosition converts a layout.Position into two floats representing
// the location of the viewport on the underlying content. It needs to know
// the number of elements in the list and the major-axis size of the list
// in order to do this. The returned values will be in the range [0,1], and
// start will be less than or equal to end.
func FromListPosition(lp layout.Position, elements int, majorAxisSize int) (start, end float32) {
// Approximate the size of the scrollable content.
lengthPx := float32(lp.Length)
meanElementHeight := lengthPx / float32(elements)
// Determine how much of the content is visible.
listOffsetF := float32(lp.Offset)
visiblePx := float32(majorAxisSize)
visibleFraction := visiblePx / lengthPx
// Compute the location of the beginning of the viewport.
viewportStart := (float32(lp.First)*meanElementHeight + listOffsetF) / lengthPx
return viewportStart, clamp1(viewportStart + visibleFraction)
}
// AnchorStrategy defines a means of attaching a scrollbar to content.
type AnchorStrategy uint8
const (
// Occupy reserves space for the scrollbar, making the underlying
// content region smaller on one axis.
Occupy AnchorStrategy = iota
// Overlay causes the scrollbar to float atop the content without
// occupying any space. Content in the underlying area can be occluded
// by the scrollbar.
Overlay
)
// ListStyle configures the presentation of a layout.List with a scrollbar.
type ListStyle struct {
state *widget.List
Main ScrollbarStyle
Cross ScrollbarStyle
AnchorStrategy
EnableCrossScrolling bool
}
// List constructs a ListStyle using the provided theme and state.
func List(th *Theme, state *widget.List) ListStyle {
return ListStyle{
state: state,
Cross: Scrollbar(th, &state.Cross),
Main: Scrollbar(th, &state.Main),
}
}
// Layout the list and its scrollbar.
func (l ListStyle) Layout(win *Window, gtx layout.Context, length int, w layout.ListElement) layout.Dimensions {
defer rtrace.StartRegion(context.Background(), "theme.ListStyle.Layout").End()
// originalConstraints are the constraints that the user passed to ListStyle.Layout. These are the constraints in
// which the list and its scrollbars have to fit.
originalConstraints := gtx.Constraints
// Determine how much space the scrollbars occupy.
mainBarWidth := gtx.Dp(l.Main.Width())
crossBarWidth := gtx.Dp(l.Cross.Width())
if l.AnchorStrategy == Occupy {
// Reserve space for the scrollbar using the gtx constraints.
max := l.state.Axis.Convert(gtx.Constraints.Max)
min := l.state.Axis.Convert(gtx.Constraints.Min)
max.Y -= mainBarWidth
if max.Y < 0 {
max.Y = 0
}
min.Y -= mainBarWidth
if min.Y < 0 {
min.Y = 0
}
if l.EnableCrossScrolling {
max.X -= crossBarWidth
if max.X < 0 {
max.X = 0
}
min.X -= crossBarWidth
if min.X < 0 {
min.X = 0
}
}
gtx.Constraints.Max = l.state.Axis.Convert(max)
gtx.Constraints.Min = l.state.Axis.Convert(min)
}
// tightListConstraints are the original constraints reduced by the space reserved for scrollbars. These will limit
// the visible portion of the layout.List.
tightListConstraints := gtx.Constraints
if l.EnableCrossScrolling {
// When cross scrolling is enabled we allow the list to draw itself however wide it wants. We'll later pan and
// crop the visible portion.
*layout.Cross(l.state.Axis, >x.Constraints.Max) = 1e6
}
m := op.Record(gtx.Ops)
if l.EnableCrossScrolling {
// Extend the minimum width to the largest element we've seen so far. layout.List.Layout adds a scroll handler
// to the area covered by the minimum constraint or the widest element it saw during that call to Layout. By
// setting the minimum to the widest known element we ensure that the scroll region doesn't shrink when large
// items go out of view.
if l.state.Widest > gtx.Constraints.Min.X {
gtx.Constraints.Min.X = l.state.Widest
}
}
listDims := l.state.List.Layout(gtx, length, w)
// crossWidth is the total, unconstrained width of the visible list elements.
crossWidth := l.state.Axis.Convert(listDims.Size).Y
// Use the widest width we've seen so far. That way, the scrollbar's handle size only changes when new, larger items
// appear, not when large items disappear.
if crossWidth > l.state.Widest {
l.state.Widest = crossWidth
} else {
crossWidth = l.state.Widest
}
// Limit listDims.Size to the size of the visible portion.
listDims.Size = tightListConstraints.Constrain(listDims.Size)
call := m.Stop()
// Constrain to the original constraints and display cropped & panned version of the list.
gtx.Constraints = originalConstraints
defer clip.Rect{Max: gtx.Constraints.Max}.Push(gtx.Ops).Pop()
delta := l.state.Cross.ScrollDistance()
l.state.CrossOffset += delta * float32(crossWidth)
if max := float32(crossWidth - listDims.Size.X); l.state.CrossOffset > max {
l.state.CrossOffset = max
}
if l.state.CrossOffset < 0 {
l.state.CrossOffset = 0
}
off := -int(math.Round(float64(l.state.CrossOffset)))
stack := op.Offset(image.Pt(off, 0)).Push(gtx.Ops)
call.Add(gtx.Ops)
stack.Pop()
// Draw the scrollbars.
mainAnchoring := layout.NE
crossAnchoring := layout.SW
if l.state.Axis == layout.Horizontal {
mainAnchoring = layout.SW
crossAnchoring = layout.NE
}
majorAxisSize := *layout.Main(l.state.Axis, &listDims.Size)
// layout.Direction respects the minimum, so ensure that the
// scrollbar will be drawn on the correct edge even if the provided
// layout.Context had a zero minimum constraint.
gtx.Constraints.Min = listDims.Size
if l.AnchorStrategy == Occupy {
min := l.state.Axis.Convert(gtx.Constraints.Min)
min.Y += mainBarWidth
if l.EnableCrossScrolling {
min.X += crossBarWidth
}
gtx.Constraints.Min = l.state.Axis.Convert(min)
}
mainAnchoring.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
if l.EnableCrossScrolling {
// Make sure the two scrollbars don't overlap, by leaving a gap in the corner where they meet.
*layout.Main(l.state.Axis, >x.Constraints.Min) -= crossBarWidth
*layout.Main(l.state.Axis, >x.Constraints.Max) -= crossBarWidth
gtx.Constraints = layout.Normalize(gtx.Constraints)
}
start, end := FromListPosition(l.state.Position, length, majorAxisSize)
return l.Main.Layout(win, gtx, l.state.Axis, start, end)
})
if l.EnableCrossScrolling {
crossAnchoring.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
// Make sure the two scrollbars don't overlap, by leaving a gap in the corner where they meet.
*layout.Cross(l.state.Axis, >x.Constraints.Min) -= mainBarWidth
*layout.Cross(l.state.Axis, >x.Constraints.Max) -= mainBarWidth
gtx.Constraints = layout.Normalize(gtx.Constraints)
start, end := float32(0), float32(1)
renderedWidth := *layout.Cross(l.state.Axis, &listDims.Size)
if crossWidth > renderedWidth {
width := float32(renderedWidth) / float32(crossWidth)
start = float32(l.state.CrossOffset) / float32(crossWidth)
end = start + width
}
return l.Cross.Layout(win, gtx, (l.state.Axis+1)%2, start, end)
})
}
if delta := l.state.Main.ScrollDistance(); delta != 0 {
// Handle any changes to the list position as a result of user interaction
// with the scrollbar.
l.state.List.ScrollBy(delta * float32(length))
}
if l.AnchorStrategy == Occupy {
// Increase the width to account for the space occupied by the scrollbar.
cross := l.state.Axis.Convert(listDims.Size)
cross.Y += mainBarWidth
if l.EnableCrossScrolling {
cross.X += crossBarWidth
}
listDims.Size = l.state.Axis.Convert(cross)
}
return listDims
}