more refactoring
This commit is contained in:
@@ -2,6 +2,10 @@ package filetypes
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
tk "modernc.org/tk9.0"
|
tk "modernc.org/tk9.0"
|
||||||
)
|
)
|
||||||
@@ -95,3 +99,19 @@ func GetTkTypes(fds []FileDescription) []tk.FileType {
|
|||||||
}
|
}
|
||||||
return fts
|
return fts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IsImage(entry os.DirEntry) bool {
|
||||||
|
if entry.IsDir() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
ext := filepath.Ext(entry.Name())
|
||||||
|
if ext == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, ft := range Valid {
|
||||||
|
if slices.Contains(ft.MacExtensions, strings.ToLower(ext[1:])) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
87
main.go
87
main.go
@@ -18,6 +18,12 @@ import (
|
|||||||
tk "modernc.org/tk9.0"
|
tk "modernc.org/tk9.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type state struct {
|
||||||
|
currentDirectory string
|
||||||
|
currentFile string
|
||||||
|
images []string
|
||||||
|
}
|
||||||
|
|
||||||
//go:embed noise.png
|
//go:embed noise.png
|
||||||
var noise []byte // this is a default image
|
var noise []byte // this is a default image
|
||||||
|
|
||||||
@@ -31,11 +37,10 @@ var fileList *tk.ToplevelWidget
|
|||||||
var lb *tk.ListboxWidget
|
var lb *tk.ListboxWidget
|
||||||
var img = tk.Label()
|
var img = tk.Label()
|
||||||
var fileListBindVar = tk.Variable("FileList")
|
var fileListBindVar = tk.Variable("FileList")
|
||||||
|
var directoryState state
|
||||||
|
|
||||||
var directoryState struct {
|
func (d state) pathToImageAtIndex(i int) string {
|
||||||
currentDirectory string
|
return filepath.Join(directoryState.currentDirectory, directoryState.images[i])
|
||||||
currentFile string
|
|
||||||
images []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func must[T any](t T, err error) T {
|
func must[T any](t T, err error) T {
|
||||||
@@ -49,33 +54,38 @@ func checkErr[T any](_ T, err error) bool {
|
|||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isImage(entry os.DirEntry) bool {
|
|
||||||
if entry.IsDir() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
ext := filepath.Ext(entry.Name())
|
|
||||||
if ext == "" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, ft := range validFileTypes {
|
|
||||||
if slices.Contains(ft.MacExtensions, ext[1:]) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func newFileInDirectory() {
|
func newFileInDirectory() {
|
||||||
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!
|
// GetOpenFile returns an array split on spaces!
|
||||||
openFileAndDirectory(file)
|
newBrowse(strings.Join(files, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
func openFileAndDirectory(file string) {
|
func newDirectory() {
|
||||||
|
dir := tk.ChooseDirectory()
|
||||||
|
if dir == "" {
|
||||||
|
log.Println("no directory chosen")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dirfiles, err := os.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("could not read chosen directory")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, v := range dirfiles {
|
||||||
|
// loop until we find an image to start the browser with
|
||||||
|
if filetypes.IsImage(v) {
|
||||||
|
newBrowse(filepath.Join(dir, v.Name()))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Printf("no images found in dir: %s", dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBrowse(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)
|
||||||
@@ -85,34 +95,13 @@ func openFileAndDirectory(file string) {
|
|||||||
}
|
}
|
||||||
directoryState.images = []string{}
|
directoryState.images = []string{}
|
||||||
for _, v := range dirfiles {
|
for _, v := range dirfiles {
|
||||||
if isImage(v) {
|
if filetypes.IsImage(v) {
|
||||||
directoryState.images = append(directoryState.images, v.Name())
|
directoryState.images = append(directoryState.images, v.Name())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateImage(file)
|
updateImage(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newDirectory() {
|
|
||||||
dir := tk.ChooseDirectory()
|
|
||||||
if dir == "" {
|
|
||||||
log.Println("no directory chosen")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
directoryState.currentDirectory = dir
|
|
||||||
dirfiles, err := os.ReadDir(directoryState.currentDirectory)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
directoryState.images = []string{}
|
|
||||||
for _, v := range dirfiles {
|
|
||||||
if isImage(v) {
|
|
||||||
directoryState.images = append(directoryState.images, v.Name())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateImage(file string) {
|
func updateImage(file string) {
|
||||||
var i image.Image
|
var i image.Image
|
||||||
|
|
||||||
@@ -181,7 +170,7 @@ func constructFileList() {
|
|||||||
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]]))
|
updateImage(directoryState.pathToImageAtIndex(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>
|
||||||
@@ -221,12 +210,12 @@ func keyPress(e *tk.Event) {
|
|||||||
}
|
}
|
||||||
case "Up", "Left":
|
case "Up", "Left":
|
||||||
if curr > 0 {
|
if curr > 0 {
|
||||||
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[curr-1]))
|
updateImage(directoryState.pathToImageAtIndex(curr - 1))
|
||||||
moveSelectInFileList(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(filepath.Join(directoryState.currentDirectory, directoryState.images[curr+1]))
|
updateImage(directoryState.pathToImageAtIndex(curr + 1))
|
||||||
moveSelectInFileList(curr + 1)
|
moveSelectInFileList(curr + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -234,7 +223,7 @@ func keyPress(e *tk.Event) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
|
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
|
||||||
tk.MacOpenDocument(openFileAndDirectory)
|
tk.MacOpenDocument(newBrowse)
|
||||||
|
|
||||||
menubar := tk.Menu()
|
menubar := tk.Menu()
|
||||||
fileMenu := menubar.Menu()
|
fileMenu := menubar.Menu()
|
||||||
|
Reference in New Issue
Block a user