paddedlayout.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. pos := fyne.NewPos(theme.Padding(), theme.Padding())
  14. siz := fyne.NewSize(size.Width-2*theme.Padding(), size.Height-2*theme.Padding())
  15. for _, child := range objects {
  16. child.Resize(siz)
  17. child.Move(pos)
  18. }
  19. }
  20. // MinSize finds the smallest size that satisfies all the child objects.
  21. // For PaddedLayout this is determined simply as the MinSize of the largest child plus padding all around.
  22. func (l *paddedLayout) MinSize(objects []fyne.CanvasObject) (min fyne.Size) {
  23. for _, child := range objects {
  24. if !child.Visible() {
  25. continue
  26. }
  27. min = min.Max(child.MinSize())
  28. }
  29. min = min.Add(fyne.NewSize(2*theme.Padding(), 2*theme.Padding()))
  30. return
  31. }
  32. // NewPaddedLayout creates a new PaddedLayout instance
  33. //
  34. // Since: 1.4
  35. func NewPaddedLayout() fyne.Layout {
  36. return &paddedLayout{}
  37. }