How to remove objects from golang fyne container

2.1k Views Asked by At

I am working on GUI application that will need to dynamically add and remove gui elements and I would like to know if there is a way to remove an element from golang fyne container. In the example code below I create the container and add elements dynamically and now I would like to be able to also remove these elements instead of hiding them. (One "solution" that I tried is to create new container, fill it and Reset the window content, but it blinks and I did not like it).

package main

import (
    "fyne.io/fyne"
    "fyne.io/fyne/app"
    "fyne.io/fyne/canvas"
    "fyne.io/fyne/layout"
    "fyne.io/fyne/widget"
    "image/color"
    "strconv"
    "time"
)

type Input struct {
    Id       string
    ThumbUrl string
}

func (input Input) GetContainer() *fyne.Container {
    thumbImg := canvas.NewText("There", color.White)
    group := widget.NewGroup(input.Id, thumbImg)
    ret := fyne.NewContainerWithLayout(layout.NewVBoxLayout(), group)
    return ret
}

type Segment struct {
    Id           string
    Inputs       []Input
    InputsColumn *fyne.Container
}

func (seg *Segment) GetSegment() fyne.CanvasObject {
    first := true
    var inputsColumn *fyne.Container
    for _, in := range seg.Inputs {
        if first {
            inputsColumn = fyne.NewContainerWithLayout(layout.NewGridLayout(1), in.GetContainer())
            first = false
        } else {
            inputsColumn.AddObject(in.GetContainer())
        }
    }
    seg.InputsColumn = inputsColumn

    ret := fyne.NewContainerWithLayout(layout.NewHBoxLayout(), seg.InputsColumn)
    return ret
}

func (seg *Segment) AddInput(input Input) {
    seg.InputsColumn.AddObject(input.GetContainer())
}

var (
    segment1 = Segment{
        Id: "VSEG_1",
        Inputs: []Input{
            {
                Id:       "VIN_HDMI1",
                ThumbUrl: "thumbIN.png",
            },
            {
                Id:       "VIN_SDI1",
                ThumbUrl: "thumbIN.png",
            },
            {
                Id:       "VIN_SDVOE1",
                ThumbUrl: "thumbIN.png",
            },
        },
    }
)

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("Container")
    segmentLayout := segment1.GetSegment()
    lay := fyne.NewContainerWithLayout(layout.NewHBoxLayout(), segmentLayout)

    myWindow.SetContent(lay)
    go changeContent(myWindow)
    myWindow.ShowAndRun()
}

func changeContent(win fyne.Window) {
    for i := 0; i < 3; i++ {
        time.Sleep(time.Second * 2)
        newInput := Input{
            Id:       "Added_ID_" + strconv.Itoa(i),
            ThumbUrl: "thumbIN.png",
        }
        segment1.AddInput(newInput)
        segment1.InputsColumn.Refresh()

    }
    for i := 0; i < 3; i++ {
        time.Sleep(time.Second * 2)
        // Remove the objects instead of hiding
        segment1.InputsColumn.Objects[i].Hide()
        segment1.InputsColumn.Refresh()
    }
    segment1.InputsColumn.Refresh()

}
1

There are 1 best solutions below

4
On BEST ANSWER

With fyne.Container you can change the Objects field directly. For example you could remove the first item added using the following:

l1 := widget.NewLabel("first")
l2 := widget.NewLabel("second")
c := fyne.NewContainer(l1, l2)
log.Println(len(c.Objects)) // 2

c.Objects = c.Objects[:1]
c.Refresh() // ensures UI reflects the change
log.Println(len(c.Objects)) // 1