Set URL after loading database
This commit is contained in:
parent
0bab92db02
commit
beef4523c2
@ -1,3 +0,0 @@
|
||||
package app
|
||||
|
||||
// TODO: Add your code here
|
@ -2,9 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"git.codemonkeysoftware.net/b/peachy-go"
|
||||
"git.codemonkeysoftware.net/b/peachy-go/desktop/routes"
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
@ -12,9 +16,64 @@ import (
|
||||
//go:embed all:static
|
||||
var assets embed.FS
|
||||
|
||||
func main() {
|
||||
const ListenPort = "9245"
|
||||
const WindowNameMain = "mainwindow"
|
||||
|
||||
r := routes.NewChiRouter()
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// Create the application
|
||||
app := application.New(application.Options{
|
||||
@ -31,14 +90,18 @@ func main() {
|
||||
ApplicationShouldTerminateAfterLastWindowClosed: true,
|
||||
},
|
||||
})
|
||||
app.OnShutdown(func() {
|
||||
closeDB()
|
||||
})
|
||||
|
||||
// V3 introduces multiple windows, so we need to create a window
|
||||
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
|
||||
Name: WindowNameMain,
|
||||
Title: "Your Project",
|
||||
Mac: application.MacWindow{
|
||||
Backdrop: application.MacBackdropTranslucent,
|
||||
},
|
||||
URL: "/", // URL to load when the window is created
|
||||
URL: "/splash", // URL to load when the window is created
|
||||
Width: 1080, // Width of the window
|
||||
Height: 720, // Height of the window
|
||||
Centered: false,
|
||||
@ -52,31 +115,24 @@ func main() {
|
||||
menu.FindByRole(application.NewFile).OnClick(func(ctx *application.Context) {
|
||||
d := application.SaveFileDialog()
|
||||
d.SetOptions(&application.SaveFileDialogOptions{
|
||||
Filters: []application.FileFilter{
|
||||
{DisplayName: "Peachy database", Pattern: "*.pit"},
|
||||
{DisplayName: "All Files", Pattern: "*.*"},
|
||||
},
|
||||
Filters: dbFileFilter(),
|
||||
AllowOtherFileTypes: true,
|
||||
Title: "Create new database",
|
||||
})
|
||||
path, err := d.PromptForSingleSelection()
|
||||
if err != nil {
|
||||
application.InfoDialog().SetMessage("Error: " + err.Error()).Show()
|
||||
}
|
||||
path, _ := d.PromptForSingleSelection()
|
||||
if path != "" {
|
||||
application.InfoDialog().SetMessage(path).Show()
|
||||
} else {
|
||||
application.InfoDialog().SetMessage("No file selected").Show()
|
||||
showError(createDB(path))
|
||||
}
|
||||
})
|
||||
menu.FindByRole(application.Open).OnClick(func(ctx *application.Context) {
|
||||
path, _ := application.OpenFileDialog().
|
||||
CanChooseFiles(true).
|
||||
PromptForSingleSelection()
|
||||
d := application.OpenFileDialog()
|
||||
d.SetOptions(&application.OpenFileDialogOptions{
|
||||
Filters: dbFileFilter(),
|
||||
CanChooseFiles: true,
|
||||
})
|
||||
path, _ := d.PromptForSingleSelection()
|
||||
if path != "" {
|
||||
application.InfoDialog().SetMessage(path).Show()
|
||||
} else {
|
||||
application.InfoDialog().SetMessage("No file selected").Show()
|
||||
showError(openDB(path))
|
||||
}
|
||||
})
|
||||
app.SetMenu(menu)
|
||||
|
@ -29,6 +29,7 @@ func hxTarget(target string) h.Attrib {
|
||||
|
||||
func HomePage() h.ParentElement {
|
||||
return e.Div()(
|
||||
e.H1()(h.Text("[untitled]")),
|
||||
e.Button(
|
||||
a.Type("button"),
|
||||
hxGet("/hello"),
|
||||
@ -38,3 +39,7 @@ func HomePage() h.ParentElement {
|
||||
e.Div(a.Id("hello"))(),
|
||||
)
|
||||
}
|
||||
|
||||
func Splash() h.Term {
|
||||
return h.Terms{}
|
||||
}
|
||||
|
@ -14,13 +14,11 @@ import (
|
||||
/*
|
||||
Create a new chi router, configure it and return it.
|
||||
*/
|
||||
func NewChiRouter() *chi.Mux {
|
||||
func NewChiRouter(port string) *chi.Mux {
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
||||
// Useful middleware, see : https://pkg.go.dev/github.com/go-chi/httplog/v2@v2.1.1#NewLogger
|
||||
logger := httplog.NewLogger("app-logger", httplog.Options{
|
||||
// All log
|
||||
LogLevel: slog.LevelInfo,
|
||||
Concise: true,
|
||||
})
|
||||
@ -40,29 +38,23 @@ func NewChiRouter() *chi.Mux {
|
||||
})
|
||||
*/
|
||||
|
||||
// Serve static files.
|
||||
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
|
||||
// Home page
|
||||
r.Get("/splash", func(w http.ResponseWriter, r *http.Request) {
|
||||
slog.Info("we splashin")
|
||||
HXRender(w, r, pages.Splash)
|
||||
})
|
||||
|
||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Render home page
|
||||
slog.Info("we homies")
|
||||
HXRender(w, r, pages.HomePage)
|
||||
|
||||
// 200 OK status
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
// Hello page
|
||||
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||
// Render hello
|
||||
HXRender(w, r, components.HelloWorld)
|
||||
|
||||
// 200 OK status
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
// Listen to port 3000.
|
||||
go http.ListenAndServe(":9245", r)
|
||||
go http.ListenAndServe(":"+port, r)
|
||||
|
||||
return r
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user