ui/screen/container.go

140 lines
3.0 KiB
Go
Raw Normal View History

package screen
import (
"errors"
"fmt"
"bitbucket.org/hevanto/ui/uiwidget"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
)
2024-04-03 16:07:24 +02:00
// 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 (
2024-04-03 16:07:24 +02:00
// HScroll Container
//
// Available yaml options:
// - content: The content of the container
HScroll Container = "HScroll"
2024-04-03 16:07:24 +02:00
// 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"
)
2024-04-03 16:07:24 +02:00
// 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,
) (
container fyne.CanvasObject,
decorator fyne.CanvasObject,
err error,
) {
switch cnt {
case HScroll:
2024-04-03 16:07:24 +02:00
container, err = cnt.buildHScrollContainer(e, s)
case Scroll:
2024-04-03 16:07:24 +02:00
container, err = cnt.buildScrollContainer(e, s)
case VScroll:
2024-04-03 16:07:24 +02:00
container, err = cnt.buildVScrollContainer(e, s)
default:
err = errors.New("invalid container")
}
if err != nil {
return
}
2024-04-03 16:07:24 +02:00
decorator = container
if e.Decorators != nil {
for _, dec := range e.Decorators {
switch dec {
case "Border":
2024-04-03 16:07:24 +02:00
decorator = uiwidget.NewWidgetBorder(decorator)
}
}
}
if e.Hidden {
2024-04-03 16:07:24 +02:00
decorator.Hide()
}
return
}
2024-04-03 16:07:24 +02:00
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
}
2024-04-03 16:07:24 +02:00
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
}
2024-04-03 16:07:24 +02:00
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
}