major reformat to move a bunch of inline funcs to stand-alone

This commit is contained in:
2025-05-10 22:42:46 -04:00
parent 45ad257097
commit c7a5041c4c

118
main.go
View File

@@ -26,8 +26,11 @@ var icon []byte // this is the app icon
//go:generate go run ./filetypes/cmd/gen.go //go:generate go run ./filetypes/cmd/gen.go
var validFileTypes = filetypes.Valid var validFileTypes = filetypes.Valid
var fileList *tk.ToplevelWidget var fileList *tk.ToplevelWidget
var lb *tk.ListboxWidget var lb *tk.ListboxWidget
var img = tk.Label()
var fileListBindVar = tk.Variable("FileList")
var directoryState struct { var directoryState struct {
currentDirectory string currentDirectory string
@@ -62,19 +65,17 @@ func isImage(entry os.DirEntry) bool {
return false return false
} }
func newFileInDirectory(img *tk.LabelWidget) func() { func newFileInDirectory() {
return func() {
files := tk.GetOpenFile(tk.Filetypes(filetypes.GetTkTypes(validFileTypes)), tk.Multiple(false)) files := tk.GetOpenFile(tk.Filetypes(filetypes.GetTkTypes(validFileTypes)), tk.Multiple(false))
if len(files) < 1 || files[0] == "" { if len(files) < 1 || files[0] == "" {
log.Println("no file chosen") log.Println("no file chosen")
return return
} }
file := strings.Join(files, " ") // GetOpenFile returns an array split on spaces! file := strings.Join(files, " ") // GetOpenFile returns an array split on spaces!
openFileAndDirectory(img, file) openFileAndDirectory(file)
}
} }
func openFileAndDirectory(img *tk.LabelWidget, file string) { func openFileAndDirectory(file string) {
directoryState.currentFile = filepath.Base(file) directoryState.currentFile = filepath.Base(file)
directoryState.currentDirectory = filepath.Dir(file) directoryState.currentDirectory = filepath.Dir(file)
dirfiles, err := os.ReadDir(directoryState.currentDirectory) dirfiles, err := os.ReadDir(directoryState.currentDirectory)
@@ -88,11 +89,10 @@ func openFileAndDirectory(img *tk.LabelWidget, file string) {
directoryState.images = append(directoryState.images, v.Name()) directoryState.images = append(directoryState.images, v.Name())
} }
} }
updateImage(file, img) updateImage(file)
} }
func newDirectory(img *tk.LabelWidget) func() { func newDirectory() {
return func() {
dir := tk.ChooseDirectory() dir := tk.ChooseDirectory()
if dir == "" { if dir == "" {
log.Println("no directory chosen") log.Println("no directory chosen")
@@ -110,11 +110,10 @@ func newDirectory(img *tk.LabelWidget) func() {
directoryState.images = append(directoryState.images, v.Name()) directoryState.images = append(directoryState.images, v.Name())
} }
} }
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[0]), img) updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[0]))
}
} }
func updateImage(file string, img *tk.LabelWidget) { func updateImage(file string) {
var i image.Image var i image.Image
f, err := os.Open(file) f, err := os.Open(file)
@@ -142,11 +141,11 @@ func updateImage(file string, img *tk.LabelWidget) {
must(strconv.Atoi(tk.WinfoScreenHeight(tk.App)))-50, must(strconv.Atoi(tk.WinfoScreenHeight(tk.App)))-50,
imaging.CatmullRom, imaging.CatmullRom,
) )
repaint(img, filepath.Base(file), tk.Data(i)) repaint(filepath.Base(file), tk.Data(i))
directoryState.currentFile = filepath.Base(file) directoryState.currentFile = filepath.Base(file)
} }
func repaint(img *tk.LabelWidget, name string, data tk.Opt) { func repaint(name string, data tk.Opt) {
// TODO: sometimes, when going from a big image to a smaller one, // TODO: sometimes, when going from a big image to a smaller one,
// the window remains the same height as the big image, // the window remains the same height as the big image,
// even as the width shrinks to fit the smaller image. // even as the width shrinks to fit the smaller image.
@@ -160,35 +159,16 @@ func repaint(img *tk.LabelWidget, name string, data tk.Opt) {
tk.App.Center() tk.App.Center()
} }
func main() { func destroyFileList(destroy bool) {
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)), tk.DefaultIcon())
img := tk.Label()
repaint(img, "", tk.Data(noise))
tk.MacOpenDocument(func(e string) {
openFileAndDirectory(img, e)
})
menubar := tk.Menu()
fileMenu := menubar.Menu()
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Accelerator("O"), tk.Command(newFileInDirectory(img)))
fileMenu.AddCommand(tk.Lbl("Open Directory"), tk.Command(newDirectory(img)))
fileMenu.AddSeparator()
checkbox := fileMenu.AddCheckbutton(tk.Lbl("Show Filelist"))
bindVar := tk.Variable("FileList")
fileMenu.EntryConfigure(checkbox, bindVar)
menubar.AddCascade(tk.Lbl("File"), tk.Underline(0), tk.Mnu(fileMenu))
tk.App.Configure(tk.Mnu(menubar))
destroyFileList := func(destroy bool) {
if destroy { if destroy {
tk.Destroy(fileList) tk.Destroy(fileList)
} }
fileList = nil fileList = nil
lb = nil lb = nil
bindVar.Set("0") fileListBindVar.Set("0")
} }
constructFileList := func() {
func constructFileList() {
fileList = tk.Toplevel() fileList = tk.Toplevel()
fileList.WmTitle("Files") fileList.WmTitle("Files")
lb = fileList.Listbox(tk.Height(0)) lb = fileList.Listbox(tk.Height(0))
@@ -201,62 +181,80 @@ func main() {
tk.Pack(lb) tk.Pack(lb)
tk.Bind(lb, "<<ListboxSelect>>", tk.Command(func() { tk.Bind(lb, "<<ListboxSelect>>", tk.Command(func() {
selection := lb.Curselection() selection := lb.Curselection()
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[selection[0]]), img) updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[selection[0]]))
})) }))
tk.Bind(fileList, "<Destroy>", tk.Command(func(e *tk.Event) { tk.Bind(fileList, "<Destroy>", tk.Command(func(e *tk.Event) {
// list closed by user click on <x> // list closed by user click on <x>
destroyFileList(false) destroyFileList(false)
})) }))
bindVar.Set("1") fileListBindVar.Set("1")
} }
tk.Bind(fileMenu, "<<MenuSelect>>", tk.Command(func() { func menuSelect() {
if bindVar.Get() == "1" && fileList == nil { if fileListBindVar.Get() == "1" && fileList == nil {
constructFileList() constructFileList()
} else if bindVar.Get() == "0" && fileList != nil { } else if fileListBindVar.Get() == "0" && fileList != nil {
destroyFileList(true) destroyFileList(true)
} }
})) }
tk.Bind(tk.App, "<KeyPress>", tk.Command(func(e *tk.Event) { 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) curr := slices.Index(directoryState.images, directoryState.currentFile)
switch e.Keysym { switch e.Keysym {
case ".": case ".":
log.Printf("state: %+v", directoryState) log.Printf("state: %+v", directoryState)
case "o": case "o":
newFileInDirectory(img)() newFileInDirectory()
case "a": case "a":
if fileList == nil { if fileList == nil {
constructFileList() constructFileList()
} else { } else {
// list closed by 'a'
destroyFileList(true) destroyFileList(true)
} }
case "Up", "Right": case "Up", "Right":
if curr > 0 { if curr > 0 {
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[curr-1]), img) updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[curr-1]))
if lb != nil { moveSelectInFileList(curr - 1)
lb.SelectionClear("0", "end")
lb.SelectionSet(curr - 1)
lb.See(curr - 1)
}
} }
case "Down", "Left": case "Down", "Left":
if curr < len(directoryState.images)-1 && curr != -1 { if curr < len(directoryState.images)-1 && curr != -1 {
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[curr+1]), img) updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[curr+1]))
if lb != nil { moveSelectInFileList(curr + 1)
lb.SelectionClear("0", "end")
lb.SelectionSet(curr + 1)
lb.See(curr + 1)
} }
} }
} }
}))
func main() {
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
tk.MacOpenDocument(openFileAndDirectory)
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 // todo: resize image based on scroll events
// tk.Bind(tk.App, "<TouchpadScroll>", tk.Command(func(e *tk.Event) { // tk.Bind(tk.App, "<TouchpadScroll>", tk.Command(func(e *tk.Event) {
// log.Printf("%v, %v", int16(e.Delta>>16), int16(e.Delta&0xFFFF)) // log.Printf("%v, %v", int16(e.Delta>>16), int16(e.Delta&0xFFFF))
// })) // }))
repaint("", tk.Data(noise))
tk.Pack(img) tk.Pack(img)
tk.App.Center() tk.App.Center()
tk.App.Wait() tk.App.Wait()