tomo/tomo.go

76 lines
1.4 KiB
Go

package tomo
import "sync"
import "image"
import "errors"
var backendLock sync.Mutex
var backend Backend
// Run initializes a backend, runs the specified callback function, and runs the
// event loop in that order. This function blocks until Stop is called, or the
// backend experiences a fatal error.
func Run (callback func ()) error {
loadPlugins()
if backend != nil {
return errors.New("there is already a backend running")
}
back, err := Initialize()
if err != nil { return err }
backendLock.Lock()
backend = back
backendLock.Unlock()
callback()
return backend.Run()
}
func assertBackend () {
if backend == nil { panic("nil backend") }
}
// Stop stops the backend, unblocking run. Run may be called again after calling
// Stop.
func Stop () {
assertBackend()
backend.Stop()
backendLock.Lock()
backend = nil
backendLock.Unlock()
}
func Do (callback func ()) {
backendLock.Lock()
if backend != nil { backend.Do(callback) }
backendLock.Unlock()
}
func NewWindow (bounds image.Rectangle) (MainWindow, error) {
assertBackend()
return backend.NewWindow(bounds)
}
func NewBox () Box {
assertBackend()
return backend.NewBox()
}
func NewTextBox () TextBox {
assertBackend()
return backend.NewTextBox()
}
func NewCanvasBox () CanvasBox {
assertBackend()
return backend.NewCanvasBox()
}
func NewContainerBox () ContainerBox {
assertBackend()
return backend.NewContainerBox()
}