gocui/plugin.go

185 lines
4.5 KiB
Go

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/log"
"go.wit.com/widget"
)
func action(a widget.Action) {
log.Log(INFO, "action() START", a.WidgetId, a.ActionType, a.WidgetType, a.ProgName)
// n := me.rootNode.findWidgetId(a.WidgetId)
n := me.treeRoot.FindWidgetId(a.WidgetId)
var w *guiWidget
if n != nil {
w = n.TK.(*guiWidget)
}
switch a.ActionType {
case widget.Add:
if w == nil {
n := me.myTree.AddNode(&a)
if n == nil {
log.Warn("WTF")
panic("WTF")
}
n.TK = initWidget(n)
if n.WidgetType == widget.Root {
me.treeRoot = n
}
addWidget(n)
w = n.TK.(*guiWidget)
if w.enable {
// don't change the color
} else {
w.setColor(&colorDisabled)
}
if w.hidden {
w.SetVisible(false)
}
} 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.Log(ERROR, "action() Add ignored for already defined widget",
a.WidgetId, a.ActionType, a.WidgetType, a.ProgName)
}
case widget.Show:
w.node.State.Hidden = false
if w.Visible() {
// widget was already shown
} else {
log.Log(INFO, "Setting Visible to true", a.ProgName)
w.SetVisible(true)
}
w.showView()
case widget.Hide:
w.node.State.Hidden = true
log.Log(NOW, "HIDE HERE. a.State.Hidden =", a.State.Hidden)
log.Log(NOW, "HIDE HERE. w.hidden =", w.hidden)
if w.Visible() {
log.Log(INFO, "Setting Visible to false", a.ProgName)
w.SetVisible(false)
} else {
// widget was already hidden
}
case widget.Set:
if a.WidgetType == widget.Flag {
log.Log(NOW, "TODO: set flag here", a.ActionType, a.WidgetType, a.ProgName)
log.Log(NOW, "TODO: n.WidgetType =", n.WidgetType, "n.String() =", a.ProgName)
} else {
if a.Value == nil {
log.Log(ERROR, "TODO: Set here. a == nil id =", a.WidgetId, "type =", a.WidgetType, "Name =", a.ProgName)
log.Log(ERROR, "TODO: Set here. id =", a.WidgetId, "n.String() =", n.String())
} else {
w.Set(a.Value)
}
}
case widget.SetText:
w.SetText(widget.GetString(a.Value))
case widget.AddText:
w.AddText(widget.GetString(a.Value))
case widget.Move:
log.Log(NOW, "attempt to move() =", a.ActionType, a.WidgetType, a.ProgName)
case widget.ToolkitClose:
log.Log(NOW, "attempting to close the plugin and release stdout and stderr")
standardClose()
case widget.Enable:
w.enable = true
w.enableColor()
case widget.Disable:
w.enable = false
w.disableColor()
case widget.Delete:
if w == nil {
return
} else {
w.hideWidgets()
w.deleteNode()
}
n.DeleteNode()
wRoot := me.treeRoot.TK.(*guiWidget)
wRoot.redoWindows(0, 0)
default:
log.Log(ERROR, "action() UNKNOWN Action Type =", a.ActionType, "WidgetType =", a.WidgetType, "Name =", a.ProgName)
}
log.Log(INFO, "action() END")
}
func (w *guiWidget) deleteGocuiViews() {
if w.v == nil {
// no gocui view to delete for this widget
} else {
me.baseGui.DeleteView(w.cuiName)
w.v = nil
}
for _, child := range w.children {
child.deleteGocuiViews()
}
}
func (w *guiWidget) deleteNode() {
p := w.parent
for i, child := range p.children {
log.Log(NOW, "parent has child:", i, child.cuiName, child.String())
if w == child {
log.Log(NOW, "Found child ==", i, child.cuiName, child.String())
log.Log(NOW, "Found n ==", i, w.cuiName, w.String())
p.children = append(p.children[:i], p.children[i+1:]...)
}
}
for i, child := range p.children {
log.Log(NOW, "parent now has child:", i, child.cuiName, child.String())
}
w.deleteGocuiViews()
}
func (w *guiWidget) AddText(text string) {
if w == nil {
log.Log(NOW, "widget is nil")
return
}
w.vals = append(w.vals, text)
for i, s := range w.vals {
log.Log(NOW, "AddText()", w.String(), i, s)
}
w.SetText(text)
}
func (w *guiWidget) SetText(text string) {
var changed bool = false
if w == nil {
log.Log(NOW, "widget is nil")
return
}
if w.labelN != text {
w.labelN = text
changed = true
}
if !changed {
return
}
if w.Visible() {
w.textResize()
w.deleteView()
w.showView()
}
}
func (w *guiWidget) Set(val any) {
if w == nil {
log.Log(WARN, "Set() w == nil. val =", val)
return
}
log.Log(INFO, "Set() value =", val)
w.value = val.(string)
if w.node.WidgetType == widget.Checkbox {
w.node.State.Checked = widget.GetBool(val)
w.setCheckbox()
}
if w.node.WidgetType == widget.Label {
w.labelN = widget.GetString(val)
}
}