ui/screen/element.go
2024-08-06 21:30:11 +02:00

216 lines
5.5 KiB
Go

package screen
import (
"fmt"
"gitea.hevanto-it.com/hevanto/i18n"
"fyne.io/fyne/v2"
)
// Size represents the yaml description of a ui size
type Size struct {
Width float32 `yaml:"width"`
Height float32 `yaml:"height"`
}
type RelativeSize struct {
Width float32 `yaml:"width"`
Height float32 `yaml:"height"`
}
type Color struct {
Red uint8 `yaml:"red"`
Green uint8 `yaml:"green"`
Blue uint8 `yaml:"blue"`
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
}
type ToolTip struct {
Content string `yaml:"content"`
Localized bool `yaml:"localized"`
}
// Element represents the yaml description of a ui element
type Element struct {
ID string `yaml:"id"`
Container Container `yaml:"container"`
Layout Layout `yaml:"layout"`
Widget Widget `yaml:"widget"`
Canvas Canvas `yaml:"canvas"`
Padding Padding `yaml:"padding"`
Margins Margins `yaml:"margins"`
// Border Layout Elements
Top *Element `yaml:"top"`
Bottom *Element `yaml:"bottom"`
Left *Element `yaml:"left"`
Right *Element `yaml:"right"`
Center []*Element `yaml:"center"`
// Other Layout Elements
Children []*Element `yaml:"children"`
Content *Element `yaml:"content"`
// Form Element
Label *Element `yaml:"label"`
Field *Element `yaml:"field"`
// Grid configuration
Columns *int `yaml:"columns"`
Rows *int `yaml:"rows"`
RowColumns *int `yaml:"rowColumns"`
ElementSize *Size `yaml:"elementSize"`
// MinSize configuration
MinSize *Size `yaml:"minSize"`
// Relative Size configuration
// Only valid for certain widgets:
// - ScoreDisplay
RelativeSize *RelativeSize `yaml:"relativeSize"`
// Widget Properties
Text string `yaml:"text"`
Localized bool `yaml:"localized"`
Icon string `yaml:"icon"`
Binding string `yaml:"binding"`
Decorators []string `yaml:"decorators"`
Options map[string]any `yaml:"options"`
Disabled bool `yaml:"disabled"`
Hidden bool `yaml:"hidden"`
ToolTip *ToolTip `yaml:"tooltip"`
// Properties shared by most widgets
Placeholder string `yaml:"placeholder"`
PlaceholderLocalized bool `yaml:"placeholderLocalized"`
// Calendar Properties
Time *string `yaml:"time"`
TimeFormat *string `yaml:"timeFormat"`
// Entry Properties
MultiLine bool `yaml:"multiLine"`
Validator string `yaml:"validator"`
// List Properties
ItemTemplate string `yaml:"itemTemplate"`
ItemRenderer string `yaml:"itemRenderer"`
ListLength string `yaml:"listLength"`
// Check Properties
Checked bool `yaml:"checked"`
// ScoreDisplay Properties
Score int `yaml:"score"`
NumDigits int `yaml:"numDigits"`
// Select Properties
SelectOptionsBinding string `yaml:"selectOptionsBinding"`
SelectOptions []string `yaml:"selectOptions"`
// Spacer Properties
FixHorizontal bool `yaml:"fixHorizontal"`
FixVertical bool `yaml:"fixVertical"`
// Canvas Properties
FillColor *Color `yaml:"fillColor"`
StrokeColor *Color `yaml:"strokeColor"`
TextColor *Color `yaml:"textColor"`
StrokeWidth *float32 `yaml:"strokeWidth"`
CornerRadius *float32 `yaml:"cornerRadius"`
TextSize *float32 `yaml:"textSize"`
ImageName string `yaml:"imageName"`
Translucency *float64 `yaml:"translucency"`
// Widgets requiring a set of resources (e.g ScoreDisplay)
ResourceSet map[string]string `yaml:"resourceSet"`
// Handlers
OnClicked string `yaml:"onClicked"`
OnUpClicked string `yaml:"onUpClicked"`
OnDownClicked string `yaml:"onDownClicked"`
OnChanged string `yaml:"onChanged"`
OnSelected string `yaml:"onSelected"`
OnUnselected string `yaml:"onUnselected"`
OnDateSelected string `yaml:"onDateSelected"`
OnOptionSelected string `yaml:"onOptionSelected"`
OnValidationChanged string `yaml:"onValidationChanged"`
}
// BuildUI generates the UI for the Element.
//
// It takes a ScreenHandler parameter and returns a fyne.CanvasObject and an error.
func (e *Element) BuildUI(s ScreenHandler) (obj fyne.CanvasObject, err error) {
var baseObject fyne.CanvasObject
defer func() {
if e.ID != "" && baseObject != nil {
s.RegisterElement(e.ID, baseObject, obj)
}
}()
if e.Container != "" {
if baseObject, obj, err = e.Container.Build(e, s); err != nil {
err = fmt.Errorf("failed to build container element: %w", err)
return
}
return
}
if e.Layout != "" {
if baseObject, obj, err = e.Layout.Build(e, s); err != nil {
err = fmt.Errorf("failed to build layout element: %w", err)
return
}
return
}
if e.Widget != "" {
if baseObject, obj, err = e.Widget.Build(e, s); err != nil {
err = fmt.Errorf("failed to build widget element: %w", err)
return
}
return
}
if e.Canvas != "" {
if baseObject, obj, err = e.Canvas.Build(e, s); err != nil {
err = fmt.Errorf("failed to build canvas element: %w", err)
return
}
return
}
return
}
func (e *Element) localize(s ScreenHandler) {
if e.Localized {
e.Text = i18n.T(s.GetLocalizer(), e.Text)
}
if e.PlaceholderLocalized {
e.Placeholder = i18n.T(s.GetLocalizer(), e.Placeholder)
}
}