tree_node.go 876 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package ultraviolet
  2. import (
  3. "sync"
  4. )
  5. type NodeList []*Node
  6. type NodeMap map[string]*Node
  7. type NodeIds map[string]int
  8. type Node struct {
  9. mutex sync.Mutex
  10. Component Component
  11. Children NodeList
  12. }
  13. func NewNode(component Component) *Node {
  14. return &Node{
  15. Children: NodeList{},
  16. Component: component,
  17. }
  18. }
  19. func (this *Node) Build(node *Node) {
  20. this.mutex.Lock()
  21. defer this.mutex.Unlock()
  22. this.Component.Clean()
  23. this.Children = NodeList{node}
  24. for _, child := range this.Children {
  25. child.update(this.Component, true)
  26. }
  27. }
  28. func (this *Node) Update() {
  29. this.mutex.Lock()
  30. defer this.mutex.Unlock()
  31. this.update(nil, false)
  32. }
  33. func (this *Node) update(parent Component, force bool) {
  34. if this.Component.Invalidate() || force {
  35. this.Component.Build(parent)
  36. force = true
  37. }
  38. for _, item := range this.Children {
  39. item.update(this.Component, force)
  40. }
  41. }