dimensionlist.go raw
1 package gel
2
3 import (
4 l "github.com/p9c/gio/layout"
5 "github.com/p9c/gio/op"
6 )
7
8 type DimensionList []l.Dimensions
9
10 func (d DimensionList) GetTotal(axis l.Axis) (total int) {
11 for i := range d {
12 total += axisMain(axis, d[i].Size)
13 }
14 return total
15 }
16
17 // PositionToCoordinate converts a list position to absolute coordinate
18 func (d DimensionList) PositionToCoordinate(position Position, axis l.Axis) (coordinate int) {
19 for i := 0; i < position.First; i++ {
20 coordinate += axisMain(axis, d[i].Size)
21 }
22 return coordinate + position.Offset
23 }
24
25 // CoordinateToPosition converts an absolute coordinate to a list position
26 func (d DimensionList) CoordinateToPosition(coordinate int, axis l.Axis) (position Position) {
27 cursor := 0
28 if coordinate < 0 {
29 coordinate = 0
30 return
31 }
32 tot := d.GetTotal(axis)
33 if coordinate > tot {
34 position.First = len(d) - 1
35 position.Offset = axisMain(axis, d[len(d)-1].Size)
36 position.BeforeEnd = false
37 return
38 }
39 var i int
40 for i = range d {
41 cursor += axisMain(axis, d[i].Size)
42 if cursor >= coordinate {
43 position.First = i
44 position.Offset = coordinate - cursor
45 position.BeforeEnd = true
46 return
47 }
48 }
49 // if it overshoots, stop it, if it is at the end, mark it
50 if coordinate >= cursor {
51 position.First = len(d) - 1
52 position.Offset = axisMain(axis, d[len(d)-1].Size)
53 position.BeforeEnd = false
54 }
55 return
56 }
57
58 // GetDimensionList returns a dimensionlist based on the given listelement
59 func GetDimensionList(gtx l.Context, length int, listElement ListElement) (dims DimensionList) {
60 // gather the dimensions of the list elements
61 for i := 0; i < length; i++ {
62 child := op.Record(gtx.Ops)
63 d := listElement(gtx, i)
64 _ = child.Stop()
65 dims = append(dims, d)
66 }
67 return
68 }
69
70 func GetDimension(gtx l.Context, w l.Widget) (dim l.Dimensions) {
71 child := op.Record(gtx.Ops)
72 dim = w(gtx)
73 _ = child.Stop()
74 return
75 }
76
77 func (d DimensionList) GetSizes(position Position, axis l.Axis) (total, before int) {
78 for i := range d {
79 inc := axisMain(axis, d[i].Size)
80 total += inc
81 if i < position.First {
82 before += inc
83 }
84 }
85 before += position.Offset
86 return
87 }
88