app: add a way to get images (from FS for now)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-05-26 20:42:35 +02:00
parent 53be19912a
commit 100b961494
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
3 changed files with 94 additions and 1 deletions

View File

@ -6,10 +6,12 @@
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
}

View File

@ -6,6 +6,8 @@
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")
}
}

BIN
xkcdreader/assets/comic.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB