summaryrefslogtreecommitdiff
path: root/gocui/plugin.go
blob: d66e8f67b6a45442203fa17cb8d6e915f396574d (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
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)
	"go.wit.com/gui/widget"
)

func action(a *widget.Action) {
	log(logInfo, "action() START", a.WidgetId, a.ActionType, a.WidgetType, a.Name)
	n := me.rootNode.findWidgetId(a.WidgetId)
	var w *guiWidget
	if (n != nil) {
		w = n.tk
	}
	switch a.ActionType {
	case widget.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 widget.Show:
		if (a.B) {
			n.showView()
		} else {
			n.hideWidgets()
		}
	case widget.Set:
		if a.WidgetType == widget.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 id =", a.WidgetId, "type =", a.WidgetType, "Name =", a.Name)
				log(logError, "TODO: Set here. id =", a.WidgetId, "n.Name =", n.Name)
			} else {
				n.Set(a.A)
			}
		}
	case widget.SetText:
		n.SetText(a.S)
	case widget.AddText:
		n.AddText(a.S)
	case widget.Move:
		log(logNow, "attempt to move() =", a.ActionType, a.WidgetType, a.Name)
	case widget.CloseToolkit:
		log(logNow, "attempting to close the plugin and release stdout and stderr")
		standardExit()
	case widget.Enable:
		if n.Visible() {
			// widget was already shown
		} else {
			log(logInfo, "Setting Visable to true", a.Name)
			n.SetVisible(true)
		}
	case widget.Disable:
		if n.Visible() {
			log(logInfo, "Setting Visable to false", a.Name)
			n.SetVisible(false)
		} else {
			// widget was already hidden
		}
	default:
		log(logError, "action() ActionType =", a.ActionType, "WidgetType =", a.WidgetType, "Name =", 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) {
	var changed bool = false
	if (n == nil) {
		log(logNow, "widget is nil")
		return
	}
	if (n.Text != text) {
		n.Text = text
		changed = true
	}
	if (n.S != text) {
		n.S = text
		changed = true
	}
	if (! changed) {
		return
	}

	if (n.Visible()) {
		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)
	}
}