centerlayout.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package layout
  2. import "fyne.io/fyne/v2"
  3. // Declare conformity with Layout interface
  4. var _ fyne.Layout = (*centerLayout)(nil)
  5. type centerLayout struct {
  6. }
  7. // NewCenterLayout creates a new CenterLayout instance
  8. func NewCenterLayout() fyne.Layout {
  9. return &centerLayout{}
  10. }
  11. // Layout is called to pack all child objects into a specified size.
  12. // For CenterLayout this sets all children to their minimum size, centered within the space.
  13. func (c *centerLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
  14. for _, child := range objects {
  15. childMin := child.MinSize()
  16. child.Resize(childMin)
  17. child.Move(fyne.NewPos(float32(size.Width-childMin.Width)/2, float32(size.Height-childMin.Height)/2))
  18. }
  19. }
  20. // MinSize finds the smallest size that satisfies all the child objects.
  21. // For CenterLayout this is determined simply as the MinSize of the largest child.
  22. func (c *centerLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
  23. minSize := fyne.NewSize(0, 0)
  24. for _, child := range objects {
  25. if !child.Visible() {
  26. continue
  27. }
  28. minSize = minSize.Max(child.MinSize())
  29. }
  30. return minSize
  31. }