Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrigato committed Sep 24, 2024
1 parent 824136d commit 223aa46
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions examples/loadimage/loadimage.go
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 25 in examples/loadimage/loadimage.go

View workflow job for this annotation

GitHub Actions / Building (ubuntu-latest)

urlWidgetLike.SetSurfaceFn undefined (type *giu.ReflectiveBoundTexture has no field or method SetSurfaceFn)

Check failure on line 25 in examples/loadimage/loadimage.go

View workflow job for this annotation

GitHub Actions / Building (windows-latest)

urlWidgetLike.SetSurfaceFn undefined (type *giu.ReflectiveBoundTexture has no field or method SetSurfaceFn)

Check failure on line 25 in examples/loadimage/loadimage.go

View workflow job for this annotation

GitHub Actions / Building (macos-latest)

urlWidgetLike.SetSurfaceFn undefined (type *giu.ReflectiveBoundTexture has no field or method SetSurfaceFn)
g.SingleWindow().Layout(
g.Label("Display image from texture"),
gopher.ToImageWidget(),
Expand All @@ -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).
Expand All @@ -48,7 +55,7 @@ func loop() {
}),
).
LayoutForFailure(
fallback.Image().Size(300, 200),
fallback.ToImageWidget().Size(300, 200),
).
OnReady(func() {
fmt.Println("Image is downloaded.")
Expand All @@ -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"),
)
}
Expand All @@ -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)

}
}

0 comments on commit 223aa46

Please sign in to comment.