2014-02-11 17:57:03 -06:00
// 11 february 2014
2014-03-12 20:55:45 -05:00
2014-02-19 10:41:10 -06:00
package ui
2014-02-11 17:57:03 -06:00
2014-06-28 09:15:24 -05:00
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.
2014-03-12 20:42:25 -05:00
//
2014-06-28 09:15:24 -05:00
// 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.)
2014-06-10 12:10:59 -05:00
//
2014-06-28 09:15:24 -05:00
// 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.
2014-06-02 20:25:28 -05:00
// If you must, and if the toolkit also has environment variable equivalents to these flags (for instance, GTK+), use those instead.
2014-06-28 09:15:24 -05:00
func Go ( ) error {
runtime . LockOSThread ( )
2014-06-28 15:37:55 -05:00
if err := uiinit ( ) ; err != nil {
2014-06-28 09:15:24 -05:00
return err
}
Ready <- struct { } { }
close ( Ready )
ui ( )
2014-06-28 09:49:23 -05:00
return nil
2014-02-11 17:57:03 -06:00
}
2014-04-06 22:33:27 -05:00
2014-06-28 09:15:24 -05:00
// 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 { } )
2014-06-28 08:37:31 -05:00
// 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.
2014-06-28 15:37:55 -05:00
// TODO make sure this acts sanely if called from uitask itself
2014-06-28 08:37:31 -05:00
func touitask ( f func ( ) ) {
2014-06-28 15:37:55 -05:00
done := make ( chan struct { } )
defer close ( done )
2014-06-28 08:37:31 -05:00
go func ( ) { // to avoid locking uitask itself
2014-06-28 15:37:55 -05:00
done2 := make ( chan struct { } ) // make the chain uitask <- f <- uitask to avoid deadlocks
defer close ( done2 )
uitask <- func ( ) {
f ( )
done2 <- struct { } { }
}
done <- <- done2
2014-06-28 08:37:31 -05:00
} ( )
2014-06-28 15:37:55 -05:00
<- done
2014-04-06 22:33:27 -05:00
}