tomo/backend.go

80 lines
2.7 KiB
Go

package tomo
import "sync"
import "image"
import "git.tebibyte.media/tomo/tomo/data"
import "git.tebibyte.media/tomo/tomo/canvas"
// Backend is any Tomo implementation. Backends handle window creation, layout,
// rendering, and events so that there can be as many platform-specific
// optimizations as possible.
type Backend interface {
// These methods create new Objects. The backend must reject any Object
// that was not made by it.
NewBox () Box
NewTextBox () TextBox
NewCanvasBox () CanvasBox
NewSurfaceBox () (SurfaceBox, error)
NewContainerBox () ContainerBox
// NewWindow creates a normal Window and returns it.
NewWindow (WindowKind, image.Rectangle) (Window, error)
// NewTexture creates a new canvs.Texture from an image. The backend
// must reject any texture that was not made by it.
NewTexture (image.Image) canvas.TextureCloser
// NewCanvas creates a new canvas.Canvas with the specified bounds. The
// backend must reject any canvas that was not made by it.
NewCanvas (image.Rectangle) canvas.CanvasCloser
// ColorRGBA returns the RGBA of a color according to the current style,
// as specified in color.Color.RGBA. It may be rendered invalid if the
// visual style changes, but the Backend must send a StyleChange event
// to all managed boxes when this happens.
ColorRGBA (id Color) (r, g, b, a uint32)
// IconTexture returns the canvas.Texture of an Icon. It may be closed
// and therefore rendered invalid if the icon set changes, but the
// Backend must send an IconSetChange event to all managed boxes when
// this happens.
IconTexture (Icon, IconSize) canvas.Texture
// MimeIconTexture returns the canvas.Texture of an icon corresponding
// to the specified MIME type. It may be closed and therefore rendered
// invalid if the icon set changes, but the Backend must send an
// IconSetChange event to all managed boxes when this happens.
MimeIconTexture (data.Mime, IconSize) canvas.Texture
// Run runs the event loop until Stop is called, or the Backend
// experiences a fatal error.
Run () error
// Stop must unblock Run. This behavior may only be called from within
// tomo.Stop.
Stop ()
// Do performs a callback function in the event loop thread as soon as
// possible. This method must be safe to call concurrently.
Do (func ())
}
var backendLock sync.Mutex
var backend Backend
func assertBackend () {
if backend == nil { panic("nil backend") }
}
// SetBackend sets the Backend that functions in this package will call upon.
// This function will panic if there is already a Backend running.
func SetBackend (back Backend) {
backendLock.Lock()
defer backendLock.Unlock()
if backend != nil {
panic("SetBackend called while another backend was running")
}
backend = back
}