Nasin now counts application windows to determine when to shut down

This commit is contained in:
Sasha Koshka 2024-08-14 11:58:06 -04:00
parent bc26e78024
commit 7da00e990e

View File

@ -15,8 +15,9 @@ type Application interface {
// Describe returns a description of the application. // Describe returns a description of the application.
Describe () ApplicationDescription Describe () ApplicationDescription
// Init performs the initial setup of the application. // Init performs the initial setup of the application. This behavior
Init () error // should return a window if it creates one.
Init () (tomo.Window, error)
} }
// ApplicationURLOpener is an application that can open a URL. // ApplicationURLOpener is an application that can open a URL.
@ -29,12 +30,15 @@ type ApplicationURLOpener interface {
// //
// Applications should support the file:// scheme at the very least, and // Applications should support the file:// scheme at the very least, and
// should also support others like http:// and https:// if possible. // should also support others like http:// and https:// if possible.
OpenURL (*url.URL) error //
// This behavior should return a window if it creates one.
OpenURL (*url.URL) (tomo.Window, error)
// OpenNone is called when the application is launched without any URLs // OpenNone is called when the application is launched without any URLs
// to open. The application may create some sort of default starting // to open. The application may create some sort of default starting
// window, or call tomo.Stop(). // window, or do nothing. This behavior should return a window if it
OpenNone () // creates one.
OpenNone () (tomo.Window, error)
} }
// ApplicationFlagAdder is an application that supports reading command line // ApplicationFlagAdder is an application that supports reading command line
@ -148,14 +152,18 @@ func RunApplication (application Application) {
if err != nil { log.Fatalln("nasin: could not set icon set:", err) } if err != nil { log.Fatalln("nasin: could not set icon set:", err) }
err = reg.SetFaceSet() err = reg.SetFaceSet()
if err != nil { log.Fatalln("nasin: could not set face set:", err) } if err != nil { log.Fatalln("nasin: could not set face set:", err) }
err = application.Init() window, err := application.Init()
if err != nil { log.Fatalln("nasin: could not run application:", err) } if err != nil { log.Fatalln("nasin: could not run application:", err) }
manageWindow(window)
// open URLs // open URLs
args := flag.Args() args := flag.Args()
applicationOpenUrls(application, args...) applicationOpenUrls(application, args...)
err = backend.Run()
if err != nil { log.Fatalln("nasin: could not run application:", err) } if windows > 0 {
err = backend.Run()
if err != nil { log.Fatalln("nasin: could not run application:", err) }
}
} }
// NewApplicationWindow creates a window for an application. It will // NewApplicationWindow creates a window for an application. It will
@ -173,44 +181,80 @@ func NewApplicationWindow (application Application, bounds image.Rectangle) (tom
return window, nil return window, nil
} }
func applicationOpenUrls (application Application, args ...string) { var windows int
if application, ok := application.(ApplicationURLOpener); ok {
if len(args) <= 0 {
application.OpenNone()
}
openedAny := false func manageWindow (window tomo.Window) {
for _, arg := range flag.Args() { if window == nil { return }
ur, err := url.Parse(arg) windows ++
if err != nil { window.OnClose(func () {
log.Fatalf ( windows --
"nasin: invalid URL %v: %v", if windows < 1 {
arg, err) tomo.Stop()
}
if ur.Scheme == "" {
ur.Scheme = "file"
}
err = application.OpenURL(ur)
if err != nil {
dialog, err := objects.NewDialogOk (
objects.DialogError, nil,
"Could Not Open URL",
fmt.Sprintf (
"Could not open %v: %v",
arg, err),
func () {
if !openedAny {
application.OpenNone()
}
})
if err != nil { log.Fatal(err) }
dialog.SetVisible(true)
}
} }
} else { })
}
func errorPopupf (title, format string, v ...any) func (func ()) {
return func (callback func ()) {
dialog, err := objects.NewDialogOk (
objects.DialogError, nil,
title,
fmt.Sprintf(format, v...),
callback)
if err != nil { log.Fatal(err) }
dialog.SetVisible(true)
manageWindow(dialog)
}
}
func applicationOpenUrls (app Application, args ...string) {
application, ok := app.(ApplicationURLOpener)
if !ok {
if len(args) > 0 { if len(args) > 0 {
log.Fatal("nasin: this application cannot open URLs") log.Fatal("nasin: this application cannot open URLs")
} }
return
}
openNone := func () bool {
window, err := application.OpenNone()
if err != nil {
log.Fatalf("nasin: could not open main window: %v", err)
return false
}
manageWindow(window)
return true
}
if len(args) <= 0 {
openNone()
return
}
openedAny := false
for _, arg := range flag.Args() {
ur, err := url.Parse(arg)
if err != nil {
log.Fatalf (
"nasin: invalid URL %v: %v",
arg, err)
}
if ur.Scheme == "" {
ur.Scheme = "file"
}
window, err := application.OpenURL(ur)
if err != nil {
errorPopupf(
"Could Not Open URL",
"Could not open %v: %v",
arg, err,
)(func () {
if !openedAny {
openNone()
}
})
}
manageWindow(window)
} }
} }