This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/tomo.go

115 lines
3.9 KiB
Go

package tomo
import "image"
import "errors"
import "image/draw"
import "image/color"
// import "git.tebibyte.media/sashakoshka/tomo/iterator"
// Image represents a simple image buffer that fulfills the image.Image
// interface while also having methods that do away with the use of the
// color.Color interface to facilitate more efficient drawing. This interface
// can be easily satisfied using an image.RGBA struct.
type Image interface {
image.Image
RGBAAt (x, y int) (c color.RGBA)
}
// Canvas is like Image but also requires a SetRGBA method. This interface can
// be easily satisfied using an image.RGBA struct.
type Canvas interface {
draw.Image
RGBAAt (x, y int) (c color.RGBA)
SetRGBA (x, y int, c color.RGBA)
}
// Element represents a basic on-screen object.
type Element interface {
// Element must implement the Image interface. Elements should start out
// with a completely blank image buffer, and only set its size and draw
// on it for the first time when sent an EventResize event.
Image
// Handle handles an event, propagating it to children if necessary.
Handle (event Event)
// Selectable returns whether this element can be selected. If this
// element contains other selectable elements, it must return true.
Selectable () (bool)
// SetDrawCallback sets a function to be called when a part of the
// element's surface is updated. The updated region will be passed to
// the callback as a sub-image.
SetDrawCallback (draw func (region Image))
// SetMinimumSizeChangeCallback sets a function to be called when the
// element's minimum width and/or height changes. When this function is
// called, the element will have already been resized and there is no
// need to send it a resize event.
SetMinimumSizeChangeCallback (notify func (width, height int))
// MinimumWidth specifies the minimum amount of pixels this element's
// width may be set to. If the element is resized to an amount smaller
// that MinimumWidth, it will instead set its width to MinimumWidth.
MinimumWidth () (minimum int)
// MinimumHeight specifies the minimum amount of pixels this element's
// height may be set to. If the element is resized to an amount smaller
// that MinimumHeight, it will instead set its height to MinimumHeight.
MinimumHeight () (minimum int)
}
// Window represents a top-level container generated by the currently running
// backend. It can contain a single element. It is hidden by default, and must
// be explicitly shown with the Show() method. If it contains no element, it
// displays a black (or transprent) background.
type Window interface {
Element
Adopt (child Element)
Child () (child Element)
SetTitle (title string)
SetIcon (sizes []image.Image)
Show ()
Hide ()
Close ()
OnClose (func ())
}
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 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 ()) {
}
// NewWindow creates a new window using the current backend, and returns it as a
// Window. If the window could not be created, an error is returned explaining
// why. If this function is called without a running backend, an error is
// returned as well.
func NewWindow (width, height int) (window Window, err error) {
if backend == nil {
err = errors.New("no backend is running.")
return
}
window, err = backend.NewWindow(width, height)
return
}