tomo/tomo.go

76 lines
1.4 KiB
Go
Raw Normal View History

2023-06-30 14:38:51 -06:00
package tomo
2023-07-15 23:06:24 -06:00
import "sync"
2023-06-30 14:38:51 -06:00
import "image"
import "errors"
2023-07-15 23:06:24 -06:00
var backendLock sync.Mutex
2023-06-30 14:38:51 -06:00
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 {
2023-06-30 17:30:17 -06:00
loadPlugins()
2023-06-30 14:38:51 -06:00
if backend != nil {
return errors.New("there is already a backend running")
}
back, err := Initialize()
if err != nil { return err }
2023-07-15 23:06:24 -06:00
backendLock.Lock()
2023-06-30 14:38:51 -06:00
backend = back
2023-07-15 23:06:24 -06:00
backendLock.Unlock()
2023-06-30 14:38:51 -06:00
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()
2023-07-15 23:06:24 -06:00
backendLock.Lock()
2023-06-30 14:38:51 -06:00
backend = nil
2023-07-15 23:06:24 -06:00
backendLock.Unlock()
2023-06-30 14:38:51 -06:00
}
2023-07-15 22:33:44 -06:00
func Do (callback func ()) {
2023-07-15 23:06:24 -06:00
backendLock.Lock()
if backend != nil { backend.Do(callback) }
backendLock.Unlock()
2023-07-15 22:33:44 -06:00
}
2023-07-01 09:45:48 -06:00
func NewWindow (bounds image.Rectangle) (MainWindow, error) {
2023-06-30 14:38:51 -06:00
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()
}