diff --git a/examples/loadimage/loadimage.go b/examples/loadimage/loadimage.go index dcb07ec6..55c7a0dd 100644 --- a/examples/loadimage/loadimage.go +++ b/examples/loadimage/loadimage.go @@ -1,23 +1,28 @@ package main import ( + "context" "fmt" "image" _ "image/jpeg" _ "image/png" "log" + "net/http" + "time" g "github.com/AllenDang/giu" ) var ( - fallback = &g.ReflectiveBoundTexture{} - gopher = &g.ReflectiveBoundTexture{} - rgba *image.RGBA + fallback = &g.ReflectiveBoundTexture{} + gopher = &g.ReflectiveBoundTexture{} + urlWidgetLike = &g.ReflectiveBoundTexture{} + rgba *image.RGBA ) func loop() { + urlWidgetLike.SetSurfaceFn(downloadUrlFn("https://static.wikia.nocookie.net/smashbros/images/0/0e/Art_Sonic_TSR.png/revision/latest?cb=20200210122913&path-prefix=fr", time.Second*10), true) g.SingleWindow().Layout( g.Label("Display image from texture"), gopher.ToImageWidget(), @@ -30,14 +35,16 @@ func loop() { fmt.Println("image from file was clicked") }).Size(860, 561), + // TODO Rewrite via func (i *ReflectiveBoundTexture) SetSurfaceFn(fn func() *image.RGBA, commit bool) error { + g.Label("Display image from url (wait few seconds to download)"), + g.ImageWithURL("https://png.pngitem.com/pimgs/s/3-36108_gopher-golang-hd-png-download.png").OnClick(func() { + fmt.Println("image from url clicked") + }).Size(300, 200), /* - g.Label("Display image from url (wait few seconds to download)"), - g.ImageWithURL("https://png.pngitem.com/pimgs/s/3-36108_gopher-golang-hd-png-download.png").OnClick(func() { - fmt.Println("image from url clicked") - - }).Size(300, 200), - g.Label("Display images from url with loading and fallback"), + g.Label("Display image from url without placeholder (no size when loading)"), + urlWidgetLike.ToImageWidget().Size(300, 200), + g.ImageWithURL( "https://png.pngitem.com/pimgs/s/424-4241958_transparent-gopher-png-golang-gopher-png-png-download.png"). Timeout(5*time.Second). @@ -48,7 +55,7 @@ func loop() { }), ). LayoutForFailure( - fallback.Image().Size(300, 200), + fallback.ToImageWidget().Size(300, 200), ). OnReady(func() { fmt.Println("Image is downloaded.") @@ -57,11 +64,8 @@ func loop() { g.Label("Handle failure event"), g.ImageWithURL("http://x.y/z.jpg").Timeout(2*time.Second).OnFailure(func(err error) { fmt.Printf("Failed to download image, Error msg is %s\n", err.Error()) - }), + }),*/ - g.Label("Display image from url without placeholder (no size when loading)"), - g.ImageWithURL("https://www.pngitem.com/pimgs/m/424-4242405_go-lang-gopher-clipart-png-download-golang-gopher.png").Size(300, 200), - */ g.Label("Footer"), ) } @@ -79,3 +83,32 @@ func main() { //wnd.SetIcon(rgba) wnd.Run(loop) } + +func downloadUrlFn(imgUrl string, timeout time.Duration) func() *image.RGBA { + return func() *image.RGBA { + client := &http.Client{Timeout: timeout} + req, err := http.NewRequestWithContext(context.Background(), "GET", imgUrl, http.NoBody) + if err != nil { + //errorFn(err) + return nil + } + resp, err := client.Do(req) + if err != nil { + //errorFn(err) + return nil + } + defer resp.Body.Close() + /*defer func() { + if closeErr := resp.Body.Close(); closeErr != nil { + errorFn(closeErr) + } + }()*/ + img, _, err := image.Decode(resp.Body) + if err != nil { + //errorFn(err) + return nil + } + return g.ImageToRgba(img) + + } +}