new-gui/toolkit/gocui/plugin.go

168 lines
3.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)
"git.wit.org/wit/gui/toolkit"
)
func Quit() {
me.baseGui.Close()
}
// set the widget start width & height
// re-run this when things change to recalibrate the position of the gocui view rect
func (w *cuiWidget) setStartWH() {
log(logInfo, "setStartWH() w.id =", w.id, "w.name", w.name)
switch w.widgetType {
case toolkit.Root:
log(logInfo, "setStartWH() rootNode w.id =", w.id, "w.name", w.name)
w.isFake = true
w.setFake()
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
case toolkit.Flag:
w.isFake = true
w.setFake()
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
case toolkit.Window:
w.setTabWH()
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
case toolkit.Tab:
w.setTabWH()
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
case toolkit.Box:
w.isFake = true
w.setFake()
w.startW = w.parent.startW
w.startH = w.parent.startH
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
case toolkit.Grid:
w.isFake = true
w.setFake()
w.startW = w.parent.startW
w.startH = w.parent.startH
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
case toolkit.Group:
w.startW = w.parent.startW + 4
w.startH = w.parent.startH + 3
t := len(w.text)
w.gocuiSize.width = t + me.buttonPadding
w.gocuiSize.height = me.defaultHeight
w.gocuiSize.startW = w.startW
w.gocuiSize.startH = w.startH
w.setWH()
w.showWidgetPlacement(logNow, "StartWH:")
w.drawView()
return
default:
w.startW = w.parent.startW
w.startH = w.parent.startH
w.setWH()
}
}
/*
func (w *cuiWidget) setStartFromParent() {
p := w.parent
switch p.widgetType {
case toolkit.Box:
w.getBoxWH()
case toolkit.Group:
w.getGroupWH()
case toolkit.Grid:
w.getGridWH()
default:
w.gocuiSize.startW = p.startW
w.gocuiSize.startH = p.startH
}
w.gocuiSize.startW = w.startW
w.gocuiSize.startH = w.startH
w.startW = w.gocuiSize.startW
w.startH = w.gocuiSize.startH
w.showWidgetPlacement(logNow, "sSFP:")
}
*/
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:
w = setupWidget(a)
w.setStartWH()
case toolkit.Show:
if (a.B) {
w.drawView()
} 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)
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.drawView()
}
func (w *cuiWidget) Set(val any) {
log(logInfo, "Set() value =", val)
var a toolkit.Action
a.ActionType = toolkit.Set
switch v := val.(type) {
case bool:
w.b = val.(bool)
case string:
w.SetText(val.(string))
case int:
w.i = val.(int)
default:
log(logError, "Set() unknown type =", v, "a =", a)
}
}