diff --git a/xkcdreader/app.go b/xkcdreader/app.go index 89991a5..5ae4e70 100644 --- a/xkcdreader/app.go +++ b/xkcdreader/app.go @@ -6,10 +6,12 @@ package xkcdreader import ( "fmt" "log" + "os" "time" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" + "fyne.io/fyne/v2/canvas" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/layout" "fyne.io/fyne/v2/theme" @@ -87,6 +89,8 @@ func makeTabs() *container.AppTabs { } func makeBrowseUI() *fyne.Container { + imgPath := "xkcdreader/assets/comic.png" + // container for the image and surrounding elements imgC := container.New( layout.NewHBoxLayout(), @@ -94,7 +98,12 @@ func makeBrowseUI() *fyne.Container { log.Println("Previous comic") }), layout.NewSpacer(), - widget.NewLabel("img placeholder"), + container.NewCenter( + // TODO(me): dynamically replace placeholder text with image once + // fetched... + // widget.NewLabel("img placeholder"), + getImg(imgPath), + ), layout.NewSpacer(), widget.NewButtonWithIcon("", theme.NavigateNextIcon(), func() { log.Println("Next comic") @@ -121,3 +130,25 @@ func makeBrowseUI() *fyne.Container { return browseUI } + +// get img from filesystem, resize it and return as *canvas.Image +func getImg(imgPath string) *canvas.Image { + _, err := os.Stat(imgPath) + + // properly handle error, perhaps panic?...don't panic, I know.. + if err != nil { + log.Println("failed to read file " + imgPath) + return canvas.NewImageFromFile("") + } + + img := canvas.NewImageFromFile(imgPath) + + img.SetMinSize( + fyne.Size{ + Height: 383, + Width: 273, + }, + ) + + return img +} diff --git a/xkcdreader/app_test.go b/xkcdreader/app_test.go index d152bcc..44277ce 100644 --- a/xkcdreader/app_test.go +++ b/xkcdreader/app_test.go @@ -6,6 +6,8 @@ package xkcdreader import ( "testing" + "fyne.io/fyne/v2" + "fyne.io/fyne/v2/canvas" "git.dotya.ml/wanderer/go-xkcdreader/cmd" ) @@ -115,3 +117,63 @@ func TestGetApp(t *testing.T) { t.Error("Failed to get application pointer using getApp()") } } + +func TestGetImgMinSize(t *testing.T) { + // init + newApp() + + // this is relative to the test file + imgPath := "assets/comic.png" + dimens := struct { + h float32 + w float32 + }{ + h: 383.0, + w: 273.0, + } + + got := getImg(imgPath) + got.SetMinSize( + fyne.Size{ + Height: 383.0, + Width: 273.0, + }, + ) + + if got.MinSize().Height != dimens.h { + t.Errorf("Failed to get img w/ proper height, want: %f, got: %f", dimens.h, got.Size().Height) + } + + if got.MinSize().Width != dimens.w { + t.Errorf("Failed to get img w/ proper width, want: %f, got: %f", dimens.w, got.Size().Width) + } +} + +func TestGetImg(t *testing.T) { + // init + newApp() + + // this is relative to the test file + imgPath := "assets/comic.png" + + got := getImg(imgPath) + got.SetMinSize( + fyne.Size{ + Height: 383.0, + Width: 273.0, + }, + ) + + testImg := canvas.NewImageFromFile(imgPath) + + gotA := got.Image + testA := testImg.Image + + if gotA != nil && testA != nil { + t.Fatal("test img or fetched img are nil") + } + + if gotA != testA { + t.Fatal("Images differ") + } +} diff --git a/xkcdreader/assets/comic.png b/xkcdreader/assets/comic.png new file mode 100644 index 0000000..d18d2de Binary files /dev/null and b/xkcdreader/assets/comic.png differ