summary refs log tree commit diff stats
path: root/lib/ui/grid.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ui/grid.go')
-rw-r--r--lib/ui/grid.go21
1 files changed, 20 insertions, 1 deletions
diff --git a/lib/ui/grid.go b/lib/ui/grid.go
index 3fc7f04..f36a16a 100644
--- a/lib/ui/grid.go
+++ b/lib/ui/grid.go
@@ -3,6 +3,7 @@ package ui
 import (
 	"fmt"
 	"math"
+	"sync"
 	"sync/atomic"
 )
 
@@ -12,8 +13,11 @@ type Grid struct {
 	rowLayout    []gridLayout
 	columns      []GridSpec
 	columnLayout []gridLayout
-	cells        []*GridCell
 	invalid      bool
+
+	// Protected by mutex
+	cells []*GridCell
+	mutex sync.RWMutex
 }
 
 const (
@@ -73,6 +77,9 @@ func (grid *Grid) Columns(spec []GridSpec) *Grid {
 }
 
 func (grid *Grid) Children() []Drawable {
+	grid.mutex.RLock()
+	defer grid.mutex.RUnlock()
+
 	children := make([]Drawable, len(grid.cells))
 	for i, cell := range grid.cells {
 		children[i] = cell.Content
@@ -85,6 +92,10 @@ func (grid *Grid) Draw(ctx *Context) {
 	if invalid {
 		grid.reflow(ctx)
 	}
+
+	grid.mutex.RLock()
+	defer grid.mutex.RUnlock()
+
 	for _, cell := range grid.cells {
 		cellInvalid := cell.invalid.Load().(bool)
 		if !cellInvalid && !invalid {
@@ -148,9 +159,11 @@ func (grid *Grid) invalidateLayout() {
 
 func (grid *Grid) Invalidate() {
 	grid.invalidateLayout()
+	grid.mutex.RLock()
 	for _, cell := range grid.cells {
 		cell.Content.Invalidate()
 	}
+	grid.mutex.RUnlock()
 }
 
 func (grid *Grid) AddChild(content Drawable) *GridCell {
@@ -159,7 +172,9 @@ func (grid *Grid) AddChild(content Drawable) *GridCell {
 		ColSpan: 1,
 		Content: content,
 	}
+	grid.mutex.Lock()
 	grid.cells = append(grid.cells, cell)
+	grid.mutex.Unlock()
 	cell.Content.OnInvalidate(grid.cellInvalidated)
 	cell.invalid.Store(true)
 	grid.invalidateLayout()
@@ -167,23 +182,27 @@ func (grid *Grid) AddChild(content Drawable) *GridCell {
 }
 
 func (grid *Grid) RemoveChild(cell *GridCell) {
+	grid.mutex.Lock()
 	for i, _cell := range grid.cells {
 		if _cell == cell {
 			grid.cells = append(grid.cells[:i], grid.cells[i+1:]...)
 			break
 		}
 	}
+	grid.mutex.Unlock()
 	grid.invalidateLayout()
 }
 
 func (grid *Grid) cellInvalidated(drawable Drawable) {
 	var cell *GridCell
+	grid.mutex.RLock()
 	for _, cell = range grid.cells {
 		if cell.Content == drawable {
 			break
 		}
 		cell = nil
 	}
+	grid.mutex.RUnlock()
 	if cell == nil {
 		panic(fmt.Errorf("Attempted to invalidate unknown cell"))
 	}
mp;id=6030d7e2e56d445ca67c6a0e8c9cf33e46bc673c'>6030d7e2 ^
80b6f47e ^
57628c0e ^
33e7c3a7 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
33e7c3a7 ^
6030d7e2 ^


dd9ba09a ^

1639687b ^
6030d7e2 ^

9d27e966 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
6030d7e2 ^
9d27e966 ^
03d50cc8 ^
9d27e966 ^
ee9a9237 ^
dd9ba09a ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
9d27e966 ^
ee9a9237 ^
6030d7e2 ^

ee9a9237 ^
6030d7e2 ^
ee9a9237 ^
6030d7e2 ^
03d50cc8 ^
6030d7e2 ^
03d50cc8 ^
ee9a9237 ^
6030d7e2 ^

33e7c3a7 ^
ee9a9237 ^
6030d7e2 ^


57628c0e ^


e0ffdcd1 ^

57628c0e ^
6030d7e2 ^
9b16f190 ^
6030d7e2 ^

4224ec81 ^
e0ffdcd1 ^
03d50cc8 ^
9b16f190 ^
15ae0717 ^
ee9a9237 ^
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