conformal / gotk3

Go bindings for GTK3
ISC License
470 stars 81 forks source link

Function added using .IdleAdd is not executed #100

Closed pzduniak closed 9 years ago

pzduniak commented 9 years ago
import (
    "bytes"
    "image"
    "image/png"
    "log"

    "github.com/conformal/gotk3/glib"
    "github.com/sourcegraph/go-webkit2/webkit2"
)

func RasterizeURI(uri string, width int, height int) ([]byte, error) {
    resultChan := make(chan []byte, 1)
    errChan := make(chan error, 1)

    log.Print("INSIDE RASTERIZEURI")

    glib.IdleAdd(func() bool {
        log.Print("INSIDE IDLEADD")

        webView := webkit2.NewWebView()

        renderSuccess := make(chan struct{}, 1)

        webView.Connect("load-changed", func(_ *glib.Object, loadEvent webkit2.LoadEvent) {
            switch loadEvent {
            case webkit2.LoadFinished:
                renderSuccess <- struct{}{}
            }
        })

        webView.LoadURI(uri)

        <-renderSuccess

        webView.GetSnapshot(func(result *image.RGBA, err error) {
            if err != nil {
                errChan <- err
            }

            buf := new(bytes.Buffer)
            err = png.Encode(buf, result)
            errChan <- err
            resultChan <- buf.Bytes()
        })

        webView.Destroy()

        return false
    })

    err := <-errChan
    if err != nil {
        return nil, err
    }

    data := <-resultChan

    return data, nil
}

output:

2015/01/03 16:32:04 INSIDE RASTERIZEURI

What am I doing incorrectly? Why aren't the contents of glib.IdleAdd executed?

jrick commented 9 years ago

Is the glib event loop running (started by gtk.Main)?

pzduniak commented 9 years ago

Thanks, I didn't notice that most packages require user to call it anyways. I think my next issue is related to sourcegraph's webkit2, so I'll go and ask there.