more refactoring

This commit is contained in:
2025-05-10 23:01:08 -04:00
parent 7956c84a94
commit ef8bf980ce
2 changed files with 58 additions and 49 deletions

View File

@@ -2,6 +2,10 @@ package filetypes
import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
tk "modernc.org/tk9.0"
)
@@ -95,3 +99,19 @@ func GetTkTypes(fds []FileDescription) []tk.FileType {
}
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
View File

@@ -18,6 +18,12 @@ import (
tk "modernc.org/tk9.0"
)
type state struct {
currentDirectory string
currentFile string
images []string
}
//go:embed noise.png
var noise []byte // this is a default image
@@ -31,11 +37,10 @@ var fileList *tk.ToplevelWidget
var lb *tk.ListboxWidget
var img = tk.Label()
var fileListBindVar = tk.Variable("FileList")
var directoryState state
var directoryState struct {
currentDirectory string
currentFile string
images []string
func (d state) pathToImageAtIndex(i int) string {
return filepath.Join(directoryState.currentDirectory, directoryState.images[i])
}
func must[T any](t T, err error) T {
@@ -49,33 +54,38 @@ func checkErr[T any](_ T, err error) bool {
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() {
files := tk.GetOpenFile(tk.Filetypes(filetypes.GetTkTypes(validFileTypes)), tk.Multiple(false))
if len(files) < 1 || files[0] == "" {
log.Println("no file chosen")
return
}
file := strings.Join(files, " ") // GetOpenFile returns an array split on spaces!
openFileAndDirectory(file)
// GetOpenFile returns an array split on spaces!
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.currentDirectory = filepath.Dir(file)
dirfiles, err := os.ReadDir(directoryState.currentDirectory)
@@ -85,34 +95,13 @@ func openFileAndDirectory(file string) {
}
directoryState.images = []string{}
for _, v := range dirfiles {
if isImage(v) {
if filetypes.IsImage(v) {
directoryState.images = append(directoryState.images, v.Name())
}
}
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) {
var i image.Image
@@ -181,7 +170,7 @@ func constructFileList() {
tk.Pack(lb)
tk.Bind(lb, "<<ListboxSelect>>", tk.Command(func() {
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) {
// list closed by user click on <x>
@@ -221,12 +210,12 @@ func keyPress(e *tk.Event) {
}
case "Up", "Left":
if curr > 0 {
updateImage(filepath.Join(directoryState.currentDirectory, directoryState.images[curr-1]))
updateImage(directoryState.pathToImageAtIndex(curr - 1))
moveSelectInFileList(curr - 1)
}
case "Down", "Right":
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)
}
}
@@ -234,7 +223,7 @@ func keyPress(e *tk.Event) {
func main() {
tk.App.IconPhoto(tk.NewPhoto(tk.Data(icon)))
tk.MacOpenDocument(openFileAndDirectory)
tk.MacOpenDocument(newBrowse)
menubar := tk.Menu()
fileMenu := menubar.Menu()