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"
|
2024-01-03 13:37:03 -06:00
|
|
|
|
2024-01-03 18:15:54 -06:00
|
|
|
"go.wit.com/log"
|
2024-01-03 18:54:08 -06:00
|
|
|
"go.wit.com/gui/toolkits"
|
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() {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(true, "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() {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("watchCallback() START")
|
2023-04-06 18:00:18 -05:00
|
|
|
for {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("doUserEvent() User sent Quit()")
|
2023-04-28 10:35:57 -05:00
|
|
|
me.rootNode.doCustom()
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Exit("wit/gui toolkit.UserQuit")
|
2023-04-26 20:56:25 -05:00
|
|
|
break
|
|
|
|
}
|
2023-04-27 00:10:23 -05:00
|
|
|
if (a.ActionType == toolkit.EnableDebug) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("doUserEvent() Enable Debugging Window")
|
|
|
|
log.Warn("doUserEvent() TODO: not implemented")
|
2024-01-03 13:37:03 -06:00
|
|
|
// DebugWindow()
|
2023-04-27 00:10:23 -05:00
|
|
|
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) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("watchCallback() UNKNOWN widget id =", a.WidgetId, a.Name)
|
2023-04-06 18:26:30 -05:00
|
|
|
} else {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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() {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("doUserEvent() widget =", n.id, n.Name, n.WidgetType, n.B)
|
2023-04-06 20:25:14 -05:00
|
|
|
if (n.Custom == nil) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("Custom() = nil. SKIPPING")
|
2023-04-06 20:25:14 -05:00
|
|
|
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) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("doUserEvent() node =", n.id, n.Name)
|
2023-04-06 18:26:30 -05:00
|
|
|
switch n.WidgetType {
|
|
|
|
case toolkit.Checkbox:
|
|
|
|
n.B = a.B
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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:
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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:
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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:
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Info("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 17:24:16 -06:00
|
|
|
if (argGui.GuiPlugin != "") {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("New.Default() try toolkit =", argGui.GuiPlugin)
|
2023-12-29 17:24:16 -06:00
|
|
|
return n.LoadToolkit(argGui.GuiPlugin)
|
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) {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Warn("New() failed to load gocui")
|
2023-04-23 11:30:54 -05:00
|
|
|
}
|
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() {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(GUI, "wit/gui Standard Window Close. name =", n.Name)
|
|
|
|
log.Log(GUI, "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() {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(true, "wit/gui Standard Window Exit. running os.Exit()")
|
|
|
|
log.Log(true, "StandardExit() attempt to exit each toolkit plugin")
|
2023-04-23 09:47:54 -05:00
|
|
|
for i, plug := range allPlugins {
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Log(true, "NewButton()", i, plug)
|
2022-11-13 08:53:03 -06:00
|
|
|
}
|
2024-01-03 18:15:54 -06:00
|
|
|
log.Exit(0)
|
2022-11-06 12:59:24 -06:00
|
|
|
}
|