gocui/plugin.go

196 lines
4.2 KiB
Go
Raw Normal View History

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/toolkits/tree"
"go.wit.com/widget"
)
func newAdd(n *tree.Node) {
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK != nil {
log.Warn("Tree Add() sent a widget we aleady seem to have")
// this is done to protect the plugin being 'refreshed' with the
// widget binary tree. TODO: find a way to keep them in sync
return
}
n.TK = initWidget(n)
if n.WidgetType == widget.Root {
me.treeRoot = n
}
addWidget(n)
/*
TODO: removed while refactoring tree
if w.enable {
// don't change the color
} else {
w = n.TK.(*guiWidget)
}
*/
// w.setColor(&colorDisabled)
w := n.TK.(*guiWidget)
w.Show()
}
func newSetTitle(n *tree.Node, s string) {
newSetText(n, s)
}
func newSetLabel(n *tree.Node, s string) {
newSetText(n, s)
}
func newSetText(n *tree.Node, s string) {
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK == nil {
log.Warn("Tree sent an action on a widget we didn't seem to have.")
return
}
w := n.TK.(*guiWidget)
w.SetText(s)
}
func newAddText(n *tree.Node, s string) {
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK == nil {
log.Warn("Tree sent an action on a widget we didn't seem to have.")
return
}
w := n.TK.(*guiWidget)
w.AddText(s)
}
func newaction(n *tree.Node, atype widget.ActionType) {
log.Log(INFO, "newaction() START", atype)
if n == nil {
log.Warn("Tree Error: Add() sent n == nil")
return
}
if n.TK == nil {
log.Warn("Tree sent an action on a widget we didn't seem to have.")
// do this init here again? Probably something
// went wrong and we should reset the our while gocui.View tree
n.TK = initWidget(n)
}
w := n.TK.(*guiWidget)
switch atype {
case widget.Show:
w.Show()
case widget.Hide:
w.Hide()
case widget.Move:
log.Log(NOW, "attempt to move() =", atype, n.WidgetType, n.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, "newaction() UNHANDLED Action Type =", atype, "WidgetType =", n.WidgetType, "Name =", n.ProgName())
}
log.Log(INFO, "newaction() END", atype, n.String())
}
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.Hide()
w.Show()
}
}
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)
}
}