From 70e0775d86b8baebaa61990bbae372a812d702c3 Mon Sep 17 00:00:00 2001 From: Asa Date: Thu, 9 Jan 2025 01:57:45 -0600 Subject: [PATCH 1/2] Added menu item. Attempted to add open button to add additional pictures to config. Saves the path so that the next time it is opened it opens the directory.. but couldn't figure out how to refresh frontend in time --- frontend/package.json.md5 | 2 +- main.go | 94 +++++++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/frontend/package.json.md5 b/frontend/package.json.md5 index cc8aa89..66903cc 100755 --- a/frontend/package.json.md5 +++ b/frontend/package.json.md5 @@ -1 +1 @@ -48d658ca1215cf1f3c91529c7cd368cf \ No newline at end of file +d7bec4a3f12ac61e42e754af266059c0 \ No newline at end of file diff --git a/main.go b/main.go index 3c7777f..410cf23 100644 --- a/main.go +++ b/main.go @@ -5,18 +5,23 @@ import ( "embed" "encoding/json" "fmt" - "golang.org/x/image/draw" "image" "image/png" "net/http" "os" "path/filepath" + "slices" "strconv" "strings" + "golang.org/x/image/draw" + "github.com/wailsapp/wails/v2" + "github.com/wailsapp/wails/v2/pkg/menu" + "github.com/wailsapp/wails/v2/pkg/menu/keys" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" + "github.com/wailsapp/wails/v2/pkg/runtime" ) //go:embed all:frontend/dist @@ -32,7 +37,7 @@ type settings struct { } func main() { - err, s := getSettings() + s, err := getSettings() if err != nil { println("Error getting settings:", err.Error()) return @@ -41,12 +46,35 @@ func main() { // Create an instance of the app structure app := NewApp(s) + AppMenu := menu.NewMenu() + FileMenu := AppMenu.AddSubmenu("File") + FileMenu.AddText("&Open", keys.CmdOrCtrl("o"), func(_ *menu.CallbackData) { + imageDirectory, err := runtime.OpenDirectoryDialog(app.ctx, runtime.OpenDialogOptions{}) + if err != nil { + println("Error opening image directory: ", err.Error()) + } + if !slices.Contains(s.ImageDirectories, imageDirectory) { + s.ImageDirectories = append(s.ImageDirectories, imageDirectory) + configDir, _ := os.UserConfigDir() + configPath := filepath.Join(configDir, "StableKeepr") + configPath = filepath.Join(configPath, "config.json") + writeSettings(&s, configPath) + // TODO: Now that we have configured the settings and saved it off, how do we handle refreshing the frontend? + app.GetListOfImages() + } + }) + FileMenu.AddSeparator() + FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) { + runtime.Quit(app.ctx) + }) + // Create application with options err = wails.Run(&options.App{ Title: "StableKeepr", Width: s.WindowWidth, Height: s.WindowHeight, Fullscreen: s.WindowMaximized, + Menu: AppMenu, AssetServer: &assetserver.Options{ Assets: assets, Handler: newLocalAssetHandler(s.ImageDirectories), @@ -63,28 +91,22 @@ func main() { } } -func getSettings() (error, settings) { +func getSettings() (settings, error) { // Get the config file from the user's config folder configDir, err := os.UserConfigDir() if err != nil { println("Error getting user config directory:", err.Error()) - return nil, settings{} + return settings{}, nil } configPath := filepath.Join(configDir, "StableKeepr") err = os.MkdirAll(configPath, 0755) if err != nil { println("Error creating config directory:", err.Error()) - return nil, settings{} + return settings{}, nil } configPath = filepath.Join(configPath, "config.json") // Does the config file exist? if _, err = os.Stat(configPath); os.IsNotExist(err) { - // Create the config file - file, err := os.Create(configPath) - if err != nil { - println("Error creating config file:", err.Error()) - return nil, settings{} - } // Write the config file blankSettings := settings{ LogLevel: "info", @@ -93,30 +115,23 @@ func getSettings() (error, settings) { WindowWidth: 1024, WindowMaximized: false, } - jsonSettings, err := json.MarshalIndent(blankSettings, "", " ") - if err != nil { - println("Error marshalling config file:", err.Error()) - return nil, settings{} - } - _, err = file.Write(jsonSettings) - err = file.Close() + err = writeSettings(&blankSettings, configPath) if err != nil { - println("Error closing config file:", err.Error()) - return nil, settings{} + return settings{}, nil } } // Read the config file jsonSettings, err := os.ReadFile(configPath) if err != nil { println("Error reading config file:", err.Error()) - return nil, settings{} + return settings{}, nil } // Unmarshal the config file var s settings err = json.Unmarshal(jsonSettings, &s) if err != nil { println("Error unmarshalling config file:", err.Error()) - return nil, settings{} + return settings{}, nil } // Default the window size to 1024x768 if s.WindowWidth == 0 { @@ -129,14 +144,40 @@ func getSettings() (error, settings) { jsonSettings, err = json.MarshalIndent(s, "", " ") if err != nil { println("Error marshalling config file:", err.Error()) - return nil, settings{} + return settings{}, nil } err = os.WriteFile(configPath, jsonSettings, 0644) if err != nil { println("Error writing config file:", err.Error()) - return nil, settings{} + return settings{}, nil } - return err, s + return s, err +} + +func writeSettings(s *settings, configPath string) error { + // Create the config file + file, err := os.Create(configPath) + if err != nil { + println("Error creating config file:", err.Error()) + return err + } + + jsonSettings, err := json.MarshalIndent(s, "", " ") + if err != nil { + println("Error marshalling config file:", err.Error()) + return err + } + _, err = file.Write(jsonSettings) + if err != nil { + println("Error writing config file: ", err.Error()) + return err + } + err = file.Close() + if err != nil { + println("Error closing config file:", err.Error()) + return err + } + return nil } // localAssetHandler is a http.HandlerFunc that serves local files from the given directories. @@ -237,9 +278,6 @@ func (h localAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, file) return } - // Serve the file - http.ServeFile(w, r, file) - return } } // If the path is a directory, return a 404 From 975bdae59cb6a25ee8b472608ddf7ee7d4acb9bc Mon Sep 17 00:00:00 2001 From: Asa Date: Sat, 11 Jan 2025 21:49:58 -0600 Subject: [PATCH 2/2] Refactored saving settings file into more logical functions, corrected a couple of typos --- main.go | 56 +++++++++++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 410cf23..a6f7d84 100644 --- a/main.go +++ b/main.go @@ -48,16 +48,14 @@ func main() { AppMenu := menu.NewMenu() FileMenu := AppMenu.AddSubmenu("File") - FileMenu.AddText("&Open", keys.CmdOrCtrl("o"), func(_ *menu.CallbackData) { + FileMenu.AddText("Add folder", keys.CmdOrCtrl("a"), func(_ *menu.CallbackData) { imageDirectory, err := runtime.OpenDirectoryDialog(app.ctx, runtime.OpenDialogOptions{}) if err != nil { println("Error opening image directory: ", err.Error()) } if !slices.Contains(s.ImageDirectories, imageDirectory) { s.ImageDirectories = append(s.ImageDirectories, imageDirectory) - configDir, _ := os.UserConfigDir() - configPath := filepath.Join(configDir, "StableKeepr") - configPath = filepath.Join(configPath, "config.json") + configPath, _ := getSettingsPath() writeSettings(&s, configPath) // TODO: Now that we have configured the settings and saved it off, how do we handle refreshing the frontend? app.GetListOfImages() @@ -91,22 +89,29 @@ func main() { } } -func getSettings() (settings, error) { +func getSettingsPath() (string, bool) { // Get the config file from the user's config folder configDir, err := os.UserConfigDir() if err != nil { println("Error getting user config directory:", err.Error()) - return settings{}, nil + return "", false } configPath := filepath.Join(configDir, "StableKeepr") err = os.MkdirAll(configPath, 0755) if err != nil { println("Error creating config directory:", err.Error()) - return settings{}, nil + return "", false } configPath = filepath.Join(configPath, "config.json") + _, err = os.Stat(configPath) + doesFileNotExist := os.IsNotExist(err) + return configPath, doesFileNotExist +} + +func getSettings() (settings, error) { + configPath, doesFileNotExist := getSettingsPath() // Does the config file exist? - if _, err = os.Stat(configPath); os.IsNotExist(err) { + if doesFileNotExist { // Write the config file blankSettings := settings{ LogLevel: "info", @@ -115,7 +120,7 @@ func getSettings() (settings, error) { WindowWidth: 1024, WindowMaximized: false, } - err = writeSettings(&blankSettings, configPath) + err := writeSettings(&blankSettings, configPath) if err != nil { return settings{}, nil } @@ -126,6 +131,7 @@ func getSettings() (settings, error) { println("Error reading config file:", err.Error()) return settings{}, nil } + // Unmarshal the config file var s settings err = json.Unmarshal(jsonSettings, &s) @@ -133,35 +139,27 @@ func getSettings() (settings, error) { println("Error unmarshalling config file:", err.Error()) return settings{}, nil } - // Default the window size to 1024x768 - if s.WindowWidth == 0 { - s.WindowWidth = 1024 - } - if s.WindowHeight == 0 { - s.WindowHeight = 768 - } - // resave the settings - jsonSettings, err = json.MarshalIndent(s, "", " ") - if err != nil { - println("Error marshalling config file:", err.Error()) - return settings{}, nil - } - err = os.WriteFile(configPath, jsonSettings, 0644) - if err != nil { - println("Error writing config file:", err.Error()) - return settings{}, nil - } + + // Resave the settings + // Do we actually need to do this at this point? + writeSettings(&s, configPath) return s, err } func writeSettings(s *settings, configPath string) error { // Create the config file - file, err := os.Create(configPath) + file, err := os.OpenFile(configPath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { println("Error creating config file:", err.Error()) return err } - + // Default the window size to 1024x768 + if s.WindowWidth == 0 { + s.WindowWidth = 1024 + } + if s.WindowHeight == 0 { + s.WindowHeight = 768 + } jsonSettings, err := json.MarshalIndent(s, "", " ") if err != nil { println("Error marshalling config file:", err.Error())