From cd47489d853e7e60e8dabe5b7be6271fa167a479 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 6 Apr 2023 11:19:04 -0500 Subject: [PATCH] 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 --- cmds/buttonplugin/main.go | 24 ++-- toolkit/gocui/add.go | 9 +- toolkit/gocui/click.go | 29 ++--- toolkit/gocui/debug.go | 11 +- toolkit/gocui/place.go | 241 ++++++++++++++++++++------------------ toolkit/gocui/structs.go | 14 +-- toolkit/gocui/tab.go | 27 ++--- 7 files changed, 164 insertions(+), 191 deletions(-) diff --git a/cmds/buttonplugin/main.go b/cmds/buttonplugin/main.go index aca8707..efecba9 100644 --- a/cmds/buttonplugin/main.go +++ b/cmds/buttonplugin/main.go @@ -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) }) diff --git a/toolkit/gocui/add.go b/toolkit/gocui/add.go index 6ff3691..e207b9f 100644 --- a/toolkit/gocui/add.go +++ b/toolkit/gocui/add.go @@ -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()") } diff --git a/toolkit/gocui/click.go b/toolkit/gocui/click.go index 795b13e..b014e16 100644 --- a/toolkit/gocui/click.go +++ b/toolkit/gocui/click.go @@ -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 } diff --git a/toolkit/gocui/debug.go b/toolkit/gocui/debug.go index 9d036bb..d8bfd6e 100644 --- a/toolkit/gocui/debug.go +++ b/toolkit/gocui/debug.go @@ -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) } diff --git a/toolkit/gocui/place.go b/toolkit/gocui/place.go index 56ef281..32a9004 100644 --- a/toolkit/gocui/place.go +++ b/toolkit/gocui/place.go @@ -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() { } diff --git a/toolkit/gocui/structs.go b/toolkit/gocui/structs.go index 74a4621..f83622a 100644 --- a/toolkit/gocui/structs.go +++ b/toolkit/gocui/structs.go @@ -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 diff --git a/toolkit/gocui/tab.go b/toolkit/gocui/tab.go index 7628333..cc00b6c 100644 --- a/toolkit/gocui/tab.go +++ b/toolkit/gocui/tab.go @@ -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:")