/
gui.go
73 lines (63 loc) · 1.59 KB
/
gui.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
67
68
69
70
71
72
73
package SimplyP2P
import (
"github.com/andlabs/ui"
"time"
)
// SimplyGui is a simple graphic interface that show
// the current state and some buttons to change its value.
type SimplyGui struct {
window *ui.Window
}
// setupUI sets the gui elements and show then.
func (n *Node) setupUI() {
gui := SimplyGui{
window: ui.NewWindow("State Peer", 300, 200, false),
}
// Close current Node at exit
gui.window.OnClosing(func(*ui.Window) bool {
ui.Quit()
_ = n.Close()
return true
})
ui.OnShouldQuit(func() bool {
gui.window.Destroy()
_ = n.Close()
return true
})
box := ui.NewVerticalBox()
state := ui.NewLabel("")
// Set state change handler (change GUI label)
n.state.SetEvent(func(actualState bool) {
if actualState {
state.SetText("Actual: True")
} else {
state.SetText("Actual: False")
}
})
// Function that create a new change state button
addChangeStateButton := func(text string, newState bool) *ui.Button {
button := ui.NewButton(text)
button.OnClicked(func(*ui.Button) {
currentTime := time.Now()
if n.state.Update(newState, currentTime) {
n.peers.Broadcast(ChangeState{
State: newState,
Time: currentTime,
})
}
})
return button
}
// Add graphic elements to box
box.Append(state, false)
box.Append(addChangeStateButton("False", false), false)
box.Append(addChangeStateButton("True", true), false)
// Add box to the window and show the window
gui.window.SetChild(box)
gui.window.Show()
}
// newGui creates a new GUI linked to current node
// and starts it in a new goroutine.
func (n *Node) newGui() {
_ = ui.Main(n.setupUI)
}