2024-01-18 00:08:37 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-01-28 21:15:15 -06:00
|
|
|
"strconv"
|
2024-01-18 00:08:37 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/awesome-gocui/gocui"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
2024-01-18 04:10:08 -06:00
|
|
|
"go.wit.com/widget"
|
2024-01-18 00:08:37 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func splitLines(s string) []string {
|
|
|
|
var lines []string
|
|
|
|
sc := bufio.NewScanner(strings.NewReader(s))
|
|
|
|
for sc.Scan() {
|
|
|
|
lines = append(lines, sc.Text())
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) textResize() bool {
|
|
|
|
// w := n.tk
|
2024-01-18 00:08:37 -06:00
|
|
|
var width, height int = 0, 0
|
|
|
|
var changed bool = false
|
|
|
|
|
2024-01-28 03:33:08 -06:00
|
|
|
for i, s := range splitLines(w.labelN) {
|
2024-01-18 00:08:37 -06:00
|
|
|
log.Log(INFO, "textResize() len =", len(s), i, s)
|
|
|
|
if width < len(s) {
|
|
|
|
width = len(s)
|
|
|
|
}
|
|
|
|
height += 1
|
|
|
|
}
|
|
|
|
if w.gocuiSize.w1 != w.gocuiSize.w0+width+me.FramePadW {
|
|
|
|
w.gocuiSize.w1 = w.gocuiSize.w0 + width + me.FramePadW
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
if w.gocuiSize.h1 != w.gocuiSize.h0+height+me.FramePadH {
|
|
|
|
w.gocuiSize.h1 = w.gocuiSize.h0 + height + me.FramePadH
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
if changed {
|
2024-02-01 10:06:09 -06:00
|
|
|
// w.showWidgetPlacement("textResize() changed")
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
|
|
|
return changed
|
|
|
|
}
|
|
|
|
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) hideView() {
|
2024-01-28 20:15:59 -06:00
|
|
|
w.deleteView()
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// display's the text of the widget in gocui
|
|
|
|
// will create a new gocui view if there isn't one or if it has been moved
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) showView() {
|
2024-01-18 00:08:37 -06:00
|
|
|
if w.cuiName == "" {
|
|
|
|
log.Log(ERROR, "showView() w.cuiName was not set for widget", w)
|
2024-01-28 21:15:15 -06:00
|
|
|
w.cuiName = strconv.Itoa(w.node.WidgetId) + " TK"
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
2024-01-28 21:15:15 -06:00
|
|
|
log.Log(INFO, "showView() labelN =", w.labelN)
|
2024-01-18 00:08:37 -06:00
|
|
|
|
2024-02-02 11:47:32 -06:00
|
|
|
w.recreateView()
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// create or recreate the gocui widget visible
|
|
|
|
// deletes the old view if it exists and recreates it
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) recreateView() {
|
2024-01-18 00:08:37 -06:00
|
|
|
var err error
|
2024-01-28 21:15:15 -06:00
|
|
|
log.Log(INFO, "recreateView() START", w.WidgetType, w.String())
|
2024-01-18 00:08:37 -06:00
|
|
|
if me.baseGui == nil {
|
|
|
|
log.Log(ERROR, "recreateView() ERROR: me.baseGui == nil", w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this deletes the button from gocui
|
|
|
|
me.baseGui.DeleteView(w.cuiName)
|
|
|
|
w.v = nil
|
|
|
|
|
2024-02-02 11:47:32 -06:00
|
|
|
w.textResize()
|
2024-01-18 00:08:37 -06:00
|
|
|
a := w.gocuiSize.w0
|
|
|
|
b := w.gocuiSize.h0
|
|
|
|
c := w.gocuiSize.w1
|
|
|
|
d := w.gocuiSize.h1
|
|
|
|
|
|
|
|
w.v, err = me.baseGui.SetView(w.cuiName, a, b, c, d, 0)
|
|
|
|
if err == nil {
|
2024-01-28 13:14:43 -06:00
|
|
|
w.showWidgetPlacement("recreateView()")
|
2024-01-18 00:08:37 -06:00
|
|
|
log.Log(ERROR, "recreateView() internal plugin error err = nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !errors.Is(err, gocui.ErrUnknownView) {
|
2024-01-28 13:14:43 -06:00
|
|
|
w.showWidgetPlacement("recreateView()")
|
2024-01-18 00:08:37 -06:00
|
|
|
log.Log(ERROR, "recreateView() internal plugin error error.IS()", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// this sets up the keybinding for the name of the window
|
|
|
|
// does this really need to be done? I think we probably already
|
|
|
|
// know everything about where all the widgets are so we could bypass
|
|
|
|
// the gocui package and just handle all the mouse events internally here (?)
|
|
|
|
// for now, the w.v.Name is a string "1", "2", "3", etc from the widgetId
|
|
|
|
|
|
|
|
// set the binding for this gocui view now that it has been created
|
|
|
|
// gocui handles overlaps of views so it will run on the view that is clicked on
|
|
|
|
me.baseGui.SetKeybinding(w.v.Name(), gocui.MouseLeft, gocui.ModNone, click)
|
|
|
|
|
|
|
|
// this actually sends the text to display to gocui
|
|
|
|
w.v.Wrap = true
|
|
|
|
w.v.Frame = w.frame
|
|
|
|
w.v.Clear()
|
2024-01-28 03:33:08 -06:00
|
|
|
fmt.Fprint(w.v, w.labelN)
|
2024-01-28 13:14:43 -06:00
|
|
|
// n.showWidgetPlacement("n.String()=" + n.String() + " n.tk.label=" + n.tk.label + " " + w.cuiName)
|
2024-01-18 00:08:37 -06:00
|
|
|
// n.dumpWidget("jwc 2")
|
|
|
|
|
|
|
|
// if you don't do this here, it will be black & white only
|
|
|
|
if w.color != nil {
|
|
|
|
w.v.FrameColor = w.color.frame
|
|
|
|
w.v.FgColor = w.color.fg
|
|
|
|
w.v.BgColor = w.color.bg
|
|
|
|
w.v.SelFgColor = w.color.selFg
|
|
|
|
w.v.SelBgColor = w.color.selBg
|
|
|
|
}
|
2024-01-28 21:15:15 -06:00
|
|
|
log.Log(INFO, "recreateView() END")
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
|
|
|
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) hideWidgets() {
|
2024-01-30 01:20:36 -06:00
|
|
|
if w == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-18 00:08:37 -06:00
|
|
|
w.isCurrent = false
|
2024-01-28 02:20:31 -06:00
|
|
|
switch w.node.WidgetType {
|
2024-01-18 00:08:37 -06:00
|
|
|
case widget.Root:
|
|
|
|
case widget.Flag:
|
|
|
|
case widget.Window:
|
|
|
|
case widget.Box:
|
|
|
|
case widget.Grid:
|
|
|
|
default:
|
2024-01-28 02:20:31 -06:00
|
|
|
w.hideView()
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
2024-01-28 02:20:31 -06:00
|
|
|
for _, child := range w.children {
|
2024-01-18 00:08:37 -06:00
|
|
|
child.hideWidgets()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 23:39:33 -06:00
|
|
|
func hideFake() {
|
|
|
|
var w *guiWidget
|
|
|
|
w = me.treeRoot.TK.(*guiWidget)
|
|
|
|
w.hideFake()
|
|
|
|
}
|
|
|
|
|
|
|
|
func showFake() {
|
|
|
|
var w *guiWidget
|
|
|
|
w = me.treeRoot.TK.(*guiWidget)
|
|
|
|
w.showFake()
|
|
|
|
}
|
|
|
|
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) hideFake() {
|
2024-01-18 00:08:37 -06:00
|
|
|
if w.isFake {
|
2024-01-28 02:20:31 -06:00
|
|
|
w.hideView()
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
2024-01-28 02:20:31 -06:00
|
|
|
for _, child := range w.children {
|
2024-01-18 00:08:37 -06:00
|
|
|
child.hideFake()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) showFake() {
|
2024-01-18 00:08:37 -06:00
|
|
|
if w.isFake {
|
2024-01-28 02:20:31 -06:00
|
|
|
// w.setFake()
|
|
|
|
w.showView()
|
2024-01-28 13:14:43 -06:00
|
|
|
w.showWidgetPlacement("showFake:")
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
2024-01-28 02:20:31 -06:00
|
|
|
for _, child := range w.children {
|
2024-01-18 00:08:37 -06:00
|
|
|
child.showFake()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 02:20:31 -06:00
|
|
|
func (w *guiWidget) showWidgets() {
|
2024-01-18 00:08:37 -06:00
|
|
|
if w.isFake {
|
|
|
|
// don't display by default
|
|
|
|
} else {
|
2024-02-04 10:08:08 -06:00
|
|
|
if w.node.State.Hidden {
|
|
|
|
// don't display hidden views
|
|
|
|
} else {
|
|
|
|
w.showView()
|
|
|
|
}
|
2024-01-18 00:08:37 -06:00
|
|
|
}
|
2024-01-28 02:20:31 -06:00
|
|
|
for _, child := range w.children {
|
2024-01-18 00:08:37 -06:00
|
|
|
child.showWidgets()
|
|
|
|
}
|
|
|
|
}
|