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
|
return 1
|
||||||
})
|
})
|
||||||
|
clearFileList()
|
||||||
for i, v := range directoryState.images {
|
for i, v := range directoryState.images {
|
||||||
|
insertIntoFileList(directoryState.images[i])
|
||||||
if v == filepath.Base(file) {
|
if v == filepath.Base(file) {
|
||||||
directoryState.i = i
|
directoryState.i = i
|
||||||
|
moveSelectionInFileList(i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateImage(file)
|
updateImage(file)
|
||||||
@@ -158,53 +161,18 @@ func repaint(name string, data tk.Opt) {
|
|||||||
tk.App.Center()
|
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() {
|
func menuSelect() {
|
||||||
if fileListBindVar.Get() == "1" && fileList == nil {
|
if fileListBindVar.Get() == "1" {
|
||||||
constructFileList()
|
showFileList()
|
||||||
} else if fileListBindVar.Get() == "0" && fileList != nil {
|
} else if fileListBindVar.Get() == "0" {
|
||||||
destroyFileList(true)
|
hideFileList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func moveSelectInFileList(target int) {
|
func updateSelection(target int) {
|
||||||
directoryState.i = target
|
directoryState.i = target
|
||||||
if lb != nil {
|
updateImage(directoryState.pathToImageAtIndex(target))
|
||||||
lb.SelectionClear("0", "end")
|
moveSelectionInFileList(target)
|
||||||
lb.SelectionSet(target)
|
|
||||||
lb.See(target)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: by default, the event callback loop in TK is fully synchronous;
|
// TODO: by default, the event callback loop in TK is fully synchronous;
|
||||||
@@ -227,28 +195,85 @@ func keyPress(e *tk.Event) {
|
|||||||
case "d":
|
case "d":
|
||||||
newDirectory()
|
newDirectory()
|
||||||
case "a":
|
case "a":
|
||||||
if fileList == nil {
|
if fileListBindVar.Get() == "0" {
|
||||||
constructFileList()
|
showFileList()
|
||||||
} else {
|
} else {
|
||||||
destroyFileList(true)
|
hideFileList()
|
||||||
}
|
}
|
||||||
case "Up", "Left":
|
case "Up", "Left":
|
||||||
if curr > 0 {
|
if curr > 0 {
|
||||||
updateImage(directoryState.pathToImageAtIndex(curr - 1))
|
updateSelection(curr - 1)
|
||||||
moveSelectInFileList(curr - 1)
|
|
||||||
}
|
}
|
||||||
case "Down", "Right":
|
case "Down", "Right":
|
||||||
if curr < len(directoryState.images)-1 && curr != -1 {
|
if curr < len(directoryState.images)-1 && curr != -1 {
|
||||||
updateImage(directoryState.pathToImageAtIndex(curr + 1))
|
updateSelection(curr + 1)
|
||||||
moveSelectInFileList(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() {
|
func main() {
|
||||||
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
|
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
|
||||||
tk.MacOpenDocument(newBrowse)
|
tk.MacOpenDocument(newBrowse)
|
||||||
|
|
||||||
|
constructFileList()
|
||||||
|
|
||||||
menubar := tk.Menu()
|
menubar := tk.Menu()
|
||||||
fileMenu := menubar.Menu()
|
fileMenu := menubar.Menu()
|
||||||
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Accelerator("O"), tk.Command(newFileInDirectory))
|
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Accelerator("O"), tk.Command(newFileInDirectory))
|
||||||
@@ -259,6 +284,7 @@ func main() {
|
|||||||
fileMenu.EntryConfigure(checkbox, fileListBindVar)
|
fileMenu.EntryConfigure(checkbox, fileListBindVar)
|
||||||
menubar.AddCascade(tk.Lbl("File"), tk.Underline(0), tk.Mnu(fileMenu))
|
menubar.AddCascade(tk.Lbl("File"), tk.Underline(0), tk.Mnu(fileMenu))
|
||||||
tk.App.Configure(tk.Mnu(menubar))
|
tk.App.Configure(tk.Mnu(menubar))
|
||||||
|
fileListBindVar.Set("0")
|
||||||
|
|
||||||
tk.Bind(fileMenu, "<<MenuSelect>>", tk.Command(menuSelect))
|
tk.Bind(fileMenu, "<<MenuSelect>>", tk.Command(menuSelect))
|
||||||
tk.Bind(tk.App, "<KeyPress>", tk.Command(keyPress))
|
tk.Bind(tk.App, "<KeyPress>", tk.Command(keyPress))
|
||||||
|
Reference in New Issue
Block a user