summaryrefslogtreecommitdiff
path: root/common/plugin.go
blob: bfc9a68388daa6a5077d2f857e301e42012426e9 (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
package main

/*
	These code should be common to all gui plugins

	There are some helper functions that are probably going to be
	the same everywhere. Mostly due to handling the binary tree structure
	and the channel communication

	For now, it's just a symlink to the 'master' version in
	./toolkit/nocui/common.go
*/

import (
	"reflect"
	"strconv"

	"go.wit.com/log"
	"go.wit.com/gui/widget"
)

// searches the binary tree for a WidgetId
func (n *node) findWidgetId(id int) *node {
	if (n == nil) {
		return nil
	}

	if n.WidgetId == id {
		return n
	}

	for _, child := range n.children {
		newN := child.findWidgetId(id)
		if (newN != nil) {
			return newN
		}
	}
	return nil
}

func (n *node) doUserEvent() {
	if (callback == nil) {
		log.Log(ERROR, "doUserEvent() callback == nil", n.WidgetId)
		return
	}
	var a widget.Action
	a.WidgetId = n.WidgetId
	a.Value = n.value
	a.ActionType = widget.User
	log.Log(INFO, "doUserEvent() START: send a user event to the callback channel")
	callback <- a
	log.Log(INFO, "doUserEvent() END:   sent a user event to the callback channel")
	return
}

// 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.)
//
// this sets the channel to send user events back from the plugin
func Callback(guiCallback chan widget.Action) {
	callback = guiCallback
}

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