2023-01-08 23:03:19 -07:00
|
|
|
package tomo
|
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "errors"
|
|
|
|
|
2023-01-09 09:17:29 -07:00
|
|
|
// ParentHooks is a struct that contains callbacks that let child elements send
|
|
|
|
// information to their parent element without the child element knowing
|
|
|
|
// anything about the parent element or containing any reference to it. When a
|
|
|
|
// parent element adopts a child element, it must set these callbacks.
|
|
|
|
type ParentHooks struct {
|
|
|
|
// Draw is called when a part of the child element's surface is updated.
|
|
|
|
// The updated region will be passed to the callback as a sub-image.
|
2023-01-13 23:54:57 -07:00
|
|
|
Draw func (region Canvas)
|
2023-01-09 09:17:29 -07:00
|
|
|
|
|
|
|
// MinimumSizeChange is called when the child 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.
|
|
|
|
MinimumSizeChange func (width, height int)
|
|
|
|
|
2023-01-10 09:51:46 -07:00
|
|
|
// SelectabilityChange is called when the chid element becomes
|
|
|
|
// selectable or non-selectable.
|
|
|
|
SelectabilityChange func (selectable bool)
|
|
|
|
|
2023-01-09 09:17:29 -07:00
|
|
|
// SelectionRequest is called when the child element element wants
|
|
|
|
// itself to be selected. If the parent element chooses to grant the
|
2023-01-11 20:30:14 -07:00
|
|
|
// request, it must send the child element a selection event and return
|
|
|
|
// true.
|
|
|
|
SelectionRequest func () (granted bool)
|
2023-01-09 09:17:29 -07:00
|
|
|
}
|
|
|
|
|
2023-01-09 09:28:18 -07:00
|
|
|
// RunDraw runs the Draw hook if it is not nil. If it is nil, it does nothing.
|
2023-01-13 23:54:57 -07:00
|
|
|
func (hooks ParentHooks) RunDraw (region Canvas) {
|
2023-01-09 09:28:18 -07:00
|
|
|
if hooks.Draw != nil {
|
|
|
|
hooks.Draw(region)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunMinimumSizeChange runs the MinimumSizeChange hook if it is not nil. If it
|
|
|
|
// is nil, it does nothing.
|
|
|
|
func (hooks ParentHooks) RunMinimumSizeChange (width, height int) {
|
|
|
|
if hooks.MinimumSizeChange != nil {
|
|
|
|
hooks.MinimumSizeChange(width, height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunSelectionRequest runs the SelectionRequest hook if it is not nil. If it is
|
|
|
|
// nil, it does nothing.
|
2023-01-11 20:30:14 -07:00
|
|
|
func (hooks ParentHooks) RunSelectionRequest () (granted bool) {
|
2023-01-09 09:28:18 -07:00
|
|
|
if hooks.SelectionRequest != nil {
|
2023-01-11 20:30:14 -07:00
|
|
|
granted = hooks.SelectionRequest()
|
2023-01-09 09:28:18 -07:00
|
|
|
}
|
2023-01-11 20:30:14 -07:00
|
|
|
return
|
2023-01-09 09:28:18 -07:00
|
|
|
}
|
|
|
|
|
2023-01-10 09:51:46 -07:00
|
|
|
// RunSelectabilityChange runs the SelectionRequest hook if it is not nil. If it
|
|
|
|
// is nil, it does nothing.
|
|
|
|
func (hooks ParentHooks) RunSelectabilityChange (selectable bool) {
|
|
|
|
if hooks.SelectabilityChange != nil {
|
|
|
|
hooks.SelectabilityChange(selectable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 23:03:19 -07:00
|
|
|
// Element represents a basic on-screen object.
|
|
|
|
type Element interface {
|
2023-01-13 23:54:57 -07:00
|
|
|
// Element must implement the Canvas interface. Elements should start
|
|
|
|
// out with a completely blank buffer, and only allocate memory and draw
|
2023-01-08 23:03:19 -07:00
|
|
|
// on it for the first time when sent an EventResize event.
|
2023-01-13 23:54:57 -07:00
|
|
|
Canvas
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
// 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.
|
2023-01-11 13:46:48 -07:00
|
|
|
Selectable () (selectable bool)
|
|
|
|
|
2023-01-11 16:32:02 -07:00
|
|
|
// Selected returns whether or not this element is currently selected.
|
|
|
|
// This will always return false if it is not selectable.
|
2023-01-11 13:46:48 -07:00
|
|
|
Selected () (selected bool)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-09 09:17:29 -07:00
|
|
|
// If this element contains other elements, and one is selected, this
|
2023-01-11 16:32:02 -07:00
|
|
|
// method will advance the selection in the specified direction. If
|
|
|
|
// the element contains selectable elements but none of them are
|
|
|
|
// selected, it will select the first selectable element. If there are
|
|
|
|
// no more children to be selected in the specified direction, the
|
|
|
|
// element will return false. If the selection could be advanced, it
|
|
|
|
// will return true. If the element contains no selectable child
|
|
|
|
// elements, it will always return false.
|
2023-01-09 09:17:29 -07:00
|
|
|
AdvanceSelection (direction int) (ok bool)
|
2023-01-08 23:03:19 -07:00
|
|
|
|
2023-01-09 09:17:29 -07:00
|
|
|
// SetParentHooks gives the element callbacks that let it send
|
|
|
|
// information to its parent element without it knowing anything about
|
|
|
|
// the parent element or containing any reference to it. When a parent
|
|
|
|
// element adopts a child element, it must set these callbacks.
|
|
|
|
SetParentHooks (callbacks ParentHooks)
|
2023-01-09 09:31:59 -07:00
|
|
|
|
|
|
|
// MinimumSize specifies the minimum amount of pixels this element's
|
|
|
|
// width and height may be set to. If the element is given a resize
|
|
|
|
// event with dimensions smaller than this, it will use its minimum
|
|
|
|
// instead of the offending dimension(s).
|
|
|
|
MinimumSize () (width, height int)
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2023-01-09 09:17:29 -07:00
|
|
|
// Adopt sets the root element of the window. There can only be one of
|
|
|
|
// these at one time.
|
2023-01-08 23:03:19 -07:00
|
|
|
Adopt (child Element)
|
2023-01-09 09:17:29 -07:00
|
|
|
|
|
|
|
// Child returns the root element of the window.
|
2023-01-08 23:03:19 -07:00
|
|
|
Child () (child Element)
|
2023-01-09 09:17:29 -07:00
|
|
|
|
|
|
|
// SetTitle sets the title that appears on the window's title bar. This
|
|
|
|
// method might have no effect with some backends.
|
2023-01-08 23:03:19 -07:00
|
|
|
SetTitle (title string)
|
2023-01-09 09:17:29 -07:00
|
|
|
|
|
|
|
// SetIcon taks in a list different sizes of the same icon and selects
|
|
|
|
// the best one to display on the window title bar, dock, or whatever is
|
|
|
|
// applicable for the given backend. This method might have no effect
|
2023-01-09 11:06:55 -07:00
|
|
|
// for some backends.
|
2023-01-09 09:17:29 -07:00
|
|
|
SetIcon (sizes []image.Image)
|
|
|
|
|
|
|
|
// Show shows the window. The window starts off hidden, so this must be
|
|
|
|
// called after initial setup to make sure it is visible.
|
|
|
|
Show ()
|
|
|
|
|
|
|
|
// Hide hides the window.
|
|
|
|
Hide ()
|
|
|
|
|
|
|
|
// Close closes the window.
|
|
|
|
Close ()
|
|
|
|
|
|
|
|
// OnClose specifies a function to be called when the window is closed.
|
|
|
|
OnClose (func ())
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// LayoutEntry associates an element with layout and positioning information so
|
|
|
|
// it can be arranged by a Layout.
|
|
|
|
type LayoutEntry struct {
|
|
|
|
Element
|
|
|
|
Position image.Point
|
|
|
|
Expand bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Layout is capable of arranging elements within a container. It is also able
|
|
|
|
// to determine the minimum amount of room it needs to do so.
|
|
|
|
type Layout interface {
|
|
|
|
// Arrange takes in a slice of entries and a bounding width and height,
|
|
|
|
// and changes the position of the entiries in the slice so that they
|
|
|
|
// are properly laid out. The given width and height should not be less
|
|
|
|
// than what is returned by MinimumSize.
|
|
|
|
Arrange (entries []LayoutEntry, width, height int)
|
|
|
|
|
|
|
|
// MinimumSize returns the minimum width and height that the layout
|
|
|
|
// needs to properly arrange the given slice of layout entries.
|
|
|
|
MinimumSize (entries []LayoutEntry) (width, height int)
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
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-01-09 09:36:37 -07:00
|
|
|
backend.Do(callback)
|
2023-01-08 23:03:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|