nocui: simulates button clicks

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-27 22:21:31 -05:00
parent 065b931106
commit c0798a2db9
7 changed files with 162 additions and 66 deletions

View File

@ -2,12 +2,7 @@
package main
import (
"log"
"fmt"
"os"
"io"
"time"
"bufio"
arg "github.com/alexflint/go-arg"
"git.wit.org/wit/gui"
)
@ -21,14 +16,17 @@ var args struct {
gui.GuiArgs
}
/*
var f1 *os.File
var f2 *os.File
var err error
*/
func init() {
arg.MustParse(&args)
fmt.Println(args.Foo, args.Bar, args.User)
/*
log.Println()
log.Println("STDOUT is now at /tmp/guilogfile")
log.Println("STDOUT is now at /tmp/guilogfile")
@ -42,8 +40,10 @@ func init() {
log.SetOutput(f1)
log.Println("This is a test log entry")
*/
}
/*
func captureSTDOUT() {
f2, _ = os.OpenFile("/tmp/my.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0664)
multiWriter := io.MultiWriter(os.Stderr, f2)
@ -68,3 +68,4 @@ func captureSTDOUT() {
// hacky sleep to ensure the go routine can write before program exits
time.Sleep(time.Second)
}
*/

View File

@ -81,9 +81,9 @@ func (n *node) Delete() {
}
}
func action(a toolkit.Action) {
log(logNow, "rawAction() START a.ActionType =", a.ActionType)
log(logNow, "rawAction() START a.S =", a.S)
func doAction(a *toolkit.Action) {
log(logNow, "doAction() START a.ActionType =", a.ActionType)
log(logNow, "doAction() START a.S =", a.S)
if (a.ActionType == toolkit.InitToolkit) {
// TODO: make sure to only do this once
@ -95,8 +95,12 @@ func action(a toolkit.Action) {
return
}
log(logNow, "rawAction() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId)
log(logNow, "doAction() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId)
switch a.WidgetType {
case toolkit.Root:
rootNode = addWidget(a)
log(logNow, "doAction() found rootNode")
return
case toolkit.Flag:
// flag(&a)
return
@ -106,10 +110,7 @@ func action(a toolkit.Action) {
switch a.ActionType {
case toolkit.Add:
// QueueMain(func() {
// add(a)
// })
// sleep(.1)
addWidget(a)
case toolkit.Show:
n.show(true)
case toolkit.Hide:
@ -142,11 +143,11 @@ func action(a toolkit.Action) {
case toolkit.Delete:
n.Delete()
case toolkit.Move:
log(logNow, "rawAction() attempt to move() =", a.ActionType, a.WidgetType)
log(logNow, "doAction() attempt to move() =", a.ActionType, a.WidgetType)
newParent := rootNode.findWidgetId(a.ParentId)
n.move(newParent)
default:
log(logError, "rawAction() Unknown =", a.ActionType, a.WidgetType)
log(logError, "doAction() Unknown =", a.ActionType, a.WidgetType)
}
log(logInfo, "rawAction() END =", a.ActionType, a.WidgetType)
log(logInfo, "doAction() END =", a.ActionType, a.WidgetType)
}

View File

@ -58,22 +58,3 @@ func addWidget(a *toolkit.Action) *node {
}
return n
}
func (n *node) doUserEvent() {
if (callback == nil) {
log(logError, "doUserEvent() callback == nil", n.WidgetId)
return
}
var a toolkit.Action
a.WidgetId = n.WidgetId
a.Name = n.Name
a.Text = n.Text
a.S = n.S
a.I = n.I
a.B = n.B
a.ActionType = toolkit.User
log(logInfo, "doUserEvent() START: send a user event to the callback channel")
callback <- a
log(logInfo, "doUserEvent() END: sent a user event to the callback channel")
return
}

65
toolkit/nocui/event.go Normal file
View File

@ -0,0 +1,65 @@
package main
import (
"git.wit.org/wit/gui/toolkit"
)
func (n *node) doWidgetClick() {
switch n.WidgetType {
case toolkit.Root:
// THIS IS THE BEGINING OF THE LAYOUT
// rootNode.nextW = 0
// rootNode.nextH = 0
// rootNode.redoTabs(true)
case toolkit.Flag:
// me.rootNode.redoColor(true)
// rootNode.dumpTree(true)
case toolkit.Window:
// setCurrentWindow(w)
case toolkit.Tab:
// setCurrentTab(w)
case toolkit.Group:
// n.placeWidgets()
// n.toggleTree()
case toolkit.Checkbox:
if (n.B) {
// n.setCheckbox(false)
} else {
// n.setCheckbox(true)
}
n.doUserEvent()
case toolkit.Grid:
// rootNode.hideWidgets()
// n.placeGrid()
// n.showWidgets()
case toolkit.Box:
// n.showWidgetPlacement(logNow, "drawTree()")
if (n.B) {
log("BOX IS HORIZONTAL", n.Name)
} else {
log("BOX IS VERTICAL", n.Name)
}
case toolkit.Button:
n.doUserEvent()
default:
}
}
func (n *node) doUserEvent() {
if (callback == nil) {
log(logError, "doUserEvent() callback == nil", n.WidgetId)
return
}
var a toolkit.Action
a.WidgetId = n.WidgetId
a.Name = n.Name
a.Text = n.Text
a.S = n.S
a.I = n.I
a.B = n.B
a.ActionType = toolkit.User
log(logInfo, "doUserEvent() START: send a user event to the callback channel")
callback <- a
log(logInfo, "doUserEvent() END: sent a user event to the callback channel")
return
}

View File

@ -1,9 +1,6 @@
package main
import (
"io"
// "fmt"
// "strings"
witlog "git.wit.org/wit/gui/log"
)
@ -17,7 +14,7 @@ var logVerbose bool = false
var outputS []string
func log(a ...any) {
witlog.Where = "wit/gocui"
witlog.Where = "wit/nocui"
witlog.Log(a...)
}
@ -28,29 +25,3 @@ func sleep(a ...any) {
func exit(a ...any) {
witlog.Exit(a...)
}
/*
func newLog(a ...any) {
s := fmt.Sprint(a...)
tmp := strings.Split(s, "\n")
outputS = append(outputS, tmp...)
if (len(outputS) > 50) {
outputS = outputS[10:]
}
if (me.baseGui != nil) {
v, _ := me.baseGui.View("msg")
if (v != nil) {
v.Clear()
fmt.Fprintln(v, strings.Join(outputS, "\n"))
}
}
}
*/
func setOutput(w io.Writer) {
if (w == nil) {
return
}
witlog.SetOutput(w)
// witlog.SetToolkitOutput(newLog)
}

View File

@ -22,7 +22,7 @@ func catchActionChannel() {
log(logNow, "catchActionChannel() SELECT widget id =", a.WidgetId, a.Name)
log(logNow, "catchActionChannel() STUFF", a.WidgetId, a.ActionType, a.WidgetType)
muAction.Lock()
action(a)
doAction(&a)
muAction.Unlock()
log(logNow, "catchActionChannel() STUFF END", a.WidgetId, a.ActionType, a.WidgetType)
}
@ -54,5 +54,6 @@ func init() {
log(logNow, "Init() start channel reciever")
go catchActionChannel()
go simpleStdin()
log(logNow, "Init() END")
}

76
toolkit/nocui/stdin.go Normal file
View File

@ -0,0 +1,76 @@
package main
import (
"os"
"fmt"
"bufio"
"strings"
"strconv"
"git.wit.org/wit/gui/toolkit"
)
func simpleStdin() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
s := scanner.Text()
s = strings.TrimSuffix(s, "\n")
switch s {
case "l":
log(true, "list widgets")
rootNode.listWidgets()
case "b":
log(true, "show buttons")
rootNode.showButtons()
case "":
fmt.Println("")
fmt.Println("Enter:")
fmt.Println("'l': list all widgets")
fmt.Println("'b': for buttons")
fmt.Println("")
default:
i, _ := strconv.Atoi(s)
log(true, "got input:", i)
n := rootNode.findWidgetId(i)
if (n != nil) {
n.dumpWidget("found node")
n.doUserEvent()
}
}
}
}
func (n *node) showButtons() {
if n.WidgetType == toolkit.Button {
n.dumpWidget("Button:")
}
for _, child := range n.children {
child.showButtons()
}
}
func (n *node) dumpWidget(pad string) {
log(true, "node:", pad, n.WidgetId, ",", n.WidgetType, ",", n.Name)
}
var depth int = 0
func (n *node) listWidgets() {
if (n == nil) {
return
}
var pad string
for i := 0; i < depth; i++ {
pad = pad + " "
}
n.dumpWidget(pad)
for _, child := range n.children {
depth += 1
child.listWidgets()
depth -= 1
}
return
}