Layout for floating buttons

This commit is contained in:
Martyn 2021-12-11 14:54:32 +01:00
parent 08904c484a
commit 1964b0202b
2 changed files with 94 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package main
import (
"fmt"
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"git.martyn.berlin/martyn/fyne-widgets/pkg/layouts"
)
func main() {
a := app.New()
w := a.NewWindow("FloatingButtons")
background := canvas.NewRectangle(color.RGBA{255, 0, 0, 128})
layout := layouts.NewFloatingControlsLayout()
layout.FloatingControlsLocation = layouts.FloatingControlsCenter
button := widget.NewButton("X", func() {
fmt.Println("button clicked")
layout.FloatingControlsLocation = layout.FloatingControlsLocation + 1
if layout.FloatingControlsLocation > 4 {
layout.FloatingControlsLocation = 0
}
w.SetContent(w.Content())
})
w.SetContent(container.New(&layout, background, button))
w.Resize(fyne.NewSize(100, 100))
w.ShowAndRun()
}

View File

@ -0,0 +1,59 @@
package layouts
import (
"fyne.io/fyne/v2"
)
type FloatingControlsLocationEnum uint32
const (
FloatingControlsTopLeft = iota
FloatingControlsTopRight
FloatingControlsBottomLeft
FloatingControlsBottomRight
FloatingControlsCenter
)
type floatingControlsLayout struct {
FloatingControlsLocation FloatingControlsLocationEnum
}
// NewFloatingControlsLayout creates a new floatingControlsLayout instance
// This layout contains exactly TWO items, one background item (first) and one
// floating container that floats according to FloatingControlsLocation
func NewFloatingControlsLayout() floatingControlsLayout {
return floatingControlsLayout{FloatingControlsTopRight}
}
func (d *floatingControlsLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
if len(objects) > 1 {
return objects[0].MinSize()
}
return fyne.NewSize(0, 0)
}
func (d *floatingControlsLayout) Layout(objects []fyne.CanvasObject, containerSize fyne.Size) {
if len(objects) == 2 {
o := objects[0]
size := containerSize
o.Resize(size)
o.Move(fyne.NewPos(0, 0))
o = objects[1]
size = o.MinSize()
o.Resize(size)
switch d.FloatingControlsLocation {
case FloatingControlsTopLeft:
o.Move(fyne.NewPos(0, 0))
case FloatingControlsTopRight:
o.Move(fyne.NewPos(containerSize.Width-size.Width, 0))
case FloatingControlsBottomLeft:
o.Move(fyne.NewPos(0, containerSize.Height-size.Height))
case FloatingControlsBottomRight:
o.Move(fyne.NewPos(containerSize.Width-size.Width, containerSize.Height-size.Height))
case FloatingControlsCenter:
o.Move(fyne.NewPos((containerSize.Width/2)-(size.Width/2), (containerSize.Height/2)-(size.Height/2)))
}
}
}