Improved documentation (and added missing methods)

This commit is contained in:
Sasha Koshka 2023-04-20 01:37:06 -04:00
parent 580b7d2ad0
commit d4b9ffb046
5 changed files with 51 additions and 8 deletions

View File

@ -9,10 +9,10 @@ import "git.tebibyte.media/sashakoshka/tomo/default/theme"
// Space is a list of spacing configurations that can be passed to some // Space is a list of spacing configurations that can be passed to some
// containers. // containers.
type Space int; const ( type Space int; const (
SpaceNone = 0 SpaceNone Space = 0
SpacePadding = 1 SpacePadding Space = 1
SpaceMargin = 2 SpaceMargin Space = 2
SpaceBoth = SpacePadding | SpaceMargin SpaceBoth Space = SpacePadding | SpaceMargin
) )
// Includes returns whether a spacing value has been or'd with another spacing // Includes returns whether a spacing value has been or'd with another spacing
@ -62,6 +62,7 @@ func NewVBox (space Space, children ...tomo.Element) (element *Box) {
return return
} }
// Draw causes the element to draw to the specified destination canvas.
func (element *Box) Draw (destination canvas.Canvas) { func (element *Box) Draw (destination canvas.Canvas) {
rocks := make([]image.Rectangle, element.entity.CountChildren()) rocks := make([]image.Rectangle, element.entity.CountChildren())
for index := 0; index < element.entity.CountChildren(); index ++ { for index := 0; index < element.entity.CountChildren(); index ++ {
@ -74,6 +75,7 @@ func (element *Box) Draw (destination canvas.Canvas) {
} }
} }
// Layout causes this element to perform a layout operation.
func (element *Box) Layout () { func (element *Box) Layout () {
margin := element.theme.Margin(tomo.PatternBackground) margin := element.theme.Margin(tomo.PatternBackground)
padding := element.theme.Padding(tomo.PatternBackground) padding := element.theme.Padding(tomo.PatternBackground)
@ -118,10 +120,14 @@ func (element *Box) Layout () {
} }
} }
// AdoptExpand adds one or more elements to the box. These elements will be
// expanded to fill in empty space.
func (element *Box) AdoptExpand (children ...tomo.Element) { func (element *Box) AdoptExpand (children ...tomo.Element) {
element.adopt(true, children...) element.adopt(true, children...)
} }
// DrawBackground draws this element's background pattern to the specified
// destination canvas.
func (element *Box) DrawBackground (destination canvas.Canvas) { func (element *Box) DrawBackground (destination canvas.Canvas) {
element.entity.DrawBackground(destination) element.entity.DrawBackground(destination)
} }

View File

@ -14,10 +14,12 @@ type container struct {
minimumSize func () minimumSize func ()
} }
// Entity returns this element's entity.
func (container *container) Entity () tomo.Entity { func (container *container) Entity () tomo.Entity {
return container.entity return container.entity
} }
// Adopt adds one or more elements to the container.
func (container *container) Adopt (children ...tomo.Element) { func (container *container) Adopt (children ...tomo.Element) {
container.adopt(false, children...) container.adopt(false, children...)
} }
@ -36,6 +38,7 @@ func (container *container) adopt (expand bool, children ...tomo.Element) {
container.entity.InvalidateLayout() container.entity.InvalidateLayout()
} }
// Disown removes one or more elements from the container.
func (container *container) Disown (children ...tomo.Element) { func (container *container) Disown (children ...tomo.Element) {
for _, child := range children { for _, child := range children {
index := container.entity.IndexOf(child) index := container.entity.IndexOf(child)
@ -48,6 +51,7 @@ func (container *container) Disown (children ...tomo.Element) {
container.entity.InvalidateLayout() container.entity.InvalidateLayout()
} }
// DisownAll removes all elements from the container.
func (container *container) DisownAll () { func (container *container) DisownAll () {
func () { func () {
for index := 0; index < container.entity.CountChildren(); index ++ { for index := 0; index < container.entity.CountChildren(); index ++ {
@ -61,11 +65,13 @@ func (container *container) DisownAll () {
container.entity.InvalidateLayout() container.entity.InvalidateLayout()
} }
// Child returns the child at the specified index.
func (container *container) Child (index int) tomo.Element { func (container *container) Child (index int) tomo.Element {
if index < 0 || index >= container.entity.CountChildren() { return nil } if index < 0 || index >= container.entity.CountChildren() { return nil }
return container.entity.Child(index) return container.entity.Child(index)
} }
// CountChildren returns the amount of children in this container.
func (container *container) CountChildren () int { func (container *container) CountChildren () int {
return container.entity.CountChildren() return container.entity.CountChildren()
} }

View File

@ -11,6 +11,9 @@ type documentEntity interface {
tomo.ScrollableEntity tomo.ScrollableEntity
} }
// Document is a scrollable container capcable of laying out flexible child
// elements. Children can be added either inline (similar to an HTML/CSS inline
// element), or expanding (similar to an HTML/CSS block element).
type Document struct { type Document struct {
container container
entity documentEntity entity documentEntity
@ -23,6 +26,7 @@ type Document struct {
onScrollBoundsChange func () onScrollBoundsChange func ()
} }
// NewDocument creates a new document container.
func NewDocument (children ...tomo.Element) (element *Document) { func NewDocument (children ...tomo.Element) (element *Document) {
element = &Document { } element = &Document { }
element.theme.Case = tomo.C("tomo", "document") element.theme.Case = tomo.C("tomo", "document")
@ -34,6 +38,7 @@ func NewDocument (children ...tomo.Element) (element *Document) {
return return
} }
// Draw causes the element to draw to the specified destination canvas.
func (element *Document) Draw (destination canvas.Canvas) { func (element *Document) Draw (destination canvas.Canvas) {
rocks := make([]image.Rectangle, element.entity.CountChildren()) rocks := make([]image.Rectangle, element.entity.CountChildren())
for index := 0; index < element.entity.CountChildren(); index ++ { for index := 0; index < element.entity.CountChildren(); index ++ {
@ -46,6 +51,7 @@ func (element *Document) Draw (destination canvas.Canvas) {
} }
} }
// Layout causes this element to perform a layout operation.
func (element *Document) Layout () { func (element *Document) Layout () {
if element.scroll.Y > element.maxScrollHeight() { if element.scroll.Y > element.maxScrollHeight() {
element.scroll.Y = element.maxScrollHeight() element.scroll.Y = element.maxScrollHeight()
@ -112,10 +118,14 @@ func (element *Document) Layout () {
} }
} }
// Adopt adds one or more elements to the container, placing each on its own
// line.
func (element *Document) Adopt (children ...tomo.Element) { func (element *Document) Adopt (children ...tomo.Element) {
element.adopt(true, children...) element.adopt(true, children...)
} }
// AdoptInline adds one or more elements to the container, packing multiple
// elements onto the same line(s).
func (element *Document) AdoptInline (children ...tomo.Element) { func (element *Document) AdoptInline (children ...tomo.Element) {
element.adopt(false, children...) element.adopt(false, children...)
} }
@ -126,6 +136,8 @@ func (element *Document) HandleChildFlexibleHeightChange (child tomo.Flexible) {
element.entity.InvalidateLayout() element.entity.InvalidateLayout()
} }
// DrawBackground draws this element's background pattern to the specified
// destination canvas.
func (element *Document) DrawBackground (destination canvas.Canvas) { func (element *Document) DrawBackground (destination canvas.Canvas) {
element.entity.DrawBackground(destination) element.entity.DrawBackground(destination)
} }

View File

@ -7,11 +7,12 @@ import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/default/theme" import "git.tebibyte.media/sashakoshka/tomo/default/theme"
import "git.tebibyte.media/sashakoshka/tomo/default/config" import "git.tebibyte.media/sashakoshka/tomo/default/config"
// ScrollMode specifies which sides of a Scroll have scroll bars.
type ScrollMode int; const ( type ScrollMode int; const (
ScrollNeither ScrollMode = 0 ScrollNeither ScrollMode = 0
ScrollVertical = 1 ScrollVertical ScrollMode = 1
ScrollHorizontal = 2 ScrollHorizontal ScrollMode = 2
ScrollBoth = ScrollVertical | ScrollHorizontal ScrollBoth ScrollMode = ScrollVertical | ScrollHorizontal
) )
// Includes returns whether a scroll mode has been or'd with another scroll // Includes returns whether a scroll mode has been or'd with another scroll
@ -20,6 +21,8 @@ func (mode ScrollMode) Includes (sub ScrollMode) bool {
return (mode & sub) > 0 return (mode & sub) > 0
} }
// Scroll adds scroll bars to any scrollable element. It also captures scroll
// wheel input.
type Scroll struct { type Scroll struct {
entity tomo.ContainerEntity entity tomo.ContainerEntity
@ -31,6 +34,7 @@ type Scroll struct {
theme theme.Wrapped theme theme.Wrapped
} }
// NewScroll creates a new scroll element.
func NewScroll (mode ScrollMode, child tomo.Scrollable) (element *Scroll) { func NewScroll (mode ScrollMode, child tomo.Scrollable) (element *Scroll) {
element = &Scroll { } element = &Scroll { }
element.theme.Case = tomo.C("tomo", "scroll") element.theme.Case = tomo.C("tomo", "scroll")
@ -69,10 +73,12 @@ func NewScroll (mode ScrollMode, child tomo.Scrollable) (element *Scroll) {
return return
} }
// Entity returns this element's entity.
func (element *Scroll) Entity () tomo.Entity { func (element *Scroll) Entity () tomo.Entity {
return element.entity return element.entity
} }
// Draw causes the element to draw to the specified destination canvas.
func (element *Scroll) Draw (destination canvas.Canvas) { func (element *Scroll) Draw (destination canvas.Canvas) {
if element.horizontal != nil && element.vertical != nil { if element.horizontal != nil && element.vertical != nil {
bounds := element.entity.Bounds() bounds := element.entity.Bounds()
@ -85,6 +91,7 @@ func (element *Scroll) Draw (destination canvas.Canvas) {
} }
} }
// Layout causes this element to perform a layout operation.
func (element *Scroll) Layout () { func (element *Scroll) Layout () {
bounds := element.entity.Bounds() bounds := element.entity.Bounds()
child := bounds child := bounds
@ -125,10 +132,13 @@ func (element *Scroll) Layout () {
} }
} }
// DrawBackground draws this element's background pattern to the specified
// destination canvas.
func (element *Scroll) DrawBackground (destination canvas.Canvas) { func (element *Scroll) DrawBackground (destination canvas.Canvas) {
element.entity.DrawBackground(destination) element.entity.DrawBackground(destination)
} }
// Adopt sets this element's child. If nil is passed, any child is removed.
func (element *Scroll) Adopt (child tomo.Scrollable) { func (element *Scroll) Adopt (child tomo.Scrollable) {
if element.child != nil { if element.child != nil {
element.entity.Disown(element.entity.IndexOf(element.child)) element.entity.Disown(element.entity.IndexOf(element.child))
@ -144,6 +154,12 @@ func (element *Scroll) Adopt (child tomo.Scrollable) {
element.entity.InvalidateLayout() element.entity.InvalidateLayout()
} }
// Child returns this element's child. If there is no child, this method will
// return nil.
func (element *Scroll) Child () tomo.Scrollable {
return element.child
}
func (element *Scroll) HandleChildMinimumSizeChange (tomo.Element) { func (element *Scroll) HandleChildMinimumSizeChange (tomo.Element) {
element.updateMinimumSize() element.updateMinimumSize()
element.entity.Invalidate() element.entity.Invalidate()
@ -172,6 +188,7 @@ func (element *Scroll) HandleScroll (
element.scrollChildBy(int(deltaX), int(deltaY)) element.scrollChildBy(int(deltaX), int(deltaY))
} }
// SetTheme sets the element's theme.
func (element *Scroll) SetTheme (theme tomo.Theme) { func (element *Scroll) SetTheme (theme tomo.Theme) {
if theme == element.theme.Theme { return } if theme == element.theme.Theme { return }
element.theme.Theme = theme element.theme.Theme = theme
@ -180,6 +197,7 @@ func (element *Scroll) SetTheme (theme tomo.Theme) {
element.entity.InvalidateLayout() element.entity.InvalidateLayout()
} }
// SetConfig sets the element's configuration.
func (element *Scroll) SetConfig (config tomo.Config) { func (element *Scroll) SetConfig (config tomo.Config) {
element.config.Config = config element.config.Config = config
} }

View File

@ -37,6 +37,7 @@ type ScrollBar struct {
onScroll func (viewport image.Point) onScroll func (viewport image.Point)
} }
// NewVScrollBar creates a new vertical scroll bar.
func NewVScrollBar () (element *ScrollBar) { func NewVScrollBar () (element *ScrollBar) {
element = &ScrollBar { element = &ScrollBar {
vertical: true, vertical: true,