2023-01-08 23:03:19 -07:00
|
|
|
package tomo
|
|
|
|
|
2023-04-10 00:36:28 -06:00
|
|
|
import "image"
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
var backend Backend
|
|
|
|
|
|
|
|
// Run initializes a backend, calls the callback function, and begins the event
|
|
|
|
// loop in that order. This function does not return until Stop() is called, or
|
|
|
|
// the backend experiences a fatal error.
|
|
|
|
func Run (callback func ()) (err error) {
|
|
|
|
backend, err = instantiateBackend()
|
2023-03-08 19:05:56 -07:00
|
|
|
if err != nil { return }
|
2023-01-08 23:03:19 -07:00
|
|
|
if callback != nil { callback() }
|
|
|
|
err = backend.Run()
|
|
|
|
backend = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop gracefully stops the event loop and shuts the backend down. Call this
|
|
|
|
// before closing your application.
|
|
|
|
func Stop () {
|
|
|
|
if backend != nil { backend.Stop() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do executes the specified callback within the main thread as soon as
|
|
|
|
// possible. This function can be safely called from other threads.
|
|
|
|
func Do (callback func ()) {
|
2023-02-02 23:25:45 -07:00
|
|
|
assertBackend()
|
2023-01-09 09:36:37 -07:00
|
|
|
backend.Do(callback)
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-04-14 23:14:36 -06:00
|
|
|
// NewEntity generates an entity for an element using the current backend.
|
|
|
|
func NewEntity (owner Element) Entity {
|
|
|
|
assertBackend()
|
|
|
|
return backend.NewEntity(owner)
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
// NewWindow creates a new window using the current backend, and returns it as a
|
2023-03-24 15:38:21 -06:00
|
|
|
// MainWindow. If the window could not be created, an error is returned
|
|
|
|
// explaining why.
|
2023-04-10 00:36:28 -06:00
|
|
|
func NewWindow (bounds image.Rectangle) (window MainWindow, err error) {
|
2023-02-02 23:25:45 -07:00
|
|
|
assertBackend()
|
2023-04-10 00:36:28 -06:00
|
|
|
return backend.NewWindow(bounds)
|
2023-01-26 12:52:43 -07:00
|
|
|
}
|
|
|
|
|
2023-02-02 23:25:45 -07:00
|
|
|
// SetTheme sets the theme of all open windows.
|
2023-03-30 23:06:29 -06:00
|
|
|
func SetTheme (theme Theme) {
|
2023-02-02 23:25:45 -07:00
|
|
|
backend.SetTheme(theme)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the configuration of all open windows.
|
2023-03-30 23:06:29 -06:00
|
|
|
func SetConfig (config Config) {
|
2023-02-02 23:25:45 -07:00
|
|
|
backend.SetConfig(config)
|
|
|
|
}
|
|
|
|
|
2023-04-10 00:58:52 -06:00
|
|
|
// Bounds creates a rectangle from an x, y, width, and height.
|
|
|
|
func Bounds (x, y, width, height int) image.Rectangle {
|
|
|
|
return image.Rect(x, y, x + width, y + height)
|
|
|
|
}
|
|
|
|
|
2023-02-02 23:25:45 -07:00
|
|
|
func assertBackend () {
|
|
|
|
if backend == nil { panic("no backend is running") }
|
|
|
|
}
|