summaryrefslogtreecommitdiff
path: root/main.go
blob: 5a2407ee51ae5c70854ec42446d6df7ffad18d9e (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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package gui

import (
	"os"
	// "embed"
	"git.wit.org/wit/gui/toolkit"
)

// 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 "git.wit.org/wit/gui/toolkit/andlabs"

const Xaxis = 0 // stack things horizontally
const Yaxis = 1 // stack things vertically

/*
	// TODO: 2023/03/03 rethink how to get a plugin or figure out how
	// golang packages can include a binary. Pull from /usr/go/go-gui/ ?
	// may this plugin work when all other plugins fail

	// if this is in the plugin, the packages can't work with go.mod builds
	# don't do this in the plugin // go:embed /usr/lib/go-gui/toolkit/gocui.so
	# don't do this in the plugin var res embed.FS
*/

func init() {
	log("init() has been run")

	Config.counter = 0
	Config.prefix = "wit"
	Config.Width = 640
	Config.Height = 480

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

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

	Config.guiChan = make(chan toolkit.Action)
	go watchCallback()
}

func doGuiChan() {
	for {
		select {
		case <-Config.ActionCh1:
			log(true, "CHANNEL ACTION 1  !!!!!")
			return
		case <-Config.ActionCh2:
			log(true, "CHANNEL ACTION 2  !!!!!")
			return
		default:
			log(true, "doGuiChan() nothing")
		}
		log(true, "doGuiChan() for()")
	}
}

// TODO: add logic to just load the 1st 'most common' gui toolkit
// and allow the 'go-arg' command line args to override the defaults
func InitPlugins(names []string) []string {
	log(debugGui, "Starting gui.Init()")

	for _, aplug := range allPlugins {
		log(debugGui, "LoadToolkit() already loaded toolkit plugin =", aplug.name)
		for _, name := range names {
			if (name == aplug.name) {
				return []string{name}
			}
		}
	}

	// try to load each plugin in the order passed to this function
	for _, name := range names {
		aPlug := LoadToolkit(name)
		if (aPlug != nil) {
			// exit because it worked!
			return []string{name}
		}
	}

	// the program didn't specify a plugin. Try to load one
	// TODO: detect the OS & user preferences to load the best one
	// TODO: commented out Init() on 02/26/2023 because I'm not sure how to run it correctly
	andlabsPlug := LoadToolkit("andlabs")
	if (andlabsPlug != nil) {
		return []string{}
	}

	gocuiPlug := LoadToolkit("andlabs")
	if (gocuiPlug != nil) {
		return []string{}
	}
	return []string{}
}

func Start() *Node {
	log(logInfo, "Start() Main(f)")
	f := func() {
	}
	go Main(f)
	sleep(1)
	return Config.rootNode
}

func watchCallback() {
	log(logNow, "makeCallback() START")
	for {
		log(logNow, "makeCallback() for loop")
	    	select {
		case a := <-Config.guiChan:
			log(logNow, "makeCallback() SELECT widget id =", a.WidgetId, a.Name)
			n := Config.rootNode.FindId(a.WidgetId)
			if (n == nil) {
				log(logError, "makeCallback() SELECT widget id =", a.WidgetId, a.Name)
			} else {
				go n.doUserEvent(a)
			}
			// this maybe a good idea?
			// TODO: Throttle user events somehow
			sleep(.1)
		}
	}
}

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

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

func LoadPlugin(name string) bool {
	startS(name)
	return true
}

func startS(name string) *Node {
	log(logInfo, "Start() Main(f) for name =", name)
	aplug := LoadToolkit(name)
	if (aplug == nil) {
		return Config.rootNode
	}
	// will this really work on mswindows & macos?
	f := func() {
	}
	go Main(f)
	sleep(1) // temp hack until chan communication is setup
	Config.rootNode.Redraw(aplug)
	return Config.rootNode
}

// This should not pass a function
func Main(f func()) {
	log(debugGui, "Starting gui.Main() (using gtk via andlabs/ui)")

	// TODO: this is linux only
	// TODO: detect if this was run from the command line (parent == bash?)
	// if DISPLAY is not set, don't even bother with loading andlabs
	if (os.Getenv("DISPLAY") == "") {
		InitPlugins([]string{"gocui"})
	} else {
		//InitPlugins([]string{"andlabs", "gocui"})
		InitPlugins([]string{"gocui", "andlabs"})
	}

	for _, aplug := range allPlugins {
		log(debugGui, "NewButton() toolkit plugin =", aplug.name)
		if (aplug.MainOk) {
			log(debugGui, "Main() Already Ran Main()", aplug.name)
			continue
		}
		if (aplug.Main == nil) {
			log(debugGui, "Main() Main == nil", aplug.name)
			continue
		}
		aplug.MainOk = true
		if (aplug.Callback == nil) {
			// TODO: don't load the module if this failed ?
			// if Callback() isn't set in the plugin, no information can be sent to it!
			log(debugError, "SERIOUS ERROR: plugin Callback() == nil. nothing will work for toolkit", aplug.name)
		} else {
			aplug.Callback(Config.guiChan)
		}

		if (aplug.PluginChannel == nil) {
			// TODO: don't load the module if this failed ?
			// if Callback() isn't set in the plugin, no information can be sent to it!
			log(debugError, "ERROR: plugin does not implement a send channel. toolkit =", aplug.name)
		} else {
			aplug.pluginChan = aplug.PluginChannel()
		}

		aplug.Main(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, aplug := range allPlugins {
		log("NewButton()", i, aplug)
		if (aplug.Quit != nil) {
			aplug.Quit()
		}
	}
	exit(0)
}