code cleanups

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-28 15:34:55 -05:00
parent 14f69c4a40
commit 15f00451b7
4 changed files with 95 additions and 106 deletions

View File

@ -139,7 +139,7 @@ Creates a window helpful for debugging this package
`func ShowDebugValues()` `func ShowDebugValues()`
### func [StandardExit](/main.go#L219) ### func [StandardExit](/main.go#L149)
`func StandardExit()` `func StandardExit()`
@ -171,7 +171,7 @@ This struct can be used with the go-arg package
The Node is a binary tree. This is how all GUI elements are stored The Node is a binary tree. This is how all GUI elements are stored
simply the name and the size of whatever GUI element exists simply the name and the size of whatever GUI element exists
#### func [New](/main.go#L190) #### func [New](/main.go#L120)
`func New() *Node` `func New() *Node`
@ -180,7 +180,7 @@ This is due to restrictions by being cross platform
some toolkit's on some operating systems don't support more than one some toolkit's on some operating systems don't support more than one
Keep things simple. Do the default expected thing whenever possible Keep things simple. Do the default expected thing whenever possible
### type [Symbol](/plugin.go#L16) ### type [Symbol](/plugin.go#L17)
`type Symbol any` `type Symbol any`

23
grid.go
View File

@ -37,7 +37,30 @@ func (n *Node) NewGrid(name string, w int, h int) *Node {
newNode.Y = h newNode.Y = h
newNode.NextX = 1 newNode.NextX = 1
newNode.NextY = 1 newNode.NextY = 1
/*
// fix values here if they are invalid. Index starts at 1
if (where.NextX < 1) {
where.NextX = 1
}
if (where.NextY < 1) {
where.NextY = 1
}
//
a.X = where.NextX
a.Y = where.NextY
*/
newaction(&a, newNode, n) newaction(&a, newNode, n)
/*
where.NextY += 1
if (where.NextY > where.Y) {
where.NextX += 1
where.NextY = 1
}
log(logInfo, "Action() END size (X,Y)", where.X, where.Y, "put next thing at (X,Y) =", where.NextX, where.NextY)
*/
return newNode return newNode
} }

70
main.go
View File

@ -2,7 +2,6 @@ package gui
import ( import (
"os" "os"
"embed"
"git.wit.org/wit/gui/toolkit" "git.wit.org/wit/gui/toolkit"
) )
@ -114,75 +113,6 @@ func (n *Node) doUserEvent(a toolkit.Action) {
} }
} }
func (n *Node) InitEmbed(resFS embed.FS) *Node {
me.resFS = resFS
return n
}
func (n *Node) LoadToolkitEmbed(name string, b []byte) *Node {
for _, aplug := range allPlugins {
log(logInfo, "LoadToolkitEmbed() already loaded toolkit plugin =", aplug.name)
if (aplug.name == name) {
log(logError, "LoadToolkitEmbed() SKIPPING", name, "as you can't load it twice")
return n
}
}
f, err := os.CreateTemp("", "sample." + name + ".so")
if (err != nil) {
return n
}
defer os.Remove(f.Name())
f.Write(b)
p := initToolkit(name, f.Name())
if (p == nil) {
log(logError, "LoadToolkitEmbed() embedded go file failed", name)
}
return n
}
func (n *Node) ListToolkits() {
for _, aplug := range allPlugins {
log(logNow, "ListToolkits() already loaded toolkit plugin =", aplug.name)
}
}
func (n *Node) LoadToolkit(name string) *Node {
log(logInfo, "LoadToolkit() START for name =", name)
plug := initPlugin(name)
if (plug == nil) {
return n
}
log(logInfo, "LoadToolkit() sending InitToolkit action to the plugin channel")
var a toolkit.Action
a.ActionType = toolkit.InitToolkit
plug.pluginChan <- a
sleep(.5) // temp hack until chan communication is setup
// TODO: find a new way to do this that is locking, safe and accurate
me.rootNode.redraw(plug)
log(logInfo, "LoadToolkit() END for name =", name)
return n
}
func (n *Node) CloseToolkit(name string) bool {
log(logInfo, "CloseToolkit() for name =", name)
for _, plug := range allPlugins {
log(debugGui, "CloseToolkit() found", plug.name)
if (plug.name == name) {
log(debugNow, "CloseToolkit() sending close", name)
var a toolkit.Action
a.ActionType = toolkit.CloseToolkit
plug.pluginChan <- a
sleep(.5)
return true
}
}
return false
}
// There should only be one of these per application // There should only be one of these per application
// This is due to restrictions by being cross platform // This is due to restrictions by being cross platform
// some toolkit's on some operating systems don't support more than one // some toolkit's on some operating systems don't support more than one

102
plugin.go
View File

@ -7,6 +7,7 @@ package gui
import ( import (
"os" "os"
"embed"
"plugin" "plugin"
"git.wit.org/wit/gui/toolkit" "git.wit.org/wit/gui/toolkit"
@ -204,34 +205,14 @@ func initToolkit(name string, filename string) *aplug {
// 2023/04/06 Queue() is also being used and channels are being used. memcopy() only // 2023/04/06 Queue() is also being used and channels are being used. memcopy() only
func newaction(a *toolkit.Action, n *Node, where *Node) { func newaction(a *toolkit.Action, n *Node, where *Node) {
// remove this
if (n != nil) { if (n != nil) {
a.WidgetId = n.id a.WidgetId = n.id
a.WidgetType = n.WidgetType a.WidgetType = n.WidgetType
a.ActionType = a.ActionType a.ActionType = a.ActionType
} }
// TODO: redo this grid logic
if (where != nil) { if (where != nil) {
log(logInfo, "Action() START on where X,Y, Next X,Y =", where.Name, where.X, where.Y, where.NextX, where.NextY)
a.ParentId = where.id a.ParentId = where.id
switch where.WidgetType {
case toolkit.Grid:
// where.Dump(true)
log(logInfo, "Action() START on Grid (X,Y)", where.X, where.Y, "put next thing at (X,Y) =", where.NextX, where.NextY)
//
// fix values here if they are invalid. Index starts at 1
if (where.NextX < 1) {
where.NextX = 1
}
if (where.NextY < 1) {
where.NextY = 1
}
//
a.X = where.NextX
a.Y = where.NextY
log(logInfo, "Action() END on Grid (X,Y)", where.X, where.Y, "put next thing at (X,Y) =", where.NextX, where.NextY)
default:
}
} }
for _, aplug := range allPlugins { for _, aplug := range allPlugins {
@ -245,18 +226,73 @@ func newaction(a *toolkit.Action, n *Node, where *Node) {
aplug.pluginChan <- *a aplug.pluginChan <- *a
sleep(.02) sleep(.02)
} }
// increment where to put the next widget in a grid or table }
if (where != nil) {
switch where.WidgetType { func (n *Node) InitEmbed(resFS embed.FS) *Node {
case toolkit.Grid: me.resFS = resFS
log(logInfo, "Action() START size (X,Y)", where.X, where.Y, "put next thing at (X,Y) =", where.NextX, where.NextY) return n
where.NextY += 1 }
if (where.NextY > where.Y) {
where.NextX += 1 func (n *Node) LoadToolkitEmbed(name string, b []byte) *Node {
where.NextY = 1 for _, aplug := range allPlugins {
} log(logInfo, "LoadToolkitEmbed() already loaded toolkit plugin =", aplug.name)
log(logInfo, "Action() END size (X,Y)", where.X, where.Y, "put next thing at (X,Y) =", where.NextX, where.NextY) if (aplug.name == name) {
default: log(logError, "LoadToolkitEmbed() SKIPPING", name, "as you can't load it twice")
return n
} }
} }
f, err := os.CreateTemp("", "sample." + name + ".so")
if (err != nil) {
return n
}
defer os.Remove(f.Name())
f.Write(b)
p := initToolkit(name, f.Name())
if (p == nil) {
log(logError, "LoadToolkitEmbed() embedded go file failed", name)
}
return n
}
func (n *Node) ListToolkits() {
for _, aplug := range allPlugins {
log(logNow, "ListToolkits() already loaded toolkit plugin =", aplug.name)
}
}
func (n *Node) LoadToolkit(name string) *Node {
log(logInfo, "LoadToolkit() START for name =", name)
plug := initPlugin(name)
if (plug == nil) {
return n
}
log(logInfo, "LoadToolkit() sending InitToolkit action to the plugin channel")
var a toolkit.Action
a.ActionType = toolkit.InitToolkit
plug.pluginChan <- a
sleep(.5) // temp hack until chan communication is setup
// TODO: find a new way to do this that is locking, safe and accurate
me.rootNode.redraw(plug)
log(logInfo, "LoadToolkit() END for name =", name)
return n
}
func (n *Node) CloseToolkit(name string) bool {
log(logInfo, "CloseToolkit() for name =", name)
for _, plug := range allPlugins {
log(debugGui, "CloseToolkit() found", plug.name)
if (plug.name == name) {
log(debugNow, "CloseToolkit() sending close", name)
var a toolkit.Action
a.ActionType = toolkit.CloseToolkit
plug.pluginChan <- a
sleep(.5)
return true
}
}
return false
} }