peachy-go/desktop/main.go

142 lines
3.3 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"
2024-11-17 01:21:51 +00:00
var globalDB peachy.DB
2024-11-15 18:55:31 +00:00
func closeDB() {
2024-11-17 01:21:51 +00:00
globalDB.Close()
2024-11-15 18:55:31 +00:00
}
// 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-17 01:21:51 +00:00
func setDB(db peachy.DB) error {
2024-11-15 18:55:31 +00:00
slog.Info("opened DB, setting URL")
globalDB = db
2024-11-17 01:21:51 +00:00
SetURL(application.Get().GetWindowByName(WindowNameMain), "/site")
2024-11-15 18:55:31 +00:00
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() {
2024-11-17 01:21:51 +00:00
r := routes.NewChiRouter(ListenPort, &globalDB)
2024-11-05 12:17:27 +00:00
app := application.New(application.Options{
Name: "Peachy",
2024-11-17 01:21:51 +00:00
Description: "A desktop CMS for static sites",
Assets: application.AssetOptions{
2024-11-05 12:17:27 +00:00
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
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-17 01:21:51 +00:00
URL: "/", // 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)
}
}