Add padding and margin layouts

This commit is contained in:
Maarten Heremans
2024-06-17 15:58:12 +02:00
parent bfd94a9e26
commit 050b94dd3a
9 changed files with 177 additions and 4 deletions

45
uilayout/margin.go Normal file
View File

@@ -0,0 +1,45 @@
package uilayout
import "fyne.io/fyne/v2"
type Margin struct {
top float32
bottom float32
left float32
right float32
}
func NewMargin(
top, bottom, left, right float32,
) *Margin {
return &Margin{
top: top,
bottom: bottom,
left: left,
right: right,
}
}
func (m *Margin) MinSize(objects []fyne.CanvasObject) fyne.Size {
w := m.left + m.right
h := m.top + m.bottom
if len(objects) > 0 {
w += objects[0].MinSize().Width
h += objects[0].MinSize().Height
}
return fyne.NewSize(w, h)
}
func (m *Margin) Layout(
objects []fyne.CanvasObject,
containerSize fyne.Size,
) {
pos := fyne.NewPos(m.left, m.top)
if len(objects) > 0 {
o := objects[0]
size := containerSize.Subtract(
fyne.NewSize(m.left+m.right, m.top+m.bottom))
o.Resize(size)
o.Move(pos)
}
}

45
uilayout/padding.go Normal file
View File

@@ -0,0 +1,45 @@
package uilayout
import "fyne.io/fyne/v2"
type Padding struct {
top float32
bottom float32
left float32
right float32
}
func NewPadding(
top, bottom, left, right float32,
) *Padding {
return &Padding{
top: top,
bottom: bottom,
left: left,
right: right,
}
}
func (p *Padding) MinSize(objects []fyne.CanvasObject) fyne.Size {
w := p.left + p.right
h := p.top + p.bottom
if len(objects) > 0 {
w += objects[0].MinSize().Width
h += objects[0].MinSize().Height
}
return fyne.NewSize(w, h)
}
func (p *Padding) Layout(
objects []fyne.CanvasObject,
containerSize fyne.Size,
) {
pos := fyne.NewPos(p.left, p.top)
if len(objects) > 0 {
o := objects[0]
size := containerSize.Subtract(
fyne.NewSize(p.left+p.right, p.top+p.bottom))
o.Resize(size)
o.Move(pos)
}
}