-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguiconfig.go
122 lines (100 loc) · 2.31 KB
/
guiconfig.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
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
111
112
113
114
115
116
117
118
119
120
121
122
package dynamic_gui_config
import (
"log"
"os"
"github.com/andlabs/ui"
)
var (
// window configuration, set during Start
windowName string
windowWidth int
windowHeight int
window *ui.Window = nil
tab *ui.Tab = nil
closer *ControlCloseWrapper = nil
)
// perform a check on global variables, panic if check fails
func check() {
if window == nil || tab == nil || closer == nil {
panic("window pointer is nil, did you call Start()?")
}
}
func setup() {
ui.OnShouldQuit(func() bool {
window.Destroy()
window = nil
tab = nil
closer = nil
return true
})
window = ui.NewWindow(windowName, windowWidth, windowHeight, true)
tab = ui.NewTab()
closer = NewControlCloseWrapper(tab)
window.SetMargined(true)
window.SetChild(closer)
window.OnClosing(func(w *ui.Window) bool {
ui.Quit()
return true
})
}
func uithread(setupdone chan<- bool) {
if err := ui.Main(func() {
setup()
// signal setup done
setupdone <- true
}); err != nil {
log.Println(err)
os.Exit(1)
}
os.Exit(0)
}
// StartDefaults is an alias for Start("config", 640, 400)
func StartDefaults() {
Start("config", 640, 400)
}
// Start starts the configuration thread and sets up the window handles
// it returns when setup is done and the handles are initialized
func Start(windowname string, width, height int) {
windowName = windowname
windowWidth = width
windowHeight = height
done := make(chan bool)
// start the goroutine with ui.Main
go uithread(done)
// wait for setup task completion
<-done
}
// Register adds the given struct pointer to the graphical interface as a new tab
func Register(name string, config interface{}) error {
// check ui instances
check()
fields, err := MakeValueControl(config)
if err != nil {
return err
}
// add a new tab to the window and add controls based on struct field
NewTab(name, fields)
return nil
}
// NewTab creates a new tab in the configuration window
func NewTab(name string, handle ValueControl) {
ui.QueueMain(func() {
ctrl := handle.Create()
if ctrl != nil {
tab.Append(name, ctrl)
}
})
}
// Show displays the configuration window
func Show() {
check()
ui.QueueMain(window.Show)
}
// Hide hides the configuration window
func Hide() {
ui.QueueMain(window.Hide)
}
// Stop triggers the program to quit
func Stop() {
ui.QueueMain(ui.Quit)
}