User interface toolkit for Go with support for SDL2 and HTML Canvas render targets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

45 lines
924 B

  1. package ui
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // PrintWidgetTree prints a widget tree to console.
  7. func PrintWidgetTree(root Widget) {
  8. fmt.Printf("--- Widget Tree of %s ---\n", root)
  9. for _, row := range WidgetTree(root) {
  10. fmt.Println(row)
  11. }
  12. }
  13. // WidgetTree returns a string representing the tree of widgets starting
  14. // at a given widget.
  15. func WidgetTree(root Widget) []string {
  16. var crawl func(int, Widget) []string
  17. crawl = func(depth int, node Widget) []string {
  18. var (
  19. prefix = strings.Repeat(" ", depth)
  20. size = node.Size()
  21. width = size.W
  22. height = size.H
  23. fixedSize = node.FixedSize()
  24. lines = []string{
  25. fmt.Sprintf("%s%s P:%s S:%dx%d (fixedSize: %+v)",
  26. prefix, node.ID(), node.Point(), width, height, fixedSize,
  27. ),
  28. }
  29. )
  30. for _, child := range node.Children() {
  31. lines = append(lines, crawl(depth+1, child)...)
  32. }
  33. return lines
  34. }
  35. return crawl(0, root)
  36. }