stacklayout.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Package layout defines the various layouts available to Fyne apps.
  2. package layout // import "fyne.io/fyne/v2/layout"
  3. import "fyne.io/fyne/v2"
  4. // Declare conformity with Layout interface
  5. var _ fyne.Layout = (*stackLayout)(nil)
  6. type stackLayout struct {
  7. }
  8. // NewStackLayout returns a new StackLayout instance. Objects are stacked
  9. // on top of each other with later objects on top of those before.
  10. // Having only a single object has no impact as CanvasObjects will
  11. // fill the available space even without a Stack.
  12. //
  13. // Since: 2.4
  14. func NewStackLayout() fyne.Layout {
  15. return &stackLayout{}
  16. }
  17. // NewMaxLayout creates a new MaxLayout instance
  18. //
  19. // Deprecated: Use layout.NewStackLayout() instead.
  20. func NewMaxLayout() fyne.Layout {
  21. return NewStackLayout()
  22. }
  23. // Layout is called to pack all child objects into a specified size.
  24. // For StackLayout this sets all children to the full size passed.
  25. func (m *stackLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
  26. topLeft := fyne.NewPos(0, 0)
  27. for _, child := range objects {
  28. child.Resize(size)
  29. child.Move(topLeft)
  30. }
  31. }
  32. // MinSize finds the smallest size that satisfies all the child objects.
  33. // For StackLayout this is determined simply as the MinSize of the largest child.
  34. func (m *stackLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
  35. minSize := fyne.NewSize(0, 0)
  36. for _, child := range objects {
  37. if !child.Visible() {
  38. continue
  39. }
  40. minSize = minSize.Max(child.MinSize())
  41. }
  42. return minSize
  43. }