Updated the examples
This commit is contained in:
parent
14080b1f88
commit
7cdc5868e5
@ -54,9 +54,16 @@ func NewHBox (space Space, children ...tomo.Element) (element *Box) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewHBox creates a new vertical box.
|
// NewHBox creates a new vertical box.
|
||||||
func NewVBox (space Space) (element *Box) {
|
func NewVBox (space Space, children ...tomo.Element) (element *Box) {
|
||||||
element = NewHBox(space)
|
element = &Box {
|
||||||
element.vertical = true
|
padding: space.Includes(SpacePadding),
|
||||||
|
margin: space.Includes(SpaceMargin),
|
||||||
|
vertical: true,
|
||||||
|
}
|
||||||
|
element.scratch = make(map[tomo.Element] scratchEntry)
|
||||||
|
element.theme.Case = tomo.C("tomo", "box")
|
||||||
|
element.entity = tomo.NewEntity(element).(tomo.ContainerEntity)
|
||||||
|
element.Adopt(children...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
104
elements/containers/common.go
Normal file
104
elements/containers/common.go
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package containers
|
||||||
|
|
||||||
|
import "image"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
||||||
|
|
||||||
|
type childManager struct {
|
||||||
|
onChange func ()
|
||||||
|
children []tomo.LayoutEntry
|
||||||
|
parent tomo.Parent
|
||||||
|
theme theme.Wrapped
|
||||||
|
config config.Wrapped
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 container's layout.
|
||||||
|
func (manager *childManager) Adopt (child tomo.Element, expand bool) {
|
||||||
|
if child0, ok := child.(tomo.Themeable); ok {
|
||||||
|
child0.SetTheme(manager.theme.Theme)
|
||||||
|
}
|
||||||
|
if child0, ok := child.(tomo.Configurable); ok {
|
||||||
|
child0.SetConfig(manager.config.Config)
|
||||||
|
}
|
||||||
|
child.SetParent(manager.parent)
|
||||||
|
|
||||||
|
manager.children = append (manager.children, tomo.LayoutEntry {
|
||||||
|
Element: child,
|
||||||
|
Expand: expand,
|
||||||
|
})
|
||||||
|
|
||||||
|
manager.onChange()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Disown removes the given child from the container if it is contained within
|
||||||
|
// it.
|
||||||
|
func (manager *childManager) Disown (child tomo.Element) {
|
||||||
|
for index, entry := range manager.children {
|
||||||
|
if entry.Element == child {
|
||||||
|
manager.clearChildEventHandlers(entry.Element)
|
||||||
|
manager.children = append (
|
||||||
|
manager.children[:index],
|
||||||
|
manager.children[index + 1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
manager.onChange()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DisownAll removes all child elements from the container at once.
|
||||||
|
func (manager *childManager) DisownAll () {
|
||||||
|
for _, entry := range manager.children {
|
||||||
|
manager.clearChildEventHandlers(entry.Element)
|
||||||
|
}
|
||||||
|
manager.children = nil
|
||||||
|
|
||||||
|
manager.onChange()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Children returns a slice containing this element's children.
|
||||||
|
func (manager *childManager) Children () (children []tomo.Element) {
|
||||||
|
children = make([]tomo.Element, len(manager.children))
|
||||||
|
for index, entry := range manager.children {
|
||||||
|
children[index] = entry.Element
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// CountChildren returns the amount of children contained within this element.
|
||||||
|
func (manager *childManager) CountChildren () (count int) {
|
||||||
|
return len(manager.children)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Child returns the child at the specified index. If the index is out of
|
||||||
|
// bounds, this method will return nil.
|
||||||
|
func (manager *childManager) Child (index int) (child tomo.Element) {
|
||||||
|
if index < 0 || index > len(manager.children) { return }
|
||||||
|
return manager.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 (manager *childManager) ChildAt (point image.Point) (child tomo.Element) {
|
||||||
|
for _, entry := range manager.children {
|
||||||
|
if point.In(entry.Bounds) {
|
||||||
|
child = entry.Element
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (manager *childManager) clearChildEventHandlers (child tomo.Element) {
|
||||||
|
child.DrawTo(nil, image.Rectangle { }, nil)
|
||||||
|
child.SetParent(nil)
|
||||||
|
|
||||||
|
if child, ok := child.(tomo.Focusable); ok {
|
||||||
|
if child.Focused() {
|
||||||
|
child.HandleUnfocus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -129,5 +129,7 @@ func imageWindow (parent tomo.Window, image image.Image) {
|
|||||||
container.AdoptExpand(elements.NewImage(image))
|
container.AdoptExpand(elements.NewImage(image))
|
||||||
container.Adopt(closeButton)
|
container.Adopt(closeButton)
|
||||||
window.Adopt(container)
|
window.Adopt(container)
|
||||||
|
|
||||||
|
closeButton.Focus()
|
||||||
window.Show()
|
window.Show()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,44 +22,40 @@ func run () {
|
|||||||
if err != nil { panic(err.Error()); return }
|
if err != nil { panic(err.Error()); return }
|
||||||
|
|
||||||
document := elements.NewDocument()
|
document := elements.NewDocument()
|
||||||
|
document.Adopt (
|
||||||
document.Adopt (elements.NewLabel (
|
elements.NewLabelWrapped (
|
||||||
"A document container is a vertically stacked container " +
|
"A document container is a vertically stacked container " +
|
||||||
"capable of properly laying out flexible elements such as " +
|
"capable of properly laying out flexible elements such as " +
|
||||||
"text-wrapped labels. You can also include normal elements " +
|
"text-wrapped labels. You can also include normal elements " +
|
||||||
"like:", true), true)
|
"like:"),
|
||||||
document.Adopt (elements.NewButton (
|
elements.NewButton("Buttons,"),
|
||||||
"Buttons,"), true)
|
elements.NewCheckbox("Checkboxes,", true),
|
||||||
document.Adopt (elements.NewCheckbox (
|
elements.NewTextBox("", "And text boxes."),
|
||||||
"Checkboxes,", true), true)
|
elements.NewLine(),
|
||||||
document.Adopt(elements.NewTextBox("", "And text boxes."), true)
|
elements.NewLabelWrapped (
|
||||||
document.Adopt (elements.NewSpacer(true), true)
|
"Document containers are meant to be placed inside of a " +
|
||||||
document.Adopt (elements.NewLabel (
|
"ScrollContainer, like this one."),
|
||||||
"Document containers are meant to be placed inside of a " +
|
elements.NewLabelWrapped (
|
||||||
"ScrollContainer, like this one.", true), true)
|
"You could use document containers to do things like display various " +
|
||||||
document.Adopt (elements.NewLabel (
|
"forms of hypertext (like HTML, gemtext, markdown, etc.), " +
|
||||||
"You could use document containers to do things like display various " +
|
"lay out a settings menu with descriptive label text between " +
|
||||||
"forms of hypertext (like HTML, gemtext, markdown, etc.), " +
|
"control groups like in iOS, or list comment or chat histories."),
|
||||||
"lay out a settings menu with descriptive label text between " +
|
elements.NewImage(logo),
|
||||||
"control groups like in iOS, or list comment or chat histories.",
|
elements.NewLabelWrapped (
|
||||||
true), true)
|
"You can also choose whether each element is on its own line " +
|
||||||
document.Adopt(elements.NewImage(logo), true)
|
"(sort of like an HTML/CSS block element) or on a line with " +
|
||||||
document.Adopt (elements.NewLabel (
|
"other adjacent elements (like an HTML/CSS inline element)."))
|
||||||
"You can also choose whether each element is on its own line " +
|
document.AdoptInline (
|
||||||
"(sort of like an HTML/CSS block element) or on a line with " +
|
elements.NewButton("Just"),
|
||||||
"other adjacent elements (like an HTML/CSS inline element).",
|
elements.NewButton("like"),
|
||||||
true), true)
|
elements.NewButton("this."))
|
||||||
document.Adopt(elements.NewButton("Just"), false)
|
document.Adopt (elements.NewLabelWrapped (
|
||||||
document.Adopt(elements.NewButton("like"), false)
|
"Oh, you're a switch? Then name all of these switches:"))
|
||||||
document.Adopt(elements.NewButton("this."), false)
|
|
||||||
document.Adopt (elements.NewLabel (
|
|
||||||
"Oh, you're a switch? Then name all of these switches:",
|
|
||||||
true), true)
|
|
||||||
for i := 0; i < 30; i ++ {
|
for i := 0; i < 30; i ++ {
|
||||||
document.Adopt(elements.NewSwitch("", false), false)
|
document.AdoptInline(elements.NewSwitch("", false))
|
||||||
}
|
}
|
||||||
|
|
||||||
window.Adopt(elements.NewScroll(document, false, true))
|
window.Adopt(elements.NewScroll(elements.ScrollVertical, document))
|
||||||
window.OnClose(tomo.Stop)
|
window.OnClose(tomo.Stop)
|
||||||
window.Show()
|
window.Show()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,15 +12,15 @@ func main () {
|
|||||||
func run () {
|
func run () {
|
||||||
window, _ := tomo.NewWindow(tomo.Bounds(0, 0, 192, 192))
|
window, _ := tomo.NewWindow(tomo.Bounds(0, 0, 192, 192))
|
||||||
window.SetTitle("adventure")
|
window.SetTitle("adventure")
|
||||||
container := elements.NewVBox(true, true)
|
container := elements.NewVBox(elements.SpaceBoth)
|
||||||
window.Adopt(container)
|
window.Adopt(container)
|
||||||
|
|
||||||
var world flow.Flow
|
var world flow.Flow
|
||||||
world.Transition = container.DisownAll
|
world.Transition = container.DisownAll
|
||||||
world.Stages = map [string] func () {
|
world.Stages = map [string] func () {
|
||||||
"start": func () {
|
"start": func () {
|
||||||
label := elements.NewLabel (
|
label := elements.NewLabelWrapped (
|
||||||
"you are standing next to a river.", true)
|
"you are standing next to a river.")
|
||||||
|
|
||||||
button0 := elements.NewButton("go in the river")
|
button0 := elements.NewButton("go in the river")
|
||||||
button0.OnClick(world.SwitchFunc("wet"))
|
button0.OnClick(world.SwitchFunc("wet"))
|
||||||