basic sizing of images

This commit is contained in:
2025-03-24 23:35:47 -04:00
parent 8b060fb2ec
commit c2e5b542d0
3 changed files with 43 additions and 15 deletions

46
main.go
View File

@@ -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()
}