summaryrefslogtreecommitdiff
path: root/main.go
blob: cc5bfcf5f52e14e34692683a1209d5a64f4d6031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package gui

import (
	"embed"
	"git.wit.org/wit/gui/toolkit"
)

// 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"

const Xaxis = 0 // stack things horizontally
const Yaxis = 1 // stack things vertically

// may this plugin work when all other plugins fail
//go:embed toolkit/gocui.so
var res embed.FS

func init() {
	log("init() has been run")

	Config.counter = 0
	Config.prefix = "wit"
	Config.Width = 640
	Config.Height = 480

	// Populates the top of the binary tree
	Config.master = addNode("guiBinaryTree")

	// used to pass debugging flags to the toolkit plugins
	Config.flag = Config.master.New("flag", toolkit.Flag, nil)
}

func doGuiChan() {
	for {
		select {
		case <-Config.ActionCh1:
			log(true, "CHANNEL ACTION 1  !!!!!")
			return
		case <-Config.ActionCh2:
			log(true, "CHANNEL ACTION 2  !!!!!")
			return
		default:
			log(true, "doGuiChan() nothing")
		}
		log(true, "doGuiChan() for()")
	}
}

func InitPlugins(names []string) {
	log(debugGui, "Starting gui.Init()")

	for _, aplug := range allPlugins {
		log(debugGui, "gui.LoadToolkit() already loaded toolkit plugin =", aplug.name)
		for _, name := range names {
			if (name == aplug.name) {
				return
			}
		}
	}

	// try to load each plugin in the order passed to this function
	for _, name := range names {
		if (LoadToolkit(name)) {
			// aplug.InitOk = true
			// aplug.Init()
			return
		}
	}

	// 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
	if (LoadToolkit("andlabs")) {
		// aplug.InitOk = true
		// aplug.Init()
		return
	}

	if (LoadToolkit("gocui")) {
		// aplug.InitOk = true
		// aplug.Init()
		return
	}

	// Should die here? TODO: need a Node to call StandardExit
	// StandardExit("golang wit/gui could not load a plugin TODO: do something to STDOUT (?)")
}

// This should not pass a function
func Main(f func()) {
	log(debugGui, "Starting gui.Main() (using gtk via andlabs/ui)")

	InitPlugins([]string{"andlabs", "gocui"})

	for _, aplug := range allPlugins {
		log(debugGui, "gui.Node.NewButton() toolkit plugin =", aplug.name)
		if (aplug.MainOk) {
			log(debugGui, "gui.Node.Main() Already Ran Main()", aplug.name)
			continue
		}
		if (aplug.Main == nil) {
			log(debugGui, "gui.Node.Main() Main == nil", aplug.name)
			continue
		}
		aplug.MainOk = true
		aplug.Main(f)
		// f()
	}
	// toolkit.Main(f)
}

// This should never be exposed(?)

// Other goroutines must use this to access the GUI
//
// You can not acess / process the GUI thread directly from
// other goroutines. This is due to the nature of how
// Linux, MacOS and Windows work (they all work differently. suprise. surprise.)
// For example: gui.Queue(NewWindow())
func Queue(f func()) {
	log(debugGui, "Sending function to gui.Main() (using gtk via andlabs/ui)")
	// toolkit.Queue(f)
	for _, aplug := range allPlugins {
		log(debugGui, "gui.Node.NewButton() toolkit plugin =", aplug.name)
		if (aplug.Queue == nil) {
			continue
		}
		aplug.Queue(f)
	}
}

// The window is destroyed but the application does not quit
func (n *Node) StandardClose() {
	log(debugGui, "wit/gui Standard Window Close. name =", n.Name)
	log(debugGui, "wit/gui Standard Window Close. n.Custom exit =", n.Custom)
}

// The window is destroyed but the application does not quit
func StandardExit() {
	log("wit/gui Standard Window Exit. running os.Exit()")
	log("gui.Node.StandardExit() attempt to exit each toolkit plugin")
	for i, aplug := range allPlugins {
		log("gui.Node.NewButton()", i, aplug)
		if (aplug.Quit != nil) {
			aplug.Quit()
		}
	}

	exit(0)
}