add app icon, file list menu control
This commit is contained in:
74
main.go
74
main.go
@@ -19,6 +19,11 @@ import (
|
||||
//go:embed noise.png
|
||||
var noise []byte
|
||||
|
||||
// this is the app icon
|
||||
//
|
||||
//go:embed icon.png
|
||||
var icon []byte
|
||||
|
||||
var validFileTypes = []tk.FileType{
|
||||
{TypeName: "BMP", Extensions: []string{".bmp"}},
|
||||
{TypeName: "JPEG", Extensions: []string{".jpg", ".jpeg"}},
|
||||
@@ -137,11 +142,53 @@ func main() {
|
||||
|
||||
menubar := tk.Menu()
|
||||
fileMenu := menubar.Menu()
|
||||
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Underline(0), tk.Accelerator("Meta+O"), tk.Command(newFileInDirectory(img)))
|
||||
fileMenu.AddCommand(tk.Lbl("Open Directory"), tk.Underline(0), tk.Command(newDirectory(img)))
|
||||
fileMenu.AddCommand(tk.Lbl("Open File"), tk.Accelerator("Meta+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 {
|
||||
tk.Destroy(fileList)
|
||||
}
|
||||
fileList = nil
|
||||
lb = nil
|
||||
bindVar.Set("0")
|
||||
}
|
||||
constructFileList := func() {
|
||||
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(filepath.Join(directoryState.currentDirectory, directoryState.images[selection[0]]), img)
|
||||
}))
|
||||
tk.Bind(fileList, "<Destroy>", tk.Command(func(e *tk.Event) {
|
||||
// list closed by user click on <x>
|
||||
destroyFileList(false)
|
||||
}))
|
||||
bindVar.Set("1")
|
||||
}
|
||||
|
||||
tk.Bind(fileMenu, "<<MenuSelect>>", tk.Command(func() {
|
||||
if bindVar.Get() == "1" && fileList == nil {
|
||||
constructFileList()
|
||||
} else if bindVar.Get() == "0" && fileList != nil {
|
||||
destroyFileList(true)
|
||||
}
|
||||
}))
|
||||
|
||||
// TODO: if someone presses the Meta key again after the openfile dialog box closes,
|
||||
// it triggers a _release_ event instead of a press event. The second time afterward, it works correctly.
|
||||
tk.Bind(tk.App, "<KeyPress>", tk.Command(func(e *tk.Event) {
|
||||
@@ -158,28 +205,10 @@ func main() {
|
||||
case "a":
|
||||
if metaActive {
|
||||
if fileList == nil {
|
||||
fileList = tk.Toplevel()
|
||||
fileList.WmTitle("files")
|
||||
lb = fileList.Listbox(tk.Height(0))
|
||||
for i := range directoryState.images {
|
||||
lb.Insert("end", directoryState.images[i])
|
||||
if directoryState.currentFile == directoryState.images[i] {
|
||||
lb.SelectionSet(i)
|
||||
}
|
||||
}
|
||||
tk.Pack(lb)
|
||||
tk.Bind(lb, "<<ListboxSelect>>", tk.Command(func() {
|
||||
selection := lb.Curselection()
|
||||
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[selection[0]]), img)
|
||||
}))
|
||||
tk.Bind(fileList, "<Destroy>", tk.Command(func(e *tk.Event) {
|
||||
// list closed by user click on <x>
|
||||
fileList = nil
|
||||
lb = nil
|
||||
}))
|
||||
constructFileList()
|
||||
} else {
|
||||
// list closed by Meta-a
|
||||
tk.Destroy(fileList)
|
||||
destroyFileList(true)
|
||||
}
|
||||
}
|
||||
case "Up":
|
||||
@@ -212,5 +241,6 @@ func main() {
|
||||
// }))
|
||||
|
||||
tk.Pack(img)
|
||||
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
|
||||
tk.App.Wait()
|
||||
}
|
||||
|
Reference in New Issue
Block a user