Skip to content

Data API

Andy Williams edited this page Jul 15, 2019 · 13 revisions

The objective of this page is to define a Data API that could be used by all widget that need to handle data like Select, Radio, Checkbox and Table.

The Table widget has not been designed / implemented yet, but some discussion have been done with regard to the Select widget and are summarized in the next sections.

Introducing the DataItem, DataMap and DataSource interfaces

The DataItem interface that embeds the fmt.Stringer interface should allow to handle complex types and at the same time allow to use the String method to handle labels. It also provides the opportunity to hook in to be informed of change events so that widgets can update accordingly.

type DataItem interface {
  String() string
  AddListener(func(DataItem))
}

The DataMap interface is like a DataItem except that it has many items each with a name. It extends DataItem so that anything returning an item can also return a map. The change listener is called when an item (or multiple) within the map is changed.

type DataMap interface {
  DataItem
  Get(string) DataItem
  AddListener(func(DataMap))
}

The DataSource interface defines an interface that returns multiple DataItems you can consider it like []DataItem except that it can support lazy loading and advanced features like paging. The change listener is notified if the number if items in the source changes - an addition or deletion - but not if items within it change.

type DataSource interface {
  Count() int
  Get(int) DataItem
  AddListener(func(DataSource))
}

The Get(int) from DataSource could also return a DataMap, if the widget you have built expects it.

Future extensions such as PagedDataSource may be added in the future.

DataSource helpers

We can add helper functions such as the creation of a data from string or data source from a slice.

func NewDataItem(Stringer) DataItem {}
func NewDataItemFromString(string) DataItem {}

func NewDataMapFromMap([string]DataItem) DataMap {}

func NewDataSourceFromSlice([]DataItem) DataSource
func NewDataSourceFromStrings([]string) DataSource

Impact on existing widgets

The introduction of the DataItem and DatSource interfaces will break the current API for the Radio and Checkbox widgets. The break is related to the type changes for the Selected, Options and OnChanged fields.

Example implementation for the Select widget

package widget

import "fmt"

type Select struct {
	baseWidget
	Selected DataItem
	Options  DataSource

	OnChanged func(DataItem) `json:"-"`
	hovered   bool
	popover   *PopOver
}

func NewSelect(options DataSource, changed func(DataItem)) *Select {
	combo := &Select{baseWidget{}, nil, options, changed, false, nil}
	options.AddListener(func(DataSource) {
		combo.Refresh()
	}())

	Renderer(combo).Layout(combo.MinSize())
	return combo
}

Welcome to the Fyne wiki.

Project documentation

Getting involved

Platform details

Clone this wiki locally