~pierrec/giox

ref: 2c5ee028295c046e95c45a7f776b0fb6177759ad giox/layoutx/list.go -rw-r--r-- 4.8 KiB
2c5ee028pierre layoutx: added Rigids and Flexes 8 months ago
                                                                                
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
package layoutx

import (
	"image"

	"gioui.org/layout"
	"gioui.org/op"
)

// inf is an infinite axis constraint.
const inf = 1e6

// ListWrap lays out as many items as possible along the main axis
// before wrapping to the cross axis.
//
// The number of items along the main axis must be the same for
// all rows or columns on the cross axis.
type ListWrap struct {
	Axis      layout.Axis
	Alignment layout.Alignment
	list      layout.List
	num       int        // number of items per row/column
	els       []listData // cache used when displaying items
}

type listData struct {
	main  int
	cross int
	base  int
	call  op.CallOp
}

func (l *ListWrap) init() {
	if l.list.Axis == l.Axis {
		l.list.Axis = Swap(l.Axis)
	}
}

// ListWrapElement is called for each row/column with the CallOp containing all the elements
// to be displayed for that row/column and their combined dimensions.
type ListWrapElement func(layout.Context, int, layout.Dimensions, op.CallOp) layout.Dimensions

func listWrapBlock(gtx layout.Context, idx int, dims layout.Dimensions, c op.CallOp) layout.Dimensions {
	c.Add(gtx.Ops)
	return dims
}

func (l *ListWrap) Layout(gtx layout.Context, num int, el layout.ListElement, w ListWrapElement) layout.Dimensions {
	l.init()
	left := l.Axis.Convert(gtx.Constraints.Max).X
	if num == 0 || left == 0 {
		return layout.Dimensions{Size: gtx.Constraints.Min}
	}

	// Consider that the same number of items are displayed for all columns/rows.
	cs := gtx.Constraints
	gtx.Constraints.Min = image.Point{}
	elgtx := gtx
	// To help keeping a constant number per column/row, give infinite space
	// on the cross axis.
	if l.Axis == layout.Horizontal {
		elgtx.Constraints.Max.Y = inf
	} else {
		elgtx.Constraints.Max.X = inf
	}
	// Calculate the number of items that can be displayed on the main axis.
	els := l.els[:0]
	first := l.list.Position.First
	var mainSize, crossSize, base int
	for i := first * l.num; left > 0 && i < num; i++ {
		m := op.Record(gtx.Ops)
		dims := el(elgtx, i)
		c := m.Stop()
		main := l.Axis.Convert(dims.Size).X
		if main == 0 {
			// Ignore elements not contributing to the main axis.
			continue
		}
		left -= main
		if left >= 0 || len(els) == 0 {
			// Add the item as there is room left or no room but
			// we still want one item displayed even if truncated
			cross := l.Axis.Convert(dims.Size).Y
			els = append(els, listData{main, cross, dims.Baseline, c})
			mainSize += main
			crossSize = max(crossSize, cross)
			base = max(base, dims.Baseline)
		}
	}
	if l.num != len(els) {
		// The number of items per column/row has changed.
		// Unless the last column/row is displayed with a number
		// less than l.num, update l.num.
		// This avoids the case where the tail of the list does not
		// use all of the column/row and skews l.num.
		if l.num == 0 || num%l.num == 0 || first != num/l.num {
			l.num = len(els)
		}
		l.els = els
	}
	if l.num == 0 {
		return layout.Dimensions{Size: gtx.Constraints.Min}
	}

	if w == nil {
		w = listWrapBlock
	}

	dims := l.list.Layout(gtx, (num+l.num-1)/l.num, func(gtx layout.Context, idx int) layout.Dimensions {
		defer op.Save(gtx.Ops).Load()
		if idx == first {
			// Reuse the items processed while calculating the number of elements per column/row.
			first = -1
		} else {
			mainSize, crossSize, base = 0, 0, 0
			els = els[:0]
			for i, n := idx*l.num, min(num, (idx+1)*l.num); i < n; i++ {
				m := op.Record(gtx.Ops)
				dims := el(elgtx, i)
				c := m.Stop()

				main := l.Axis.Convert(dims.Size).X
				if main == 0 {
					// Ignore elements not contributing to the main axis.
					n--
					continue
				}
				cross := l.Axis.Convert(dims.Size).Y
				els = append(els, listData{main, cross, dims.Baseline, c})

				mainSize += main
				crossSize = max(crossSize, cross)
				base = max(base, dims.Baseline)
			}
		}

		m := op.Record(gtx.Ops)
		for _, el := range els {
			var cross int
			switch l.Alignment {
			case layout.Start:
			case layout.End:
				cross = crossSize - el.cross
			case layout.Middle:
				cross = (crossSize - el.cross) / 2
			case layout.Baseline:
				if l.Axis == layout.Horizontal {
					cross = base - el.base
				}
			}
			if cross == 0 {
				el.call.Add(gtx.Ops)
			} else {
				pt := l.Axis.Convert(image.Pt(0, cross))
				op.Offset(layout.FPt(pt)).Add(gtx.Ops)
				el.call.Add(gtx.Ops)
				op.Offset(layout.FPt(pt.Mul(-1))).Add(gtx.Ops)
			}
			pt := l.Axis.Convert(image.Pt(el.main, 0))
			op.Offset(layout.FPt(pt)).Add(gtx.Ops)
		}
		c := m.Stop()
		size := l.Axis.Convert(image.Pt(mainSize, crossSize))
		dims := layout.Dimensions{Size: size, Baseline: base}
		gtx.Constraints.Max.X = max(gtx.Constraints.Max.X, size.X)
		gtx.Constraints.Max.Y = max(gtx.Constraints.Max.Y, size.Y)
		return w(gtx, idx, dims, c)
	})
	dims.Size.X = max(dims.Size.X, cs.Min.X)
	dims.Size.Y = max(dims.Size.Y, cs.Min.Y)
	return dims
}

func (l *ListWrap) Position() layout.Position {
	return l.list.Position
}