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-03-12 08:47:16 -05:00
|
|
|
// "embed"
|
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
|
|
|
|
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-03-29 23:03:04 -05:00
|
|
|
Config.flag = Config.rootNode.New("flag", 0, nil)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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-03-29 23:03:04 -05:00
|
|
|
func Start() *Node {
|
|
|
|
log(logInfo, "Start() Main(f)")
|
|
|
|
f := func() {
|
|
|
|
}
|
|
|
|
go Main(f)
|
|
|
|
sleep(1)
|
|
|
|
return Config.rootNode
|
|
|
|
}
|
|
|
|
|
2023-04-06 18:00:18 -05:00
|
|
|
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)
|
2023-04-06 18:26:30 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
n.Custom()
|
|
|
|
}
|
|
|
|
|
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-06 14:16:59 -05:00
|
|
|
func (n *Node) LoadPlugin(name string) bool {
|
|
|
|
StartS(name)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-03-29 23:03:04 -05:00
|
|
|
func StartS(name string) *Node {
|
|
|
|
log(logInfo, "Start() Main(f) for name =", name)
|
2023-04-07 09:18:03 -05:00
|
|
|
aplug := LoadToolkit(name)
|
|
|
|
if (aplug == nil) {
|
2023-03-29 23:03:04 -05:00
|
|
|
return Config.rootNode
|
|
|
|
}
|
|
|
|
// will this really work on mswindows & macos?
|
|
|
|
f := func() {
|
|
|
|
}
|
|
|
|
go Main(f)
|
|
|
|
sleep(1) // temp hack until chan communication is setup
|
2023-04-07 09:18:03 -05:00
|
|
|
Config.rootNode.Redraw(aplug)
|
2023-03-29 23:03:04 -05:00
|
|
|
return Config.rootNode
|
|
|
|
}
|
|
|
|
|
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-07 21:22:51 -05:00
|
|
|
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) {
|
|
|
|
// 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: Callback() == nil. nothing will work for plugin", aplug.name)
|
|
|
|
} else {
|
2023-04-06 18:00:18 -05:00
|
|
|
aplug.Callback(Config.guiChan)
|
|
|
|
}
|
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-03-29 23:03:04 -05:00
|
|
|
/*
|
|
|
|
This is deprecated and will be implemented more correctly with waitgroups
|
|
|
|
|
2022-11-13 08:53:03 -06:00
|
|
|
// This should never be exposed(?)
|
|
|
|
|
2021-10-31 14:21:36 -05:00
|
|
|
// Other goroutines must use this to access the GUI
|
2021-10-06 08:36:28 -05:00
|
|
|
//
|
|
|
|
// You can not acess / process the GUI thread directly from
|
2021-10-06 10:43:58 -05:00
|
|
|
// other goroutines. This is due to the nature of how
|
2021-10-06 08:36:28 -05:00
|
|
|
// Linux, MacOS and Windows work (they all work differently. suprise. surprise.)
|
2022-10-20 06:55:42 -05:00
|
|
|
// For example: gui.Queue(NewWindow())
|
2022-11-14 14:30:28 -06:00
|
|
|
func Queue(f func()) {
|
2023-02-25 14:05:25 -06:00
|
|
|
log(debugGui, "Sending function to gui.Main() (using gtk via andlabs/ui)")
|
2022-11-13 08:53:03 -06:00
|
|
|
// toolkit.Queue(f)
|
|
|
|
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.Queue == nil) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
aplug.Queue(f)
|
|
|
|
}
|
2021-10-06 08:36:28 -05:00
|
|
|
}
|
2023-03-29 23:03:04 -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
|
|
|
}
|