forked from asticode/go-astilectron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_pool.go
66 lines (60 loc) · 1.17 KB
/
display_pool.go
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
package astilectron
import "sync"
// displayPool represents a display pool
type displayPool struct {
d map[int64]*Display
m *sync.Mutex
}
// newDisplayPool creates a new display pool
func newDisplayPool() *displayPool {
return &displayPool{
d: make(map[int64]*Display),
m: &sync.Mutex{},
}
}
// all returns all the displays
func (p *displayPool) all() (ds []*Display) {
p.m.Lock()
defer p.m.Unlock()
ds = []*Display{}
for _, d := range p.d {
ds = append(ds, d)
}
return
}
// primary returns the primary display
// It defaults to the last display
func (p *displayPool) primary() (d *Display) {
p.m.Lock()
defer p.m.Unlock()
for _, d = range p.d {
if d.primary {
return
}
}
return
}
// update updates the pool based on event displays
func (p *displayPool) update(e *EventDisplays) {
p.m.Lock()
defer p.m.Unlock()
var ids = make(map[int64]bool)
for _, o := range e.All {
ids[*o.ID] = true
var primary bool
if *o.ID == *e.Primary.ID {
primary = true
}
if d, ok := p.d[*o.ID]; ok {
d.primary = primary
*d.o = *o
} else {
p.d[*o.ID] = newDisplay(o, primary)
}
}
for id := range p.d {
if _, ok := ids[id]; !ok {
delete(p.d, id)
}
}
}