tomo/tomo.go

98 lines
2.4 KiB
Go
Raw Normal View History

2023-06-30 20:38:51 +00:00
package tomo
2023-07-16 05:06:24 +00:00
import "sync"
2023-06-30 20:38:51 +00:00
import "image"
import "errors"
import "git.tebibyte.media/tomo/tomo/canvas"
2023-06-30 20:38:51 +00:00
2023-07-16 05:06:24 +00:00
var backendLock sync.Mutex
2023-06-30 20:38:51 +00: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 23:30:17 +00:00
loadPlugins()
2023-06-30 20:38:51 +00:00
if backend != nil {
return errors.New("there is already a backend running")
}
back, err := Initialize()
if err != nil { return err }
2023-07-16 05:06:24 +00:00
backendLock.Lock()
2023-06-30 20:38:51 +00:00
backend = back
2023-07-16 05:06:24 +00:00
backendLock.Unlock()
2023-06-30 20:38:51 +00: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-16 05:06:24 +00:00
backendLock.Lock()
2023-06-30 20:38:51 +00:00
backend = nil
2023-07-16 05:06:24 +00:00
backendLock.Unlock()
2023-06-30 20:38:51 +00:00
}
2023-07-19 01:49:36 +00:00
// Do performs a callback function in the event loop thread as soon as possible.
2023-07-16 04:33:44 +00:00
func Do (callback func ()) {
2023-07-16 05:06:24 +00:00
backendLock.Lock()
if backend != nil { backend.Do(callback) }
backendLock.Unlock()
2023-07-16 04:33:44 +00:00
}
2023-07-19 01:49:36 +00:00
// NewWindow creates and returns a window within the specified bounds on screen.
2023-07-01 15:45:48 +00:00
func NewWindow (bounds image.Rectangle) (MainWindow, error) {
2023-06-30 20:38:51 +00:00
assertBackend()
return backend.NewWindow(bounds)
}
// NewPlainWindow is like NewWindow, but it creates an undecorated window that
// does not appear in window lists. It is intended for creating things like
// docks, panels, etc.
func NewPlainWindow (bounds image.Rectangle) (MainWindow, error) {
assertBackend()
return backend.NewPlainWindow(bounds)
}
2023-07-19 01:49:36 +00:00
// NewBox creates and returns a basic Box.
2023-06-30 20:38:51 +00:00
func NewBox () Box {
assertBackend()
return backend.NewBox()
}
2023-07-19 01:49:36 +00:00
// NewTextBox creates and returns a Box that can display text.
2023-06-30 20:38:51 +00:00
func NewTextBox () TextBox {
assertBackend()
return backend.NewTextBox()
}
2023-07-19 01:49:36 +00:00
// NewCanvasBox creates and returns a Box that can display custom graphics.
2023-06-30 20:38:51 +00:00
func NewCanvasBox () CanvasBox {
assertBackend()
return backend.NewCanvasBox()
}
2023-07-19 01:49:36 +00:00
// NewContainerBox creates and returns a Box that can contain other boxes.
2023-06-30 20:38:51 +00:00
func NewContainerBox () ContainerBox {
assertBackend()
return backend.NewContainerBox()
}
2023-08-20 21:54:06 +00:00
// NewTexture creates a new texture from an image. When no longer in use, it
// must be freed using Close().
func NewTexture (source image.Image) canvas.TextureCloser {
2023-08-20 21:54:06 +00:00
assertBackend()
return backend.NewTexture(source)
}