Custom View

A custom View must implement the CustomView interface, which extends the ViewsContainer and View interfaces.A custom View is created based on another, which is named Super View.

To simplify the task, there is already a basic CustomView implementation in the form of a CustomViewData structure.

Let's consider creating a custom View using the built-in Button element as an example:

1) declare the Button interface as extending CustomView, and the buttonData structure as extending CustomViewData

type Button interface {
type buttonData struct {

2) implement the CreateSuperView function

func (button *buttonData) CreateSuperView(session Session) View {
	return rui.NewListLayout(session, rui.Params{
		rui.Semantics:       rui.ButtonSemantics,
		rui.Style:           "ruiButton",
		rui.StyleDisabled:   "ruiDisabledButton",
		rui.HorizontalAlign: rui.CenterAlign,
		rui.VerticalAlign:   rui.CenterAlign,
		rui.Orientation:     rui.StartToEndOrientation,

3) if necessary, override the methods of the CustomView interface, for Button this is the Focusable() function(since the button can receive focus, but ListLayout does not)

func (button *buttonData) Focusable() bool {
	return true

4) write a function to create a Button:

func NewButton(session rui.Session, params rui.Params) Button {
	button := new(buttonData)
	rui.InitCustomView(button, "Button", session, params)
	return button

When creating a CustomView, it is mandatory to call the InitCustomView function.This function initializes the CustomViewData structure.The first argument is a pointer to the structure to be initialized,the second is the name assigned to your View, the third is the session and the fourth is the parameters

5) registering the item. It is recommended to register in the init method of the package

rui.RegisterViewCreator("Button", func(session rui.Session) rui.View {
	return NewButton(session, nil)

All! The new element is ready