Re-organized module structure

This commit is contained in:
2023-03-30 23:19:04 -04:00
parent 719b7b99ac
commit 53bfc8df68
62 changed files with 458 additions and 532 deletions

View File

@@ -1,3 +0,0 @@
// Package basicElements provides standard elements that are commonly used in
// GUI applications.
package basicElements

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
// import "runtime/debug"
@@ -34,7 +34,7 @@ type Button struct {
// NewButton creates a new button with the specified label text.
func NewButton (text string) (element *Button) {
element = &Button { showText: true }
element.theme.Case = theme.C("basic", "button")
element.theme.Case = theme.C("tomo", "button")
element.Core, element.core = core.NewCore(element, element.drawAll)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.core, element.drawAndPush)

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/input"
@@ -28,7 +28,7 @@ type Checkbox struct {
// NewCheckbox creates a new cbeckbox with the specified label text.
func NewCheckbox (text string, checked bool) (element *Checkbox) {
element = &Checkbox { checked: checked }
element.theme.Case = theme.C("basic", "checkbox")
element.theme.Case = theme.C("tomo", "checkbox")
element.Core, element.core = core.NewCore(element, element.draw)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.core, element.redo)

View File

@@ -1,13 +1,12 @@
package containers
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/layouts"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// Container is an element capable of containg other elements, and arranging
@@ -17,8 +16,8 @@ type Container struct {
*core.Propagator
core core.CoreControl
layout layouts.Layout
children []layouts.LayoutEntry
layout tomo.Layout
children []tomo.LayoutEntry
warping bool
config config.Wrapped
@@ -29,9 +28,9 @@ type Container struct {
}
// NewContainer creates a new container.
func NewContainer (layout layouts.Layout) (element *Container) {
func NewContainer (layout tomo.Layout) (element *Container) {
element = &Container { }
element.theme.Case = theme.C("containers", "container")
element.theme.Case = theme.C("tomo", "container")
element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element, element.core)
element.SetLayout(layout)
@@ -39,7 +38,7 @@ func NewContainer (layout layouts.Layout) (element *Container) {
}
// SetLayout sets the layout of this container.
func (element *Container) SetLayout (layout layouts.Layout) {
func (element *Container) SetLayout (layout tomo.Layout) {
element.layout = layout
element.updateMinimumSize()
if element.core.HasImage() {
@@ -51,17 +50,17 @@ func (element *Container) SetLayout (layout layouts.Layout) {
// Adopt adds a new child element to the container. If expand is set to true,
// the element will expand (instead of contract to its minimum size), in
// whatever way is defined by the current layout.
func (element *Container) Adopt (child elements.Element, expand bool) {
if child0, ok := child.(elements.Themeable); ok {
func (element *Container) Adopt (child tomo.Element, expand bool) {
if child0, ok := child.(tomo.Themeable); ok {
child0.SetTheme(element.theme.Theme)
}
if child0, ok := child.(elements.Configurable); ok {
if child0, ok := child.(tomo.Configurable); ok {
child0.SetConfig(element.config.Config)
}
child.SetParent(element)
// add child
element.children = append (element.children, layouts.LayoutEntry {
element.children = append (element.children, tomo.LayoutEntry {
Element: child,
Expand: expand,
})
@@ -98,7 +97,7 @@ func (element *Container) Warp (callback func ()) {
// Disown removes the given child from the container if it is contained within
// it.
func (element *Container) Disown (child elements.Element) {
func (element *Container) Disown (child tomo.Element) {
for index, entry := range element.children {
if entry.Element == child {
element.clearChildEventHandlers(entry.Element)
@@ -116,11 +115,11 @@ func (element *Container) Disown (child elements.Element) {
}
}
func (element *Container) clearChildEventHandlers (child elements.Element) {
func (element *Container) clearChildEventHandlers (child tomo.Element) {
child.DrawTo(nil, image.Rectangle { }, nil)
child.SetParent(nil)
if child, ok := child.(elements.Focusable); ok {
if child, ok := child.(tomo.Focusable); ok {
if child.Focused() {
child.HandleUnfocus()
}
@@ -142,8 +141,8 @@ func (element *Container) DisownAll () {
}
// Children returns a slice containing this element's children.
func (element *Container) Children () (children []elements.Element) {
children = make([]elements.Element, len(element.children))
func (element *Container) Children () (children []tomo.Element) {
children = make([]tomo.Element, len(element.children))
for index, entry := range element.children {
children[index] = entry.Element
}
@@ -157,14 +156,14 @@ func (element *Container) CountChildren () (count int) {
// Child returns the child at the specified index. If the index is out of
// bounds, this method will return nil.
func (element *Container) Child (index int) (child elements.Element) {
func (element *Container) Child (index int) (child tomo.Element) {
if index < 0 || index > len(element.children) { return }
return element.children[index].Element
}
// ChildAt returns the child that contains the specified x and y coordinates. If
// there are no children at the coordinates, this method will return nil.
func (element *Container) ChildAt (point image.Point) (child elements.Element) {
func (element *Container) ChildAt (point image.Point) (child tomo.Element) {
for _, entry := range element.children {
if point.In(entry.Bounds) {
child = entry.Element
@@ -207,7 +206,7 @@ func (element *Container) redoAll () {
// NotifyMinimumSizeChange notifies the container that the minimum size of a
// child element has changed.
func (element *Container) NotifyMinimumSizeChange (child elements.Element) {
func (element *Container) NotifyMinimumSizeChange (child tomo.Element) {
element.updateMinimumSize()
element.redoAll()
element.core.DamageAll()

View File

@@ -1,12 +1,11 @@
package containers
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/layouts"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
type DocumentContainer struct {
@@ -14,7 +13,7 @@ type DocumentContainer struct {
*core.Propagator
core core.CoreControl
children []layouts.LayoutEntry
children []tomo.LayoutEntry
scroll image.Point
warping bool
contentBounds image.Rectangle
@@ -28,24 +27,24 @@ type DocumentContainer struct {
// NewDocumentContainer creates a new document container.
func NewDocumentContainer () (element *DocumentContainer) {
element = &DocumentContainer { }
element.theme.Case = theme.C("containers", "documentContainer")
element.theme.Case = theme.C("tomo", "documentContainer")
element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element, element.core)
return
}
// Adopt adds a new child element to the container.
func (element *DocumentContainer) Adopt (child elements.Element) {
func (element *DocumentContainer) Adopt (child tomo.Element) {
// set event handlers
if child0, ok := child.(elements.Themeable); ok {
if child0, ok := child.(tomo.Themeable); ok {
child0.SetTheme(element.theme.Theme)
}
if child0, ok := child.(elements.Configurable); ok {
if child0, ok := child.(tomo.Configurable); ok {
child0.SetConfig(element.config.Config)
}
// add child
element.children = append (element.children, layouts.LayoutEntry {
element.children = append (element.children, tomo.LayoutEntry {
Element: child,
})
@@ -80,7 +79,7 @@ func (element *DocumentContainer) Warp (callback func ()) {
// Disown removes the given child from the container if it is contained within
// it.
func (element *DocumentContainer) Disown (child elements.Element) {
func (element *DocumentContainer) Disown (child tomo.Element) {
for index, entry := range element.children {
if entry.Element == child {
element.clearChildEventHandlers(entry.Element)
@@ -98,11 +97,11 @@ func (element *DocumentContainer) Disown (child elements.Element) {
}
}
func (element *DocumentContainer) clearChildEventHandlers (child elements.Element) {
func (element *DocumentContainer) clearChildEventHandlers (child tomo.Element) {
child.DrawTo(nil, image.Rectangle { }, nil)
child.SetParent(nil)
if child, ok := child.(elements.Focusable); ok {
if child, ok := child.(tomo.Focusable); ok {
if child.Focused() {
child.HandleUnfocus()
}
@@ -124,8 +123,8 @@ func (element *DocumentContainer) DisownAll () {
}
// Children returns a slice containing this element's children.
func (element *DocumentContainer) Children () (children []elements.Element) {
children = make([]elements.Element, len(element.children))
func (element *DocumentContainer) Children () (children []tomo.Element) {
children = make([]tomo.Element, len(element.children))
for index, entry := range element.children {
children[index] = entry.Element
}
@@ -139,14 +138,14 @@ func (element *DocumentContainer) CountChildren () (count int) {
// Child returns the child at the specified index. If the index is out of
// bounds, this method will return nil.
func (element *DocumentContainer) Child (index int) (child elements.Element) {
func (element *DocumentContainer) Child (index int) (child tomo.Element) {
if index < 0 || index > len(element.children) { return }
return element.children[index].Element
}
// ChildAt returns the child that contains the specified x and y coordinates. If
// there are no children at the coordinates, this method will return nil.
func (element *DocumentContainer) ChildAt (point image.Point) (child elements.Element) {
func (element *DocumentContainer) ChildAt (point image.Point) (child tomo.Element) {
for _, entry := range element.children {
if point.In(entry.Bounds) {
child = entry.Element
@@ -178,7 +177,7 @@ func (element *DocumentContainer) redoAll () {
artist.DrawShatter(element.core, pattern, element.Bounds(), rocks...)
element.partition()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
if element.onScrollBoundsChange != nil {
@@ -205,7 +204,7 @@ func (element *DocumentContainer) partition () {
// NotifyMinimumSizeChange notifies the container that the minimum size of a
// child element has changed.
func (element *DocumentContainer) NotifyMinimumSizeChange (child elements.Element) {
func (element *DocumentContainer) NotifyMinimumSizeChange (child tomo.Element) {
element.redoAll()
element.core.DamageAll()
}
@@ -214,7 +213,7 @@ func (element *DocumentContainer) NotifyMinimumSizeChange (child elements.Elemen
// affecting a child's flexible height have changed. This method is
// expected to be called by flexible child element when their content
// changes.
func (element *DocumentContainer) NotifyFlexibleHeightChange (child elements.Flexible) {
func (element *DocumentContainer) NotifyFlexibleHeightChange (child tomo.Flexible) {
element.redoAll()
element.core.DamageAll()
}
@@ -300,7 +299,7 @@ func (element *DocumentContainer) doLayout () {
if width < bounds.Dx() {
width = bounds.Dx()
}
if typedChild, ok := entry.Element.(elements.Flexible); ok {
if typedChild, ok := entry.Element.(tomo.Flexible); ok {
height = typedChild.FlexibleHeightFor(width)
}

View File

@@ -1,13 +1,13 @@
package containers
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
// ScrollContainer is a container that is capable of holding a scrollable
// element.
@@ -16,9 +16,9 @@ type ScrollContainer struct {
*core.Propagator
core core.CoreControl
child elements.Scrollable
horizontal *basicElements.ScrollBar
vertical *basicElements.ScrollBar
child tomo.Scrollable
horizontal *elements.ScrollBar
vertical *elements.ScrollBar
config config.Wrapped
theme theme.Wrapped
@@ -31,12 +31,12 @@ type ScrollContainer struct {
// bars.
func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
element = &ScrollContainer { }
element.theme.Case = theme.C("containers", "scrollContainer")
element.theme.Case = theme.C("tomo", "scrollContainer")
element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element, element.core)
if horizontal {
element.horizontal = basicElements.NewScrollBar(false)
element.horizontal = elements.NewScrollBar(false)
element.setUpChild(element.horizontal)
element.horizontal.OnScroll (func (viewport image.Point) {
if element.child != nil {
@@ -50,7 +50,7 @@ func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
})
}
if vertical {
element.vertical = basicElements.NewScrollBar(true)
element.vertical = elements.NewScrollBar(true)
element.setUpChild(element.vertical)
element.vertical.OnScroll (func (viewport image.Point) {
if element.child != nil {
@@ -70,7 +70,7 @@ func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
// Adopt adds a scrollable element to the scroll container. The container can
// only contain one scrollable element at a time, and when a new one is adopted
// it replaces the last one.
func (element *ScrollContainer) Adopt (child elements.Scrollable) {
func (element *ScrollContainer) Adopt (child tomo.Scrollable) {
// disown previous child if it exists
if element.child != nil {
element.disownChild(child)
@@ -90,20 +90,20 @@ func (element *ScrollContainer) Adopt (child elements.Scrollable) {
}
}
func (element *ScrollContainer) setUpChild (child elements.Element) {
func (element *ScrollContainer) setUpChild (child tomo.Element) {
child.SetParent(element)
if child, ok := child.(elements.Themeable); ok {
if child, ok := child.(tomo.Themeable); ok {
child.SetTheme(element.theme.Theme)
}
if child, ok := child.(elements.Configurable); ok {
if child, ok := child.(tomo.Configurable); ok {
child.SetConfig(element.config.Config)
}
}
func (element *ScrollContainer) disownChild (child elements.Scrollable) {
func (element *ScrollContainer) disownChild (child tomo.Scrollable) {
child.DrawTo(nil, image.Rectangle { }, nil)
child.SetParent(nil)
if child, ok := child.(elements.Focusable); ok {
if child, ok := child.(tomo.Focusable); ok {
if child.Focused() {
child.HandleUnfocus()
}
@@ -112,14 +112,14 @@ func (element *ScrollContainer) disownChild (child elements.Scrollable) {
// NotifyMinimumSizeChange notifies the container that the minimum size of a
// child element has changed.
func (element *ScrollContainer) NotifyMinimumSizeChange (child elements.Element) {
func (element *ScrollContainer) NotifyMinimumSizeChange (child tomo.Element) {
element.redoAll()
element.core.DamageAll()
}
// NotifyScrollBoundsChange notifies the container that the scroll bounds or
// axes of a child have changed.
func (element *ScrollContainer) NotifyScrollBoundsChange (child elements.Scrollable) {
func (element *ScrollContainer) NotifyScrollBoundsChange (child tomo.Scrollable) {
element.updateEnabled()
viewportBounds := element.child.ScrollViewportBounds()
contentBounds := element.child.ScrollContentBounds()
@@ -165,7 +165,7 @@ func (element *ScrollContainer) CountChildren () (count int) {
// Child returns the child at the specified index. If the index is out of
// bounds, this method will return nil.
func (element *ScrollContainer) Child (index int) (child elements.Element) {
func (element *ScrollContainer) Child (index int) (child tomo.Element) {
switch index {
case 0: return element.child
case 1:

View File

@@ -2,16 +2,16 @@ package core
import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/elements"
// Core is a struct that implements some core functionality common to most
// widgets. It is meant to be embedded directly into a struct.
type Core struct {
canvas canvas.Canvas
bounds image.Rectangle
parent elements.Parent
outer elements.Element
parent tomo.Parent
outer tomo.Element
metrics struct {
minimumWidth int
@@ -26,7 +26,7 @@ type Core struct {
// element that it will be a part of. If outer is nil, this function will return
// nil.
func NewCore (
outer elements.Element,
outer tomo.Element,
drawSizeChange func (),
) (
core *Core,
@@ -57,7 +57,7 @@ func (core *Core) MinimumSize () (width, height int) {
// MinimumSize fulfils the tomo.Element interface. This should not need to be
// overridden, unless you want to detect when the element is parented or
// unparented.
func (core *Core) SetParent (parent elements.Parent) {
func (core *Core) SetParent (parent tomo.Parent) {
if parent != nil && core.parent != nil {
panic("core.SetParent: element already has a parent")
}
@@ -118,12 +118,12 @@ func (control CoreControl) Buffer () (data []color.RGBA, stride int) {
}
// Parent returns the element's parent.
func (control CoreControl) Parent () elements.Parent {
func (control CoreControl) Parent () tomo.Parent {
return control.core.parent
}
// Outer returns the outer element given when the control was constructed.
func (control CoreControl) Outer () elements.Element {
func (control CoreControl) Outer () tomo.Element {
return control.core.outer
}

View File

@@ -1,8 +1,8 @@
package core
// import "runtime/debug"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/elements"
// FocusableCore is a struct that can be embedded into objects to make them
// focusable, giving them the default keynav behavior.
@@ -42,9 +42,9 @@ func (core *FocusableCore) Focused () (focused bool) {
func (core *FocusableCore) Focus () {
if !core.enabled || core.focused { return }
parent := core.core.Parent()
if parent, ok := parent.(elements.FocusableParent); ok {
if parent, ok := parent.(tomo.FocusableParent); ok {
core.focused = parent.RequestFocus (
core.core.Outer().(elements.Focusable))
core.core.Outer().(tomo.Focusable))
}
}

View File

@@ -1,15 +1,15 @@
package core
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/elements"
// Container represents an object that can provide access to a list of child
// elements.
type Container interface {
Child (index int) elements.Element
Child (index int) tomo.Element
CountChildren () int
}
@@ -20,7 +20,7 @@ type Container interface {
type Propagator struct {
core CoreControl
container Container
drags [10]elements.MouseTarget
drags [10]tomo.MouseTarget
focused bool
}
@@ -49,9 +49,9 @@ func (propagator *Propagator) Focused () (focused bool) {
func (propagator *Propagator) Focus () {
if propagator.focused == true { return }
parent := propagator.core.Parent()
if parent, ok := parent.(elements.FocusableParent); ok && parent != nil {
if parent, ok := parent.(tomo.FocusableParent); ok && parent != nil {
propagator.focused = parent.RequestFocus (
propagator.core.Outer().(elements.Focusable))
propagator.core.Outer().(tomo.Focusable))
}
}
@@ -90,7 +90,7 @@ func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (acc
// focus in the specified direction
firstFocusedChild :=
propagator.container.Child(firstFocused).
(elements.Focusable)
(tomo.Focusable)
// before we move the focus, the currently focused child
// may also be able to move its focus. if the child is able
@@ -107,7 +107,7 @@ func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (acc
child, focusable :=
propagator.container.Child(index).
(elements.Focusable)
(tomo.Focusable)
if focusable && child.HandleFocus(direction) {
// we have found one, so we now actually move
// the focus.
@@ -126,12 +126,12 @@ func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (acc
// return true and the child element should behave as if a HandleFocus
// call was made.
func (propagator *Propagator) RequestFocus (
child elements.Focusable,
child tomo.Focusable,
) (
granted bool,
) {
if parent, ok := propagator.core.Parent().(elements.FocusableParent); ok {
if parent.RequestFocus(propagator.core.Outer().(elements.Focusable)) {
if parent, ok := propagator.core.Parent().(tomo.FocusableParent); ok {
if parent.RequestFocus(propagator.core.Outer().(tomo.Focusable)) {
propagator.HandleUnfocus()
propagator.focused = true
granted = true
@@ -142,26 +142,26 @@ func (propagator *Propagator) RequestFocus (
// RequestFocusMotion notifies the parent that a child element wants the
// focus to be moved to the next focusable element.
func (propagator *Propagator) RequestFocusNext (child elements.Focusable) {
func (propagator *Propagator) RequestFocusNext (child tomo.Focusable) {
if !propagator.focused { return }
if parent, ok := propagator.core.Parent().(elements.FocusableParent); ok {
parent.RequestFocusNext(propagator.core.Outer().(elements.Focusable))
if parent, ok := propagator.core.Parent().(tomo.FocusableParent); ok {
parent.RequestFocusNext(propagator.core.Outer().(tomo.Focusable))
}
}
// RequestFocusMotion notifies the parent that a child element wants the
// focus to be moved to the previous focusable element.
func (propagator *Propagator) RequestFocusPrevious (child elements.Focusable) {
func (propagator *Propagator) RequestFocusPrevious (child tomo.Focusable) {
if !propagator.focused { return }
if parent, ok := propagator.core.Parent().(elements.FocusableParent); ok {
parent.RequestFocusPrevious(propagator.core.Outer().(elements.Focusable))
if parent, ok := propagator.core.Parent().(tomo.FocusableParent); ok {
parent.RequestFocusPrevious(propagator.core.Outer().(tomo.Focusable))
}
}
// HandleDeselection causes this element to mark itself and all of its children
// as unfocused.
func (propagator *Propagator) HandleUnfocus () {
propagator.forFocusable (func (child elements.Focusable) bool {
propagator.forFocusable (func (child tomo.Focusable) bool {
child.HandleUnfocus()
return true
})
@@ -170,8 +170,8 @@ func (propagator *Propagator) HandleUnfocus () {
// HandleKeyDown propogates the keyboard event to the currently selected child.
func (propagator *Propagator) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
propagator.forFocused (func (child elements.Focusable) bool {
typedChild, handlesKeyboard := child.(elements.KeyboardTarget)
propagator.forFocused (func (child tomo.Focusable) bool {
typedChild, handlesKeyboard := child.(tomo.KeyboardTarget)
if handlesKeyboard {
typedChild.HandleKeyDown(key, modifiers)
}
@@ -181,8 +181,8 @@ func (propagator *Propagator) HandleKeyDown (key input.Key, modifiers input.Modi
// HandleKeyUp propogates the keyboard event to the currently selected child.
func (propagator *Propagator) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
propagator.forFocused (func (child elements.Focusable) bool {
typedChild, handlesKeyboard := child.(elements.KeyboardTarget)
propagator.forFocused (func (child tomo.Focusable) bool {
typedChild, handlesKeyboard := child.(tomo.KeyboardTarget)
if handlesKeyboard {
typedChild.HandleKeyUp(key, modifiers)
}
@@ -195,7 +195,7 @@ func (propagator *Propagator) HandleKeyUp (key input.Key, modifiers input.Modifi
func (propagator *Propagator) HandleMouseDown (x, y int, button input.Button) {
child, handlesMouse :=
propagator.childAt(image.Pt(x, y)).
(elements.MouseTarget)
(tomo.MouseTarget)
if handlesMouse {
propagator.drags[button] = child
child.HandleMouseDown(x, y, button)
@@ -218,7 +218,7 @@ func (propagator *Propagator) HandleMouseUp (x, y int, button input.Button) {
func (propagator *Propagator) HandleMotion (x, y int) {
handled := false
for _, child := range propagator.drags {
if child, ok := child.(elements.MotionTarget); ok {
if child, ok := child.(tomo.MotionTarget); ok {
child.HandleMotion(x, y)
handled = true
}
@@ -226,7 +226,7 @@ func (propagator *Propagator) HandleMotion (x, y int) {
if !handled {
child := propagator.childAt(image.Pt(x, y))
if child, ok := child.(elements.MotionTarget); ok {
if child, ok := child.(tomo.MotionTarget); ok {
child.HandleMotion(x, y)
}
}
@@ -236,15 +236,15 @@ func (propagator *Propagator) HandleMotion (x, y int) {
// pointer.
func (propagator *Propagator) HandleScroll (x, y int, deltaX, deltaY float64) {
child := propagator.childAt(image.Pt(x, y))
if child, ok := child.(elements.ScrollTarget); ok {
if child, ok := child.(tomo.ScrollTarget); ok {
child.HandleScroll(x, y, deltaX, deltaY)
}
}
// SetTheme sets the theme of all children to the specified theme.
func (propagator *Propagator) SetTheme (theme theme.Theme) {
propagator.forChildren (func (child elements.Element) bool {
typedChild, themeable := child.(elements.Themeable)
propagator.forChildren (func (child tomo.Element) bool {
typedChild, themeable := child.(tomo.Themeable)
if themeable {
typedChild.SetTheme(theme)
}
@@ -254,8 +254,8 @@ func (propagator *Propagator) SetTheme (theme theme.Theme) {
// SetConfig sets the theme of all children to the specified config.
func (propagator *Propagator) SetConfig (config config.Config) {
propagator.forChildren (func (child elements.Element) bool {
typedChild, configurable := child.(elements.Configurable)
propagator.forChildren (func (child tomo.Element) bool {
typedChild, configurable := child.(tomo.Configurable)
if configurable {
typedChild.SetConfig(config)
}
@@ -270,7 +270,7 @@ func (propagator *Propagator) focusFirstFocusableElement (
) (
ok bool,
) {
propagator.forFocusable (func (child elements.Focusable) bool {
propagator.forFocusable (func (child tomo.Focusable) bool {
if child.HandleFocus(direction) {
propagator.focused = true
ok = true
@@ -286,8 +286,8 @@ func (propagator *Propagator) focusLastFocusableElement (
) (
ok bool,
) {
propagator.forChildrenReverse (func (child elements.Element) bool {
typedChild, focusable := child.(elements.Focusable)
propagator.forChildrenReverse (func (child tomo.Element) bool {
typedChild, focusable := child.(tomo.Focusable)
if focusable && typedChild.HandleFocus(direction) {
propagator.focused = true
ok = true
@@ -300,7 +300,7 @@ func (propagator *Propagator) focusLastFocusableElement (
// ----------- Iterator utilities ----------- //
func (propagator *Propagator) forChildren (callback func (child elements.Element) bool) {
func (propagator *Propagator) forChildren (callback func (child tomo.Element) bool) {
for index := 0; index < propagator.container.CountChildren(); index ++ {
child := propagator.container.Child(index)
if child == nil { continue }
@@ -308,7 +308,7 @@ func (propagator *Propagator) forChildren (callback func (child elements.Element
}
}
func (propagator *Propagator) forChildrenReverse (callback func (child elements.Element) bool) {
func (propagator *Propagator) forChildrenReverse (callback func (child tomo.Element) bool) {
for index := propagator.container.CountChildren() - 1; index > 0; index -- {
child := propagator.container.Child(index)
if child == nil { continue }
@@ -316,8 +316,8 @@ func (propagator *Propagator) forChildrenReverse (callback func (child elements.
}
}
func (propagator *Propagator) childAt (position image.Point) (child elements.Element) {
propagator.forChildren (func (current elements.Element) bool {
func (propagator *Propagator) childAt (position image.Point) (child tomo.Element) {
propagator.forChildren (func (current tomo.Element) bool {
if position.In(current.Bounds()) {
child = current
}
@@ -326,9 +326,9 @@ func (propagator *Propagator) childAt (position image.Point) (child elements.Ele
return
}
func (propagator *Propagator) forFocused (callback func (child elements.Focusable) bool) {
propagator.forChildren (func (child elements.Element) bool {
typedChild, focusable := child.(elements.Focusable)
func (propagator *Propagator) forFocused (callback func (child tomo.Focusable) bool) {
propagator.forChildren (func (child tomo.Element) bool {
typedChild, focusable := child.(tomo.Focusable)
if focusable && typedChild.Focused() {
if !callback(typedChild) { return false }
}
@@ -336,9 +336,9 @@ func (propagator *Propagator) forFocused (callback func (child elements.Focusabl
})
}
func (propagator *Propagator) forFocusable (callback func (child elements.Focusable) bool) {
propagator.forChildren (func (child elements.Element) bool {
typedChild, focusable := child.(elements.Focusable)
func (propagator *Propagator) forFocusable (callback func (child tomo.Focusable) bool) {
propagator.forChildren (func (child tomo.Element) bool {
typedChild, focusable := child.(tomo.Focusable)
if focusable {
if !callback(typedChild) { return false }
}
@@ -348,7 +348,7 @@ func (propagator *Propagator) forFocusable (callback func (child elements.Focusa
func (propagator *Propagator) firstFocused () int {
for index := 0; index < propagator.container.CountChildren(); index ++ {
child, focusable := propagator.container.Child(index).(elements.Focusable)
child, focusable := propagator.container.Child(index).(tomo.Focusable)
if focusable && child.Focused() {
return index
}

View File

@@ -1,6 +1,3 @@
// Package elements provides several standard interfaces that elements can
// fulfill in order to inform other elements of their capabilities and what
// events they are able to process. Sub-packages of this package provide
// pre-made standard elements, as well as tools that can be used to easily
// create more.
// Package elements provides standard elements that are commonly used in GUI
// applications.
package elements

View File

@@ -1,178 +0,0 @@
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/config"
// Element represents a basic on-screen object.
type Element interface {
// Bounds reports the element's bounding box. This must reflect the
// bounding last given to the element by DrawTo.
Bounds () image.Rectangle
// 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)
// SetParent sets the parent container of the element. This should only
// be called by the parent when the element is adopted. If parent is set
// to nil, it will mark itself as not having a parent. If this method is
// passed a non-nil value and the element already has a parent, it will
// panic.
SetParent (Parent)
// DrawTo gives the element a canvas to draw on, along with a bounding
// box to be used for laying out the element. This should only be called
// by the parent element. This is typically a region of the parent
// element's canvas.
DrawTo (canvas canvas.Canvas, bounds image.Rectangle, onDamage func (region image.Rectangle))
}
// Focusable 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 Focusable interface {
Element
// Focused returns whether or not this element or any of its children
// are currently focused.
Focused () bool
// Focus focuses this element, if its parent element grants the
// request.
Focus ()
// HandleFocus causes this element to mark itself as focused. If the
// element does not have children, it is disabled, or there are no more
// selectable children in the given direction, it should return false
// and do nothing. Otherwise, it should select itself and any children
// (if applicable) and return true.
HandleFocus (direction input.KeynavDirection) (accepted bool)
// HandleDeselection causes this element to mark itself and all of its
// children as unfocused.
HandleUnfocus ()
}
// KeyboardTarget represents an element that can receive keyboard input.
type KeyboardTarget interface {
Element
// HandleKeyDown is called when a key is pressed down or repeated 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 input.Key, modifiers input.Modifiers)
// HandleKeyUp is called when a key is released while this element has
// keyboard focus.
HandleKeyUp (key input.Key, modifiers input.Modifiers)
}
// MouseTarget represents an element that can receive mouse events.
type MouseTarget interface {
Element
// HandleMouseDown is called when a mouse button is pressed down on this
// element.
HandleMouseDown (x, y int, button input.Button)
// HandleMouseUp is called when a mouse button is released that was
// originally pressed down on this element.
HandleMouseUp (x, y int, button input.Button)
}
// MotionTarget represents an element that can receive mouse motion events.
type MotionTarget interface {
Element
// HandleMotion 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.
HandleMotion (x, y int)
}
// ScrollTarget represents an element that can receive mouse scroll events.
type ScrollTarget interface {
Element
// 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)
}
// Flexible represents an element who's preferred minimum height can change in
// response to its width.
type Flexible interface {
Element
// FlexibleHeightFor returns what the element's minimum height would be
// if resized to a specified width. This does not actually alter the
// state of the element in any way, but it may perform significant work,
// so it should be called sparingly.
//
// It is reccomended that parent containers check for this interface and
// take this method's value into account in order to support things like
// flow layouts and text wrapping, but it is not absolutely necessary.
// The element's MinimumSize method will still return the absolute
// minimum size that the element may be resized to.
//
// It is important to note that if a parent container checks for
// flexible chilren, it itself will likely need to be flexible.
FlexibleHeightFor (width int) int
}
// Scrollable represents an element that can be scrolled. It acts as a viewport
// through which its contents can be observed.
type Scrollable interface {
Element
// ScrollContentBounds returns the full content size of the element.
ScrollContentBounds () image.Rectangle
// ScrollViewportBounds returns the size and position of the element's
// viewport relative to ScrollBounds.
ScrollViewportBounds () image.Rectangle
// ScrollTo scrolls the viewport to the specified point relative to
// ScrollBounds.
ScrollTo (position image.Point)
// ScrollAxes returns the supported axes for scrolling.
ScrollAxes () (horizontal, vertical bool)
}
// Collapsible represents an element who's minimum width and height can be
// manually resized. Scrollable elements should implement this if possible.
type Collapsible interface {
Element
// Collapse collapses the element's minimum width and height. A value of
// zero for either means that the element's normal value is used.
Collapse (width, height int)
}
// Themeable represents an element that can modify its appearance to fit within
// a theme.
type Themeable interface {
Element
// SetTheme sets the element's theme to something fulfilling the
// theme.Theme interface.
SetTheme (theme.Theme)
}
// Configurable represents an element that can modify its behavior to fit within
// a set of configuration parameters.
type Configurable interface {
Element
// SetConfig sets the element's configuration to something fulfilling
// the config.Config interface.
SetConfig (config.Config)
}

View File

@@ -22,7 +22,7 @@ type AnalogClock struct {
// NewAnalogClock creates a new analog clock that displays the specified time.
func NewAnalogClock (newTime time.Time) (element *AnalogClock) {
element = &AnalogClock { }
element.theme.Case = theme.C("fun", "clock")
element.theme.Case = theme.C("tomo", "clock")
element.Core, element.core = core.NewCore(element, element.draw)
element.core.SetMinimumSize(64, 64)
return

View File

@@ -52,7 +52,7 @@ func NewPiano (low, high music.Octave) (element *Piano) {
keynavPressed: make(map[music.Note] bool),
}
element.theme.Case = theme.C("fun", "piano")
element.theme.Case = theme.C("tomo", "piano")
element.Core, element.core = core.NewCore (element, func () {
element.recalculate()
element.draw()

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
@@ -18,7 +18,7 @@ func NewIcon (id theme.Icon, size theme.IconSize) (element *Icon) {
id: id,
size: size,
}
element.theme.Case = theme.C("basic", "icon")
element.theme.Case = theme.C("tomo", "icon")
element.Core, element.core = core.NewCore(element, element.draw)
element.updateMinimumSize()
return

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/canvas"

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "golang.org/x/image/math/fixed"
import "git.tebibyte.media/sashakoshka/tomo/theme"
@@ -29,7 +29,7 @@ type Label struct {
// wrapped.
func NewLabel (text string, wrap bool) (element *Label) {
element = &Label { }
element.theme.Case = theme.C("basic", "label")
element.theme.Case = theme.C("tomo", "label")
element.Core, element.core = core.NewCore(element, element.handleResize)
element.SetWrap(wrap)
element.SetText(text)

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
// Numeric is a type constraint representing a number.
type Numeric interface {

View File

@@ -1,13 +1,13 @@
package basicElements
package elements
import "fmt"
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// List is an element that contains several objects that a user can select.
@@ -37,7 +37,7 @@ type List struct {
// NewList creates a new list element with the specified entries.
func NewList (entries ...ListEntry) (element *List) {
element = &List { selectedEntry: -1 }
element.theme.Case = theme.C("basic", "list")
element.theme.Case = theme.C("tomo", "list")
element.Core, element.core = core.NewCore(element, element.handleResize)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore (element.core, func () {
@@ -435,7 +435,7 @@ func (element *List) updateMinimumSize () {
}
func (element *List) scrollBoundsChange () {
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
if element.onScrollBoundsChange != nil {

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
@@ -26,7 +26,7 @@ func NewListEntry (text string, onSelect func ()) (entry ListEntry) {
text: text,
onSelect: onSelect,
}
entry.theme.Case = theme.C("basic", "listEntry")
entry.theme.Case = theme.C("tomo", "listEntry")
entry.drawer.SetText([]rune(text))
entry.updateBounds()
return

View File

@@ -1,53 +0,0 @@
package elements
// Parent represents a type capable of containing child elements.
type Parent interface {
// NotifyMinimumSizeChange notifies the container that a child element's
// minimum size has changed. This method is expected to be called by
// child elements when their minimum size changes.
NotifyMinimumSizeChange (child Element)
}
// FocusableParent represents a parent with keyboard navigation support.
type FocusableParent interface {
Parent
// RequestFocus notifies the parent that a child element is requesting
// keyboard focus. If the parent grants the request, the method will
// return true and the child element should behave as if a HandleFocus
// call was made.
RequestFocus (child Focusable) (granted bool)
// RequestFocusMotion notifies the parent that a child element wants the
// focus to be moved to the next focusable element.
RequestFocusNext (child Focusable)
// RequestFocusMotion notifies the parent that a child element wants the
// focus to be moved to the previous focusable element.
RequestFocusPrevious (child Focusable)
}
// FlexibleParent represents a parent that accounts for elements with
// flexible height.
type FlexibleParent interface {
Parent
// NotifyFlexibleHeightChange notifies the parent that the parameters
// affecting a child's flexible height have changed. This method is
// expected to be called by flexible child element when their content
// changes.
NotifyFlexibleHeightChange (child Flexible)
}
// ScrollableParent represents a parent that can change the scroll
// position of its child element(s).
type ScrollableParent interface {
Parent
// NotifyScrollBoundsChange notifies the parent that a child's scroll
// content bounds or viewport bounds have changed. This is expected to
// be called by child elements when they change their supported scroll
// axes, their scroll position (either autonomously or as a result of a
// call to ScrollTo()), or their content size.
NotifyScrollBoundsChange (child Scrollable)
}

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/theme"
@@ -19,7 +19,7 @@ type ProgressBar struct {
// level.
func NewProgressBar (progress float64) (element *ProgressBar) {
element = &ProgressBar { progress: progress }
element.theme.Case = theme.C("basic", "progressBar")
element.theme.Case = theme.C("tomo", "progressBar")
element.Core, element.core = core.NewCore(element, element.draw)
return
}

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/input"
@@ -45,9 +45,9 @@ func NewScrollBar (vertical bool) (element *ScrollBar) {
enabled: true,
}
if vertical {
element.theme.Case = theme.C("basic", "scrollBarHorizontal")
element.theme.Case = theme.C("tomo", "scrollBarHorizontal")
} else {
element.theme.Case = theme.C("basic", "scrollBarVertical")
element.theme.Case = theme.C("tomo", "scrollBarVertical")
}
element.Core, element.core = core.NewCore(element, element.handleResize)
element.updateMinimumSize()

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/input"
@@ -35,9 +35,9 @@ func NewSlider (value float64, vertical bool) (element *Slider) {
vertical: vertical,
}
if vertical {
element.theme.Case = theme.C("basic", "sliderVertical")
element.theme.Case = theme.C("tomo", "sliderVertical")
} else {
element.theme.Case = theme.C("basic", "sliderHorizontal")
element.theme.Case = theme.C("tomo", "sliderHorizontal")
}
element.Core, element.core = core.NewCore(element, element.draw)
element.FocusableCore,

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
@@ -19,7 +19,7 @@ type Spacer struct {
// will appear as a line.
func NewSpacer (line bool) (element *Spacer) {
element = &Spacer { line: line }
element.theme.Case = theme.C("basic", "spacer")
element.theme.Case = theme.C("tomo", "spacer")
element.Core, element.core = core.NewCore(element, element.draw)
element.updateMinimumSize()
return

View File

@@ -1,4 +1,4 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/input"
@@ -32,7 +32,7 @@ func NewSwitch (text string, on bool) (element *Switch) {
checked: on,
text: text,
}
element.theme.Case = theme.C("basic", "switch")
element.theme.Case = theme.C("tomo", "switch")
element.Core, element.core = core.NewCore(element, element.draw)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.core, element.redo)

View File

@@ -23,7 +23,7 @@ type Mouse struct {
// NewMouse creates a new mouse test element.
func NewMouse () (element *Mouse) {
element = &Mouse { c: theme.C("testing", "mouse") }
element = &Mouse { c: theme.C("tomo", "mouse") }
element.Core, element.core = core.NewCore(element, element.draw)
element.core.SetMinimumSize(32, 32)
return

View File

@@ -1,12 +1,12 @@
package basicElements
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/textmanip"
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
@@ -43,7 +43,7 @@ type TextBox struct {
// text.
func NewTextBox (placeholder, value string) (element *TextBox) {
element = &TextBox { }
element.theme.Case = theme.C("basic", "textBox")
element.theme.Case = theme.C("tomo", "textBox")
element.Core, element.core = core.NewCore(element, element.handleResize)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore (element.core, func () {
@@ -62,7 +62,7 @@ func NewTextBox (placeholder, value string) (element *TextBox) {
func (element *TextBox) handleResize () {
element.scrollToCursor()
element.draw()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@@ -197,7 +197,7 @@ func (element *TextBox) HandleKeyDown(key input.Key, modifiers input.Modifiers)
}
if (textChanged || scrollMemory != element.scroll) {
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@@ -294,7 +294,7 @@ func (element *TextBox) ScrollTo (position image.Point) {
if element.scroll > maxPosition { element.scroll = maxPosition }
element.redo()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}

View File

@@ -1,61 +0,0 @@
package elements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/data"
// 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.
type Window interface {
// Adopt sets the root element of the window. There can only be one of
// these at one time.
Adopt (Element)
// Child returns the root element of the window.
Child () Element
// SetTitle sets the title that appears on the window's title bar. This
// method might have no effect with some backends.
SetTitle (string)
// 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
// for some backends.
SetIcon (sizes []image.Image)
// NewModal creates a new modal dialog window.
NewModal (width, height int) (window Window, err error)
// Copy puts data into the clipboard.
Copy (data.Data)
// Paste requests the data currently in the clipboard. When the data is
// available, the callback is called with the clipboard data. If there
// was no data matching the requested mime type found, nil is passed to
// the callback instead.
Paste (callback func (data.Data, error), accept ...data.Mime)
// 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 ())
}
// MainWindow is a window capable of owning multiple sub-windows.
type MainWindow interface {
Window
// NewPanel creates a panel window that is semantically tied to this
// window. This is intended to be used for utility windows, tool bars,
// torn-off menus, etc.
NewPanel (width, height int) (window Window, err error)
}