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-08 15:34:36 -05:00
|
|
|
/*
|
2023-03-01 11:35:36 -06:00
|
|
|
func doGuiChan() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-Config.ActionCh1:
|
|
|
|
log(true, "CHANNEL ACTION 1 !!!!!")
|
|
|
|
return
|
|
|
|
case <-Config.ActionCh2:
|
|
|
|
log(true, "CHANNEL ACTION 2 !!!!!")
|
|
|
|
return
|
|
|
|
default:
|
2023-03-03 14:41:38 -06:00
|
|
|
log(true, "doGuiChan() nothing")
|
2023-03-01 11:35:36 -06:00
|
|
|
}
|
2023-03-03 14:41:38 -06:00
|
|
|
log(true, "doGuiChan() for()")
|
2023-03-01 11:35:36 -06:00
|
|
|
}
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
2023-04-08 15:34:36 -05:00
|
|
|
*/
|
2022-11-13 08:53:03 -06:00
|
|
|
|
2023-04-08 15:34:36 -05:00
|
|
|
/*
|
2023-04-07 09:18:03 -05:00
|
|
|
// 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 {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "Starting gui.Init()")
|
2022-11-13 08:53:03 -06:00
|
|
|
|
|
|
|
for _, aplug := range allPlugins {
|
2023-03-29 23:03:04 -05:00
|
|
|
log(debugGui, "LoadToolkit() already loaded toolkit plugin =", aplug.name)
|
2023-02-25 14:05:25 -06:00
|
|
|
for _, name := range names {
|
|
|
|
if (name == aplug.name) {
|
2023-04-07 09:18:03 -05:00
|
|
|
return []string{name}
|
2023-02-25 14:05:25 -06:00
|
|
|
}
|
|
|
|
}
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
|
2023-02-25 14:05:25 -06:00
|
|
|
// try to load each plugin in the order passed to this function
|
|
|
|
for _, name := range names {
|
2023-04-07 09:18:03 -05:00
|
|
|
aPlug := LoadToolkit(name)
|
|
|
|
if (aPlug != nil) {
|
|
|
|
// exit because it worked!
|
|
|
|
return []string{name}
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-25 14:05:25 -06:00
|
|
|
// 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
|
2023-04-07 09:18:03 -05:00
|
|
|
andlabsPlug := LoadToolkit("andlabs")
|
|
|
|
if (andlabsPlug != nil) {
|
|
|
|
return []string{}
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
2022-11-06 19:57:20 -06:00
|
|
|
|
2023-04-07 09:18:03 -05:00
|
|
|
gocuiPlug := LoadToolkit("andlabs")
|
|
|
|
if (gocuiPlug != nil) {
|
|
|
|
return []string{}
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
2023-04-07 09:18:03 -05:00
|
|
|
return []string{}
|
2022-10-20 06:55:42 -05:00
|
|
|
}
|
2023-04-08 15:34:36 -05:00
|
|
|
*/
|
2023-03-29 23:03:04 -05: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 15:25:03 -05:00
|
|
|
// if (LoadPlugin("andlabs")) {
|
|
|
|
// log(logError, "New() failed to load andlabs")
|
|
|
|
// }
|
2023-03-29 23:03:04 -05:00
|
|
|
return Config.rootNode
|
|
|
|
}
|
|
|
|
|
2023-04-08 15:34:36 -05:00
|
|
|
/*
|
2022-11-13 08:53:03 -06:00
|
|
|
// This should not pass a function
|
2021-10-06 08:36:28 -05:00
|
|
|
func Main(f func()) {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "Starting gui.Main() (using gtk via andlabs/ui)")
|
|
|
|
|
2023-04-07 09:18:03 -05:00
|
|
|
// 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 {
|
2023-04-08 12:08:57 -05:00
|
|
|
// InitPlugins([]string{"andlabs", "gocui"})
|
|
|
|
InitPlugins([]string{"gocui", "andlabs"})
|
2023-04-06 18:00:18 -05:00
|
|
|
}
|
|
|
|
|
2022-11-13 08:53:03 -06:00
|
|
|
for _, aplug := range allPlugins {
|
2023-04-07 21:22:51 -05:00
|
|
|
log(debugGui, "NewButton() toolkit plugin =", aplug.name)
|
2022-11-13 08:53:03 -06:00
|
|
|
if (aplug.MainOk) {
|
2023-04-07 21:22:51 -05:00
|
|
|
log(debugGui, "Main() Already Ran Main()", aplug.name)
|
2022-11-13 08:53:03 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (aplug.Main == nil) {
|
2023-04-07 21:22:51 -05:00
|
|
|
log(debugGui, "Main() Main == nil", aplug.name)
|
2022-11-13 08:53:03 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
aplug.MainOk = true
|
2023-04-07 21:22:51 -05:00
|
|
|
if (aplug.Callback == nil) {
|
2023-04-07 21:54:57 -05:00
|
|
|
// TODO: don't load the module if this failed ?
|
2023-04-07 21:22:51 -05:00
|
|
|
// if Callback() isn't set in the plugin, no information can be sent to it!
|
2023-04-07 21:54:57 -05:00
|
|
|
log(debugError, "SERIOUS ERROR: plugin Callback() == nil. nothing will work for toolkit", aplug.name)
|
2023-04-07 21:22:51 -05:00
|
|
|
} else {
|
2023-04-06 18:00:18 -05:00
|
|
|
aplug.Callback(Config.guiChan)
|
|
|
|
}
|
2023-04-07 21:54:57 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-11-13 08:53:03 -06:00
|
|
|
aplug.Main(f)
|
|
|
|
}
|
2023-04-06 18:00:18 -05:00
|
|
|
|
2021-10-06 08:36:28 -05:00
|
|
|
}
|
2023-04-08 15:34:36 -05:00
|
|
|
*/
|
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
|
|
|
}
|