fyneloader

package module
v0.0.0-...-dd47076 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 18 Imported by: 0

README

fyne-loader

Overview

fyne-loader is a dynamic UI builder for the fyne UI toolkit, useful for prototyping fyne UIs in a manner that doesn't require re-compiling between every run.

How it works

UI elements may be generated by loading a YAML or JSON file with the Loader type, which returns all loaded elements from the file as a map of names to fyne.CanvasObject instances. To see an example of how this works in code, see ./cmd/example/main.go.

The load functions only return errors when there is a YAML or JSON error that prevents parsing of the file. Otherwise, all errors are reported through a context object; this allows the loader to report all errors encountered, as well as attempt to continue even when errors are present.

Supported Widgets

The current set of supported widgets is:

  • Accordion
  • Button
  • Card
  • Check
  • HBox
  • Label
  • RadioGroup
  • Slider
  • Spacer
  • VBox

Extra widgets which are planned to be supported are:

  • AdaptiveGrid
  • BorderContainer
  • CenterContainer
  • Entry
  • Form
  • Grid
  • GridWrap
  • Hyperlink
  • Icon
  • MaxContainer
  • PaddedContainer
  • ProgressBar
  • ProgressBarInfinite
  • Scroll
  • Select
  • SelectEntry
  • Separator
  • Split
  • Tabs
  • TextGrid
  • Toolbar

Adding New Widgets

New widgets may be added by creating a function which matches the type CreateElementFn and adding it to a loader with (*Loader).RegisterElement. This allows adding any arbitrary element to the loader. The function given should handle both string and map[string]interface{} values.

Documentation

Index

Constants

View Source
const (
	KeyAlign       = "align"
	KeyChild       = "child"
	KeyChildren    = "children"
	KeyDisabled    = "disabled"
	KeyFunc        = "func"
	KeyHidden      = "hidden"
	KeyIconPlace   = "icon-placement"
	KeyImageFill   = "image-fill"
	KeyImagePath   = "image-path"
	KeyImageURI    = "image-uri"
	KeyImportance  = "importance"
	KeyItems       = "items"
	KeyMax         = "max"
	KeyMin         = "min"
	KeyMultiOpen   = "multi-open"
	KeyOpen        = "open"
	KeyOptions     = "options"
	KeyOrientation = "orientation"
	KeyRequired    = "required"
	KeySelected    = "selected"
	KeyStep        = "step"
	KeyStyle       = "style"
	KeySubTitle    = "subtitle"
	KeyText        = "text"
	KeyTitle       = "title"
	KeyType        = "type"
	KeyWrap        = "wrap"
)

Key constants define the JSON/YAML syntax that the loader accepts.

View Source
const (
	ValueBreak      = "break"
	ValueCenter     = "center"
	ValueContain    = "contain"
	ValueDefault    = "default"
	ValueHigh       = "high"
	ValueHorizontal = "horizontal"
	ValueLeading    = "leading"
	ValueLow        = "low"
	ValueMedium     = "medium"
	ValueOff        = "off"
	ValueOriginal   = "original"
	ValueStretch    = "stretch"
	ValueTrailing   = "trailing"
	ValueTruncate   = "truncate"
	ValueVertical   = "vertical"
	ValueWord       = "word"
)

Value constants define constant values that the loader accepts.

Variables

This section is empty.

Functions

func CreateAccordion

func CreateAccordion(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateAccordion creates a new Accordion widget.

func CreateButton

func CreateButton(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateButton creates a new button using the data in v.

func CreateCard

func CreateCard(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateCard creates a new Card widget.

func CreateCheck

func CreateCheck(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateCheck creates a new Check widget.

func CreateHBox

func CreateHBox(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateHBox creates a new HBox container.

func CreateHSpacer

func CreateHSpacer(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateHSpacer creates a new horizontal spacer.

func CreateLabel

func CreateLabel(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateLabel creates a new Label.

func CreateRadioGroup

func CreateRadioGroup(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateRadioGroup creates a new RadioGroup widget.

func CreateSlider

func CreateSlider(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateSlider creates a new Slider widget.

func CreateSpacer

func CreateSpacer(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateSpacer creates a new spacer which expands both vertically and horizontally.

func CreateVBox

func CreateVBox(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateVBox creates a new HBox container.

func CreateVSpacer

func CreateVSpacer(ctx *errctx.Context, l *Loader, data map[string]interface{}) fyne.CanvasObject

CreateVSpacer creates a new vertical spacer.

func GetButtonAlign

func GetButtonAlign(ctx *errctx.Context, data map[string]interface{}) widget.ButtonAlign

GetButtonAlign fetches and interprets a string from the map as a button alignment.

func GetButtonIconPlacement

func GetButtonIconPlacement(ctx *errctx.Context, data map[string]interface{}) widget.ButtonIconPlacement

GetButtonIconPlacement fetches and interprets a string from the map as a button icon placement value.

func GetButtonImportance

func GetButtonImportance(ctx *errctx.Context, data map[string]interface{}) widget.ButtonImportance

GetButtonImportance fetches and interprets a string from the map as a button importance value.

func GetFnBoolToVoid

func GetFnBoolToVoid(l *Loader, data map[string]interface{}, key string) (func(bool), error)

GetFnBoolToVoid fetches a func(bool) from the registered functions in the loader.

func GetFnFloat64ToVoid

func GetFnFloat64ToVoid(l *Loader, data map[string]interface{}, key string) (func(float64), error)

GetFnFloat64ToVoid fetches a func(float64) from the registered functions in the loader.

func GetFnStringToVoid

func GetFnStringToVoid(l *Loader, data map[string]interface{}, key string) (func(string), error)

GetFnStringToVoid fetches a func(string) from the registered functions in the loader.

func GetFnVoidToVoid

func GetFnVoidToVoid(l *Loader, data map[string]interface{}, key string) (func(), error)

GetFnVoidToVoid fetches a func() from the registered functions in the loader.

func GetOrientation

func GetOrientation(ctx *errctx.Context, data map[string]interface{}, defval widget.Orientation) widget.Orientation

GetOrientation fetches and interprets a string from the map as an orientation value.

func GetStringEnumAsInt

func GetStringEnumAsInt(data map[string]interface{}, key string, allowed []string, values []int, def int) (int, error)

GetStringEnumAsInt fetches a string value from the map and converts it to an integer.

func GetStringFromArray

func GetStringFromArray(data map[string]interface{}, key string, opts []string) (string, error)

GetStringFromArray fetches either a string value or an integer value.

If the value is a string, it must be present in the given array.

func GetTextAlign

func GetTextAlign(ctx *errctx.Context, data map[string]interface{}) fyne.TextAlign

GetTextAlign fetches and interprets a string from the map as a text alignment.

func GetTextStyle

func GetTextStyle(ctx *errctx.Context, data map[string]interface{}, key string) fyne.TextStyle

GetTextStyle fetches and interprets a string from the map as a text style.

func GetTextWrap

func GetTextWrap(ctx *errctx.Context, data map[string]interface{}) fyne.TextWrap

GetTextWrap fetches and interprets a string from the map as a text wrap value.

Types

type ArrayIndexOutOfBoundsError

type ArrayIndexOutOfBoundsError struct {
	Index int64
}

ArrayIndexOutOfBoundsError is an error indicating that the given index was out of bounds for the source array.

func (ArrayIndexOutOfBoundsError) Error

type ConflictingKeysError

type ConflictingKeysError struct {
	Keys []string
}

ConflictingKeysError is an error which indicates that some keys in the configuration conflict.

func (ConflictingKeysError) Error

func (e ConflictingKeysError) Error() string

type ConstError

type ConstError string

ConstError is a simple constant error type.

const (
	// ErrFetchURIDisabled indicates that an image URI was parsed using a
	// loader with that disabled.
	ErrFetchURIDisabled ConstError = "loading images from URIs is disabled"

	// ErrNoWidgetType indicates that an element map did not have a type field.
	ErrNoWidgetType ConstError = "no type tag on element definition"

	// ErrUnknownFileExt indicates that a file path did not have a known
	// extension.
	ErrUnknownFileExt ConstError = "unknown file extension"

	// ErrInvalidOption indicates that an option given was not valid.
	ErrInvalidOption ConstError = "invalid option"
)

func (ConstError) Error

func (e ConstError) Error() string

type CreateElementFn

type CreateElementFn func(*errctx.Context, *Loader, map[string]interface{}) fyne.CanvasObject

CreateElementFn is the function type used for the element creation callbacks.

type FunctionTypeError

type FunctionTypeError struct {
	Func interface{}
}

FunctionTypeError is an error which indicates that a function type did not match any of the allowed types.

func (FunctionTypeError) Error

func (e FunctionTypeError) Error() string

type Loader

type Loader struct {
	FetchURIs bool
	// contains filtered or unexported fields
}

Loader allows for loading UI definitions at runtime.

func New

func New() *Loader

New returns a new Loader instance.

func (*Loader) GetChild

func (l *Loader) GetChild(ctx *errctx.Context, data map[string]interface{}) fyne.CanvasObject

GetChild fetches the value for the 'child' key and attempts to unpack it as an element.

func (*Loader) GetFunc

func (l *Loader) GetFunc(name string) (interface{}, error)

GetFunc returns the function with the given name.

If a function with the given name was not registered, this function will return nil and an error indicating such.

func (*Loader) GetImage

func (l *Loader) GetImage(ctx *errctx.Context, data map[string]interface{}) *canvas.Image

GetImage fetches and loads an image from a series of keys.

func (*Loader) ReadFile

func (l *Loader) ReadFile(ctx *errctx.Context, path string) (map[string]fyne.CanvasObject, error)

ReadFile reads a file as either YAML or JSON.

func (*Loader) ReadFileJSON

func (l *Loader) ReadFileJSON(ctx *errctx.Context, path string) (map[string]fyne.CanvasObject, error)

ReadFileJSON reads a file as a JSON definition file.

func (*Loader) ReadFileYAML

func (l *Loader) ReadFileYAML(ctx *errctx.Context, path string) (map[string]fyne.CanvasObject, error)

ReadFileYAML reads a file as a YAML definition file.

func (*Loader) ReadJSON

func (l *Loader) ReadJSON(ctx *errctx.Context, in io.Reader) (map[string]fyne.CanvasObject, error)

ReadJSON takes a Reader and interprets it as JSON data.

func (*Loader) ReadYAML

func (l *Loader) ReadYAML(ctx *errctx.Context, in io.Reader) (map[string]fyne.CanvasObject, error)

ReadYAML takes a Reader and interprets it as YAML data.

func (*Loader) RegisterElement

func (l *Loader) RegisterElement(name string, fn CreateElementFn)

RegisterElement registers a new element callback.

If the function callback is nil and there already exists a callback for the given name, the callback will be removed. This function will replace a callback with no error if a name is repeated.

func (*Loader) RegisterFunc

func (l *Loader) RegisterFunc(name string, fn interface{}) error

RegisterFunc registers a new function available for use within generated UI elements.

The type of fn must be a function type; if it is not then the function will return an error. If a name is repeated, the function will be replaced in the set of available functions.

func (*Loader) Unmarshal

func (l *Loader) Unmarshal(ctx *errctx.Context, data map[string]interface{}) (map[string]fyne.CanvasObject, error)

Unmarshal takes a YAML or JSON map and creates a map of widgets from it.

func (*Loader) Unpack

func (l *Loader) Unpack(ctx *errctx.Context, v interface{}) fyne.CanvasObject

Unpack handles loading a single widget.

type UndefinedFunctionError

type UndefinedFunctionError struct {
	Name string
}

UndefinedFunctionError is an error which indicates that the function with the given name was not registered.

func (UndefinedFunctionError) Error

func (e UndefinedFunctionError) Error() string

type UnknownElementType

type UnknownElementType struct {
	TypeName string
}

UnknownElementType is an error which indicates that the element with the given name is unknown.

func (UnknownElementType) Error

func (e UnknownElementType) Error() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL