gocui: more color cleanups

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-03 11:52:54 -05:00
parent 7f907e2b19
commit eca967bf61
11 changed files with 65 additions and 222 deletions

View File

@ -1,191 +0,0 @@
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"log"
"github.com/awesome-gocui/gocui"
)
func main() {
g, err := gocui.NewGui(gocui.OutputNormal, true)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.Cursor = false
g.Mouse = true
g.SetManagerFunc(layout)
if err := keybindings(g); err != nil {
log.Panicln(err)
}
if err := g.MainLoop(); err != nil && !errors.Is(err, gocui.ErrQuit) {
log.Panicln(err)
}
}
var initialMouseX, initialMouseY, xOffset, yOffset int
var globalMouseDown, msgMouseDown, movingMsg bool
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if _, err := g.View("msg"); msgMouseDown && err == nil {
moveMsg(g)
}
if v, err := g.SetView("global", -1, -1, maxX, maxY, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
v.Frame = false
}
if v, err := g.SetView("but1", 2, 2, 22, 7, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
fmt.Fprintln(v, "Button 1 - line 1")
fmt.Fprintln(v, "Button 1 - line 2")
fmt.Fprintln(v, "Button 1 - line 3")
fmt.Fprintln(v, "Button 1 - line 4")
if _, err := g.SetCurrentView("but1"); err != nil {
return err
}
}
if v, err := g.SetView("but2", 24, 2, 44, 4, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
fmt.Fprintln(v, "Button 2 - line 1")
}
updateHighlightedView(g)
return nil
}
func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
return err
}
for _, n := range []string{"but1", "but2"} {
if err := g.SetKeybinding(n, gocui.MouseLeft, gocui.ModNone, showMsg); err != nil {
return err
}
}
if err := g.SetKeybinding("", gocui.MouseRelease, gocui.ModNone, mouseUp); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.MouseLeft, gocui.ModNone, globalDown); err != nil {
return err
}
if err := g.SetKeybinding("msg", gocui.MouseLeft, gocui.ModNone, msgDown); err != nil {
return err
}
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func showMsg(g *gocui.Gui, v *gocui.View) error {
var l string
var err error
if _, err := g.SetCurrentView(v.Name()); err != nil {
return err
}
_, cy := v.Cursor()
if l, err = v.Line(cy); err != nil {
l = ""
}
maxX, maxY := g.Size()
if v, err := g.SetView("msg", maxX/2-10, maxY/2, maxX/2+10, maxY/2+2, 0); err == nil || errors.Is(err, gocui.ErrUnknownView) {
v.Clear()
v.SelBgColor = gocui.ColorCyan
v.SelFgColor = gocui.ColorBlack
fmt.Fprintln(v, l)
}
return nil
}
func updateHighlightedView(g *gocui.Gui) {
mx, my := g.MousePosition()
for _, view := range g.Views() {
view.Highlight = false
}
if v, err := g.ViewByPosition(mx, my); err == nil {
v.Highlight = true
}
}
func moveMsg(g *gocui.Gui) {
mx, my := g.MousePosition()
if !movingMsg && (mx != initialMouseX || my != initialMouseY) {
movingMsg = true
}
g.SetView("msg", mx-xOffset, my-yOffset, mx-xOffset+20, my-yOffset+2, 0)
}
func msgDown(g *gocui.Gui, v *gocui.View) error {
initialMouseX, initialMouseY = g.MousePosition()
if vx, vy, _, _, err := g.ViewPosition("msg"); err == nil {
xOffset = initialMouseX - vx
yOffset = initialMouseY - vy
msgMouseDown = true
}
return nil
}
func globalDown(g *gocui.Gui, v *gocui.View) error {
mx, my := g.MousePosition()
if vx0, vy0, vx1, vy1, err := g.ViewPosition("msg"); err == nil {
if mx >= vx0 && mx <= vx1 && my >= vy0 && my <= vy1 {
return msgDown(g, v)
}
}
globalMouseDown = true
maxX, _ := g.Size()
msg := fmt.Sprintf("Mouse down at: %d,%d", mx, my)
x := mx - len(msg)/2
if x < 0 {
x = 0
} else if x+len(msg)+1 > maxX-1 {
x = maxX - 1 - len(msg) - 1
}
if v, err := g.SetView("globalDown", x, my-1, x+len(msg)+1, my+1, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
v.WriteString(msg)
}
return nil
}
func mouseUp(g *gocui.Gui, v *gocui.View) error {
if msgMouseDown {
msgMouseDown = false
if movingMsg {
movingMsg = false
return nil
} else {
g.DeleteView("msg")
}
} else if globalMouseDown {
globalMouseDown = false
g.DeleteView("globalDown")
}
return nil
}

View File

@ -18,15 +18,14 @@ func (w *cuiWidget) doWidgetClick() {
case toolkit.Flag:
me.rootNode.redoColor(true)
case toolkit.Window:
me.rootNode.redoTabs(true)
w.redoBox(true)
w.toggleTree()
me.rootNode.redoColor(true)
case toolkit.Tab:
me.rootNode.redoTabs(true)
w.redoBox(true)
w.toggleTree()
me.rootNode.redoColor(true)
// w.toggleTree()
// me.rootNode.redoColor(true)
case toolkit.Box:
w.showWidgetPlacement(logNow, "drawTree()")
if (w.horizontal) {
@ -63,8 +62,7 @@ func (w *cuiWidget) drawTree(draw bool) {
w.textResize()
w.drawView()
} else {
me.baseGui.DeleteView(w.cuiName)
w.v = nil
w.deleteView()
}
for _, child := range w.children {

View File

@ -8,13 +8,14 @@ import (
// ColorBlack ColorRed ColorGreen ColorYellow ColorBlue ColorMagenta ColorCyan ColorWhite
// gocui.GetColor("#FFAA55") // Dark Purple
func (w *cuiWidget) SetDefaultWidgetColor() {
log(logInfo, "SetDefaultWidgetColor() on", w.widgetType, w.name)
func (w *cuiWidget) setDefaultWidgetColor() {
log(logInfo, "setDefaultWidgetColor() on", w.widgetType, w.name)
v, _ := me.baseGui.View(w.cuiName)
if (v == nil) {
log(logError, "SetDefaultWidgetColor() failed on view == nil")
log(logError, "setDefaultWidgetColor() failed on view == nil")
return
}
sleep(.05)
// v.BgColor = gocui.GetColor("#FFAA55") // Dark Purple
// v.BgColor = gocui.GetColor("#88AA55") // heavy purple
// v.BgColor = gocui.GetColor("#111111") // crazy red
@ -83,13 +84,13 @@ func (w *cuiWidget) SetColor(c string) {
}
}
func (w *cuiWidget) SetDefaultHighlight() {
func (w *cuiWidget) setDefaultHighlight() {
if (w.v == nil) {
log(logError, "SetColor() failed on view == nil")
return
}
// w.v.SelBgColor = gocui.ColorGreen
// w.v.SelFgColor = gocui.ColorBlack
w.v.SelBgColor = gocui.ColorGreen
w.v.SelFgColor = gocui.ColorBlack
}
func randColor() gocui.Attribute {
@ -105,7 +106,8 @@ func (w *cuiWidget) redoColor(draw bool) {
}
sleep(.05)
w.SetDefaultWidgetColor()
w.setDefaultHighlight()
// w.setDefaultWidgetColor()
for _, child := range w.children {
child.redoColor(draw)

View File

@ -70,6 +70,13 @@ func setupCtrlDownWidget() {
// me.rootNode.Append(w)
}
func (w *cuiWidget) deleteView() {
if (w.v != nil) {
me.baseGui.DeleteView(w.cuiName)
}
w.v = nil
}
func (n *cuiWidget) Append(child *cuiWidget) {
n.children = append(n.children, child)
// child.parent = n

View File

@ -22,7 +22,6 @@ func actionDump(b bool, a *toolkit.Action) {
}
func (w *cuiWidget) dumpTree(draw bool) {
log(logNow, "dumpTree() START", w)
if (w == nil) {
return
}

View File

@ -38,6 +38,8 @@ func defaultKeybindings(g *gocui.Gui) error {
return nil
}
var showDebug bool = true
// dump out the widgets
func addDebugKeys(g *gocui.Gui) {
// dump all widget info to the log
@ -45,6 +47,13 @@ func addDebugKeys(g *gocui.Gui) {
func(g *gocui.Gui, v *gocui.View) error {
log(logNow, "gocui.SetKeyBinding() dumpTree() START")
me.rootNode.dumpTree(true)
if (showDebug) {
me.rootNode.showFake()
showDebug = false
} else {
me.rootNode.hideFake()
showDebug = true
}
return nil
})

View File

@ -5,10 +5,10 @@ import (
"git.wit.org/wit/gui/toolkit"
)
var fakeStartWidth int = 80
var fakeStartHeight int = 0
var fakeStartWidth int = 10
var fakeStartHeight int = 3
func (w *cuiWidget) setFake() {
if (w.visable) {
if (w.isFake == false) {
return
}
t := len(w.name)
@ -21,29 +21,30 @@ func (w *cuiWidget) setFake() {
w.realSize.h1 = w.realSize.h0 + w.realHeight
fakeStartHeight += 3
if (fakeStartHeight > 24) {
fakeStartHeight = 0
fakeStartHeight = 3
fakeStartWidth += 20
}
w.showWidgetPlacement(logNow, "setFake()")
}
func findPlace(w *cuiWidget) {
w.isFake = false
w.visable = true
switch w.widgetType {
case toolkit.Root:
w.visable = false
w.isFake = true
w.setFake()
case toolkit.Flag:
w.visable = false
w.isFake = true
w.setFake()
case toolkit.Grid:
w.visable = false
w.isFake = true
w.setFake()
case toolkit.Box:
w.visable = false
w.isFake = true
w.setFake()
default:
w.redoBox(true)
// w.redoBox(true)
}
}

View File

@ -58,7 +58,7 @@ func (w *cuiWidget) SetText(text string) {
w.text = text
w.s = text
w.textResize()
me.baseGui.DeleteView(w.cuiName)
w.deleteView()
w.drawView()
}

View File

@ -87,7 +87,8 @@ type cuiWidget struct {
vals []string // dropdown menu options
visable bool // widget types like 'box' are 'false'
visable bool // track if it's currently supposed to be shown
isFake bool // widget types like 'box' are 'false'
realWidth int // the real width
realHeight int // the real height
realSize rectType // the display size of this widget

View File

@ -16,16 +16,31 @@ func (w *cuiWidget) hideWidgets() {
case toolkit.Box:
case toolkit.Grid:
default:
if (w.v != nil) {
me.baseGui.DeleteView(w.cuiName)
w.v = nil
}
w.deleteView()
}
for _, child := range w.children {
child.hideWidgets()
}
}
func (w *cuiWidget) hideFake() {
if (w.isFake) {
w.deleteView()
}
for _, child := range w.children {
child.hideFake()
}
}
func (w *cuiWidget) showFake() {
if (w.isFake) {
w.drawView()
}
for _, child := range w.children {
child.showFake()
}
}
func (w *cuiWidget) showWidgets() {
w.drawView()
for _, child := range w.children {
@ -74,7 +89,7 @@ func (w *cuiWidget) redoTabs(draw bool) {
me.rootNode.logicalSize.w1 = w.realSize.w1 + 1
me.rootNode.logicalSize.h1 = 0
me.baseGui.DeleteView(w.cuiName)
w.deleteView()
w.v = nil
w.drawView()
w.showWidgetPlacement(logNow, "redoTabs()")

View File

@ -69,10 +69,12 @@ func (w *cuiWidget) drawView() {
w.v, err = me.baseGui.SetView(w.cuiName, a, b, c, d, 0)
if err == nil {
w.showWidgetPlacement(logError, "drawView()")
log(logError, "drawView() internal plugin error err = nil")
return
}
if !errors.Is(err, gocui.ErrUnknownView) {
w.showWidgetPlacement(logError, "drawView()")
log(logError, "drawView() internal plugin error error.IS()", err)
return
}
@ -82,5 +84,5 @@ func (w *cuiWidget) drawView() {
w.v.Wrap = true
fmt.Fprintln(w.v, " " + w.text)
// w.SetDefaultWidgetColor()
w.setDefaultWidgetColor()
}