From 0c4572727cb4045895bf77bc1e420008aedbac44 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Wed, 14 May 2025 22:30:48 -0400 Subject: [PATCH] transition fileList management to deiconify/withdraw --- main.go | 124 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 49 deletions(-) diff --git a/main.go b/main.go index 1eefdec..fa47aff 100644 --- a/main.go +++ b/main.go @@ -105,9 +105,12 @@ func newBrowse(file string) { } return 1 }) + clearFileList() for i, v := range directoryState.images { + insertIntoFileList(directoryState.images[i]) if v == filepath.Base(file) { directoryState.i = i + moveSelectionInFileList(i) } } updateImage(file) @@ -158,53 +161,18 @@ func repaint(name string, data tk.Opt) { tk.App.Center() } -// TODO: make this use withdraw/deiconify -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 == directoryState.i { - lb.SelectionSet(i) - } - } - tk.Pack(lb) - tk.Bind(lb, "<>", tk.Command(func() { - selection := lb.Curselection() - updateImage(directoryState.pathToImageAtIndex(selection[0])) - })) - tk.Bind(fileList, "", tk.Command(func(e *tk.Event) { - // list closed by user click on - destroyFileList(false) - })) - fileListBindVar.Set("1") -} - func menuSelect() { - if fileListBindVar.Get() == "1" && fileList == nil { - constructFileList() - } else if fileListBindVar.Get() == "0" && fileList != nil { - destroyFileList(true) + if fileListBindVar.Get() == "1" { + showFileList() + } else if fileListBindVar.Get() == "0" { + hideFileList() } } -func moveSelectInFileList(target int) { +func updateSelection(target int) { directoryState.i = target - if lb != nil { - lb.SelectionClear("0", "end") - lb.SelectionSet(target) - lb.See(target) - } + updateImage(directoryState.pathToImageAtIndex(target)) + moveSelectionInFileList(target) } // TODO: by default, the event callback loop in TK is fully synchronous; @@ -227,28 +195,85 @@ func keyPress(e *tk.Event) { case "d": newDirectory() case "a": - if fileList == nil { - constructFileList() + if fileListBindVar.Get() == "0" { + showFileList() } else { - destroyFileList(true) + hideFileList() } case "Up", "Left": if curr > 0 { - updateImage(directoryState.pathToImageAtIndex(curr - 1)) - moveSelectInFileList(curr - 1) + updateSelection(curr - 1) } case "Down", "Right": if curr < len(directoryState.images)-1 && curr != -1 { - updateImage(directoryState.pathToImageAtIndex(curr + 1)) - moveSelectInFileList(curr + 1) + updateSelection(curr + 1) } } } +func constructFileList() { + fileList = tk.Toplevel() + fileList.WmTitle("Files") + tk.WmWithdraw(fileList.Window) + lb = fileList.Listbox(tk.Height(0)) + tk.Pack(lb) + tk.Bind(lb, "<>", tk.Command(func() { + updateSelection(lb.Curselection()[0]) + })) + tk.Bind(fileList, "", tk.Command(func(e *tk.Event) { + // wipe the reference so next show event recreates + fileList = nil + fileListBindVar.Set("0") + })) +} + +func showFileList() { + if fileList == nil { + constructFileList() + // repopulate + lb.Delete("0", "end") + for i := range directoryState.images { + lb.Insert("end", directoryState.images[i]) + if directoryState.i == i { + lb.SelectionSet(i) + } + } + } + tk.WmDeiconify(fileList.Window) + fileListBindVar.Set("1") +} + +func hideFileList() { + tk.WmWithdraw(fileList.Window) + fileListBindVar.Set("0") +} + +func insertIntoFileList(filename string) { + if fileList != nil { + lb.Insert("end", filename) + } +} + +func clearFileList() { + if fileList != nil { + lb.Delete("0", "end") + } +} + +func moveSelectionInFileList(target int) { + if fileList != nil { + lb.SelectionClear("0", "end") + lb.SelectionSet(target) + lb.See(target) + } +} + func main() { tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon))) tk.MacOpenDocument(newBrowse) + constructFileList() + menubar := tk.Menu() fileMenu := menubar.Menu() fileMenu.AddCommand(tk.Lbl("Open File"), tk.Accelerator("O"), tk.Command(newFileInDirectory)) @@ -259,6 +284,7 @@ func main() { fileMenu.EntryConfigure(checkbox, fileListBindVar) menubar.AddCascade(tk.Lbl("File"), tk.Underline(0), tk.Mnu(fileMenu)) tk.App.Configure(tk.Mnu(menubar)) + fileListBindVar.Set("0") tk.Bind(fileMenu, "<>", tk.Command(menuSelect)) tk.Bind(tk.App, "", tk.Command(keyPress))