-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.go
More file actions
110 lines (94 loc) · 2.05 KB
/
Copy pathcontainer.go
File metadata and controls
110 lines (94 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package fasttui
import (
"slices"
"sync"
)
const defaultContainerChildrenCap = 8
type Container struct {
mu sync.RWMutex
children []Component
}
func NewContainer() *Container {
return &Container{
children: make([]Component, 0, defaultContainerChildrenCap),
}
}
func (c *Container) AddChild(component Component) {
c.mu.Lock()
defer c.mu.Unlock()
c.children = append(c.children, component)
}
func (c *Container) RemoveChild(component Component) {
c.mu.Lock()
defer c.mu.Unlock()
for i, child := range c.children {
if child == component {
c.children = slices.Delete(c.children, i, i+1)
return
}
}
}
func (c *Container) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
c.children = nil
}
func (c *Container) Invalidate() {
for _, child := range c.childrenSnapshot() {
child.Invalidate()
}
}
func (c *Container) Render(width int) []string {
snapshot := c.childrenSnapshot()
if len(snapshot) == 0 {
return nil
}
lines := make([]string, 0, len(snapshot)*2)
for _, child := range snapshot {
lines = append(lines, child.Render(width)...)
}
return lines
}
func (c *Container) HandleInput(data string) {}
func (c *Container) WantsKeyRelease() bool {
return false
}
func (c *Container) GetChildren() []Component {
c.mu.RLock()
defer c.mu.RUnlock()
if len(c.children) == 0 {
return nil
}
out := make([]Component, len(c.children))
copy(out, c.children)
return out
}
func (c *Container) RemoveChildAt(index int) {
c.mu.Lock()
defer c.mu.Unlock()
if index >= 0 && index < len(c.children) {
c.children = slices.Delete(c.children, index, index+1)
}
}
func (c *Container) InsertChildAt(index int, component Component) {
c.mu.Lock()
defer c.mu.Unlock()
if index < 0 {
index = 0
}
if index >= len(c.children) {
c.children = append(c.children, component)
return
}
c.children = slices.Insert(c.children, index, component)
}
func (c *Container) childrenSnapshot() []Component {
c.mu.RLock()
defer c.mu.RUnlock()
if len(c.children) == 0 {
return nil
}
out := make([]Component, len(c.children))
copy(out, c.children)
return out
}