2

I wanted to use the Progress bar from gxui, but get not what I expected. The example works as it should,but change it I did not succeed. Here is the code:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/math"
    "github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
    theme := flags.CreateTheme(driver)

    layout := theme.CreateLinearLayout()
    layout.SetHorizontalAlignment(gxui.AlignCenter)

    progressBar := theme.CreateProgressBar()
    progressBar.SetDesiredSize(math.Size{W: 480, H: 60})

    button := theme.CreateButton()
    button.SetText("Start")
    t0 := time.Now()
    button.OnClick(func(gxui.MouseEvent) {
        progressBar.SetTarget(100)
        N := 100

        for count := 0; count < N; count++ {
            resp, err := http.Get("http://example.com")
            if err != nil {
                log.Fatal(err)
            }
            defer resp.Body.Close()

            if count%10 == 0 {

                go func() {
                    driver.Call(func() {
                        fmt.Println("Tuk")
                        progressBar.SetProgress(count * 100 / N)
                    })
                }()
                fmt.Println(count)
                fmt.Println(ioutil.ReadAll(resp.Body))
                fmt.Printf("Elapsed time: %v\n", time.Since(t0))
            }
        }
        progressBar.SetProgress(50)
    })

    layout.AddChild(button)
    layout.AddChild(progressBar)

    window := theme.CreateWindow(500, 100, "Test")
    window.SetScale(flags.DefaultScaleFactor)
    window.AddChild(layout)
    window.OnClose(driver.Terminate)
}

func main() {
    gl.StartDriver(appMain)
}

Since I used goroutine, it is assumed that the output text will alternate, but all goroutine performed print only after the main thread. What am I doing wrong and how to fix it?

1 Answers1

1

The difference is that your goroutine enter the queue executing the UI routine, as written in the documentation:

// Call queues f to be run on the UI go-routine, returning before f may have been called.

UI-routine executes a loop, so can not simultaneously handle the change tape ProgressBar. To get the desired result, it is necessary run handling function in a separate goroutine. Modified code:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "time"

    "github.com/google/gxui"
    "github.com/google/gxui/drivers/gl"
    "github.com/google/gxui/math"
    "github.com/google/gxui/samples/flags"
)

func appMain(driver gxui.Driver) {
    theme := flags.CreateTheme(driver)

    layout := theme.CreateLinearLayout()
    layout.SetHorizontalAlignment(gxui.AlignCenter)

    progressBar := theme.CreateProgressBar()
    progressBar.SetDesiredSize(math.Size{W: 480, H: 60})
    progressBar.SetTarget(100)
    button := theme.CreateButton()
    button.SetText("Start")
    t0 := time.Now()
    button.OnClick(func(gxui.MouseEvent) {
        go func() {
            N := 100
            for count := 0; count < N; count++ {
                resp, err := http.Get("http://example.com")
                if err != nil {
                    log.Fatal(err)
                }
                defer resp.Body.Close()

                driver.Call(func() {
                    progressBar.SetProgress(count * 100 / N)
                })

                fmt.Println(count)
                fmt.Println(ioutil.ReadAll(resp.Body))
                fmt.Printf("Elapsed time: %v\n", time.Since(t0))

            }
            progressBar.SetTarget(100)
        }()
    })

    layout.AddChild(button)
    layout.AddChild(progressBar)

    window := theme.CreateWindow(500, 100, "Test")
    window.SetScale(flags.DefaultScaleFactor)
    window.AddChild(layout)
    window.OnClose(driver.Terminate)

}

func main() {
    gl.StartDriver(appMain)
}