andlabs / ui

Platform-native GUI library for Go.
Other
8.33k stars 651 forks source link

ProgressBar not updated under linux #360

Open FrolovDmitry opened 5 years ago

FrolovDmitry commented 5 years ago

this code works on windows and not work under linux:

pbar := ui.NewProgressBar()
button := ui.NewButton("TEST")
button.OnClicked(func(*ui.Button) {
    for i := 1; i <= 100; i++ {
        pbar.SetValue(i)
        time.Sleep(10 * time.Millisecond)
    }
})
andlabs commented 5 years ago

You can't use time.Sleep() in the UI thread to wait, because nothing will happen while that function is called, including redrawing. (It only seems to work on Windows because of some Windows-specific tricks that not every control uses.)

You can use a goroutine and ui.QueueMain() to update the progressbar on an interval. I might add a ui.Timer() at some point in the future (libui already has it).

FrolovDmitry commented 5 years ago

thanks, it worked:

type progress func(current int) 

pbar := ui.NewProgressBar()
button := ui.NewButton("TEST")
button.OnClicked(func(*ui.Button) {     
    button.Disable()
    pbar.Show()
    myFunc(func(value int) {
        ui.QueueMain(func() {
            pbar.SetValue(value)
        })
    })
})

func myFunc(p progress) {
    go func() {
        for i := 1; i <= 100; i++ {
            time.Sleep(10 * time.Millisecond)
            p(i)
        }
    }()
}
andlabs commented 5 years ago

Keeping open until I add this to the documentation I am now writing.