basic sizing of images
This commit is contained in:
8
go.mod
8
go.mod
@@ -2,12 +2,14 @@ module git.yetaga.in/alazyreader/why
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require modernc.org/tk9.0 v0.62.1-0.20250224173824-a16d85dfdb90
|
||||
require (
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
modernc.org/tk9.0 v0.62.1-0.20250224173824-a16d85dfdb90
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/JackMordaunt/icns v1.0.0 // indirect
|
||||
github.com/adrg/xdg v0.5.3 // indirect
|
||||
github.com/disintegration/imaging v1.6.2 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/ebitengine/purego v0.8.2 // indirect
|
||||
github.com/evilsocket/islazy v1.11.0 // indirect
|
||||
@@ -21,7 +23,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 // indirect
|
||||
golang.org/x/image v0.24.0 // indirect
|
||||
golang.org/x/image v0.25.0 // indirect
|
||||
golang.org/x/net v0.35.0 // indirect
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
modernc.org/fileutil v1.3.0 // indirect
|
||||
|
4
go.sum
4
go.sum
@@ -39,8 +39,8 @@ github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8
|
||||
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678 h1:mchzmB1XO2pMaKFRqk/+MV3mgGG96aqaPXaMifQU47w=
|
||||
golang.org/x/exp v0.0.0-20231108232855-2478ac86f678/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.24.0 h1:AN7zRgVsbvmTfNyqIbbOraYL8mSwcKncEj8ofjgzcMQ=
|
||||
golang.org/x/image v0.24.0/go.mod h1:4b/ITuLfqYq1hqZcjofwctIhi7sZh2WaCjvsBNjjya8=
|
||||
golang.org/x/image v0.25.0 h1:Y6uW6rH1y5y/LK1J8BPWZtr6yZ7hrsy6hFrXjgsc2fQ=
|
||||
golang.org/x/image v0.25.0/go.mod h1:tCAmOEGthTtkalusGp1g3xa2gke8J6c2N565dTyl9Rs=
|
||||
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
|
||||
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.35.0 h1:T5GQRQb2y08kTAByq9L4/bz8cipCdA8FbRTXewonqY8=
|
||||
|
46
main.go
46
main.go
@@ -2,12 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"io"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
tk "modernc.org/tk9.0"
|
||||
)
|
||||
|
||||
@@ -27,6 +28,7 @@ var validFileTypes = []tk.FileType{
|
||||
}
|
||||
|
||||
var metaActive bool
|
||||
var fileList *tk.ToplevelWidget
|
||||
|
||||
var directoryState struct {
|
||||
currentDirectory string
|
||||
@@ -95,28 +97,42 @@ func newDirectory(img *tk.LabelWidget) func() {
|
||||
}
|
||||
|
||||
func updateImage(file string, img *tk.LabelWidget) {
|
||||
f, err := os.Open(file)
|
||||
i, err := imaging.Open(file, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return
|
||||
}
|
||||
i, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
log.Println(err.Error())
|
||||
return
|
||||
// todo: make these bounds max out at display size
|
||||
if i.Bounds().Dy() > i.Bounds().Dx() {
|
||||
i = imaging.Resize(i, 0, 1000, imaging.CatmullRom)
|
||||
} else {
|
||||
i = imaging.Resize(i, 1000, 0, imaging.CatmullRom)
|
||||
}
|
||||
img.Configure(tk.Image(tk.NewPhoto(tk.Data(i))))
|
||||
repaint(img, filepath.Base(file), tk.Data(i))
|
||||
directoryState.currentFile = filepath.Base(file)
|
||||
}
|
||||
|
||||
func repaint(img *tk.LabelWidget, name string, opt tk.Opt) {
|
||||
photo := tk.NewPhoto(opt)
|
||||
log.Printf("height: %v, width: %v", photo.Height(), photo.Width())
|
||||
img.Configure(tk.Image(tk.NewPhoto(opt)))
|
||||
if name != "" {
|
||||
tk.App.WmTitle(fmt.Sprintf("why | %s", name))
|
||||
} else {
|
||||
tk.App.WmTitle("why")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
img := tk.Label(tk.Image(tk.NewPhoto(tk.Data(noise))))
|
||||
img := tk.Label()
|
||||
repaint(img, "", tk.Data(noise))
|
||||
|
||||
menubar := tk.Menu()
|
||||
fileMenu := menubar.Menu()
|
||||
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Underline(0), tk.Accelerator("Meta+O"), tk.Command(newFileInDirectory(img)))
|
||||
fileMenu.AddCommand(tk.Lbl("Open Directory"), tk.Underline(0), tk.Command(newDirectory(img)))
|
||||
menubar.AddCascade(tk.Lbl("File"), tk.Underline(0), tk.Mnu(fileMenu))
|
||||
tk.App.Configure(tk.Mnu(menubar))
|
||||
|
||||
// TODO: if someone presses the Meta key again after the openfile dialog box closes,
|
||||
// it triggers a _release_ event instead of a press event. The second time afterward, it works correctly.
|
||||
@@ -133,7 +149,17 @@ func main() {
|
||||
}
|
||||
case "a":
|
||||
if metaActive {
|
||||
tk.Toplevel()
|
||||
if fileList == nil {
|
||||
fileList = tk.Toplevel()
|
||||
fileList.WmTitle("files")
|
||||
tk.Bind(fileList, "<Destroy>", tk.Command(func(e *tk.Event) {
|
||||
// list closed by user click on <x>
|
||||
fileList = nil
|
||||
}))
|
||||
} else {
|
||||
// list closed by Meta-a
|
||||
tk.Destroy(fileList)
|
||||
}
|
||||
}
|
||||
case "Up":
|
||||
if curr > 0 {
|
||||
@@ -157,5 +183,5 @@ func main() {
|
||||
// }))
|
||||
|
||||
tk.Pack(img)
|
||||
tk.App.Configure(tk.Mnu(menubar)).Center().Wait()
|
||||
tk.App.Wait()
|
||||
}
|
||||
|
Reference in New Issue
Block a user