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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package gui
import (
"os"
"go.wit.com/log"
"go.wit.com/widget"
)
// TODO: make a fake 'plugin' channel of communication to andlabs for mswindows
// Windows doesn't support plugins. How can I keep andlabs and only compile it on windows?
// https://forum.heroiclabs.com/t/setting-up-goland-to-compile-plugins-on-windows/594/5
// import toolkit "go.wit.com/gui/toolkit/andlabs"
const Xaxis = 0 // stack things horizontally
const Yaxis = 1 // stack things vertically
func init() {
log.Log(INFO, "init() has been run")
me.counter = 0
// me.prefix = "wit"
// Populates the top of the binary tree
me.rootNode = addNode()
me.rootNode.progname = "guiBinaryTree"
me.rootNode.WidgetType = widget.Root
me.rootNode.hidden = false // always send the rootNode to the toolkits
// used to pass debugging flags to the toolkit plugins
me.flag = me.rootNode.newNode("flag", 0)
me.flag.WidgetType = widget.Flag
me.flag = me.rootNode.newNode("stdout", 0)
me.flag.WidgetType = widget.Stdout
me.guiChan = make(chan widget.Action, 1)
go watchCallback()
}
// lookup the widget by the id sent from the toolkit
func (n *Node) findId(i int) *Node {
if n == nil {
return nil
}
if n.id == i {
return n
}
for _, child := range n.children {
newN := child.findId(i)
if newN != nil {
return newN
}
}
return nil
}
func pluginCounter(a *widget.Action) {
var found bool = false
for _, aplug := range allPlugins {
if a.ProgName == aplug.name {
aplug.count += 1
found = true
}
}
if !found {
// TODO: fix this by making seperate channels for each plugin?
log.Log(WARN, "ListToolkits() got event from unidentified plugin")
}
}
func toolkitPanic(pname string) {
log.Log(WARN, "toolkitPanic() in", pname)
log.Log(WARN, "toolkitPanic() unload toolkit plugin here", pname)
me.rootNode.ListToolkits()
for _, aplug := range allPlugins {
if aplug.name == pname {
log.Log(WARN, "toolkitPanic() FOUND PLUGIN =", aplug.name)
log.Log(WARN, "toolkitPanic() unload here aplug.dead =", aplug.dead)
log.Log(WARN, "toolkitPanic() TODO: stop talking to plugin:", pname)
aplug.dead = true
log.Log(WARN, "toolkitPanic() unload here aplug.dead =", aplug.dead)
// me.rootNode.CloseToolkit(aplug.name)
// panic("panic trapped!")
// log.Sleep(.5)
// me.rootNode.LoadToolkit("gocui")
}
}
// log.UnsetTmp()
// StandardExit()
log.Log(WARN, "toolkitPanic() attempt to load nocui")
me.rootNode.LoadToolkit("nocui")
}
func watchCallback() {
log.Log(INFO, "guiChan() START")
for {
log.Log(CHANGE, "guiChan() select restarted")
select {
case a := <-me.guiChan:
pluginCounter(&a)
if a.ActionType == widget.ToolkitPanic {
toolkitPanic(a.ProgName)
break
}
// 99.9% of events are just widget changes
n := me.rootNode.findId(a.WidgetId)
if n != nil {
log.Log(INFO, "guiChan() FOUND widget id =", n.id, n.progname)
n.gotUserEvent(a)
break
}
// if not a widget change, something more bizare
if a.ActionType == widget.UserQuit {
log.Log(WARN, "guiChan() User sent Quit()")
log.Exit("wit/gui toolkit.UserQuit")
break
}
if a.ActionType == widget.ToolkitLoad {
newPlug := widget.GetString(a.Value)
log.Log(WARN, "Attempt to load a new toolkit", newPlug, "here")
me.rootNode.LoadToolkit(newPlug)
}
if a.ActionType == widget.EnableDebug {
log.Log(WARN, "guiChan() Enable Debugging Window")
log.Log(WARN, "guiChan() TODO: not implemented")
log.Log(WARN, "guiChan() Listing Toolkits:")
PLUG.SetBool(true)
me.rootNode.ListToolkits()
me.rootNode.ListChildren(true)
/*
for i, aplug := range allPlugins {
log.Log("plug =", i, aplug.name)
if aplug.name == "andlabs" {
log.Log("Found plug =", i, aplug.name)
//closePlugin(aplug)
allPlugins = allPlugins[1:]
}
}
*/
break
}
log.Log(WARN, "guiChan() Action could not be found or handled", a.ActionType, a)
}
}
}
// stores the value returned then
// spawns the user defined custom routine
// hopefully everything is designed smart enough
// that it doesn't matter what happens outside of here
// TODO: implement throttling someday
func (n *Node) gotUserEvent(a widget.Action) {
log.Log(CHANGE, "gotUserEvent() received event node =", n.id, n.progname, a.Value)
switch n.WidgetType {
case widget.Dropdown:
// n.checked = a.State.Checked // TODO: do this and/or time to switch to protobuf
n.currentS = a.State.CurrentS
case widget.Combobox:
// n.checked = a.State.Checked // TODO: do this and/or time to switch to protobuf
n.currentS = a.State.CurrentS
case widget.Checkbox:
// n.checked = a.State.Checked // TODO: do this and/or time to switch to protobuf
n.checked = a.State.Checked
default:
}
n.currentS = a.State.CurrentS
log.Log(CHANGE, "gotUserEvent() n.Bool() =", n.Bool(), "n.String() =", n.String())
if n.Custom == nil {
log.Log(CHANGE, "a Custom() function was not set for this widget")
return
}
go n.Custom()
}
// There should only be one of these per application
// This is due to restrictions by being cross platform
// some toolkit's on some operating systems don't support more than one
// Keep things simple. Do the default expected thing whenever possible
func New() *Node {
return me.rootNode
}
// try to load andlabs, if that doesn't work, fall back to the console
func (n *Node) Default() *Node {
if argGui.GuiPlugin != "" {
log.Log(WARN, "New.Default() try toolkit =", argGui.GuiPlugin)
return n.LoadToolkit(argGui.GuiPlugin)
}
// if DISPLAY isn't set, return since gtk can't load
// TODO: figure out how to check what to do in macos and mswindows
if os.Getenv("DISPLAY") == "" {
if n.LoadToolkit("gocui") == nil {
log.Log(WARN, "New() failed to load gocui")
}
return n
}
if n.LoadToolkit("andlabs") != nil {
return n
}
n.LoadToolkit("gocui")
return n
}
// The window is destroyed but the application does not quit
func (n *Node) StandardClose() {
log.Log(INFO, "wit/gui Standard Window Close. name =", n.progname)
log.Log(INFO, "wit/gui Standard Window Close. n.Custom exit =", n.Custom)
}
// The window is destroyed and the application exits
// TODO: properly exit the plugin since Quit() doesn't do it
func StandardExit() {
log.Log(INFO, "wit/gui Standard Window Exit. running os.Exit()")
log.Log(INFO, "StandardExit() attempt to exit each toolkit plugin")
for i, plug := range allPlugins {
log.Log(INFO, "NewButton()", i, plug)
}
log.Exit(0)
}
// The window is destroyed and the application exits
// TODO: properly exit the plugin since Quit() doesn't do it
func (n *Node) StandardExit() {
log.Log(INFO, "wit/gui Standard Window Exit. running os.Exit()")
log.Log(INFO, "StandardExit() attempt to exit each toolkit plugin")
for i, plug := range allPlugins {
log.Log(INFO, "NewButton()", i, plug)
}
log.Exit(0)
}
|