paddedlayout.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package layout
  2. import (
  3. "fyne.io/fyne/v2"
  4. "fyne.io/fyne/v2/theme"
  5. )
  6. // Declare conformity with Layout interface
  7. var _ fyne.Layout = (*paddedLayout)(nil)
  8. type paddedLayout struct {
  9. }
  10. // Layout is called to pack all child objects into a specified size.
  11. // For PaddedLayout this sets all children to the full size passed minus padding all around.
  12. func (l *paddedLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
  13. padding := theme.Padding()
  14. pos := fyne.NewPos(padding, padding)
  15. siz := fyne.NewSize(size.Width-2*padding, size.Height-2*padding)
  16. for _, child := range objects {
  17. child.Resize(siz)
  18. child.Move(pos)
  19. }
  20. }
  21. // MinSize finds the smallest size that satisfies all the child objects.
  22. // For PaddedLayout this is determined simply as the MinSize of the largest child plus padding all around.
  23. func (l *paddedLayout) MinSize(objects []fyne.CanvasObject) (min fyne.Size) {
  24. for _, child := range objects {
  25. if !child.Visible() {
  26. continue
  27. }
  28. min = min.Max(child.MinSize())
  29. }
  30. min = min.Add(fyne.NewSquareSize(2 * theme.Padding()))
  31. return
  32. }
  33. // NewPaddedLayout creates a new PaddedLayout instance
  34. //
  35. // Since: 1.4
  36. func NewPaddedLayout() fyne.Layout {
  37. return &paddedLayout{}
  38. }