Search code examples
gofyne

Using fyne widgets with OOP-style


I want to combine some standard widget in one custom widget. I can do it if put all of widget fields into one container like so:

package main

import (
    "fmt"

    "fyne.io/fyne"
    "fyne.io/fyne/app"
    "fyne.io/fyne/layout"
    "fyne.io/fyne/widget"
)

type MyWidget struct {
    widget.BaseWidget

    Cont      *fyne.Container
    text      *widget.Label
    statusBar *widget.Label
    b1        *widget.Button
    b2        *widget.Button

    count uint
}

func (t *MyWidget) Init() {
    t.b1 = widget.NewButton("1", func() {
        t.text.SetText("1")
        t.count++
        t.statusBar.SetText(fmt.Sprint(t.count))
    })
    t.b2 = widget.NewButton("2", func() { t.text.SetText("2") })
    t.statusBar = widget.NewLabel("status")
    bottom := fyne.NewContainerWithLayout(layout.NewCenterLayout(), t.statusBar)
    t.text = widget.NewLabelWithStyle("0", fyne.TextAlignTrailing, fyne.TextStyle{Bold: true})
    t.Cont = fyne.NewContainerWithLayout(layout.NewBorderLayout(nil, bottom, nil, nil),
        bottom, fyne.NewContainerWithLayout(
            layout.NewGridLayoutWithRows(4),
            fyne.NewContainerWithLayout(layout.NewCenterLayout(), t.text),
            layout.NewSpacer(),
            fyne.NewContainerWithLayout(layout.NewGridLayout(2), t.b1, t.b2),
            layout.NewSpacer(),
        ))
}

func Load() *MyWidget {
    obj := &MyWidget{BaseWidget: widget.BaseWidget{}}
    obj.Init()
    return obj
}

func main() {
    f := app.New()
    w := f.NewWindow("")
    obj := Load()
    w.SetContent(obj.Cont)
    w.ShowAndRun()
}

I used to use GUI toolkits where top widget has opportunity to set container for holding child widgets. Is it possible get solution with Fyne without exported inner container?


Solution

  • I would recommend you look at using a container instead. (I.e. ‘fyne.NewContainerWithLayout(myLayout, widgets...)’.

    Widgets and containers are distinct in Fyne. Widgets are an encapsulation for logic, with a renderer to display, Containers are used to group multiple widgets. There are some widgets that bridge the gap, such as widget.Box and widget.Group, but they typically expose a container, or re-export the container methods.

    Generally you don’t make a tree of widgets, but rather a container tree with widgets at the loop.