gocui: more or less correct sizes

init to normal state (debug=off)
    size handling cleanups
    keep trying to fix grid widget
    move towards cmdline/auto loading of gocui

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-06 11:19:04 -05:00
parent c1edca0f0e
commit cd47489d85
7 changed files with 164 additions and 191 deletions

View File

@ -12,6 +12,8 @@ import (
var title string = "Demo Plugin Window" var title string = "Demo Plugin Window"
var outfile string = "/tmp/guilogfile" var outfile string = "/tmp/guilogfile"
var buttonCounter int = 5
func main() { func main() {
// time.Sleep(5 * time.Second) // time.Sleep(5 * time.Second)
// var w *gui.Node // var w *gui.Node
@ -46,8 +48,6 @@ func main() {
gui.StandardExit() gui.StandardExit()
} }
var counter int = 5
// This creates a window // This creates a window
func buttonWindow() { func buttonWindow() {
var w, t, g, more, more2 *gui.Node var w, t, g, more, more2 *gui.Node
@ -79,17 +79,13 @@ func buttonWindow() {
// this set the xterm and mate-terminal window title. maybe works generally? // this set the xterm and mate-terminal window title. maybe works generally?
fmt.Println("\033]0;" + title + "blah \007") fmt.Println("\033]0;" + title + "blah \007")
gui.StartS("gocui") gui.StartS("gocui")
})
g.NewButton("Redraw 'gocui'", func () {
fmt.Println("\033]0;" + title + "blah2 \007")
gui.Redraw("gocui") gui.Redraw("gocui")
}) })
g.NewButton("NewButton(more)", func () { g.NewButton("NewButton(more)", func () {
log.Println("new foobar 2. Adding button 'foobar 3'") log.Println("new foobar 2. Adding button 'foobar 3'")
name := "foobar " + strconv.Itoa(counter) name := "foobar " + strconv.Itoa(buttonCounter)
counter += 1 buttonCounter += 1
more.NewButton(name, func () { more.NewButton(name, func () {
log.Println("Got all the way to main() name =", name) log.Println("Got all the way to main() name =", name)
}) })
@ -97,8 +93,8 @@ func buttonWindow() {
g.NewButton("NewButton(more2)", func () { g.NewButton("NewButton(more2)", func () {
log.Println("new foobar 2. Adding button 'foobar 3'") log.Println("new foobar 2. Adding button 'foobar 3'")
name := "foobar " + strconv.Itoa(counter) name := "foobar " + strconv.Itoa(buttonCounter)
counter += 1 buttonCounter += 1
more2.NewButton(name, func () { more2.NewButton(name, func () {
log.Println("Got all the way to main() name =", name) log.Println("Got all the way to main() name =", name)
}) })
@ -106,8 +102,8 @@ func buttonWindow() {
g.NewButton("NewButton(more2 d)", func () { g.NewButton("NewButton(more2 d)", func () {
log.Println("new foobar 2. Adding button 'foobar 3'") log.Println("new foobar 2. Adding button 'foobar 3'")
name := "d" + strconv.Itoa(counter) name := "d" + strconv.Itoa(buttonCounter)
counter += 1 buttonCounter += 1
more2.NewButton(name, func () { more2.NewButton(name, func () {
log.Println("Got all the way to main() name =", name) log.Println("Got all the way to main() name =", name)
}) })
@ -115,8 +111,8 @@ func buttonWindow() {
g.NewButton("NewGroup()", func () { g.NewButton("NewGroup()", func () {
log.Println("new foobar 2. Adding button 'foobar 3'") log.Println("new foobar 2. Adding button 'foobar 3'")
name := "neat " + strconv.Itoa(counter) name := "neat " + strconv.Itoa(buttonCounter)
counter += 1 buttonCounter += 1
more.NewGroup(name) more.NewGroup(name)
}) })

View File

@ -16,8 +16,8 @@ func (w *cuiWidget) setFake() {
w.gocuiSize.width = t + 2 w.gocuiSize.width = t + 2
w.gocuiSize.height = me.defaultHeight w.gocuiSize.height = me.defaultHeight
w.gocuiSize.startW = fakeStartWidth w.gocuiSize.w0 = fakeStartWidth
w.gocuiSize.startH = fakeStartHeight w.gocuiSize.h0 = fakeStartHeight
fakeStartHeight += 3 fakeStartHeight += 3
if (fakeStartHeight > 24) { if (fakeStartHeight > 24) {
@ -68,15 +68,10 @@ func (w *cuiWidget) addWidget() {
t := len(w.text) t := len(w.text)
w.gocuiSize.width = t + me.buttonPadding w.gocuiSize.width = t + me.buttonPadding
w.gocuiSize.height = me.defaultHeight w.gocuiSize.height = me.defaultHeight
w.gocuiSize.startW = w.startW
w.gocuiSize.startH = w.startH
w.setWH()
return return
default: default:
w.startW = w.parent.startW w.startW = w.parent.startW
w.startH = w.parent.startH w.startH = w.parent.startH
w.setWH()
} }
w.showWidgetPlacement(logInfo, "addWidget()") w.showWidgetPlacement(logInfo, "addWidget()")
} }

View File

@ -19,18 +19,18 @@ func (w *cuiWidget) doWidgetClick() {
me.rootNode.redoColor(true) me.rootNode.redoColor(true)
case toolkit.Window: case toolkit.Window:
me.rootNode.hideWidgets() me.rootNode.hideWidgets()
w.drawBox() w.placeWidgets()
w.showWidgets() w.showWidgets()
case toolkit.Tab: case toolkit.Tab:
me.rootNode.hideWidgets() me.rootNode.hideWidgets()
w.drawBox() w.placeWidgets()
w.showWidgets() w.showWidgets()
case toolkit.Group: case toolkit.Group:
w.drawBox() w.placeWidgets()
w.toggleTree() w.toggleTree()
case toolkit.Grid: case toolkit.Grid:
me.rootNode.hideWidgets() me.rootNode.hideWidgets()
w.drawGrid() w.placeGrid()
w.showWidgets() w.showWidgets()
case toolkit.Box: case toolkit.Box:
// w.showWidgetPlacement(logNow, "drawTree()") // w.showWidgetPlacement(logNow, "drawTree()")
@ -39,7 +39,7 @@ func (w *cuiWidget) doWidgetClick() {
} else { } else {
log("BOX IS VERTICAL", w.name) log("BOX IS VERTICAL", w.name)
} }
w.drawBox() w.placeWidgets()
w.toggleTree() w.toggleTree()
default: default:
} }
@ -156,32 +156,21 @@ func ctrlDown(g *gocui.Gui, v *gocui.View) error {
if (found == nil) { if (found == nil) {
found = me.rootNode found = me.rootNode
} }
me.ctrlDown.gocuiSize.startW = found.startW found.setRealSize()
me.ctrlDown.gocuiSize.startH = found.startH
me.ctrlDown.gocuiSize.width = found.realWidth me.ctrlDown.gocuiSize.width = found.realWidth
me.ctrlDown.gocuiSize.height = found.realHeight me.ctrlDown.gocuiSize.height = found.realHeight
me.ctrlDown.gocuiSize.w0 = found.startW
me.ctrlDown.gocuiSize.h0 = found.startH
me.ctrlDown.setWH() me.ctrlDown.setWH()
if (me.ctrlDown.v == nil) { if (me.ctrlDown.v == nil) {
me.ctrlDown.text = found.text me.ctrlDown.text = found.text
me.ctrlDown.showWidgetPlacement(logNow, "drawTree()") me.ctrlDown.showWidgetPlacement(logNow, "ctrlDown:")
me.ctrlDown.drawView() me.ctrlDown.drawView()
} else { } else {
me.ctrlDown.deleteView() me.ctrlDown.deleteView()
} }
/*
v, err := g.SetView("ctrlDown", maxX/2-10, maxY/2, maxX/2+10, maxY/2+2, 0)
if (err != nil) {
log(logError, "ctrlDown() g.SetView() error:", err)
return
}
v.Clear()
v.SelBgColor = gocui.ColorCyan
v.SelFgColor = gocui.ColorBlack
fmt.Fprintln(v, l)
*/
log(logNow, "ctrlDown()", w, h) log(logNow, "ctrlDown()", w, h)
return nil return nil
} }

View File

@ -48,15 +48,10 @@ func (w *cuiWidget) showWidgetPlacement(b bool, s string) {
pId = w.parent.id pId = w.parent.id
} }
s1 = fmt.Sprintf("(wId,pId)=(%2d,%2d) ", w.id, pId) s1 = fmt.Sprintf("(wId,pId)=(%2d,%2d) ", w.id, pId)
s1 += fmt.Sprintf("start()=(%2d,%2d) ", w.startW, w.startH) s1 += fmt.Sprintf("s/n (%2d,%2d) (%2d,%2d) ", w.startW, w.startH, w.nextW, w.nextH)
s1 += fmt.Sprintf("size()=(%2d,%2d) ", w.realWidth, w.realHeight) s1 += fmt.Sprintf("size (%2d,%2d) ", w.realWidth, w.realHeight)
s1 += fmt.Sprintf("gocui()=(%2d,%2d,%2d,%2d,%2d,%2d) ", s1 += fmt.Sprintf("gocui=(%2d,%2d)(%2d,%2d,%2d,%2d)",
w.gocuiSize.width, w.gocuiSize.height, w.gocuiSize.width, w.gocuiSize.height,
w.gocuiSize.w0, w.gocuiSize.h0, w.gocuiSize.w1, w.gocuiSize.h1) w.gocuiSize.w0, w.gocuiSize.h0, w.gocuiSize.w1, w.gocuiSize.h1)
switch w.widgetType {
case toolkit.Grid:
s1 += fmt.Sprintf("next()=(%2d,%2d)", w.nextW, w.nextH)
default:
}
log(b, s1, s, w.widgetType, ",", w.name) // , "text=", w.text) log(b, s1, s, w.widgetType, ",", w.name) // , "text=", w.text)
} }

View File

@ -1,47 +1,53 @@
package main package main
import ( import (
"fmt"
// "github.com/awesome-gocui/gocui" // "github.com/awesome-gocui/gocui"
"git.wit.org/wit/gui/toolkit" "git.wit.org/wit/gui/toolkit"
) )
/* func (w *cuiWidget) placeBox() {
// find the start (w,h) for child a inside a box widget if (w.widgetType != toolkit.Box) {
func (w *cuiWidget) setBoxWH() { return
p := w.parent // the parent must be a box widget
// update parent gocuiSize
p.realWidth = 0
p.realHeight = 0
for _, child := range p.children {
p.realWidth += child.realWidth
p.realHeight += child.realHeight
} }
w.startW = w.parent.nextW
w.startH = w.parent.nextH
w.nextW = w.parent.nextW
w.nextH = w.parent.nextH
w.realWidth = 0
w.realHeight = 0
// compute child offset var maxW int
w.startW = p.startW var maxH int
w.startH = p.startH for _, child := range w.children {
for _, child := range p.children { w.showWidgetPlacement(logNow, "boxS()")
if (p.horizontal) { child.placeWidgets()
log("BOX IS HORIZONTAL (w,h)", w.startW, w.startH) if (w.horizontal) {
log("BOX IS HORIZONTAL (w,h)", w.startW, w.startH) log(logVerbose, "BOX IS HORIZONTAL")
log("BOX IS HORIZONTAL (w,h)", w.startW, w.startH) // expand based on the child width
w.startW += child.realWidth w.nextW += child.realWidth
w.realWidth += child.realWidth
} else { } else {
log("BOX IS VERTICAL (w,h)", w.startW, w.startH) log(logVerbose, "BOX IS VERTICAL")
log("BOX IS VERTICAL (w,h)", w.startW, w.startH) // expand based on the child height
log("BOX IS VERTICAL (w,h)", w.startW, w.startH) w.nextH += child.realHeight
w.startH += child.realHeight w.realHeight += child.realHeight
} }
if child == w { if (maxW < child.realWidth) {
return maxW = child.realWidth
}
if (maxH < child.realHeight) {
maxH = child.realHeight
} }
} }
return if (w.horizontal) {
w.realHeight = maxH
} else {
w.realWidth = maxW
}
w.showWidgetPlacement(logNow, "boxE()")
} }
*/
// find the start (w,h) for child a inside a Group widget
func (w *cuiWidget) getGroupWH() { func (w *cuiWidget) getGroupWH() {
p := w.parent // the parent must be a group widget p := w.parent // the parent must be a group widget
@ -66,7 +72,7 @@ func (w *cuiWidget) getGroupWH() {
return return
} }
func (w *cuiWidget) drawBox() { func (w *cuiWidget) placeWidgets() {
if (w == nil) { if (w == nil) {
return return
} }
@ -75,98 +81,99 @@ func (w *cuiWidget) drawBox() {
} }
p := w.parent p := w.parent
if (p == nil) { if (p == nil) {
log(logInfo, "redoBox()", w.id, "parent == nil") log(logInfo, "place()", w.id, "parent == nil")
return return
} }
switch w.widgetType { switch w.widgetType {
case toolkit.Window: case toolkit.Window:
// draw only one thing
for _, child := range w.children { for _, child := range w.children {
child.drawBox() w.startW = me.rawW
return w.startH = me.rawH
w.nextW = me.rawW
w.nextH = me.rawH
w.showWidgetPlacement(logNow, "place()")
child.placeWidgets()
if (w.realWidth < child.realWidth) {
w.realWidth = child.realWidth
}
if (w.realHeight < child.realHeight) {
w.realHeight = child.realHeight
}
w.showWidgetPlacement(logNow, "place()")
} }
case toolkit.Tab: case toolkit.Tab:
// draw only one thing
for _, child := range w.children { for _, child := range w.children {
child.drawBox() w.startW = me.rawW
return w.startH = me.rawH
w.nextW = me.rawW
w.nextH = me.rawH
w.showWidgetPlacement(logNow, "place()")
child.placeWidgets()
if (w.realWidth < child.realWidth) {
w.realWidth = child.realWidth
}
if (w.realHeight < child.realHeight) {
w.realHeight = child.realHeight
}
w.showWidgetPlacement(logNow, "place()")
} }
case toolkit.Grid: case toolkit.Grid:
w.startW = p.startW w.showWidgetPlacement(logNow, "place()")
w.startH = p.startH w.placeGrid()
w.drawGrid() w.showWidgetPlacement(logNow, "place()")
w.showWidgetPlacement(logNow, "drawBox:")
case toolkit.Box: case toolkit.Box:
w.startW = p.startW w.showWidgetPlacement(logNow, "place()")
w.startH = p.startH w.placeBox()
w.nextW = p.startW w.showWidgetPlacement(logNow, "place()")
w.nextH = p.startH
var maxW int
var maxH int
for _, child := range w.children {
child.drawBox()
if (w.horizontal) {
log("BOX IS HORIZONTAL")
// expand based on the child width
w.startW += child.realWidth
} else {
log("BOX IS VERTICAL")
// expand based on the child height
w.startH += child.realHeight
}
if (maxW < child.realWidth) {
maxW = child.realWidth
}
if (maxH < child.realHeight) {
maxH = child.realHeight
}
}
w.realWidth = maxW
w.realHeight = maxH
w.showWidgetPlacement(logNow, "drawBox:")
case toolkit.Group: case toolkit.Group:
w.startW = p.startW w.startW = p.nextW
w.startH = p.startH w.startH = p.nextH
w.nextW = p.startW w.nextW = p.nextW
w.nextH = p.startH w.nextH = p.nextH
w.gocuiSize.startW = w.startW
w.gocuiSize.startH = w.startH w.moveTo(p.nextW, p.nextH)
// set real width at the beginning
w.realWidth = w.gocuiSize.width w.realWidth = w.gocuiSize.width
w.realHeight = w.gocuiSize.height w.realHeight = w.gocuiSize.height
w.setWH()
w.startW = p.startW + 4 // indent the widgets for a group
w.startH = p.startH + 3 w.nextW = p.nextW + 4
w.nextH = p.nextH + 3
w.showWidgetPlacement(logNow, "place()")
var maxW int var maxW int
var maxH int
for _, child := range w.children { for _, child := range w.children {
child.drawBox() child.showWidgetPlacement(logNow, "place()")
// reset nextW to straight down child.placeWidgets()
w.startH += child.realHeight child.showWidgetPlacement(logNow, "place()")
// increment straight down
w.nextH += child.realHeight
w.realHeight += child.realHeight
// track largest width
if (maxW < child.realWidth) { if (maxW < child.realWidth) {
maxW = child.realWidth maxW = child.realWidth
} }
if (maxH < child.realHeight) {
maxH = child.realHeight
}
} }
// add real width of largest child
w.realWidth += maxW w.realWidth += maxW
w.realHeight += maxH w.showWidgetPlacement(logNow, "place()")
w.showWidgetPlacement(logNow, "drawBox:")
default: default:
w.startW = p.startW w.startW = p.nextW
w.startH = p.startH w.startH = p.nextH
w.gocuiSize.startW = w.startW w.nextW = p.nextW
w.gocuiSize.startH = w.startH w.nextH = p.nextH
w.gocuiSize.w0 = w.startW
w.gocuiSize.h0 = w.startH
w.setWH() w.setWH()
w.showWidgetPlacement(logNow, "drawBox:") w.showWidgetPlacement(logNow, "place()")
} }
} }
func (w *cuiWidget) setWH() { func (w *cuiWidget) setWH() {
w.gocuiSize.w0 = w.gocuiSize.startW
w.gocuiSize.h0 = w.gocuiSize.startH
w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width
w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height
} }
@ -175,15 +182,22 @@ func (w *cuiWidget) moveTo(leftW int, topH int) {
if (w.isFake) { if (w.isFake) {
return return
} }
w.gocuiSize.startW = leftW w.gocuiSize.w0 = leftW
w.gocuiSize.startH = topH w.gocuiSize.h0 = topH
w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width
w.setWH() w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height
w.showWidgetPlacement(logNow, "moveTo()") w.showWidgetPlacement(logInfo, "moveTo()")
} }
func (w *cuiWidget) drawGrid() { func (w *cuiWidget) placeGrid() {
w.showWidgetPlacement(logNow, "gridBounds:") w.showWidgetPlacement(logNow, "grid0:")
if (w.widgetType != toolkit.Grid) {
return
}
w.startW = w.parent.nextW
w.startH = w.parent.nextH
w.nextW = w.parent.nextW
w.nextH = w.parent.nextH
var wCount int = 0 var wCount int = 0
var hCount int = 0 var hCount int = 0
@ -191,7 +205,6 @@ func (w *cuiWidget) drawGrid() {
// increment for the next child // increment for the next child
w.nextW = w.startW + wCount * 20 w.nextW = w.startW + wCount * 20
w.nextH = w.startH + hCount * 2 w.nextH = w.startH + hCount * 2
// child.drawBox()
// set the child's realWidth, and grid offset // set the child's realWidth, and grid offset
child.parentH = hCount child.parentH = hCount
@ -202,8 +215,8 @@ func (w *cuiWidget) drawGrid() {
if (w.heights[hCount] < child.realHeight) { if (w.heights[hCount] < child.realHeight) {
w.heights[hCount] = child.realHeight w.heights[hCount] = child.realHeight
} }
log(logNow, "redoBox(GRID) (w,h count)", wCount, hCount, "(X,Y)", w.x, w.y, w.name) log(logVerbose, "grid1: (w,h count)", wCount, hCount, "(X,Y)", w.x, w.y, w.name)
child.showWidgetPlacement(logNow, "grid:") child.showWidgetPlacement(logNow, "grid1: " + fmt.Sprintf("next()=(%2d,%2d)", w.nextW, w.nextH))
if ((wCount + 1) < w.y) { if ((wCount + 1) < w.y) {
wCount += 1 wCount += 1
@ -224,11 +237,11 @@ func (w *cuiWidget) drawGrid() {
} }
for _, child := range w.children { for _, child := range w.children {
child.showWidgetPlacement(logVerbose, "gridBounds:") child.showWidgetPlacement(logVerbose, "grid2:")
var totalW, totalH int var totalW, totalH int
for i, val := range w.widths { for i, val := range w.widths {
if (i < child.parentW) { if (i < child.parentW) {
log(logVerbose, "gridBounds() (w, widths[])", i, val) log(logVerbose, "grid2: (w, widths[])", i, val)
totalW += w.widths[i] totalW += w.widths[i]
} }
} }
@ -239,18 +252,16 @@ func (w *cuiWidget) drawGrid() {
} }
// the new corner to move the child to // the new corner to move the child to
realW := w.nextW + totalW w.nextW = w.startW + totalW
realH := w.nextH + totalH w.nextH = w.startH + totalH
log(logNow, "gridBounds()", child.id, "parent (W,H) =", child.parentW, child.parentH, child.placeWidgets()
"total (W,H) =", totalW, totalH, child.showWidgetPlacement(logInfo, "grid2:")
"real (W,H) =", realW, realH)
w.startW = realW
w.startH = realH
child.drawBox()
child.showWidgetPlacement(logInfo, "gridBounds:")
log(logInfo) log(logInfo)
} }
// w.updateLogicalSizes() // w.updateLogicalSizes()
w.showWidgetPlacement(logNow, "gridBounds:") w.showWidgetPlacement(logNow, "grid3:")
}
func (w *cuiWidget) setRealSize() {
} }

View File

@ -60,8 +60,8 @@ var (
// corner starts at in the upper left corner // corner starts at in the upper left corner
type rectType struct { type rectType struct {
// where the widget should calculate it's existance from // where the widget should calculate it's existance from
startW int // startW int
startH int // startH int
// the actual size // the actual size
width int width int
@ -85,10 +85,14 @@ type cuiWidget struct {
// visable bool // track if it's currently supposed to be shown // visable bool // track if it's currently supposed to be shown
isFake bool // widget types like 'box' are 'false' isFake bool // widget types like 'box' are 'false'
// where the widget should add children // where the widget's real corner is
startW int startW int
startH int startH int
// where the next child should be placed
nextW int
nextH int
// the widget size to reserve or things will overlap // the widget size to reserve or things will overlap
realWidth int realWidth int
realHeight int realHeight int
@ -104,10 +108,6 @@ type cuiWidget struct {
parentW int parentW int
parentH int parentH int
// deprecate
nextW int
nextH int
// things from toolkit/action // things from toolkit/action
b bool b bool
i int i int

View File

@ -53,33 +53,20 @@ func (w *cuiWidget) showWidgets() {
} }
func (w *cuiWidget) setTabWH() { func (w *cuiWidget) setTabWH() {
// set the start and size of the tab gocui button
t := len(w.text) t := len(w.text)
w.gocuiSize.width = t + me.buttonPadding w.gocuiSize.width = t + me.buttonPadding
w.gocuiSize.height = me.defaultHeight w.gocuiSize.height = me.defaultHeight
w.gocuiSize.w0 = me.rootNode.nextW
w.gocuiSize.h0 = me.rootNode.nextH
w.gocuiSize.startW = me.rootNode.startW // move the rootNode width over for the next window or tab
w.gocuiSize.startH = me.rootNode.startH me.rootNode.nextW += w.gocuiSize.width + me.padW
w.startW = me.rawW w.startW = me.rawW
w.startH = me.rawH w.startH = me.rawH
w.nextW = me.rawW
var f func (widget *cuiWidget) w.nextH = me.rawH
// find buttons that are below where the mouse button click
f = func(widget *cuiWidget) {
if (widget == w) {
return
}
if ((widget.widgetType == toolkit.Window) || (widget.widgetType == toolkit.Tab)) {
w.gocuiSize.startW += widget.gocuiSize.width + me.padW
}
for _, child := range widget.children {
f(child)
}
}
f(me.rootNode)
w.setWH() w.setWH()
w.showWidgetPlacement(logNow, "setTabWH:") w.showWidgetPlacement(logNow, "setTabWH:")