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

View File

@ -43,7 +43,9 @@ func (c Canvas) Build(
return return
} }
decorator = applyDecorators(e, cnv) decorator = applyPadding(e, cnv)
decorator = applyDecorators(e, decorator)
decorator = applyMargins(e, decorator)
if e.Hidden { if e.Hidden {
decorator.Hide() decorator.Hide()

View File

@ -67,7 +67,9 @@ func (cnt Container) Build(
return return
} }
decorator = applyDecorators(e, cont) decorator = applyPadding(e, cont)
decorator = applyDecorators(e, decorator)
decorator = applyMargins(e, decorator)
if e.Hidden { if e.Hidden {
decorator.Hide() decorator.Hide()

View File

@ -25,6 +25,28 @@ type Color struct {
Alpha uint8 `yaml:"alpha"` Alpha uint8 `yaml:"alpha"`
} }
type Margins struct {
Top float32 `yaml:"top"`
Bottom float32 `yaml:"bottom"`
Left float32 `yaml:"left"`
Right float32 `yaml:"right"`
}
func (m *Margins) HasMargins() bool {
return m.Top > 0 || m.Bottom > 0 || m.Left > 0 || m.Right > 0
}
type Padding struct {
Top float32 `yaml:"top"`
Bottom float32 `yaml:"bottom"`
Left float32 `yaml:"left"`
Right float32 `yaml:"right"`
}
func (p *Padding) HasPadding() bool {
return p.Top > 0 || p.Bottom > 0 || p.Left > 0 || p.Right > 0
}
// Element represents the yaml description of a ui element // Element represents the yaml description of a ui element
type Element struct { type Element struct {
ID string `yaml:"id"` ID string `yaml:"id"`
@ -34,6 +56,9 @@ type Element struct {
Widget Widget `yaml:"widget"` Widget Widget `yaml:"widget"`
Canvas Canvas `yaml:"canvas"` Canvas Canvas `yaml:"canvas"`
Padding Padding `yaml:"padding"`
Margins Margins `yaml:"margins"`
// Border Layout Elements // Border Layout Elements
Top *Element `yaml:"top"` Top *Element `yaml:"top"`
Bottom *Element `yaml:"bottom"` Bottom *Element `yaml:"bottom"`

View File

@ -112,7 +112,9 @@ func (l Layout) Build(
return return
} }
decorator = applyDecorators(e, lay) decorator = applyPadding(e, lay)
decorator = applyDecorators(e, decorator)
decorator = applyMargins(e, decorator)
if e.Hidden { if e.Hidden {
decorator.Hide() decorator.Hide()

25
screen/margin.go Normal file
View File

@ -0,0 +1,25 @@
package screen
import (
"bitbucket.org/hevanto/ui/uilayout"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
)
func applyMargins(
e *Element,
obj fyne.CanvasObject,
) (
margin fyne.CanvasObject,
) {
margin = obj
if e.Margins.HasMargins() {
margin = container.New(uilayout.NewMargin(
e.Margins.Top,
e.Margins.Bottom,
e.Margins.Left,
e.Margins.Right,
), obj)
}
return
}

25
screen/padding.go Normal file
View File

@ -0,0 +1,25 @@
package screen
import (
"bitbucket.org/hevanto/ui/uilayout"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
)
func applyPadding(
e *Element,
obj fyne.CanvasObject,
) (
padding fyne.CanvasObject,
) {
padding = obj
if e.Padding.HasPadding() {
padding = container.New(uilayout.NewPadding(
e.Padding.Top,
e.Padding.Bottom,
e.Padding.Left,
e.Padding.Right,
), obj)
}
return
}

View File

@ -255,7 +255,9 @@ func (w Widget) Build(
return return
} }
decorator = applyDecorators(e, widget) decorator = applyPadding(e, widget)
decorator = applyDecorators(e, decorator)
decorator = applyMargins(e, decorator)
if e.Hidden { if e.Hidden {
decorator.Hide() decorator.Hide()

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)
}
}