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-04-08 15:34:36 -05:00
|
|
|
// "embed" // reminder to not attempt this within the 'wit/gui' package
|
2023-03-29 23:03:04 -05:00
|
|
|
"git.wit.org/wit/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
|
|
|
|
// import toolkit "git.wit.org/wit/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
|
|
|
|
2023-03-12 08:47:16 -05:00
|
|
|
/*
|
|
|
|
// 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
|
|
|
|
*/
|
2022-11-13 08:53:03 -06:00
|
|
|
|
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
|
|
|
|
|
|
|
Config.counter = 0
|
|
|
|
Config.prefix = "wit"
|
2023-03-01 11:35:36 -06:00
|
|
|
Config.Width = 640
|
|
|
|
Config.Height = 480
|
2022-10-20 06:55:42 -05:00
|
|
|
|
2022-11-06 12:59:24 -06:00
|
|
|
// Populates the top of the binary tree
|
2023-03-29 23:03:04 -05:00
|
|
|
Config.rootNode = addNode("guiBinaryTree")
|
|
|
|
Config.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-04-08 11:06:50 -05:00
|
|
|
Config.flag = Config.rootNode.newNode("flag", 0, nil)
|
2023-03-29 23:03:04 -05:00
|
|
|
Config.flag.WidgetType = toolkit.Flag
|
2023-04-07 21:22:51 -05:00
|
|
|
|
|
|
|
Config.guiChan = make(chan toolkit.Action)
|
|
|
|
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-04-08 14:31:00 -05:00
|
|
|
log(logNow, "watchCallback() restarted select for toolkit user events")
|
2023-04-06 18:00:18 -05:00
|
|
|
select {
|
|
|
|
case a := <-Config.guiChan:
|
2023-04-06 18:26:30 -05:00
|
|
|
n := Config.rootNode.FindId(a.WidgetId)
|
|
|
|
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-04-08 14:31:00 -05:00
|
|
|
log(logNow, "watchCallback() FOUND widget id =", n.id, n.Name)
|
|
|
|
n.doUserEvent(a)
|
2023-04-06 18:26:30 -05:00
|
|
|
}
|
|
|
|
// this maybe a good idea?
|
|
|
|
// TODO: Throttle user events somehow
|
|
|
|
sleep(.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-06 20:25:14 -05:00
|
|
|
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
|
|
|
|
}
|
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) {
|
|
|
|
log(logNow, "doUserEvent() node =", n.id, n.Name)
|
|
|
|
switch n.WidgetType {
|
|
|
|
case toolkit.Checkbox:
|
|
|
|
n.B = a.B
|
2023-04-06 20:25:14 -05:00
|
|
|
log(logNow, "doUserEvent() node =", n.id, n.Name, "set to:", n.B)
|
|
|
|
n.doCustom()
|
2023-04-06 18:26:30 -05:00
|
|
|
case toolkit.Button:
|
2023-04-06 20:25:14 -05:00
|
|
|
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()
|
2023-04-06 19:48:24 -05:00
|
|
|
case toolkit.Window:
|
2023-04-06 20:25:14 -05:00
|
|
|
log(logNow, "doUserEvent() node =", n.id, n.Name, "window closed")
|
|
|
|
n.doCustom()
|
2023-04-06 18:26:30 -05:00
|
|
|
default:
|
|
|
|
log(logNow, "doUserEvent() type =", n.WidgetType)
|
2023-04-06 18:00:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-08 11:06:50 -05:00
|
|
|
func LoadPlugin(name string) bool {
|
2023-04-08 15:34:36 -05:00
|
|
|
log(logInfo, "Start() Main(f) for name =", name)
|
|
|
|
newPlugin := LoadToolkit(name)
|
|
|
|
if (newPlugin == nil) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1) // temp hack until chan communication is setup
|
|
|
|
|
|
|
|
// TODO: find a new way to do this that is locking, safe and accurate
|
|
|
|
Config.rootNode.redraw(newPlugin)
|
2023-04-06 14:16:59 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if (LoadPlugin("gocui")) {
|
|
|
|
log(logError, "New() failed to load gocui")
|
|
|
|
}
|
|
|
|
// 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-03-29 23:03:04 -05:00
|
|
|
return Config.rootNode
|
|
|
|
}
|
2023-04-11 21:37:36 -05:00
|
|
|
if (LoadPlugin("andlabs")) {
|
|
|
|
log(logError, "New() failed to load andlabs")
|
2023-04-06 18:00:18 -05:00
|
|
|
}
|
2023-04-11 21:37:36 -05:00
|
|
|
return Config.rootNode
|
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")
|
2022-11-13 08:53:03 -06:00
|
|
|
for i, aplug := range allPlugins {
|
2023-04-07 21:22:51 -05:00
|
|
|
log("NewButton()", i, aplug)
|
2022-11-13 08:53:03 -06:00
|
|
|
if (aplug.Quit != nil) {
|
|
|
|
aplug.Quit()
|
|
|
|
}
|
|
|
|
}
|
2023-03-01 11:35:36 -06:00
|
|
|
exit(0)
|
2022-11-06 12:59:24 -06:00
|
|
|
}
|