andlabs kinda works with a channel

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-07 18:16:16 -05:00
parent 8f6e971948
commit 60a4a1bfd6
7 changed files with 110 additions and 26 deletions

View File

@ -37,7 +37,7 @@ func (n *Node) Redraw(p *aplug) {
} }
func (n *Node) redo(p *aplug) { func (n *Node) redo(p *aplug) {
log(logNow, "redo()", n.id, n.WidgetType, n.Name) log(logNow, "redo()", p.name, n.id, n.WidgetType, n.Name)
var a *toolkit.Action var a *toolkit.Action
a = new(toolkit.Action) a = new(toolkit.Action)

View File

@ -11,12 +11,12 @@ var stretchy bool // expand things like buttons to the maximum size
var padded bool // add space between things like buttons var padded bool // add space between things like buttons
var margin bool // add space around the frames of windows var margin bool // add space around the frames of windows
var debugToolkit bool var debugToolkit bool = true
var debugChange bool var debugChange bool = true
var debugPlugin bool var debugPlugin bool = true
var debugAction bool var debugAction bool = true
var debugFlags bool var debugFlags bool = true
var debugGrid bool var debugGrid bool = true
var debugNow bool = true var debugNow bool = true
var debugError bool = true var debugError bool = true

View File

@ -7,9 +7,9 @@ import (
// various debugging flags // various debugging flags
var logNow bool = true // useful for active development var logNow bool = true // useful for active development
var logError bool = true var logError bool = true
var logWarn bool = false var logWarn bool = true
var logInfo bool = false var logInfo bool = true
var logVerbose bool = false var logVerbose bool = true
func log(a ...any) { func log(a ...any) {
witlog.Where = "wit/gui/andlabs" witlog.Where = "wit/gui/andlabs"

View File

@ -11,15 +11,43 @@ import (
//go:embed resources //go:embed resources
var res embed.FS var res embed.FS
var pluginChan chan toolkit.Action
func catchActionChannel() {
log(logNow, "makeCallback() START")
for {
log(logNow, "makeCallback() for loop")
select {
case a := <-pluginChan:
log(logNow, "makeCallback() SELECT widget id =", a.WidgetId, a.Name)
// go Action(a)
if (a.WidgetType == toolkit.Window) {
log(logNow, "makeCallback() WINDOW START")
go ui.Main( func() {
log(logNow, "ui.Main() WINDOW START")
rawAction(&a)
log(logNow, "ui.Main() WINDOW END")
})
sleep(.5)
log(logNow, "makeCallback() WINDOW END")
} else {
log(logNow, "makeCallback() STUFF")
rawAction(&a)
/*
Queue( func() {
rawAction(&a)
})
*/
log(logNow, "makeCallback() STUFF END")
}
sleep(.1)
}
}
}
func Main(f func()) { func Main(f func()) {
log(debugToolkit, "Starting gui.Main() (using gtk via andlabs/ui)") log(debugNow, "gui.Main() START (using gtk via andlabs/ui)")
ui.Main( func() { f() // support the old way. deprecate this
log(debugToolkit, "Starting gui.Main() (using gtk via andlabs/ui)")
// time.Sleep(1 * time.Second)
// NewWindow2("helloworld2", 200, 100)
f()
})
} }
// this sets the channel to send user events back from the plugin // this sets the channel to send user events back from the plugin
@ -36,13 +64,14 @@ func Callback(guiCallback chan toolkit.Action) {
// For example: Queue(NewWindow()) // For example: Queue(NewWindow())
// //
func Queue(f func()) { func Queue(f func()) {
log(debugToolkit, "Sending function to ui.QueueMain()") log(logNow, "Sending function to ui.QueueMain()")
log(debugPlugin, "using gui.Queue() in this plugin DOES BREAK. TODO: solve this with channels") log(logNow, "using gui.Queue() in this plugin DOES BREAK. TODO: solve this with channels")
ui.QueueMain(f) ui.QueueMain(f)
} }
// This is important. This sets the defaults for the gui. Without this, there isn't correct padding, etc // This is important. This sets the defaults for the gui. Without this, there isn't correct padding, etc
func Init() { func Init() {
log(logNow, "Init() START")
log(debugToolkit, "Init()") log(debugToolkit, "Init()")
// Can you pass values to a plugin init() ? Otherwise, there is no way to safely print // Can you pass values to a plugin init() ? Otherwise, there is no way to safely print
// log(debugToolkit, "gui/toolkit init() Setting defaultBehavior = true") // log(debugToolkit, "gui/toolkit init() Setting defaultBehavior = true")
@ -52,6 +81,25 @@ func Init() {
// mapToolkits = make(map[*toolkit.Widget]*andlabsT) // mapToolkits = make(map[*toolkit.Widget]*andlabsT)
andlabs = make(map[int]*andlabsT) andlabs = make(map[int]*andlabsT)
pluginChan = make(chan toolkit.Action)
log(logNow, "Init() ui.Main() start")
go catchActionChannel()
/*
ui.Main( func() {
log(logNow, "gui.Main() IN (using gtk via andlabs/ui)")
var a toolkit.Action
a.Name = "jcarr"
a.Width = 640
a.Height = 480
a.WidgetId = 0
newWindow(&a)
// time.Sleep(1 * time.Second)
// NewWindow2("helloworld2", 200, 100)
log(logNow, "gui.Main() EXIT (using gtk via andlabs/ui)")
})
*/
log(logNow, "Init() END")
} }
// TODO: properly exit the plugin since Quit() doesn't do it // TODO: properly exit the plugin since Quit() doesn't do it

View File

@ -23,24 +23,28 @@ func Send(p *toolkit.Widget, c *toolkit.Widget) {
} }
func Action(a *toolkit.Action) { func Action(a *toolkit.Action) {
log(logNow, "Action() START")
if (a == nil) { if (a == nil) {
log(debugPlugin, "Action = nil") log(debugPlugin, "Action = nil")
return return
} }
pluginChan <- *a
/*
f := func() { f := func() {
rawAction(a) rawAction(a)
} }
// f() // f()
Queue(f) Queue(f)
*/
log(logNow, "Action() END")
} }
func rawAction(a *toolkit.Action) { func rawAction(a *toolkit.Action) {
log(debugAction, "rawAction() START a.ActionType =", a.ActionType)
log(debugAction, "rawAction() START a.S =", a.S)
log(debugAction, "Action() START a.ActionType =", a.ActionType) log(logNow, "rawAction() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId)
log(debugAction, "Action() START a.S =", a.S)
log(logInfo, "Action() START a.WidgetId =", a.WidgetId, "a.ParentId =", a.ParentId)
switch a.WidgetType { switch a.WidgetType {
case toolkit.Flag: case toolkit.Flag:
flag(a) flag(a)
@ -87,12 +91,12 @@ func rawAction(a *toolkit.Action) {
case toolkit.Delete: case toolkit.Delete:
uiDelete(a) uiDelete(a)
case toolkit.Move: case toolkit.Move:
log(debugNow, "attempt to move() =", a.ActionType, a.WidgetType) log(debugNow, "rawAction() attempt to move() =", a.ActionType, a.WidgetType)
move(a) move(a)
default: default:
log(debugError, "Action() Unknown =", a.ActionType, a.WidgetType) log(debugError, "rawAction() Unknown =", a.ActionType, a.WidgetType)
} }
log(debugAction, "Action() END =", a.ActionType, a.WidgetType) log(debugAction, "rawAction() END =", a.ActionType, a.WidgetType)
} }
func flag(a *toolkit.Action) { func flag(a *toolkit.Action) {

View File

@ -54,6 +54,10 @@ func (w *cuiWidget) drawView() {
} }
} }
if (me.baseGui == nil) {
log(logError, "drawView() ERROR: me.baseGui == nil", w)
return
}
v, _ := me.baseGui.View(w.cuiName) v, _ := me.baseGui.View(w.cuiName)
if (v != nil) { if (v != nil) {
log(logError, "drawView() already defined for name", w.cuiName) log(logError, "drawView() already defined for name", w.cuiName)

View File

@ -27,3 +27,31 @@ func Watchdog() {
time.Sleep(watchtime * time.Second / 10) time.Sleep(watchtime * time.Second / 10)
} }
} }
// https://www.reddit.com/r/golang/comments/12em87q/how_to_run_periodic_tasks/
/*
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
done := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
done <- true
}()
for {
select {
case <-done:
fmt.Println("Done!")
return
case t := <-ticker.C:
fmt.Println("Current time: ", t)
}
}
}
*/