From ffa1bbe0b91a8c812ddcea5c5d65e55f60d07f33 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 30 Jun 2014 22:48:12 -0400 Subject: Restored the previous new API. I'm going to change it so that events are callbacks rather than using a window handler, but other than that... yeah. --- init.go | 61 +++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 18 deletions(-) (limited to 'init.go') diff --git a/init.go b/init.go index 2e59301..72f24ef 100644 --- a/init.go +++ b/init.go @@ -2,28 +2,53 @@ package ui -// Go sets up the UI environment and runs main in a goroutine. -// If initialization fails, Go returns an error and main is not called. -// Otherwise, Go does not return to its caller until main does, at which point it returns nil. -// After it returns, you cannot call future ui functions/methods meaningfully. -// -// It is not safe to call ui.Go() in a goroutine. It must be called directly from main(). +import ( + "runtime" +) + +// Go sets up the UI environment and pulses Ready. +// If initialization fails, Go returns an error and Ready is not pulsed. +// Otherwise, Go does not return to its caller until Stop is pulsed, at which point Go() will return nil. +// After Go() returns, you cannot call future ui functions/methods meaningfully. +// Pulsing Stop will cause Go() to return immediately; the programmer is responsible for cleaning up (for instance, hiding open Windows) beforehand. // -// This model is undesirable, but Cocoa limitations require it. +// It is not safe to call ui.Go() in a goroutine. It must be called directly from main(). This means if your code calls other code-modal servers (such as http.ListenAndServe()), they must be run from goroutines. (This is due to limitations in various OSs, such as Mac OS X.) // -// Go does not process the command line for flags (that is, it does not call flag.Parse()), nor does package ui add any of the underlying toolkit's supported command-line flags. +// Go() does not process the command line for flags (that is, it does not call flag.Parse()), nor does package ui add any of the underlying toolkit's supported command-line flags. // If you must, and if the toolkit also has environment variable equivalents to these flags (for instance, GTK+), use those instead. -func Go(main func()) error { - return ui(main) +func Go() error { + runtime.LockOSThread() + if err := uiinit(); err != nil { + return err + } + Ready <- struct{}{} + close(Ready) + ui() + return nil } -// AppQuit is pulsed when the user decides to quit the program if their operating system provides a facility for quitting an entire application, rather than merely close all windows (for instance, Mac OS X via the Dock icon). -// You should assign one of your Windows's Closing to this variable so the user choosing to quit the application is treated the same as closing that window. -// If you do not respond to this signal, nothing will happen; regardless of whether or not you respond to this signal, the application will not quit. -// Do not merely check this channel alone; it is not guaranteed to be pulsed on all systems or in all conditions. -var AppQuit chan struct{} +// Ready is pulsed when Go() is ready to begin accepting requests to the safe methods. +// Go() will wait for something to receive on Ready, then Ready will be closed. +var Ready = make(chan struct{}) + +// Stop should be pulsed when you are ready for Go() to return. +// Pulsing Stop will cause Go() to return immediately; the programmer is responsible for cleaning up (for instance, hiding open Windows) beforehand. +// Do not pulse Stop more than once. +var Stop = make(chan struct{}) -func init() { - // don't expose this in the documentation - AppQuit = newEvent() +// This function is a simple helper functionn that basically pushes the effect of a function call for later. This allows the selected safe Window methods to be safe. +// TODO make sure this acts sanely if called from uitask itself +func touitask(f func()) { + done := make(chan struct{}) + defer close(done) + go func() { // to avoid locking uitask itself + done2 := make(chan struct{}) // make the chain uitask <- f <- uitask to avoid deadlocks + defer close(done2) + uitask <- func() { + f() + done2 <- struct{}{} + } + done <- <-done2 + }() + <-done } -- cgit v1.2.3