2021-10-06 08:36:28 -05:00
|
|
|
package gui
|
|
|
|
|
2021-10-06 10:43:58 -05:00
|
|
|
import (
|
2023-04-07 09:18:03 -05:00
|
|
|
"os"
|
2023-12-20 14:15:00 -06:00
|
|
|
"go.wit.com/gui/toolkit"
|
2021-10-06 10:43:58 -05:00
|
|
|
)
|
2021-10-06 08:36:28 -05:00
|
|
|
|
2023-04-08 15:34:36 -05:00
|
|
|
// TODO: make a fake 'plugin' channel of communication to andlabs for mswindows
|
2022-11-13 08:53:03 -06:00
|
|
|
// 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
|
2023-12-20 14:15:00 -06:00
|
|
|
// import toolkit "go.wit.com/gui/toolkit/andlabs"
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2022-11-06 12:59:24 -06:00
|
|
|
const Xaxis = 0 // stack things horizontally
|
|
|
|
const Yaxis = 1 // stack things vertically
|
2022-10-20 06:55:42 -05:00
|
|
|
|
|
|
|
func init() {
|
2023-03-01 11:35:36 -06:00
|
|
|
log("init() has been run")
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2023-04-28 10:35:57 -05:00
|
|
|
me.counter = 0
|
|
|
|
me.prefix = "wit"
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2022-11-06 12:59:24 -06:00
|
|
|
// Populates the top of the binary tree
|
2023-04-28 10:35:57 -05:00
|
|
|
me.rootNode = addNode("guiBinaryTree")
|
|
|
|
me.rootNode.WidgetType = toolkit.Root
|
2023-03-01 11:35:36 -06:00
|
|
|
|
2023-03-03 14:41:38 -06:00
|
|
|
// used to pass debugging flags to the toolkit plugins
|
2023-05-09 20:25:37 -05:00
|
|
|
me.flag = me.rootNode.newNode("flag", 0)
|
2023-04-28 10:35:57 -05:00
|
|
|
me.flag.WidgetType = toolkit.Flag
|
2023-04-07 21:22:51 -05:00
|
|
|
|
2023-05-09 20:25:37 -05:00
|
|
|
me.flag = me.rootNode.newNode("stdout", 0)
|
2023-04-28 10:35:57 -05:00
|
|
|
me.flag.WidgetType = toolkit.Stdout
|
2023-04-26 20:56:25 -05:00
|
|
|
|
2023-04-28 10:35:57 -05:00
|
|
|
me.guiChan = make(chan toolkit.Action, 1)
|
2023-04-07 21:22:51 -05:00
|
|
|
go watchCallback()
|
2023-03-01 11:35:36 -06:00
|
|
|
}
|
|
|
|
|
2023-04-06 18:00:18 -05:00
|
|
|
func watchCallback() {
|
2023-04-08 14:31:00 -05:00
|
|
|
log(logInfo, "watchCallback() START")
|
2023-04-06 18:00:18 -05:00
|
|
|
for {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "watchCallback() restarted select for toolkit user events")
|
2023-04-06 18:00:18 -05:00
|
|
|
select {
|
2023-04-28 10:35:57 -05:00
|
|
|
case a := <-me.guiChan:
|
2023-04-26 20:56:25 -05:00
|
|
|
if (a.ActionType == toolkit.UserQuit) {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() User sent Quit()")
|
2023-04-28 10:35:57 -05:00
|
|
|
me.rootNode.doCustom()
|
2023-04-26 20:56:25 -05:00
|
|
|
exit("wit/gui toolkit.UserQuit")
|
|
|
|
break
|
|
|
|
}
|
2023-04-27 00:10:23 -05:00
|
|
|
if (a.ActionType == toolkit.EnableDebug) {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() Enable Debugging Window")
|
2023-04-27 00:10:23 -05:00
|
|
|
DebugWindow()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-04-28 10:35:57 -05:00
|
|
|
n := me.rootNode.FindId(a.WidgetId)
|
2023-04-06 18:26:30 -05:00
|
|
|
if (n == nil) {
|
2023-04-08 14:31:00 -05:00
|
|
|
log(logError, "watchCallback() UNKNOWN widget id =", a.WidgetId, a.Name)
|
2023-04-06 18:26:30 -05:00
|
|
|
} else {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "watchCallback() FOUND widget id =", n.id, n.Name)
|
2023-04-08 14:31:00 -05:00
|
|
|
n.doUserEvent(a)
|
2023-04-06 18:26:30 -05:00
|
|
|
}
|
|
|
|
// this maybe a good idea?
|
|
|
|
// TODO: Throttle user events somehow
|
2023-12-14 10:36:56 -06:00
|
|
|
// sleep(.01) // hack that throttles user events
|
2023-04-06 18:26:30 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 20:25:14 -05:00
|
|
|
func (n *Node) doCustom() {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() widget =", n.id, n.Name, n.WidgetType, n.B)
|
2023-04-06 20:25:14 -05:00
|
|
|
if (n.Custom == nil) {
|
|
|
|
log(debugError, "Custom() = nil. SKIPPING")
|
|
|
|
return
|
|
|
|
}
|
2023-04-08 14:31:00 -05:00
|
|
|
go n.Custom()
|
2023-04-06 20:25:14 -05:00
|
|
|
}
|
|
|
|
|
2023-04-06 18:26:30 -05:00
|
|
|
func (n *Node) doUserEvent(a toolkit.Action) {
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name)
|
2023-04-06 18:26:30 -05:00
|
|
|
switch n.WidgetType {
|
|
|
|
case toolkit.Checkbox:
|
|
|
|
n.B = a.B
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.B)
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
2023-04-06 18:26:30 -05:00
|
|
|
case toolkit.Button:
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "button clicked")
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
|
|
|
case toolkit.Combobox:
|
|
|
|
n.S = a.S
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.S)
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
|
|
|
case toolkit.Dropdown:
|
|
|
|
n.S = a.S
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.S)
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
|
|
|
case toolkit.Textbox:
|
|
|
|
n.S = a.S
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.S)
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
|
|
|
case toolkit.Spinner:
|
|
|
|
n.I = a.I
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.I)
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
|
|
|
case toolkit.Slider:
|
|
|
|
n.I = a.I
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "set to:", n.I)
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
2023-04-06 19:48:24 -05:00
|
|
|
case toolkit.Window:
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() node =", n.id, n.Name, "window closed")
|
2023-04-06 20:25:14 -05:00
|
|
|
n.doCustom()
|
2023-04-06 18:26:30 -05:00
|
|
|
default:
|
2023-12-20 05:58:33 -06:00
|
|
|
log(logInfo, "doUserEvent() type =", n.WidgetType)
|
2023-04-06 18:00:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 15:34:36 -05:00
|
|
|
// 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 {
|
2023-04-28 10:35:57 -05:00
|
|
|
return me.rootNode
|
2023-04-12 13:00:29 -05:00
|
|
|
}
|
|
|
|
|
2023-04-23 11:30:54 -05:00
|
|
|
// try to load andlabs, if that doesn't work, fall back to the console
|
2023-04-12 13:00:29 -05:00
|
|
|
func (n *Node) Default() *Node {
|
2023-12-29 09:24:06 -06:00
|
|
|
if (guiArg.Gui != "") {
|
|
|
|
log(logError, "New.Default() try toolkit =", guiArg.Gui)
|
|
|
|
return n.LoadToolkit(guiArg.Gui)
|
2023-12-02 19:02:51 -06:00
|
|
|
}
|
2023-04-08 15:34:36 -05:00
|
|
|
// 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") == "") {
|
2023-04-23 11:30:54 -05:00
|
|
|
if (n.LoadToolkit("gocui") == nil) {
|
|
|
|
log(logError, "New() failed to load gocui")
|
|
|
|
}
|
2023-04-12 13:00:29 -05:00
|
|
|
return n
|
2023-03-29 23:03:04 -05:00
|
|
|
}
|
2023-04-23 11:30:54 -05:00
|
|
|
if (n.LoadToolkit("andlabs") != nil) {
|
|
|
|
return n
|
2023-04-06 18:00:18 -05:00
|
|
|
}
|
2023-04-23 11:30:54 -05:00
|
|
|
n.LoadToolkit("gocui")
|
2023-04-12 13:00:29 -05:00
|
|
|
return n
|
2021-10-06 08:36:28 -05:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:59:24 -06:00
|
|
|
// The window is destroyed but the application does not quit
|
2023-03-01 11:35:36 -06:00
|
|
|
func (n *Node) StandardClose() {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "wit/gui Standard Window Close. name =", n.Name)
|
2023-03-01 11:35:36 -06:00
|
|
|
log(debugGui, "wit/gui Standard Window Close. n.Custom exit =", n.Custom)
|
2022-11-06 12:59:24 -06:00
|
|
|
}
|
|
|
|
|
2023-03-12 08:47:16 -05:00
|
|
|
// The window is destroyed and the application exits
|
|
|
|
// TODO: properly exit the plugin since Quit() doesn't do it
|
2023-03-01 11:35:36 -06:00
|
|
|
func StandardExit() {
|
|
|
|
log("wit/gui Standard Window Exit. running os.Exit()")
|
2023-04-07 21:22:51 -05:00
|
|
|
log("StandardExit() attempt to exit each toolkit plugin")
|
2023-04-23 09:47:54 -05:00
|
|
|
for i, plug := range allPlugins {
|
|
|
|
log("NewButton()", i, plug)
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
2023-03-01 11:35:36 -06:00
|
|
|
exit(0)
|
2022-11-06 12:59:24 -06:00
|
|
|
}
|