The base tomo module only retains a singleton backend
This commit is contained in:
parent
cd8371a3f3
commit
363779a947
@ -1,4 +1,4 @@
|
|||||||
package canvas
|
package artist
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "image/draw"
|
import "image/draw"
|
@ -2,12 +2,11 @@ package artist
|
|||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "image/color"
|
import "image/color"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
||||||
|
|
||||||
type Icon interface {
|
type Icon interface {
|
||||||
// Draw draws the icon to the destination canvas at the specified point,
|
// Draw draws the icon to the destination canvas at the specified point,
|
||||||
// using the specified color (if the icon is monochrome).
|
// using the specified color (if the icon is monochrome).
|
||||||
Draw (destination canvas.Canvas, color color.RGBA, at image.Point)
|
Draw (destination Canvas, color color.RGBA, at image.Point)
|
||||||
|
|
||||||
// Bounds returns the bounds of the icon.
|
// Bounds returns the bounds of the icon.
|
||||||
Bounds () image.Rectangle
|
Bounds () image.Rectangle
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package artist
|
package artist
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
||||||
|
|
||||||
// Pattern is capable of drawing to a canvas within the bounds of a given
|
// Pattern is capable of drawing to a canvas within the bounds of a given
|
||||||
// clipping rectangle.
|
// clipping rectangle.
|
||||||
@ -10,5 +9,5 @@ type Pattern interface {
|
|||||||
// specified bounds. The given bounds can be smaller or larger than the
|
// specified bounds. The given bounds can be smaller or larger than the
|
||||||
// bounds of the destination canvas. The destination canvas can be cut
|
// bounds of the destination canvas. The destination canvas can be cut
|
||||||
// using canvas.Cut() to draw only a specific subset of a pattern.
|
// using canvas.Cut() to draw only a specific subset of a pattern.
|
||||||
Draw (destination canvas.Canvas, bounds image.Rectangle)
|
Draw (destination Canvas, bounds image.Rectangle)
|
||||||
}
|
}
|
||||||
|
41
backend.go
41
backend.go
@ -1,7 +1,6 @@
|
|||||||
package tomo
|
package tomo
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "errors"
|
|
||||||
|
|
||||||
// Backend represents a connection to a display server, or something similar.
|
// Backend represents a connection to a display server, or something similar.
|
||||||
// It is capable of managing an event loop, and creating windows.
|
// It is capable of managing an event loop, and creating windows.
|
||||||
@ -32,33 +31,21 @@ type Backend interface {
|
|||||||
SetConfig (Config)
|
SetConfig (Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BackendFactory represents a function capable of constructing a backend
|
var backend Backend
|
||||||
// struct. Any connections should be initialized within this function. If there
|
|
||||||
// any errors encountered during this process, the function should immediately
|
|
||||||
// stop, clean up any resources, and return an error.
|
|
||||||
type BackendFactory func () (backend Backend, err error)
|
|
||||||
|
|
||||||
// RegisterBackend registers a backend factory. When an application calls
|
// GetBackend returns the currently running backend.
|
||||||
// tomo.Run(), the first registered backend that does not throw an error will be
|
func GetBackend () Backend {
|
||||||
// used.
|
return backend
|
||||||
func RegisterBackend (factory BackendFactory) {
|
|
||||||
factories = append(factories, factory)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var factories []BackendFactory
|
// SetBackend sets the currently running backend. The backend can only be set
|
||||||
|
// once—if there already is one then this function will do nothing.
|
||||||
func instantiateBackend () (backend Backend, err error) {
|
func SetBackend (b Backend) {
|
||||||
// find a suitable backend
|
if backend != nil { return }
|
||||||
for _, factory := range factories {
|
backend = b
|
||||||
backend, err = factory()
|
}
|
||||||
if err == nil && backend != nil { return }
|
|
||||||
}
|
// Bounds creates a rectangle from an x, y, width, and height.
|
||||||
|
func Bounds (x, y, width, height int) image.Rectangle {
|
||||||
// if none were found, but there was no error produced, produce an
|
return image.Rect(x, y, x + width, y + height)
|
||||||
// error
|
|
||||||
if err == nil {
|
|
||||||
err = errors.New("no available backends")
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
// Package canvas provides a canvas interface that is able to return a pixel
|
|
||||||
// buffer for drawing. This makes it considerably more efficient than the
|
|
||||||
// standard draw.Image.
|
|
||||||
package canvas
|
|
@ -1,6 +1,6 @@
|
|||||||
package tomo
|
package tomo
|
||||||
|
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||||
|
|
||||||
// Element represents a basic on-screen object. Extended element interfaces are
|
// Element represents a basic on-screen object. Extended element interfaces are
|
||||||
// defined in the ability module.
|
// defined in the ability module.
|
||||||
@ -8,7 +8,7 @@ type Element interface {
|
|||||||
// Draw causes the element to draw to the specified canvas. The bounds
|
// Draw causes the element to draw to the specified canvas. The bounds
|
||||||
// of this canvas specify the area that is actually drawn to, while the
|
// of this canvas specify the area that is actually drawn to, while the
|
||||||
// Entity bounds specify the actual area of the element.
|
// Entity bounds specify the actual area of the element.
|
||||||
Draw (canvas.Canvas)
|
Draw (artist.Canvas)
|
||||||
|
|
||||||
// Entity returns this element's entity.
|
// Entity returns this element's entity.
|
||||||
Entity () Entity
|
Entity () Entity
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package tomo
|
package tomo
|
||||||
|
|
||||||
import "image"
|
import "image"
|
||||||
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||||
|
|
||||||
// Entity is a handle given to elements by the backend. Extended entity
|
// Entity is a handle given to elements by the backend. Extended entity
|
||||||
// interfaces are defined in the ability module.
|
// interfaces are defined in the ability module.
|
||||||
@ -28,5 +28,5 @@ type Entity interface {
|
|||||||
// labels. If there is no parent element (that is, the element is
|
// labels. If there is no parent element (that is, the element is
|
||||||
// directly inside of the window), the backend will draw a default
|
// directly inside of the window), the backend will draw a default
|
||||||
// background pattern.
|
// background pattern.
|
||||||
DrawBackground (canvas.Canvas)
|
DrawBackground (artist.Canvas)
|
||||||
}
|
}
|
||||||
|
63
tomo.go
63
tomo.go
@ -1,63 +0,0 @@
|
|||||||
package tomo
|
|
||||||
|
|
||||||
import "image"
|
|
||||||
|
|
||||||
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()
|
|
||||||
if err != nil { return }
|
|
||||||
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 ()) {
|
|
||||||
assertBackend()
|
|
||||||
backend.Do(callback)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewEntity generates an entity for an element using the current backend.
|
|
||||||
func NewEntity (owner Element) Entity {
|
|
||||||
assertBackend()
|
|
||||||
return backend.NewEntity(owner)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewWindow creates a new window using the current backend, and returns it as a
|
|
||||||
// MainWindow. If the window could not be created, an error is returned
|
|
||||||
// explaining why.
|
|
||||||
func NewWindow (bounds image.Rectangle) (window MainWindow, err error) {
|
|
||||||
assertBackend()
|
|
||||||
return backend.NewWindow(bounds)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetTheme sets the theme of all open windows.
|
|
||||||
func SetTheme (theme Theme) {
|
|
||||||
backend.SetTheme(theme)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetConfig sets the configuration of all open windows.
|
|
||||||
func SetConfig (config Config) {
|
|
||||||
backend.SetConfig(config)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertBackend () {
|
|
||||||
if backend == nil { panic("no backend is running") }
|
|
||||||
}
|
|
Reference in New Issue
Block a user