summaryrefslogtreecommitdiff
path: root/main.go
blob: 3254d0ae7f33e415a0c45cf4d361f0634fd7b462 (plain)
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
package gui

import (
	"os"
	"time"
	"go.wit.com/gui/toolkit"
)

// 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("init() has been run")

	me.counter = 0
	me.prefix = "wit"

	// Populates the top of the binary tree
	me.rootNode = addNode("guiBinaryTree")
	me.rootNode.WidgetType = toolkit.Root

	// used to pass debugging flags to the toolkit plugins
	me.flag = me.rootNode.newNode("flag", 0)
	me.flag.WidgetType = toolkit.Flag

	me.flag = me.rootNode.newNode("stdout", 0)
	me.flag.WidgetType = toolkit.Stdout

	me.guiChan = make(chan toolkit.Action, 1)
	go watchCallback()
}

func watchCallback() {
	log(logInfo, "watchCallback() START")
	for {
		log(logInfo, "watchCallback() restarted select for toolkit user events")
	    	select {
		case a := <-me.guiChan:
			if (a.ActionType == toolkit.UserQuit) {
				log(logInfo, "doUserEvent() User sent Quit()")
				me.rootNode.doCustom()
				exit("wit/gui toolkit.UserQuit")
				break
			}
			if (a.ActionType == toolkit.EnableDebug) {
				log(logInfo, "doUserEvent() Enable Debugging Window")
				DebugWindow()
				break
			}

			n := me.rootNode.FindId(a.WidgetId)
			if (n == nil) {
				log(logError, "watchCallback() UNKNOWN widget id =", a.WidgetId, a.Name)
			} else {
				log(logInfo, "watchCallback() FOUND widget id =", n.id, n.Name)
				n.doUserEvent(a)
			}
			// this maybe a good idea?
			// TODO: Throttle user events somehow
			// sleep(.01) // hack that throttles user events
		}
	}
}

func (n *Node) doCustom() {
	log(logInfo, "doUserEvent() widget =", n.id, n.Name, n.WidgetType, n.B)
	if (n.Custom == nil) {
		log(debugError, "Custom() = nil. SKIPPING")
		return
	}
	go n.Custom()
}

func (n *Node) doUserEvent(a toolkit.Action) {
	log(logInfo, "doUserEvent() node =", n.id, n.Name)
	switch n.WidgetType {
	case toolkit.Checkbox:
		n.B = a.B
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.B)
		n.doCustom()
	case toolkit.Button:
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "button clicked")
		n.doCustom()
	case toolkit.Combobox:
		n.S = a.S
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.S)
		n.doCustom()
	case toolkit.Dropdown:
		n.S = a.S
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.S)
		n.doCustom()
	case toolkit.Textbox:
		n.S = a.S
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.S)
		n.doCustom()
	case toolkit.Spinner:
		n.I = a.I
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.I)
		n.doCustom()
	case toolkit.Slider:
		n.I = a.I
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.I)
		n.doCustom()
	case toolkit.Window:
		log(logInfo, "doUserEvent() node =", n.id, n.Name, "window closed")
		n.doCustom()
	default:
		log(logInfo, "doUserEvent() type =", n.WidgetType)
	}
}

// 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 {
	// start the GUI debugger if --gui-debug is true
	checkDebug()

	if (argGui.GuiPlugin != "") {
		log(logError, "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(logError, "New() failed to load gocui")
		}
		return n
	}
	if (n.LoadToolkit("andlabs") != nil) {
		return n
	}
	n.LoadToolkit("gocui")
	return n
}

func checkDebug() {
	if ! ArgDebug() {
		return
	}
	f := func() {
		log(debugGui, "wit/gui START DEBUG")
		log(debugGui, "wit/gui START DEBUG")
		log(debugGui, "wit/gui START DEBUG")
		time.Sleep(2 * time.Second)
		log(debugGui, "wit/gui START DEBUG")
		log(debugGui, "wit/gui START DEBUG")
		log(debugGui, "wit/gui START DEBUG")
		DebugWindow()
		log(debugGui, "wit/gui END DEBUG")
		log(debugGui, "wit/gui END DEBUG")
		log(debugGui, "wit/gui END DEBUG")
	}
	go f()
}

// The window is destroyed but the application does not quit
func (n *Node) StandardClose() {
	log(debugGui, "wit/gui Standard Window Close. name =", n.Name)
	log(debugGui, "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("wit/gui Standard Window Exit. running os.Exit()")
	log("StandardExit() attempt to exit each toolkit plugin")
	for i, plug := range allPlugins {
		log("NewButton()", i, plug)
	}
	exit(0)
}