boxlayout.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = (*boxLayout)(nil)
  8. type boxLayout struct {
  9. horizontal bool
  10. }
  11. // NewHBoxLayout returns a horizontal box layout for stacking a number of child
  12. // canvas objects or widgets left to right. The objects are always displayed
  13. // at their horizontal MinSize. Use a different layout if the objects are intended
  14. // to be larger then their horizontal MinSize.
  15. func NewHBoxLayout() fyne.Layout {
  16. return &boxLayout{true}
  17. }
  18. // NewVBoxLayout returns a vertical box layout for stacking a number of child
  19. // canvas objects or widgets top to bottom. The objects are always displayed
  20. // at their vertical MinSize. Use a different layout if the objects are intended
  21. // to be larger then their vertical MinSize.
  22. func NewVBoxLayout() fyne.Layout {
  23. return &boxLayout{false}
  24. }
  25. func (g *boxLayout) isSpacer(obj fyne.CanvasObject) bool {
  26. if !obj.Visible() {
  27. return false // invisible spacers don't impact layout
  28. }
  29. spacer, ok := obj.(SpacerObject)
  30. if !ok {
  31. return false
  32. }
  33. if g.horizontal {
  34. return spacer.ExpandHorizontal()
  35. }
  36. return spacer.ExpandVertical()
  37. }
  38. // Layout is called to pack all child objects into a specified size.
  39. // For a VBoxLayout this will pack objects into a single column where each item
  40. // is full width but the height is the minimum required.
  41. // Any spacers added will pad the view, sharing the space if there are two or more.
  42. func (g *boxLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
  43. spacers := 0
  44. total := float32(0)
  45. for _, child := range objects {
  46. if !child.Visible() {
  47. continue
  48. }
  49. if g.isSpacer(child) {
  50. spacers++
  51. continue
  52. }
  53. if g.horizontal {
  54. total += child.MinSize().Width
  55. } else {
  56. total += child.MinSize().Height
  57. }
  58. }
  59. padding := theme.Padding()
  60. var extra float32
  61. if g.horizontal {
  62. extra = size.Width - total - (padding * float32(len(objects)-spacers-1))
  63. } else {
  64. extra = size.Height - total - (padding * float32(len(objects)-spacers-1))
  65. }
  66. extraCell := float32(0)
  67. if spacers > 0 {
  68. extraCell = extra / float32(spacers)
  69. }
  70. x, y := float32(0), float32(0)
  71. for _, child := range objects {
  72. if !child.Visible() {
  73. continue
  74. }
  75. if g.isSpacer(child) {
  76. if g.horizontal {
  77. x += extraCell
  78. } else {
  79. y += extraCell
  80. }
  81. continue
  82. }
  83. child.Move(fyne.NewPos(x, y))
  84. if g.horizontal {
  85. width := child.MinSize().Width
  86. x += padding + width
  87. child.Resize(fyne.NewSize(width, size.Height))
  88. } else {
  89. height := child.MinSize().Height
  90. y += padding + height
  91. child.Resize(fyne.NewSize(size.Width, height))
  92. }
  93. }
  94. }
  95. // MinSize finds the smallest size that satisfies all the child objects.
  96. // For a BoxLayout this is the width of the widest item and the height is
  97. // the sum of of all children combined with padding between each.
  98. func (g *boxLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
  99. minSize := fyne.NewSize(0, 0)
  100. addPadding := false
  101. padding := theme.Padding()
  102. for _, child := range objects {
  103. if !child.Visible() || g.isSpacer(child) {
  104. continue
  105. }
  106. childMin := child.MinSize()
  107. if g.horizontal {
  108. minSize.Height = fyne.Max(childMin.Height, minSize.Height)
  109. minSize.Width += childMin.Width
  110. if addPadding {
  111. minSize.Width += padding
  112. }
  113. } else {
  114. minSize.Width = fyne.Max(childMin.Width, minSize.Width)
  115. minSize.Height += childMin.Height
  116. if addPadding {
  117. minSize.Height += padding
  118. }
  119. }
  120. addPadding = true
  121. }
  122. return minSize
  123. }