summaryrefslogtreecommitdiff
path: root/toolkit/gocui/plugin.go
blob: 6d597b49027f4d7707e1b135a6881a8880e5c4b3 (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
package main

import (
	// if you include more than just this import
	// then your plugin might be doing something un-ideal (just a guess from 2023/02/27)
	"git.wit.org/wit/gui/toolkit"
)

func action(a *toolkit.Action) {
	log(logInfo, "action() START", a.WidgetId, a.ActionType, a.WidgetType, a.Name)
	n := me.rootNode.findWidgetId(a.WidgetId)
	var w *cuiWidget
	if (n != nil) {
		w = n.tk
	}
	switch a.ActionType {
	case toolkit.Add:
		if (w == nil) {
			n := addNode(a)
			// w = n.tk
			n.addWidget()
		} else {
			// this is done to protect the plugin being 'refreshed' with the
			// widget binary tree. TODO: find a way to keep them in sync
			log(logError, "action() Add ignored for already defined widget",
				a.WidgetId, a.ActionType, a.WidgetType, a.Name)
		}
	case toolkit.Show:
		if (a.B) {
			n.showView()
		} else {
			n.hideWidgets()
		}
	case toolkit.Set:
		if a.WidgetType == toolkit.Flag {
			log(logNow, "TODO: set flag here", a.ActionType, a.WidgetType, a.Name)
			log(logNow, "TODO: n.WidgetType =", n.WidgetType, "n.Name =", a.Name)
		} else {
			if (a.A == nil) {
				log(logError, "TODO: Set here. a == nil", a.ActionType, "WidgetType =", a.WidgetType, "Name =", a.Name)
			} else {
				n.Set(a.A)
			}
		}
	case toolkit.SetText:
		n.SetText(a.S)
	case toolkit.AddText:
		n.AddText(a.S)
	case toolkit.Move:
		log(logNow, "attempt to move() =", a.ActionType, a.WidgetType, a.Name)
	case toolkit.CloseToolkit:
		log(logNow, "attempting to close the plugin and release stdout and stderr")
		standardExit()
	default:
		log(logError, "action() Unknown =", a.ActionType, a.WidgetType, a.Name)
	}
	log(logInfo, "action() END")
}

func (n *node) AddText(text string) {
	if (n == nil) {
		log(logNow, "widget is nil")
		return
	}
	n.vals = append(n.vals, text)
	for i, s := range n.vals {
		log(logNow, "AddText()", n.Name, i, s)
	}
	n.SetText(text)
}

func (n *node) SetText(text string) {
	if (n == nil) {
		log(logNow, "widget is nil")
		return
	}
	n.S = text
	n.Text = text

	n.textResize()
	n.deleteView()
	n.showView()
}

func (n *node) Set(val any) {
	// w := n.tk
	log(logInfo, "Set() value =", val)

	switch v := val.(type) {
	case bool:
		n.B = val.(bool)
		n.setCheckbox(val.(bool))
	case string:
		n.SetText(val.(string))
	case int:
		n.I = val.(int)
	default:
		log(logError, "Set() unknown type =", val, v)
	}
}

// this passes the user event back from the plugin
func (n *node) doUserEvent() {
	if (me.callback == nil) {
		log(logError, "doUserEvent() no callback channel was configured")
		return
	}
	var a toolkit.Action
	a.WidgetId = n.WidgetId
	a.Name = n.Name
	a.Text = n.Text
	a.B = n.B
	a.ActionType = toolkit.User
	log(logInfo, "doUserEvent() START:   send a button click callback()", a.WidgetId, a.Name)
	me.callback <- a
	log(logInfo, "doUserEvent() END:     sent a button click callback()", a.WidgetId, a.Name)
}