summaryrefslogtreecommitdiff
path: root/toolkit/andlabs/main.go
blob: 5a994219c3c7f376c9f5d18069d83dbb56b8b0e5 (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
package main

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

	"github.com/andlabs/ui"
	// the _ means we only need this for the init()
	_ "github.com/andlabs/ui/winmanifest"
)

//go:embed resources
var res embed.FS

// this is the channel we get requests to make widgets
var pluginChan chan toolkit.Action

var uiMainUndef bool = true
var uiMain sync.Once
var muAction sync.Mutex

func catchActionChannel() {
	log(logNow, "catchActionChannel() START")
	for {
		log(logNow, "catchActionChannel() for loop")
		uiMain.Do(func() {
			go ui.Main(demoUI)
		})
	    	select {
		case a := <-pluginChan:
			log(logNow, "catchActionChannel() SELECT widget id =", a.WidgetId, a.Name)
			/*
			// go Action(a)
			if (uiMainUndef) {
				log(logError,"catchActionChannel() main() was not run yet")
				log(logError,"catchActionChannel() main() was not run yet")
				log(logError,"catchActionChannel() main() was not run yet")
				log(logError,"catchActionChannel() ui.Main() START")
				log(logError,"catchActionChannel() ui.Main() START")
				log(logError,"catchActionChannel() ui.Main() START")
				log(logError,"catchActionChannel() ui.Main() START")
				sleep(1)
				// go ui.Main(demoUI)
				// go ui.Main( func() {
				// 	rawAction(a)
				// })
				// probably not needed, but in here for now under development
				uiMainUndef = false
				sleep(1)
			} else {
				log(logNow, "catchActionChannel() STUFF", a.WidgetId, a.ActionType, a.WidgetType)
				rawAction(a)
				log(logNow, "catchActionChannel() STUFF END", a.WidgetId, a.ActionType, a.WidgetType)
			}
			*/
			log(logNow, "catchActionChannel() STUFF", a.WidgetId, a.ActionType, a.WidgetType)
			muAction.Lock()
			rawAction(a)
			muAction.Unlock()
			log(logNow, "catchActionChannel() STUFF END", a.WidgetId, a.ActionType, a.WidgetType)
		}
	}
}

/*
func main(f func()) {
	log(debugNow, "Main() START (using gtk via andlabs/ui)")
	f() // support the old way. deprecate this
}
*/

// this sets the channel to send user events back from the plugin
func Callback(guiCallback chan toolkit.Action) {
	callback = guiCallback
}

func PluginChannel() chan toolkit.Action {
	return pluginChan
}


// 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: Queue(NewWindow())
//
func queue(f func()) {
	log(logNow, "Sending function to ui.QueueMain()")
	log(logNow, "using gui.Queue() in this plugin DOES BREAK. TODO: solve this with channels")
	ui.QueueMain(f)
}

// This is important. This sets the defaults for the gui. Without this, there isn't correct padding, etc
func Init() {
	log(logNow, "Init() START")
	log(debugToolkit, "Init()")
	// Can you pass values to a plugin init() ? Otherwise, there is no way to safely print
	// log(debugToolkit, "gui/toolkit init() Setting defaultBehavior = true")
	setDefaultBehavior(true)

	andlabs = make(map[int]*andlabsT)
	pluginChan = make(chan toolkit.Action, 1)

	log(logNow, "Init() start channel reciever")
	go catchActionChannel()
	log(logNow, "Init() END")
}

// TODO: properly exit the plugin since Quit() doesn't do it
func Quit() {
	log(debugToolkit, "Quit() TODO: close the toolkit cleanly")
}