Split up the Element interface's functionality

This commit is contained in:
Sasha Koshka 2023-01-14 22:18:06 -05:00
parent ddf48d2333
commit 8cfb8eeaef
2 changed files with 73 additions and 122 deletions

View File

@ -14,11 +14,7 @@ type ParentHooks struct {
// have already been resized and there is no need to send it a resize
// event.
MinimumSizeChange func (width, height int)
// SelectabilityChange is called when the chid element becomes
// selectable or non-selectable.
SelectabilityChange func (selectable bool)
// SelectionRequest is called when the child element element wants
// itself to be selected. If the parent element chooses to grant the
// request, it must send the child element a selection event and return
@ -50,14 +46,6 @@ func (hooks ParentHooks) RunSelectionRequest () (granted bool) {
return
}
// 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)
}
}
// Element represents a basic on-screen object.
type Element interface {
// Element must implement the Canvas interface. Elements should start
@ -65,27 +53,6 @@ type Element interface {
// on it for the first time when sent an EventResize event.
Canvas
// 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 () (selectable bool)
// Selected returns whether or not this element is currently selected.
// This will always return false if it is not selectable.
Selected () (selected bool)
// If this element contains other elements, and one is selected, this
// 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.
AdvanceSelection (direction int) (ok bool)
// 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
@ -98,3 +65,75 @@ type Element interface {
// instead of the offending dimension(s).
MinimumSize () (width, height int)
}
// SelectionDirection represents a keyboard navigation direction.
type SelectionDirection int
const (
SelectionDirectionNeutral SelectionDirection = 0
SelectionDirectionBackward SelectionDirection = -1
SelectionDirectionForward SelectionDirection = 1
)
// Selectable represents an element that has keyboard navigation support. This
// includes inputs, buttons, sliders, etc. as well as any elements that have
// children (so keyboard navigation events can be propagated downward).
type Selectable interface {
Element
// Selected returns whether or not this element is currently selected.
Selected () (selected bool)
// Select selects this element, if its parent element grants the
// request.
Select ()
// HandleSelection causes this element to mark itself as selected, if it
// can currently be. Otherwise, it will return false and do nothing.
HandleSelection (direction SelectionDirection) (accepted bool)
// HandleDeselection causes this element to mark itself and all of its
// children as deselected.
HandleDeselection ()
}
// KeyboardTarget represents an element that can receive keyboard input.
type KeyboardTarget interface {
Element
// HandleKeyDown is called when a key is pressed down while this element
// has keyboard focus. It is important to note that not every key down
// event is guaranteed to be paired with exactly one key up event. This
// is the reason a list of modifier keys held down at the time of the
// key press is given.
HandleKeyDown (key Key, modifiers Modifiers, repeated bool)
// HandleKeyUp is called when a key is released while this element has
// keyboard focus.
HandleKeyUp (key Key)
}
// MouseTarget represents an element that can receive mouse events.
type MouseTarget interface {
Element
// Each of these handler methods is passed the position of the mouse
// cursor at the time of the event as x, y.
// HandleMouseDown is called when a mouse button is pressed down on this
// element.
HandleMouseDown (x, y int, button Button)
// HandleMouseUp is called when a mouse button is released that was
// originally pressed down on this element.
HandleMouseUp (x, y int, button Button)
// HandleMouseMove is called when the mouse is moved over this element,
// or the mouse is moving while being held down and originally pressed
// down on this element.
HandleMouseMove (x, y int)
// HandleScroll is called when the mouse is scrolled. The X and Y
// direction of the scroll event are passed as deltaX and deltaY.
HandleScroll (x, y int, deltaX, deltaY float64)
}

View File

@ -1,88 +0,0 @@
package tomo
// Event represents any event. Use a type switch to figure out what sort of
// event it is.
type Event interface { }
// EventResize is sent to an element when its parent decides to resize it.
// Elements should not do anything if the width and height do not change.
type EventResize struct {
// The width and height the element should not be less than the
// element's reported minimum width and height. If by some chance they
// are anyways, the element should use its minimum width and height
// instead.
Width, Height int
}
// EventKeyDown is sent to the currently selected element when a key on the
// keyboard is pressed. Containers must propagate this event downwards.
type EventKeyDown struct {
Key
Modifiers
Repeated bool
}
// EventKeyDown is sent to the currently selected element when a key on the
// keyboard is released. Containers must propagate this event downwards.
type EventKeyUp struct {
Key
Modifiers
}
// EventMouseDown is sent to the element the mouse is positioned over when it is
// clicked. Containers must propagate this event downwards, with X and Y values
// relative to the top left corner of the child element.
type EventMouseDown struct {
// The button that was released
Button
// The X and Y position of the mouse cursor at the time of the event,
// relative to the top left corner of the element
X, Y int
}
// EventMouseUp is sent to the element that was positioned under the mouse the
// last time this particular mouse button was pressed down when it is released.
// Containers must propagate this event downwards, with X and Y values relative
// to the top left corner of the child element.
type EventMouseUp struct {
// The button that was released
Button
// The X and Y position of the mouse cursor at the time of the event,
// relative to the top left corner of the element
X, Y int
}
// EventMouseMove is sent to the element positioned under the mouse cursor when
// the mouse moves, or if a mouse button is currently being pressed, the element
// that the mouse was positioned under when it was pressed down. Containers must
// propogate this event downwards, with X and Y values relative to the top left
// corner of the child element.
type EventMouseMove struct {
// The X and Y position of the mouse cursor at the time of the event,
// relative to the top left corner of the element
X, Y int
}
// EventScroll is sent to the element positioned under the mouse cursor when the
// scroll wheel (or equivalent) is spun. Containers must propogate this event
// downwards.
type EventScroll struct {
// The X and Y position of the mouse cursor at the time of the event,
// relative to the top left corner of the element
X, Y int
// The X and Y amount the scroll wheel moved
ScrollX, ScrollY int
}
// EventSelect is sent to selectable elements when they become selected, whether
// by a mouse click or by keyboard navigation. Containers must propagate this
// event downwards.
type EventSelect struct { }
// EventDeselect is sent to selectable elements when they stop being selected,
// whether by a mouse click or by keyboard navigation. Containers must propagate
// this event downwards.
type EventDeselect struct { }