roblillack / spot

React-like desktop GUI toolkit for Go
MIT License
1.11k stars 17 forks source link

ListBox: rows blank & OnSelect not working after toggling ListBox #22

Closed Suremeo closed 4 months ago

Suremeo commented 4 months ago

Its kind of hard to explain, but here is an example of the issue

package main

import (
    "fmt"
    "github.com/roblillack/spot"
    "github.com/roblillack/spot/ui"
    "time"
)

func main() {
    ui.Init()
    spot.BuildFn(func(ctx *spot.RenderContext) spot.Component {
        toggle, setToggle := spot.UseState[bool](ctx, true)

        components := []spot.Component{
            &ui.Button{
                Width:  200,
                Height: 20,
                Title:  "toggle",
                OnClick: func() {
                    setToggle(!toggle)
                },
            },
        }

        if toggle {
            components = append(components, &ui.ListBox{
                Y:         20,
                Width:     200,
                Height:    180,
                Values:    []string{"Item 1", "Item 2"},
                Selection: []int{0},
                OnSelect: func(ints []int) {
                    fmt.Printf("Selected %v at %v\n", ints[0], time.Now().Unix())
                },
            })
        }

        return &ui.Window{
            Title:    "Issue #22",
            Width:    200,
            Height:   200,
            Children: components,
        }
    }).Mount()

    ui.Run()
}

When the program starts, it works fine, but when you press toggle, then press toggle again, the rows are all blank, and the OnSelect callback is broken, even though the values are being passed through the exact same way.

This is a problem because if you want to have a multi-page app it breaks functionality for the ListBox, other components seem to function as expected when doing the same type of test

roblillack commented 4 months ago

Hey @Suremeo, thanks for reporting this issue (including the nice testcase). It should be working as expected, now.