-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdimensionlist.go
87 lines (79 loc) · 2.06 KB
/
dimensionlist.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
package gel
import (
l "github.com/p9c/gio/layout"
"github.com/p9c/gio/op"
)
type DimensionList []l.Dimensions
func (d DimensionList) GetTotal(axis l.Axis) (total int) {
for i := range d {
total += axisMain(axis, d[i].Size)
}
return total
}
// PositionToCoordinate converts a list position to absolute coordinate
func (d DimensionList) PositionToCoordinate(position Position, axis l.Axis) (coordinate int) {
for i := 0; i < position.First; i++ {
coordinate += axisMain(axis, d[i].Size)
}
return coordinate + position.Offset
}
// CoordinateToPosition converts an absolute coordinate to a list position
func (d DimensionList) CoordinateToPosition(coordinate int, axis l.Axis) (position Position) {
cursor := 0
if coordinate < 0 {
coordinate = 0
return
}
tot := d.GetTotal(axis)
if coordinate > tot {
position.First = len(d) - 1
position.Offset = axisMain(axis, d[len(d)-1].Size)
position.BeforeEnd = false
return
}
var i int
for i = range d {
cursor += axisMain(axis, d[i].Size)
if cursor >= coordinate {
position.First = i
position.Offset = coordinate - cursor
position.BeforeEnd = true
return
}
}
// if it overshoots, stop it, if it is at the end, mark it
if coordinate >= cursor {
position.First = len(d) - 1
position.Offset = axisMain(axis, d[len(d)-1].Size)
position.BeforeEnd = false
}
return
}
// GetDimensionList returns a dimensionlist based on the given listelement
func GetDimensionList(gtx l.Context, length int, listElement ListElement) (dims DimensionList) {
// gather the dimensions of the list elements
for i := 0; i < length; i++ {
child := op.Record(gtx.Ops)
d := listElement(gtx, i)
_ = child.Stop()
dims = append(dims, d)
}
return
}
func GetDimension(gtx l.Context, w l.Widget) (dim l.Dimensions) {
child := op.Record(gtx.Ops)
dim = w(gtx)
_ = child.Stop()
return
}
func (d DimensionList) GetSizes(position Position, axis l.Axis) (total, before int) {
for i := range d {
inc := axisMain(axis, d[i].Size)
total += inc
if i < position.First {
before += inc
}
}
before += position.Offset
return
}