2019-04-10 00:35:44 +00:00
|
|
|
package ui
|
|
|
|
|
2019-12-29 07:56:00 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2019-04-10 00:35:44 +00:00
|
|
|
|
2020-06-04 07:50:06 +00:00
|
|
|
// PrintWidgetTree prints a widget tree to console.
|
|
|
|
func PrintWidgetTree(root Widget) {
|
|
|
|
fmt.Printf("--- Widget Tree of %s ---\n", root)
|
|
|
|
for _, row := range WidgetTree(root) {
|
|
|
|
fmt.Println(row)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
// WidgetTree returns a string representing the tree of widgets starting
|
|
|
|
// at a given widget.
|
|
|
|
func WidgetTree(root Widget) []string {
|
|
|
|
var crawl func(int, Widget) []string
|
2019-12-29 07:56:00 +00:00
|
|
|
|
2019-04-10 00:35:44 +00:00
|
|
|
crawl = func(depth int, node Widget) []string {
|
|
|
|
var (
|
2019-12-29 07:56:00 +00:00
|
|
|
prefix = strings.Repeat(" ", depth)
|
|
|
|
size = node.Size()
|
|
|
|
width = size.W
|
|
|
|
height = size.H
|
|
|
|
fixedSize = node.FixedSize()
|
|
|
|
|
|
|
|
lines = []string{
|
|
|
|
fmt.Sprintf("%s%s P:%s S:%dx%d (fixedSize: %+v)",
|
|
|
|
prefix, node.ID(), node.Point(), width, height, fixedSize,
|
|
|
|
),
|
|
|
|
}
|
2019-04-10 00:35:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, child := range node.Children() {
|
|
|
|
lines = append(lines, crawl(depth+1, child)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
|
|
|
return crawl(0, root)
|
|
|
|
}
|