forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcenterlayout.go
39 lines (31 loc) · 1.08 KB
/
centerlayout.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
package layout
import "fyne.io/fyne/v2"
// Declare conformity with Layout interface
var _ fyne.Layout = (*centerLayout)(nil)
type centerLayout struct {
}
// NewCenterLayout creates a new CenterLayout instance
func NewCenterLayout() fyne.Layout {
return ¢erLayout{}
}
// Layout is called to pack all child objects into a specified size.
// For CenterLayout this sets all children to their minimum size, centered within the space.
func (c *centerLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
for _, child := range objects {
childMin := child.MinSize()
child.Resize(childMin)
child.Move(fyne.NewPos(float32(size.Width-childMin.Width)/2, float32(size.Height-childMin.Height)/2))
}
}
// MinSize finds the smallest size that satisfies all the child objects.
// For CenterLayout this is determined simply as the MinSize of the largest child.
func (c *centerLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
minSize := fyne.NewSize(0, 0)
for _, child := range objects {
if !child.Visible() {
continue
}
minSize = minSize.Max(child.MinSize())
}
return minSize
}