ui/screen/container.go
2024-06-17 15:58:12 +02:00

133 lines
2.8 KiB
Go

package screen
import (
"errors"
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
)
// The constants for the Containers
//
// Available yaml options for all containers:
// - id: The ID for exporting the container to code
// - decorators: The decorators to wrap the container into
// - hidden: Wether the container is hidden or not
type Container string
const (
// HScroll Container
//
// Available yaml options:
// - content: The content of the container
HScroll Container = "HScroll"
// Scroll Container
//
// Available yaml options:
// - content: The content of the container
Scroll Container = "Scroll"
// VScroll Container
//
// Available yaml options:
// - content: The content of the container
VScroll Container = "VScroll"
)
// Build builds the container and decorator for the given Container and Element,
// using the provided ScreenHandler to fetch functions, data bindings, etc.
//
// Parameters:
// - e *Element - the element to build the container for
// - s ScreenHandler - the screen handler for the container
//
// Returns the CanvasObject for the widget, the CanvasObject for the decorator
// I fno decorators are specified, the widget and decorator will be the same.
func (cnt Container) Build(
e *Element,
s ScreenHandler,
) (
cont fyne.CanvasObject,
decorator fyne.CanvasObject,
err error,
) {
switch cnt {
case HScroll:
cont, err = cnt.buildHScrollContainer(e, s)
case Scroll:
cont, err = cnt.buildScrollContainer(e, s)
case VScroll:
cont, err = cnt.buildVScrollContainer(e, s)
default:
err = errors.New("invalid container")
}
if err != nil {
return
}
decorator = applyPadding(e, cont)
decorator = applyDecorators(e, decorator)
decorator = applyMargins(e, decorator)
if e.Hidden {
decorator.Hide()
}
return
}
func (ctn Container) buildHScrollContainer(
e *Element,
s ScreenHandler,
) (c fyne.CanvasObject, err error) {
if e.Content == nil {
err = errors.New("HScroll: no content defined")
return
}
content, err := e.Content.BuildUI(s)
if err != nil {
err = fmt.Errorf("HScroll: failed to build content: %w", err)
return
}
c = container.NewHScroll(content)
return
}
func (ctn Container) buildScrollContainer(
e *Element,
s ScreenHandler,
) (c fyne.CanvasObject, err error) {
if e.Content == nil {
err = errors.New("Scroll: no content defined")
return
}
content, err := e.Content.BuildUI(s)
if err != nil {
err = fmt.Errorf("Scroll: failed to build content: %w", err)
return
}
c = container.NewScroll(content)
return
}
func (ctn Container) buildVScrollContainer(
e *Element,
s ScreenHandler,
) (c fyne.CanvasObject, err error) {
if e.Content == nil {
err = errors.New("VScroll: no content defined")
return
}
content, err := e.Content.BuildUI(s)
if err != nil {
err = fmt.Errorf("VScroll: failed to build content: %w", err)
return
}
c = container.NewScroll(content)
return
}