From 132efe17fea1d34bb0504770e464de8009326b37 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 26 Apr 2023 01:59:42 -0500 Subject: [PATCH] gocui: closer to working layout Signed-off-by: Jeff Carr --- redraw.go | 8 ---- toolkit/gocui/add.go | 21 +++++------ toolkit/gocui/checkbox.go | 12 ++++-- toolkit/gocui/click.go | 6 +-- toolkit/gocui/common.go | 8 ++-- toolkit/gocui/debug.go | 2 +- toolkit/gocui/keybindings.go | 4 +- toolkit/gocui/main.go | 13 +------ toolkit/gocui/place.go | 72 +++++++++++++++--------------------- toolkit/gocui/structs.go | 25 +++++++++---- toolkit/gocui/tab.go | 16 +++++--- toolkit/gocui/view.go | 5 +-- toolkit/widget.go | 6 --- window.go | 44 ---------------------- 14 files changed, 90 insertions(+), 152 deletions(-) diff --git a/redraw.go b/redraw.go index dee535a..48f1d17 100644 --- a/redraw.go +++ b/redraw.go @@ -31,18 +31,10 @@ func (n *Node) redo(plug *aplug) { a.WidgetType = n.WidgetType a.WidgetId = n.id - // used for new Windows -// a.Width = n.Width -// a.Height = n.Height - // used for anything that needs a range a.X = n.X a.Y = n.Y - // implement here for grids and tables ? -// a.NextX = n.NextX -// a.NextY = n.NextY - // used for values a.I = n.I a.S = n.S diff --git a/toolkit/gocui/add.go b/toolkit/gocui/add.go index b58189e..dc81130 100644 --- a/toolkit/gocui/add.go +++ b/toolkit/gocui/add.go @@ -6,28 +6,27 @@ import ( ) // TODO: make these defaults in config struct definition -var fakeStartWidth int = 40 -var fakeStartHeight int = 3 +var fakeStartWidth int = me.DevelOffsetW +var fakeStartHeight int = me.TabH + me.FramePadH func (w *cuiWidget) setFake() { w.isFake = true t := len(w.name) // setup fake labels for non-visable things off screen - w.gocuiSize.width = t + me.PadW - w.gocuiSize.height = me.DefaultHeight + me.PadH w.gocuiSize.w0 = fakeStartWidth w.gocuiSize.h0 = fakeStartHeight + w.gocuiSize.w1 = w.gocuiSize.w0 + t + me.PadW + w.gocuiSize.h1 = w.gocuiSize.h0 + me.DefaultHeight + me.PadH - w.realWidth = w.gocuiSize.width + me.FramePadW - w.realHeight = w.gocuiSize.height + me.FramePadH + w.realWidth = w.gocuiSize.Width() + me.FramePadW + w.realHeight = w.gocuiSize.Height() + me.FramePadH fakeStartHeight += w.realHeight // TODO: use the actual max hight of the terminal window if (fakeStartHeight > 24) { - fakeStartHeight = 3 - fakeStartWidth += 20 + fakeStartHeight = me.TabH + me.FramePadH + fakeStartWidth += me.DevelOffsetW } - w.setWH() if (logInfo) { w.drawView() } @@ -70,8 +69,8 @@ func (w *cuiWidget) addWidget() { w.startH = w.parent.startH + me.DefaultHeight + me.FramePadH t := len(w.text) - w.gocuiSize.width = t + me.FramePadW - w.gocuiSize.height = me.DefaultHeight + me.FramePadH + w.gocuiSize.w1 = w.gocuiSize.w0 + t + me.FramePadW + w.gocuiSize.h1 = w.gocuiSize.h0 + me.DefaultHeight + me.FramePadH return default: w.startW = w.parent.startW diff --git a/toolkit/gocui/checkbox.go b/toolkit/gocui/checkbox.go index 8e6abf1..4a0808a 100644 --- a/toolkit/gocui/checkbox.go +++ b/toolkit/gocui/checkbox.go @@ -17,10 +17,16 @@ func (w *cuiWidget) setCheckbox(b bool) { w.text = " " + w.name } t := len(w.text) + 1 - w.realWidth = t - w.gocuiSize.width = t + w.gocuiSize.w1 = w.gocuiSize.w0 + t + + w.realWidth = w.gocuiSize.Width() + me.PadW + w.realHeight = w.gocuiSize.Height() + me.PadH + + if w.frame { + w.realWidth += me.FramePadW + w.realHeight += me.FramePadH + } - w.setWH() w.deleteView() w.drawView() } diff --git a/toolkit/gocui/click.go b/toolkit/gocui/click.go index a43dab4..ae845df 100644 --- a/toolkit/gocui/click.go +++ b/toolkit/gocui/click.go @@ -182,12 +182,10 @@ func ctrlDown(g *gocui.Gui, v *gocui.View) error { found = me.rootNode } 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() - + me.ctrlDown.gocuiSize.w1 = me.ctrlDown.gocuiSize.w0 + found.realWidth + me.ctrlDown.gocuiSize.h1 = me.ctrlDown.gocuiSize.h0 + found.realHeight if (me.ctrlDown.v == nil) { me.ctrlDown.text = found.text me.ctrlDown.showWidgetPlacement(logNow, "ctrlDown:") diff --git a/toolkit/gocui/common.go b/toolkit/gocui/common.go index 9f10e94..93dfbb6 100644 --- a/toolkit/gocui/common.go +++ b/toolkit/gocui/common.go @@ -20,11 +20,11 @@ func makeWidget(a *toolkit.Action) *cuiWidget { t := len(w.text) - w.gocuiSize.width = t + me.PadW - w.gocuiSize.height = me.DefaultHeight + me.PadH + w.gocuiSize.w1 = w.gocuiSize.w0 + t + me.PadW + w.gocuiSize.h1 = w.gocuiSize.h0 + me.DefaultHeight + me.PadH - w.realWidth = w.gocuiSize.width - w.realHeight = w.gocuiSize.height + w.realWidth = w.gocuiSize.Width() + w.realHeight = w.gocuiSize.Height() // set the gocui view.Frame = true by default w.frame = true diff --git a/toolkit/gocui/debug.go b/toolkit/gocui/debug.go index 9b57b08..e3f3586 100644 --- a/toolkit/gocui/debug.go +++ b/toolkit/gocui/debug.go @@ -32,7 +32,7 @@ func (w *cuiWidget) showWidgetPlacement(b bool, s string) { 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.Width(), w.gocuiSize.Height(), w.gocuiSize.w0, w.gocuiSize.h0, w.gocuiSize.w1, w.gocuiSize.h1) log(b, s1, s, w.widgetType, ",", w.name) // , "text=", w.text) } diff --git a/toolkit/gocui/keybindings.go b/toolkit/gocui/keybindings.go index 23db0eb..c3e92d7 100644 --- a/toolkit/gocui/keybindings.go +++ b/toolkit/gocui/keybindings.go @@ -45,8 +45,10 @@ func addDebugKeys(g *gocui.Gui) { g.SetKeybinding("", 'd', gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { log(logNow, "gocui.SetKeyBinding() dumpTree() START") - me.rootNode.dumpTree(true) + // me.rootNode.dumpTree(true) if (showDebug) { + fakeStartWidth = me.DevelOffsetW + fakeStartHeight = me.TabH + me.FramePadH me.rootNode.showFake() showDebug = false } else { diff --git a/toolkit/gocui/main.go b/toolkit/gocui/main.go index f1a0106..492d935 100644 --- a/toolkit/gocui/main.go +++ b/toolkit/gocui/main.go @@ -13,19 +13,10 @@ import ( // to this toolkit from the wit/gui golang package func init() { log(logInfo, "Init() of awesome-gocui") + + // init the config struct default values Set(&me, "default") -// me.groupPadding = 4 -// me.buttonPadding = 3 - - // todo, remove all of these -// me.defaultWidth = 10 -// me.defaultHeight = 2 // this means by default one line of text in a button - -// me.horizontalPadding = 20 -// me.horizontalPadding = 20 - // todo, remove all of these - me.pluginChan = make(chan toolkit.Action) log(logNow, "Init() start pluginChan") diff --git a/toolkit/gocui/place.go b/toolkit/gocui/place.go index 50e3b6f..124908f 100644 --- a/toolkit/gocui/place.go +++ b/toolkit/gocui/place.go @@ -48,31 +48,6 @@ func (w *cuiWidget) placeBox() { w.showWidgetPlacement(logNow, "boxE()") } -/* -func (w *cuiWidget) getGroupWH() { - p := w.parent // the parent must be a group widget - - // update parent gocuiSize - // p.realWidth = - // p.realHeight += me.DefaultHeight + me.PadH + me.FramePadH - for _, child := range p.children { - p.realWidth += child.realWidth - p.realHeight += child.realHeight - } - - // compute child offset - w.startW = p.startW - w.startH = p.startH - for _, child := range p.children { - w.startH += child.realHeight - if child == w { - return - } - } - return -} -*/ - func (w *cuiWidget) placeWidgets() { if (w == nil) { return @@ -120,29 +95,31 @@ func (w *cuiWidget) placeWidgets() { w.showWidgetPlacement(logNow, "place()") } case toolkit.Grid: - w.showWidgetPlacement(logNow, "place()") + w.showWidgetPlacement(logNow, "placeGrid() START") w.placeGrid() - w.showWidgetPlacement(logNow, "place()") + w.showWidgetPlacement(logNow, "placeGrid() END") case toolkit.Box: - w.showWidgetPlacement(logNow, "place()") + w.showWidgetPlacement(logNow, "placeBox() START") w.placeBox() - w.showWidgetPlacement(logNow, "place()") + w.showWidgetPlacement(logNow, "placeBox() END") case toolkit.Group: + // move the group to the parent's next location 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 + // initialize the real width to just the group gocui view + w.realWidth = w.gocuiSize.Width() + me.FramePadW + w.realHeight = w.gocuiSize.Height() + me.FramePadH // indent the widgets for a group - w.nextW = p.nextW + 4 - w.nextH = p.nextH + 3 + w.nextW = p.nextW + me.GroupPadW + w.nextH = p.nextH + w.realHeight w.showWidgetPlacement(logNow, "place()") + + // mow move all the children aka: run place() on them var maxW int for _, child := range w.children { child.showWidgetPlacement(logNow, "place()") @@ -167,33 +144,42 @@ func (w *cuiWidget) placeWidgets() { w.startH = p.nextH w.nextW = p.nextW w.nextH = p.nextH + newW := w.gocuiSize.Width() + newH := w.gocuiSize.Height() w.gocuiSize.w0 = w.startW w.gocuiSize.h0 = w.startH - w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width - w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height - if (w.realWidth < w.gocuiSize.width) { - w.realWidth = w.gocuiSize.width + w.gocuiSize.w1 = w.gocuiSize.w0 + newW + w.gocuiSize.h1 = w.gocuiSize.h0 + newH + + // the realSize should not be smaller than the gocui view (?) + // this might not be a needed check? Maybe there are legit exceptions? + if (w.realWidth < newW) { + w.realWidth = newW } - if (w.realHeight < w.gocuiSize.height) { - w.realHeight = w.gocuiSize.height + if (w.realHeight < newH) { + w.realHeight = newH } w.showWidgetPlacement(logNow, "place()") } } +/* func (w *cuiWidget) setWH() { w.gocuiSize.w1 = w.gocuiSize.w0 + w.gocuiSize.width w.gocuiSize.h1 = w.gocuiSize.h0 + w.gocuiSize.height } +*/ func (w *cuiWidget) moveTo(leftW int, topH int) { if (w.isFake) { return } + newW := w.gocuiSize.Width() + newH := w.gocuiSize.Height() 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.gocuiSize.w1 = w.gocuiSize.w0 + newW + w.gocuiSize.h1 = w.gocuiSize.h0 + newH w.showWidgetPlacement(logInfo, "moveTo()") } diff --git a/toolkit/gocui/structs.go b/toolkit/gocui/structs.go index b3c780f..9cc4eb8 100644 --- a/toolkit/gocui/structs.go +++ b/toolkit/gocui/structs.go @@ -53,6 +53,10 @@ type config struct { // additional amount of space to put between window & tab widgets TabPadW int `default:"2" dense:"0"` + // how far down to start Window or Tab headings + WindowH int `default:"1" dense:"0"` + TabH int `default:"2" dense:"0"` + // additional amount of space to indent on a group GroupPadW int `default:"6" dense:"2"` @@ -60,16 +64,15 @@ type config struct { RawW int `default:"7"` RawH int `default:"3"` + // offset for the hidden widgets + DevelOffsetW int `default:"20"` + bookshelf bool // do you want things arranged in the box like a bookshelf or a stack? canvas bool // if set to true, the windows are a raw canvas menubar bool // for windows stretchy bool // expand things like buttons to the maximum size padded bool // add space between things like buttons margin bool // add space around the frames of windows - -// horizontalPadding int - // groupPadding int `default:"6" dense:"2"` // this is supposed to be how far to indent to the left - // buttonPadding int `default:"4" dense:"3"` // if 3, buttons slightly overlap } var ( @@ -85,14 +88,22 @@ type rectType struct { // startW int // startH int - // the actual size - width int - height int + // the is a shortcut to access +// width int // this is always w1 - w0 + height int // this is always h1 - h0 // this is the gocui way w0, h0, w1, h1 int // left top right bottom } +func (r *rectType) Width() int { + return r.w1 - r.w0 +} + +func (r *rectType) Height() int { + return r.h1 - r.h0 +} + type cuiWidget struct { id int // widget ID // parentId int diff --git a/toolkit/gocui/tab.go b/toolkit/gocui/tab.go index ecaf5fd..4102702 100644 --- a/toolkit/gocui/tab.go +++ b/toolkit/gocui/tab.go @@ -34,6 +34,8 @@ func (w *cuiWidget) hideFake() { func (w *cuiWidget) showFake() { if (w.isFake) { + w.setFake() + w.showWidgetPlacement(logNow, "showFake:") w.drawView() } for _, child := range w.children { @@ -55,20 +57,22 @@ 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.PadW - w.gocuiSize.height = me.DefaultHeight + me.PadH w.gocuiSize.w0 = me.rootNode.nextW w.gocuiSize.h0 = me.rootNode.nextH + w.gocuiSize.w1 = w.gocuiSize.w1 + t + me.PadW + w.gocuiSize.h1 = w.gocuiSize.h1 + me.DefaultHeight + me.PadH + + w.realWidth = w.gocuiSize.Width() + w.realHeight = w.gocuiSize.Height() if w.frame { - w.realWidth = w.gocuiSize.width + me.FramePadW - w.realHeight = w.gocuiSize.height + me.FramePadH + w.realWidth += me.FramePadW + w.realHeight += me.FramePadH } // move the rootNode width over for the next window or tab - me.rootNode.nextW += w.gocuiSize.width + me.TabPadW + me.rootNode.nextW += w.realWidth + me.TabPadW - w.setWH() w.showWidgetPlacement(logNow, "setTabWH:") } diff --git a/toolkit/gocui/view.go b/toolkit/gocui/view.go index bc5848f..f348321 100644 --- a/toolkit/gocui/view.go +++ b/toolkit/gocui/view.go @@ -30,9 +30,8 @@ func (w *cuiWidget) textResize() { } height = i } - w.gocuiSize.width = width + me.FramePadW - w.gocuiSize.height = height + me.FramePadH - w.setWH() + w.gocuiSize.w1 = w.gocuiSize.w0 + width + me.FramePadW + w.gocuiSize.h1 = w.gocuiSize.h0 + height + me.FramePadH w.showWidgetPlacement(logNow, "textResize()") } diff --git a/toolkit/widget.go b/toolkit/widget.go index 51fe8b5..d4542bc 100644 --- a/toolkit/widget.go +++ b/toolkit/widget.go @@ -38,12 +38,6 @@ type Action struct { X int Y int - // This GUI is intended for simple things - // Is having an option like 640x480 really a good idea? - // TODO: remove Width & Height measured in pixels? -// Width int -// Height int - // Put space around elements to improve look & feel Margin bool diff --git a/window.go b/window.go index ebf4a85..7189fcc 100644 --- a/window.go +++ b/window.go @@ -5,7 +5,6 @@ import ( ) // This routine creates a blank window with a Title and size (W x H) -// func (n *Node) NewWindow(title string) *Node { var newNode *Node @@ -25,46 +24,3 @@ func (n *Node) NewWindow(title string) *Node { return newNode } - -/* -// This routine can not have any arguements due to the nature of how -// it can be passed via the 'andlabs/ui' queue which, because it is -// cross platform, must pass UI changes into the OS threads (that is -// my guess). -func NewWindow() *Node { - var newNode *Node - var custom func() - - // If the user didn't set a custom Exit() use the standard exit() function - // This makes sure the GUI properly closes everything (GTK, QT, console ui, etc exit) - if (Config.Exit != nil) { - log(debugGui, "setting a custom exit") - custom = func() { - log(debugChange, "Running a custom exit()", Config.Exit) - log(debugChange, "Running a custom exit() Config.Title =", Config.Title) - log(debugChange, "Running a custom exit() Config.Width =", Config.Width) - Config.Exit(newNode) - } - } else { - log(debugGui, "setting the standard exit") - custom = func () { - log(debugChange, "Running StandardExit()") - StandardExit() - } - } - // Windows are created off of the master node of the Binary Tree - newNode = Config.rootNode.newNode(Config.Title, toolkit.Window, custom) - - log(logInfo, "NewWindow()", Config.Title) - - var a toolkit.Action - a.ActionType = toolkit.Add - a.Width = Config.Width - a.Height = Config.Height - a.Name = Config.Title - a.Text = Config.Title - newaction(&a, newNode, Config.rootNode) - - return newNode -} -*/