diff --git a/application.go b/application.go index fca53d5..ccec8c6 100644 --- a/application.go +++ b/application.go @@ -15,9 +15,8 @@ type Application interface { // Describe returns a description of the application. Describe () ApplicationDescription - // Init performs the initial setup of the application. This behavior - // should return a window if it creates one. - Init () (tomo.Window, error) + // Init performs the initial setup of the application. + Init () (error) // Stop stops the application and does not return until all ongoing // operations have been completely shut down. @@ -34,15 +33,11 @@ type ApplicationURLOpener interface { // // Applications should support the file:// scheme at the very least, and // should also support others like http:// and https:// if possible. - // - // This behavior should return a window if it creates one. - OpenURL (*url.URL) (tomo.Window, error) + OpenURL (*url.URL) error // OpenNone is called when the application is launched without any URLs - // to open. The application may create some sort of default starting - // window, or do nothing. This behavior should return a window if it - // creates one. - OpenNone () (tomo.Window, error) + // to open. + OpenNone () error } // ApplicationFlagAdder is an application that supports reading command line @@ -156,18 +151,18 @@ func RunApplication (application Application) { if err != nil { log.Fatalln("nasin: could not set icon set:", err) } err = reg.SetFaceSet() if err != nil { log.Fatalln("nasin: could not set face set:", err) } - window, err := application.Init() + err = application.Init() if err != nil { log.Fatalln("nasin: could not run application:", err) } - manageWindow(window) // open URLs args := flag.Args() applicationOpenUrls(application, args...) - if windows > 0 { + if manager.count > 0 { err = backend.Run() if err != nil { log.Fatalln("nasin: could not run application:", err) } } + application.Stop() } // NewApplicationWindow creates a window for an application. It will @@ -185,19 +180,6 @@ func NewApplicationWindow (application Application, bounds image.Rectangle) (tom return window, nil } -var windows int - -func manageWindow (window tomo.Window) { - if window == nil { return } - windows ++ - window.OnClose(func () { - windows -- - if windows < 1 { - tomo.Stop() - } - }) -} - func errorPopupf (title, format string, v ...any) func (func ()) { return func (callback func ()) { dialog, err := objects.NewDialogOk ( @@ -207,7 +189,7 @@ func errorPopupf (title, format string, v ...any) func (func ()) { callback) if err != nil { log.Fatal(err) } dialog.SetVisible(true) - manageWindow(dialog) + WaitFor(dialog) } } @@ -221,12 +203,11 @@ func applicationOpenUrls (app Application, args ...string) { } openNone := func () bool { - window, err := application.OpenNone() + err := application.OpenNone() if err != nil { log.Fatalf("nasin: could not open main window: %v", err) return false } - manageWindow(window) return true } @@ -246,7 +227,7 @@ func applicationOpenUrls (app Application, args ...string) { if ur.Scheme == "" { ur.Scheme = "file" } - window, err := application.OpenURL(ur) + err = application.OpenURL(ur) if err != nil { errorPopupf( "Could Not Open URL", @@ -258,7 +239,6 @@ func applicationOpenUrls (app Application, args ...string) { } }) } - manageWindow(window) } } diff --git a/manager.go b/manager.go new file mode 100644 index 0000000..5cd99df --- /dev/null +++ b/manager.go @@ -0,0 +1,33 @@ +package nasin + +import "git.tebibyte.media/tomo/tomo" +import "git.tebibyte.media/tomo/tomo/event" + +var manager struct { + count int +} + +type funcCookie func () +func (cookie funcCookie) Close () { + cookie() +} + +// WaitFor ensures that the application will stay running while the given window +// is open. +func WaitFor (window tomo.Window) event.Cookie { + manager.count ++ + + isManaged := true + handleClose := func () { + if !isManaged { return } + isManaged = false + manager.count -- + if manager.count < 1 { + tomo.Stop() + } + } + + return event.MultiCookie ( + window.OnClose(handleClose), + funcCookie(handleClose)) +}