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 outfile string = "/tmp/guilogfile"
var buttonCounter int = 5
func main() {
// time.Sleep(5 * time.Second)
// var w *gui.Node
@ -46,8 +48,6 @@ func main() {
gui.StandardExit()
}
var counter int = 5
// This creates a window
func buttonWindow() {
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?
fmt.Println("\033]0;" + title + "blah \007")
gui.StartS("gocui")
})
g.NewButton("Redraw 'gocui'", func () {
fmt.Println("\033]0;" + title + "blah2 \007")
gui.Redraw("gocui")
})
g.NewButton("NewButton(more)", func () {
log.Println("new foobar 2. Adding button 'foobar 3'")
name := "foobar " + strconv.Itoa(counter)
counter += 1
name := "foobar " + strconv.Itoa(buttonCounter)
buttonCounter += 1
more.NewButton(name, func () {
log.Println("Got all the way to main() name =", name)
})
@ -97,8 +93,8 @@ func buttonWindow() {
g.NewButton("NewButton(more2)", func () {
log.Println("new foobar 2. Adding button 'foobar 3'")
name := "foobar " + strconv.Itoa(counter)
counter += 1
name := "foobar " + strconv.Itoa(buttonCounter)
buttonCounter += 1
more2.NewButton(name, func () {
log.Println("Got all the way to main() name =", name)
})
@ -106,8 +102,8 @@ func buttonWindow() {
g.NewButton("NewButton(more2 d)", func () {
log.Println("new foobar 2. Adding button 'foobar 3'")
name := "d" + strconv.Itoa(counter)
counter += 1
name := "d" + strconv.Itoa(buttonCounter)
buttonCounter += 1
more2.NewButton(name, func () {
log.Println("Got all the way to main() name =", name)
})
@ -115,8 +111,8 @@ func buttonWindow() {
g.NewButton("NewGroup()", func () {
log.Println("new foobar 2. Adding button 'foobar 3'")
name := "neat " + strconv.Itoa(counter)
counter += 1
name := "neat " + strconv.Itoa(buttonCounter)
buttonCounter += 1
more.NewGroup(name)
})

View File

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

View File

@ -19,18 +19,18 @@ func (w *cuiWidget) doWidgetClick() {
me.rootNode.redoColor(true)
case toolkit.Window:
me.rootNode.hideWidgets()
w.drawBox()
w.placeWidgets()
w.showWidgets()
case toolkit.Tab:
me.rootNode.hideWidgets()
w.drawBox()
w.placeWidgets()
w.showWidgets()
case toolkit.Group:
w.drawBox()
w.placeWidgets()
w.toggleTree()
case toolkit.Grid:
me.rootNode.hideWidgets()
w.drawGrid()
w.placeGrid()
w.showWidgets()
case toolkit.Box:
// w.showWidgetPlacement(logNow, "drawTree()")
@ -39,7 +39,7 @@ func (w *cuiWidget) doWidgetClick() {
} else {
log("BOX IS VERTICAL", w.name)
}
w.drawBox()
w.placeWidgets()
w.toggleTree()
default:
}
@ -156,32 +156,21 @@ func ctrlDown(g *gocui.Gui, v *gocui.View) error {
if (found == nil) {
found = me.rootNode
}
me.ctrlDown.gocuiSize.startW = found.startW
me.ctrlDown.gocuiSize.startH = found.startH
found.setRealSize()
me.ctrlDown.gocuiSize.width = found.realWidth
me.ctrlDown.gocuiSize.height = found.realHeight
me.ctrlDown.gocuiSize.w0 = found.startW
me.ctrlDown.gocuiSize.h0 = found.startH
me.ctrlDown.setWH()
if (me.ctrlDown.v == nil) {
me.ctrlDown.text = found.text
me.ctrlDown.showWidgetPlacement(logNow, "drawTree()")
me.ctrlDown.showWidgetPlacement(logNow, "ctrlDown:")
me.ctrlDown.drawView()
} else {
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)
return nil
}

View File

@ -48,15 +48,10 @@ func (w *cuiWidget) showWidgetPlacement(b bool, s string) {
pId = w.parent.id
}
s1 = fmt.Sprintf("(wId,pId)=(%2d,%2d) ", w.id, pId)
s1 += fmt.Sprintf("start()=(%2d,%2d) ", w.startW, w.startH)
s1 += fmt.Sprintf("size()=(%2d,%2d) ", w.realWidth, w.realHeight)
s1 += fmt.Sprintf("gocui()=(%2d,%2d,%2d,%2d,%2d,%2d) ",
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("gocui=(%2d,%2d)(%2d,%2d,%2d,%2d)",
w.gocuiSize.width, w.gocuiSize.height,
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)
}

View File

@ -1,47 +1,53 @@
package main
import (
"fmt"
// "github.com/awesome-gocui/gocui"
"git.wit.org/wit/gui/toolkit"
)
/*
// find the start (w,h) for child a inside a box widget
func (w *cuiWidget) setBoxWH() {
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
func (w *cuiWidget) placeBox() {
if (w.widgetType != toolkit.Box) {
return
}
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
w.startW = p.startW
w.startH = p.startH
for _, child := range p.children {
if (p.horizontal) {
log("BOX IS HORIZONTAL (w,h)", w.startW, w.startH)
log("BOX IS HORIZONTAL (w,h)", w.startW, w.startH)
log("BOX IS HORIZONTAL (w,h)", w.startW, w.startH)
w.startW += child.realWidth
var maxW int
var maxH int
for _, child := range w.children {
w.showWidgetPlacement(logNow, "boxS()")
child.placeWidgets()
if (w.horizontal) {
log(logVerbose, "BOX IS HORIZONTAL")
// expand based on the child width
w.nextW += child.realWidth
w.realWidth += child.realWidth
} else {
log("BOX IS VERTICAL (w,h)", w.startW, w.startH)
log("BOX IS VERTICAL (w,h)", w.startW, w.startH)
log("BOX IS VERTICAL (w,h)", w.startW, w.startH)
w.startH += child.realHeight
log(logVerbose, "BOX IS VERTICAL")
// expand based on the child height
w.nextH += child.realHeight
w.realHeight += child.realHeight
}
if child == w {
return
if (maxW < child.realWidth) {
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() {
p := w.parent // the parent must be a group widget
@ -66,7 +72,7 @@ func (w *cuiWidget) getGroupWH() {
return
}
func (w *cuiWidget) drawBox() {
func (w *cuiWidget) placeWidgets() {
if (w == nil) {
return
}
@ -75,98 +81,99 @@ func (w *cuiWidget) drawBox() {
}
p := w.parent
if (p == nil) {
log(logInfo, "redoBox()", w.id, "parent == nil")
log(logInfo, "place()", w.id, "parent == nil")
return
}
switch w.widgetType {
case toolkit.Window:
// draw only one thing
for _, child := range w.children {
child.drawBox()
return
w.startW = me.rawW
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:
// draw only one thing
for _, child := range w.children {
child.drawBox()
return
w.startW = me.rawW
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:
w.startW = p.startW
w.startH = p.startH
w.drawGrid()
w.showWidgetPlacement(logNow, "drawBox:")
w.showWidgetPlacement(logNow, "place()")
w.placeGrid()
w.showWidgetPlacement(logNow, "place()")
case toolkit.Box:
w.startW = p.startW
w.startH = p.startH
w.nextW = p.startW
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:")
w.showWidgetPlacement(logNow, "place()")
w.placeBox()
w.showWidgetPlacement(logNow, "place()")
case toolkit.Group:
w.startW = p.startW
w.startH = p.startH
w.nextW = p.startW
w.nextH = p.startH
w.gocuiSize.startW = w.startW
w.gocuiSize.startH = w.startH
w.startW = p.nextW
w.startH = p.nextH
w.nextW = p.nextW
w.nextH = p.nextH
w.moveTo(p.nextW, p.nextH)
// set real width at the beginning
w.realWidth = w.gocuiSize.width
w.realHeight = w.gocuiSize.height
w.setWH()
w.startW = p.startW + 4
w.startH = p.startH + 3
// indent the widgets for a group
w.nextW = p.nextW + 4
w.nextH = p.nextH + 3
w.showWidgetPlacement(logNow, "place()")
var maxW int
var maxH int
for _, child := range w.children {
child.drawBox()
// reset nextW to straight down
w.startH += child.realHeight
child.showWidgetPlacement(logNow, "place()")
child.placeWidgets()
child.showWidgetPlacement(logNow, "place()")
// increment straight down
w.nextH += child.realHeight
w.realHeight += child.realHeight
// track largest width
if (maxW < child.realWidth) {
maxW = child.realWidth
}
if (maxH < child.realHeight) {
maxH = child.realHeight
}
}
// add real width of largest child
w.realWidth += maxW
w.realHeight += maxH
w.showWidgetPlacement(logNow, "drawBox:")
w.showWidgetPlacement(logNow, "place()")
default:
w.startW = p.startW
w.startH = p.startH
w.gocuiSize.startW = w.startW
w.gocuiSize.startH = w.startH
w.startW = p.nextW
w.startH = p.nextH
w.nextW = p.nextW
w.nextH = p.nextH
w.gocuiSize.w0 = w.startW
w.gocuiSize.h0 = w.startH
w.setWH()
w.showWidgetPlacement(logNow, "drawBox:")
w.showWidgetPlacement(logNow, "place()")
}
}
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.h1 = w.gocuiSize.h0 + w.gocuiSize.height
}
@ -175,15 +182,22 @@ func (w *cuiWidget) moveTo(leftW int, topH int) {
if (w.isFake) {
return
}
w.gocuiSize.startW = leftW
w.gocuiSize.startH = topH
w.setWH()
w.showWidgetPlacement(logNow, "moveTo()")
w.gocuiSize.w0 = leftW
w.gocuiSize.h0 = topH
w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width
w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height
w.showWidgetPlacement(logInfo, "moveTo()")
}
func (w *cuiWidget) drawGrid() {
w.showWidgetPlacement(logNow, "gridBounds:")
func (w *cuiWidget) placeGrid() {
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 hCount int = 0
@ -191,7 +205,6 @@ func (w *cuiWidget) drawGrid() {
// increment for the next child
w.nextW = w.startW + wCount * 20
w.nextH = w.startH + hCount * 2
// child.drawBox()
// set the child's realWidth, and grid offset
child.parentH = hCount
@ -202,8 +215,8 @@ func (w *cuiWidget) drawGrid() {
if (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)
child.showWidgetPlacement(logNow, "grid:")
log(logVerbose, "grid1: (w,h count)", wCount, hCount, "(X,Y)", w.x, w.y, w.name)
child.showWidgetPlacement(logNow, "grid1: " + fmt.Sprintf("next()=(%2d,%2d)", w.nextW, w.nextH))
if ((wCount + 1) < w.y) {
wCount += 1
@ -224,11 +237,11 @@ func (w *cuiWidget) drawGrid() {
}
for _, child := range w.children {
child.showWidgetPlacement(logVerbose, "gridBounds:")
child.showWidgetPlacement(logVerbose, "grid2:")
var totalW, totalH int
for i, val := range w.widths {
if (i < child.parentW) {
log(logVerbose, "gridBounds() (w, widths[])", i, val)
log(logVerbose, "grid2: (w, widths[])", i, val)
totalW += w.widths[i]
}
}
@ -239,18 +252,16 @@ func (w *cuiWidget) drawGrid() {
}
// the new corner to move the child to
realW := w.nextW + totalW
realH := w.nextH + totalH
w.nextW = w.startW + totalW
w.nextH = w.startH + totalH
log(logNow, "gridBounds()", child.id, "parent (W,H) =", child.parentW, child.parentH,
"total (W,H) =", totalW, totalH,
"real (W,H) =", realW, realH)
w.startW = realW
w.startH = realH
child.drawBox()
child.showWidgetPlacement(logInfo, "gridBounds:")
child.placeWidgets()
child.showWidgetPlacement(logInfo, "grid2:")
log(logInfo)
}
// 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
type rectType struct {
// where the widget should calculate it's existance from
startW int
startH int
// startW int
// startH int
// the actual size
width int
@ -85,10 +85,14 @@ type cuiWidget struct {
// visable bool // track if it's currently supposed to be shown
isFake bool // widget types like 'box' are 'false'
// where the widget should add children
// where the widget's real corner is
startW int
startH int
// where the next child should be placed
nextW int
nextH int
// the widget size to reserve or things will overlap
realWidth int
realHeight int
@ -104,10 +108,6 @@ type cuiWidget struct {
parentW int
parentH int
// deprecate
nextW int
nextH int
// things from toolkit/action
b bool
i int

View File

@ -53,33 +53,20 @@ func (w *cuiWidget) showWidgets() {
}
func (w *cuiWidget) setTabWH() {
// set the start and size of the tab gocui button
t := len(w.text)
w.gocuiSize.width = t + me.buttonPadding
w.gocuiSize.height = me.defaultHeight
w.gocuiSize.w0 = me.rootNode.nextW
w.gocuiSize.h0 = me.rootNode.nextH
w.gocuiSize.startW = me.rootNode.startW
w.gocuiSize.startH = me.rootNode.startH
// move the rootNode width over for the next window or tab
me.rootNode.nextW += w.gocuiSize.width + me.padW
w.startW = me.rawW
w.startH = me.rawH
var f func (widget *cuiWidget)
// 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.nextW = me.rawW
w.nextH = me.rawH
w.setWH()
w.showWidgetPlacement(logNow, "setTabWH:")