-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlayout.go
65 lines (54 loc) · 1.42 KB
/
layout.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
package main
import (
"math"
"fyne.io/fyne/v2"
)
const (
minWidth float32 = 300
minHeight float32 = 200
cellSpace float32 = 16
)
type nomadLayout struct {
cols int
}
func (l *nomadLayout) Layout(objs []fyne.CanvasObject, s fyne.Size) {
outer := cellSpace
if fyne.CurrentDevice().IsMobile() {
outer = 0
}
l.cols = int((s.Width - outer*2 + cellSpace) / (minWidth + cellSpace))
colWidth := (s.Width-outer*2+cellSpace)/float32(l.cols) - cellSpace
cellSize := fyne.NewSize(colWidth, minHeight)
offset := 0
pos := fyne.Position{X: outer, Y: outer}
for _, o := range objs {
o.Resize(cellSize)
o.Move(pos)
offset++
pos.X += colWidth + cellSpace
if offset >= l.cols {
offset = 0
pos.X = outer
pos.Y += minHeight + cellSpace
}
}
}
func (l *nomadLayout) MinSize(cells []fyne.CanvasObject) fyne.Size {
// we calculate how much is required to scroll to fit in all of the cells
cols := l.cols
if cols < 1 { // possibly not layed out yet
cols = 1
}
rows := int(math.Ceil(float64(len(cells)) / float64(l.cols)))
height := float32((minHeight+cellSpace*4)*float32(rows) - cellSpace)
if fyne.CurrentDevice().IsMobile() {
return fyne.NewSize(minWidth, height)
}
return fyne.NewSize(minWidth, height+cellSpace*2)
}
func (l *nomadLayout) minOuterSize() fyne.Size {
if fyne.CurrentDevice().IsMobile() {
return fyne.NewSize(minWidth, minHeight)
}
return fyne.NewSize(minWidth+cellSpace*2, minHeight+cellSpace*2)
}