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

import (
	"os"
	// 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 Quit() {
	me.baseGui.Close()
}

func action(a *toolkit.Action) {
	log(logInfo, "action() START", a.WidgetId, a.ActionType, a.WidgetType, a.Name)
	w := findWidget(a.WidgetId, me.rootNode)
	switch a.ActionType {
	case toolkit.Add:
		if (w == nil) {
			w = makeWidget(a)
			w.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) {
			w.showView()
		} else {
			w.hideWidgets()
		}
	case toolkit.Set:
		w.Set(a.A)
	case toolkit.SetText:
		w.SetText(a.S)
	case toolkit.AddText:
		w.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")
		me.baseGui.Close()
		// defer outf.Close()
		setOutput(os.Stdout)
	default:
		log(logError, "action() Unknown =", a.ActionType, a.WidgetType, a.Name)
	}
	log(logInfo, "action() END")
}

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

func (w *cuiWidget) SetText(text string) {
	if (w == nil) {
		log(logNow, "widget is nil")
		return
	}
	w.text = text
	w.s = text
	w.textResize()
	w.deleteView()
	w.showView()
}

func (w *cuiWidget) Set(val any) {
	log(logInfo, "Set() value =", val)

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