251 lines
6.0 KiB
Go
251 lines
6.0 KiB
Go
package main
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"image"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.yetaga.in/alazyreader/why/filetypes"
|
|
"github.com/dblezek/tga"
|
|
"github.com/disintegration/imaging"
|
|
"golang.org/x/image/webp"
|
|
tk "modernc.org/tk9.0"
|
|
)
|
|
|
|
type state struct {
|
|
currentDirectory string
|
|
currentFile string
|
|
images []string
|
|
}
|
|
|
|
//go:embed noise.png
|
|
var noise []byte // this is a default image
|
|
|
|
//go:embed icon.png
|
|
var icon []byte // this is the app icon
|
|
|
|
//go:generate go run ./filetypes/cmd/gen.go
|
|
var validFileTypes = filetypes.Valid
|
|
|
|
var fileList *tk.ToplevelWidget
|
|
var lb *tk.ListboxWidget
|
|
var img = tk.Label()
|
|
var fileListBindVar = tk.Variable("FileList")
|
|
var directoryState state
|
|
|
|
func (d state) pathToImageAtIndex(i int) string {
|
|
return filepath.Join(directoryState.currentDirectory, directoryState.images[i])
|
|
}
|
|
|
|
func must[T any](t T, err error) T {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return t
|
|
}
|
|
|
|
func checkErr[T any](_ T, err error) bool {
|
|
return err == nil
|
|
}
|
|
|
|
func newFileInDirectory() {
|
|
files := tk.GetOpenFile(tk.Filetypes(filetypes.GetTkTypes(validFileTypes)), tk.Multiple(false))
|
|
if len(files) < 1 || files[0] == "" {
|
|
log.Println("no file chosen")
|
|
return
|
|
}
|
|
// GetOpenFile returns an array split on spaces!
|
|
newBrowse(strings.Join(files, " "))
|
|
}
|
|
|
|
func newDirectory() {
|
|
dir := tk.ChooseDirectory()
|
|
if dir == "" {
|
|
log.Println("no directory chosen")
|
|
return
|
|
}
|
|
dirfiles, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
log.Println("could not read chosen directory")
|
|
return
|
|
}
|
|
for _, v := range dirfiles {
|
|
// loop until we find an image to start the browser with
|
|
if filetypes.IsImage(v) {
|
|
newBrowse(filepath.Join(dir, v.Name()))
|
|
return
|
|
}
|
|
}
|
|
log.Printf("no images found in dir: %s", dir)
|
|
}
|
|
|
|
func newBrowse(file string) {
|
|
directoryState.currentFile = filepath.Base(file)
|
|
directoryState.currentDirectory = filepath.Dir(file)
|
|
dirfiles, err := os.ReadDir(directoryState.currentDirectory)
|
|
if err != nil {
|
|
log.Printf("could not open dir: %v", err)
|
|
return
|
|
}
|
|
directoryState.images = []string{}
|
|
for _, v := range dirfiles {
|
|
if filetypes.IsImage(v) {
|
|
directoryState.images = append(directoryState.images, v.Name())
|
|
}
|
|
}
|
|
updateImage(file)
|
|
}
|
|
|
|
func updateImage(file string) {
|
|
var i image.Image
|
|
|
|
f, err := os.Open(file)
|
|
if err != nil {
|
|
log.Printf("error opening image: %v", err)
|
|
return
|
|
}
|
|
defer f.Close()
|
|
|
|
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(file), "."))
|
|
if checkErr(imaging.FormatFromExtension(ext)) {
|
|
i, err = imaging.Decode(f, imaging.AutoOrientation(true))
|
|
} else if ext == "webp" {
|
|
i, err = webp.Decode(f)
|
|
} else if ext == "tga" {
|
|
i, err = tga.Decode(f)
|
|
}
|
|
if err != nil {
|
|
log.Printf("error decoding image: %v", err)
|
|
return
|
|
}
|
|
i = imaging.Fit(i,
|
|
// -50 to give some space to breathe around the edges
|
|
must(strconv.Atoi(tk.WinfoScreenWidth(tk.App)))-50,
|
|
must(strconv.Atoi(tk.WinfoScreenHeight(tk.App)))-50,
|
|
imaging.CatmullRom,
|
|
)
|
|
repaint(filepath.Base(file), tk.Data(i))
|
|
directoryState.currentFile = filepath.Base(file)
|
|
}
|
|
|
|
func repaint(name string, data tk.Opt) {
|
|
// TODO: sometimes, when going from a big image to a smaller one,
|
|
// the window remains the same height as the big image,
|
|
// even as the width shrinks to fit the smaller image.
|
|
// This only seems to happen when the large image was full-screen height.
|
|
img.Configure(tk.Image(tk.NewPhoto(data)))
|
|
if name != "" {
|
|
tk.App.WmTitle(fmt.Sprintf("why | %s", name))
|
|
} else {
|
|
tk.App.WmTitle("why")
|
|
}
|
|
tk.App.Center()
|
|
}
|
|
|
|
func destroyFileList(destroy bool) {
|
|
if destroy {
|
|
tk.Destroy(fileList)
|
|
}
|
|
fileList = nil
|
|
lb = nil
|
|
fileListBindVar.Set("0")
|
|
}
|
|
|
|
func constructFileList() {
|
|
fileList = tk.Toplevel()
|
|
fileList.WmTitle("Files")
|
|
lb = fileList.Listbox(tk.Height(0))
|
|
for i := range directoryState.images {
|
|
lb.Insert("end", directoryState.images[i])
|
|
if i == slices.Index(directoryState.images, directoryState.currentFile) {
|
|
lb.SelectionSet(i)
|
|
}
|
|
}
|
|
tk.Pack(lb)
|
|
tk.Bind(lb, "<<ListboxSelect>>", tk.Command(func() {
|
|
selection := lb.Curselection()
|
|
updateImage(directoryState.pathToImageAtIndex(selection[0]))
|
|
}))
|
|
tk.Bind(fileList, "<Destroy>", tk.Command(func(e *tk.Event) {
|
|
// list closed by user click on <x>
|
|
destroyFileList(false)
|
|
}))
|
|
fileListBindVar.Set("1")
|
|
}
|
|
|
|
func menuSelect() {
|
|
if fileListBindVar.Get() == "1" && fileList == nil {
|
|
constructFileList()
|
|
} else if fileListBindVar.Get() == "0" && fileList != nil {
|
|
destroyFileList(true)
|
|
}
|
|
}
|
|
|
|
func moveSelectInFileList(target int) {
|
|
if lb != nil {
|
|
lb.SelectionClear("0", "end")
|
|
lb.SelectionSet(target)
|
|
lb.See(target)
|
|
}
|
|
}
|
|
|
|
func keyPress(e *tk.Event) {
|
|
curr := slices.Index(directoryState.images, directoryState.currentFile)
|
|
switch e.Keysym {
|
|
case ".":
|
|
log.Printf("state: %+v", directoryState)
|
|
case "o":
|
|
newFileInDirectory()
|
|
case "a":
|
|
if fileList == nil {
|
|
constructFileList()
|
|
} else {
|
|
destroyFileList(true)
|
|
}
|
|
case "Up", "Left":
|
|
if curr > 0 {
|
|
updateImage(directoryState.pathToImageAtIndex(curr - 1))
|
|
moveSelectInFileList(curr - 1)
|
|
}
|
|
case "Down", "Right":
|
|
if curr < len(directoryState.images)-1 && curr != -1 {
|
|
updateImage(directoryState.pathToImageAtIndex(curr + 1))
|
|
moveSelectInFileList(curr + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
|
|
tk.MacOpenDocument(newBrowse)
|
|
|
|
menubar := tk.Menu()
|
|
fileMenu := menubar.Menu()
|
|
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Accelerator("O"), tk.Command(newFileInDirectory))
|
|
fileMenu.AddCommand(tk.Lbl("Open Directory"), tk.Command(newDirectory))
|
|
fileMenu.AddSeparator()
|
|
checkbox := fileMenu.AddCheckbutton(tk.Lbl("Show Filelist"))
|
|
|
|
fileMenu.EntryConfigure(checkbox, fileListBindVar)
|
|
menubar.AddCascade(tk.Lbl("File"), tk.Underline(0), tk.Mnu(fileMenu))
|
|
tk.App.Configure(tk.Mnu(menubar))
|
|
|
|
tk.Bind(fileMenu, "<<MenuSelect>>", tk.Command(menuSelect))
|
|
tk.Bind(tk.App, "<KeyPress>", tk.Command(keyPress))
|
|
// todo: resize image based on scroll events
|
|
// tk.Bind(tk.App, "<TouchpadScroll>", tk.Command(func(e *tk.Event) {
|
|
// log.Printf("%v, %v", int16(e.Delta>>16), int16(e.Delta&0xFFFF))
|
|
// }))
|
|
|
|
repaint("", tk.Data(noise))
|
|
tk.Pack(img)
|
|
tk.App.Center()
|
|
tk.App.Wait()
|
|
}
|