peachy-go/desktop/main.go

146 lines
3.5 KiB
Go
Raw Normal View History

2024-11-05 12:17:27 +00:00
package main
import (
"embed"
2024-11-15 18:55:31 +00:00
"fmt"
2024-11-05 12:17:27 +00:00
"log"
2024-11-15 18:55:31 +00:00
"log/slog"
2024-11-05 12:17:27 +00:00
"net/http"
2024-11-15 18:55:31 +00:00
"net/url"
2024-11-05 12:17:27 +00:00
2024-11-15 18:55:31 +00:00
"git.codemonkeysoftware.net/b/peachy-go"
2024-11-05 12:17:27 +00:00
"git.codemonkeysoftware.net/b/peachy-go/desktop/routes"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed all:static
var assets embed.FS
2024-11-15 18:55:31 +00:00
const ListenPort = "9245"
const WindowNameMain = "mainwindow"
var globalDB *peachy.DB
func closeDB() {
if globalDB != nil {
globalDB.Close()
}
}
// SetURl is a workaround for WebviewWindow.SetURL not adding the base URL
// to relative URLs.
// TODO Report this or something.
func SetURL(win application.Window, path string) application.Window {
slog.Info("setting URL the hard way")
u, _ := url.JoinPath("http://wails.localhost:"+ListenPort+"/", path)
return win.SetURL(u)
}
2024-11-05 12:17:27 +00:00
2024-11-15 18:55:31 +00:00
func setDB(db *peachy.DB) error {
slog.Info("opened DB, setting URL")
globalDB = db
SetURL(application.Get().GetWindowByName(WindowNameMain), "/").SetTitle("you opened a DB")
return nil
}
func createDB(filename string) error {
db, err := peachy.Create(filename)
if err != nil {
return fmt.Errorf("Failed to create database: %w", err)
}
return setDB(db)
}
func openDB(filename string) error {
db, err := peachy.Open(filename)
if err != nil {
return fmt.Errorf("Failed to open database: %w", err)
}
return setDB(db)
}
func showError(err error) {
if err != nil {
application.ErrorDialog().SetMessage(err.Error()).Show()
}
}
func dbFileFilter() []application.FileFilter {
return []application.FileFilter{
{DisplayName: "Peachy database", Pattern: "*.pit"},
{DisplayName: "All Files", Pattern: "*.*"},
}
}
func main() {
r := routes.NewChiRouter(ListenPort)
2024-11-05 12:17:27 +00:00
// Create the application
app := application.New(application.Options{
Name: "Peachy",
Description: "A demo of using raw HTML & CSS", // Description of the application
Assets: application.AssetOptions{ // Assets to embed (our static files)
Handler: application.AssetFileServerFS(assets),
Middleware: func(next http.Handler) http.Handler {
r.NotFound(next.ServeHTTP)
return r
},
},
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
})
2024-11-15 18:55:31 +00:00
app.OnShutdown(func() {
closeDB()
})
2024-11-05 12:17:27 +00:00
// V3 introduces multiple windows, so we need to create a window
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
2024-11-15 18:55:31 +00:00
Name: WindowNameMain,
2024-11-05 12:17:27 +00:00
Title: "Your Project",
Mac: application.MacWindow{
Backdrop: application.MacBackdropTranslucent,
},
2024-11-15 18:55:31 +00:00
URL: "/splash", // URL to load when the window is created
Width: 1080, // Width of the window
Height: 720, // Height of the window
2024-11-05 12:17:27 +00:00
Centered: false,
})
2024-11-05 22:41:46 +00:00
menu := app.NewMenu()
fileMenu := menu.AddSubmenu("File")
fileMenu.AddRole(application.NewFile)
fileMenu.AddRole(application.Open)
fileMenu.Append(application.NewFileMenu().GetSubmenu())
menu.FindByRole(application.NewFile).OnClick(func(ctx *application.Context) {
d := application.SaveFileDialog()
d.SetOptions(&application.SaveFileDialogOptions{
2024-11-15 18:55:31 +00:00
Filters: dbFileFilter(),
2024-11-05 22:41:46 +00:00
AllowOtherFileTypes: true,
Title: "Create new database",
})
2024-11-15 18:55:31 +00:00
path, _ := d.PromptForSingleSelection()
2024-11-05 22:41:46 +00:00
if path != "" {
2024-11-15 18:55:31 +00:00
showError(createDB(path))
2024-11-05 22:41:46 +00:00
}
})
menu.FindByRole(application.Open).OnClick(func(ctx *application.Context) {
2024-11-15 18:55:31 +00:00
d := application.OpenFileDialog()
d.SetOptions(&application.OpenFileDialogOptions{
Filters: dbFileFilter(),
CanChooseFiles: true,
})
path, _ := d.PromptForSingleSelection()
2024-11-05 22:41:46 +00:00
if path != "" {
2024-11-15 18:55:31 +00:00
showError(openDB(path))
2024-11-05 22:41:46 +00:00
}
})
app.SetMenu(menu)
2024-11-05 12:17:27 +00:00
err := app.Run()
if err != nil {
log.Fatal(err)
}
}