transition fileList management to deiconify/withdraw
This commit is contained in:
124
main.go
124
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, "<<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)
|
||||
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, "<<ListboxSelect>>", tk.Command(func() {
|
||||
updateSelection(lb.Curselection()[0])
|
||||
}))
|
||||
tk.Bind(fileList, "<Destroy>", 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, "<<MenuSelect>>", tk.Command(menuSelect))
|
||||
tk.Bind(tk.App, "<KeyPress>", tk.Command(keyPress))
|
||||
|
Reference in New Issue
Block a user