2024-03-30 19:26:48 +01:00
|
|
|
package screen
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"bitbucket.org/hevanto/ui/uiwidget"
|
|
|
|
"fyne.io/fyne/v2"
|
|
|
|
"fyne.io/fyne/v2/container"
|
2024-04-11 17:19:57 +02:00
|
|
|
"fyne.io/fyne/v2/layout"
|
2024-03-30 19:26:48 +01:00
|
|
|
)
|
|
|
|
|
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
|
2024-03-30 19:26:48 +01:00
|
|
|
type Container string
|
|
|
|
|
|
|
|
const (
|
2024-04-03 16:07:24 +02:00
|
|
|
// HScroll Container
|
|
|
|
//
|
|
|
|
// Available yaml options:
|
|
|
|
// - content: The content of the container
|
2024-03-30 19:26:48 +01:00
|
|
|
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
|
2024-03-30 19:26:48 +01:00
|
|
|
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,
|
|
|
|
) (
|
2024-04-11 17:19:57 +02:00
|
|
|
cont fyne.CanvasObject,
|
2024-04-03 16:07:24 +02:00
|
|
|
decorator fyne.CanvasObject,
|
|
|
|
err error,
|
|
|
|
) {
|
2024-03-30 19:26:48 +01:00
|
|
|
switch cnt {
|
|
|
|
case HScroll:
|
2024-04-11 17:19:57 +02:00
|
|
|
cont, err = cnt.buildHScrollContainer(e, s)
|
2024-03-30 19:26:48 +01:00
|
|
|
case Scroll:
|
2024-04-11 17:19:57 +02:00
|
|
|
cont, err = cnt.buildScrollContainer(e, s)
|
2024-03-30 19:26:48 +01:00
|
|
|
case VScroll:
|
2024-04-11 17:19:57 +02:00
|
|
|
cont, err = cnt.buildVScrollContainer(e, s)
|
2024-03-30 19:26:48 +01:00
|
|
|
default:
|
|
|
|
err = errors.New("invalid container")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-11 17:19:57 +02:00
|
|
|
decorator = cont
|
2024-04-03 16:07:24 +02:00
|
|
|
if e.Decorators != nil {
|
2024-03-30 19:26:48 +01:00
|
|
|
for _, dec := range e.Decorators {
|
|
|
|
switch dec {
|
|
|
|
case "Border":
|
2024-04-03 16:07:24 +02:00
|
|
|
decorator = uiwidget.NewWidgetBorder(decorator)
|
2024-04-11 17:19:57 +02:00
|
|
|
case "HCenter":
|
|
|
|
decorator = container.NewHBox(
|
|
|
|
layout.NewSpacer(),
|
|
|
|
decorator,
|
|
|
|
layout.NewSpacer())
|
|
|
|
case "VCenter":
|
|
|
|
decorator = container.NewVBox(
|
|
|
|
layout.NewSpacer(),
|
|
|
|
decorator,
|
|
|
|
layout.NewSpacer())
|
2024-03-30 19:26:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Hidden {
|
2024-04-03 16:07:24 +02:00
|
|
|
decorator.Hide()
|
2024-03-30 19:26:48 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-03 16:07:24 +02:00
|
|
|
func (ctn Container) buildHScrollContainer(
|
|
|
|
e *Element,
|
|
|
|
s ScreenHandler,
|
|
|
|
) (c fyne.CanvasObject, err error) {
|
2024-03-30 19:26:48 +01:00
|
|
|
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) {
|
2024-03-30 19:26:48 +01:00
|
|
|
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) {
|
2024-03-30 19:26:48 +01:00
|
|
|
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
|
|
|
|
}
|