ui/screen/element.go

210 lines
5.4 KiB
Go
Raw Normal View History

2024-03-30 17:45:07 +01:00
package screen
import (
"fmt"
"gitea.hevanto-it.com/hevanto/i18n"
2024-03-30 17:45:07 +01:00
"fyne.io/fyne/v2"
)
2024-04-03 16:07:24 +02:00
// Size represents the yaml description of a ui size
2024-03-30 17:45:07 +01:00
type Size struct {
Width float32 `yaml:"width"`
Height float32 `yaml:"height"`
}
2024-04-19 16:44:36 +02:00
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"`
}
2024-06-17 15:58:12 +02:00
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
}
2024-04-03 16:07:24 +02:00
// Element represents the yaml description of a ui element
2024-03-30 17:45:07 +01:00
type Element struct {
ID string `yaml:"id"`
Container Container `yaml:"container"`
Layout Layout `yaml:"layout"`
Widget Widget `yaml:"widget"`
2024-04-19 16:44:36 +02:00
Canvas Canvas `yaml:"canvas"`
2024-03-30 17:45:07 +01:00
2024-06-17 15:58:12 +02:00
Padding Padding `yaml:"padding"`
Margins Margins `yaml:"margins"`
2024-03-30 17:45:07 +01:00
// 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"`
2024-03-30 17:45:07 +01:00
// 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"`
2024-04-19 16:44:36 +02:00
// Relative Size configuration
// Only valid for certain widgets:
// - ScoreDisplay
RelativeSize *RelativeSize `yaml:"relativeSize"`
2024-03-30 17:45:07 +01:00
// 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"`
2024-04-11 14:52:32 +02:00
// Properties shared by most widgets
Placeholder string `yaml:"placeholder"`
PlaceholderLocalized bool `yaml:"placeholderLocalized"`
2024-03-30 17:45:07 +01:00
// Calendar Properties
Time *string `yaml:"time"`
TimeFormat *string `yaml:"timeFormat"`
2024-04-03 16:07:24 +02:00
// Entry Properties
2024-04-11 14:52:32 +02:00
MultiLine bool `yaml:"multiLine"`
Validator string `yaml:"validator"`
2024-04-03 16:07:24 +02:00
2024-03-30 17:45:07 +01:00
// List Properties
ItemTemplate string `yaml:"itemTemplate"`
ItemRenderer string `yaml:"itemRenderer"`
ListLength string `yaml:"listLength"`
// Check Properties
Checked bool `yaml:"checked"`
2024-04-19 16:44:36 +02:00
// ScoreDisplay Properties
Score int `yaml:"score"`
NumDigits int `yaml:"numDigits"`
2024-04-11 14:52:32 +02:00
// Select Properties
SelectOptionsBinding string `yaml:"selectOptionsBinding"`
SelectOptions []string `yaml:"selectOptions"`
2024-03-30 17:45:07 +01:00
// Spacer Properties
FixHorizontal bool `yaml:"fixHorizontal"`
FixVertical bool `yaml:"fixVertical"`
2024-04-19 16:44:36 +02:00
// 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"`
2024-03-30 17:45:07 +01:00
// Handlers
2024-04-03 16:07:24 +02:00
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"`
2024-04-11 14:52:32 +02:00
OnOptionSelected string `yaml:"onOptionSelected"`
2024-04-03 16:07:24 +02:00
OnValidationChanged string `yaml:"onValidationChanged"`
2024-03-30 17:45:07 +01:00
}
2024-04-03 16:07:24 +02:00
// BuildUI generates the UI for the Element.
//
// It takes a ScreenHandler parameter and returns a fyne.CanvasObject and an error.
2024-03-30 17:45:07 +01:00
func (e *Element) BuildUI(s ScreenHandler) (obj fyne.CanvasObject, err error) {
2024-04-03 16:07:24 +02:00
var baseObject fyne.CanvasObject
2024-03-30 17:45:07 +01:00
defer func() {
2024-04-03 16:07:24 +02:00
if e.ID != "" && baseObject != nil {
s.RegisterElement(e.ID, baseObject, obj)
2024-03-30 17:45:07 +01:00
}
}()
if e.Container != "" {
2024-04-03 16:07:24 +02:00
if baseObject, obj, err = e.Container.Build(e, s); err != nil {
err = fmt.Errorf("failed to build container element: %w", err)
return
}
return
}
2024-03-30 17:45:07 +01:00
if e.Layout != "" {
2024-04-03 16:07:24 +02:00
if baseObject, obj, err = e.Layout.Build(e, s); err != nil {
2024-03-30 17:45:07 +01:00
err = fmt.Errorf("failed to build layout element: %w", err)
return
}
return
}
if e.Widget != "" {
2024-04-03 16:07:24 +02:00
if baseObject, obj, err = e.Widget.Build(e, s); err != nil {
2024-03-30 17:45:07 +01:00
err = fmt.Errorf("failed to build widget element: %w", err)
return
}
return
}
2024-04-19 16:44:36 +02:00
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
}
2024-03-30 17:45:07 +01:00
return
}
func (e *Element) localize(s ScreenHandler) {
if e.Localized {
e.Text = i18n.T(s.GetLocalizer(), e.Text)
}
2024-04-03 16:07:24 +02:00
if e.PlaceholderLocalized {
e.Placeholder = i18n.T(s.GetLocalizer(), e.Placeholder)
}
2024-03-30 17:45:07 +01:00
}