allow passing of embed plugin files

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2023-04-28 07:29:46 -05:00
parent c0798a2db9
commit ed7becf5ee
8 changed files with 63 additions and 19 deletions

View File

@ -139,7 +139,7 @@ Creates a window helpful for debugging this package
`func ShowDebugValues()` `func ShowDebugValues()`
### func [StandardExit](/main.go#L197) ### func [StandardExit](/main.go#L231)
`func StandardExit()` `func StandardExit()`
@ -157,13 +157,13 @@ This goroutine can be used like a watchdog timer
## Types ## Types
### type [GuiArgs](/structs.go#L26) ### type [GuiArgs](/structs.go#L27)
`type GuiArgs struct { ... }` `type GuiArgs struct { ... }`
This struct can be used with the go-arg package This struct can be used with the go-arg package
### type [GuiConfig](/structs.go#L32) ### type [GuiConfig](/structs.go#L33)
`type GuiConfig struct { ... }` `type GuiConfig struct { ... }`
@ -173,14 +173,14 @@ This struct can be used with the go-arg package
var Config GuiConfig var Config GuiConfig
``` ```
### type [Node](/structs.go#L59) ### type [Node](/structs.go#L63)
`type Node struct { ... }` `type Node struct { ... }`
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#L168) #### func [New](/main.go#L202)
`func New() *Node` `func New() *Node`

View File

@ -64,6 +64,10 @@ func (n *Node) DebugTab(title string) *Node {
//////////////////////// window debugging things ////////////////////////////////// //////////////////////// window debugging things //////////////////////////////////
g1 = newN.NewGroup("list things") g1 = newN.NewGroup("list things")
g1.NewButton("List toolkits", func () {
dropdownWindow(g1)
Config.rootNode.ListToolkits()
})
g1.NewButton("List Windows", func () { g1.NewButton("List Windows", func () {
dropdownWindow(g1) dropdownWindow(g1)
}) })

View File

@ -10,9 +10,6 @@ func (n *Node) NewImage(name string) *Node {
var a toolkit.Action var a toolkit.Action
a.ActionType = toolkit.Add a.ActionType = toolkit.Add
// a.Widget = &newNode.widget
// a.Where = &n.widget
// action(&a)
newaction(&a, newNode, n) newaction(&a, newNode, n)
return newNode return newNode

36
main.go
View File

@ -2,7 +2,7 @@ package gui
import ( import (
"os" "os"
// "embed" // reminder to not attempt this within the 'wit/gui' package "embed"
"git.wit.org/wit/gui/toolkit" "git.wit.org/wit/gui/toolkit"
) )
@ -126,6 +126,40 @@ func (n *Node) doUserEvent(a toolkit.Action) {
} }
} }
func (n *Node) InitEmbed(resFS embed.FS) *Node {
Config.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 { func (n *Node) LoadToolkit(name string) *Node {
log(logInfo, "LoadToolkit() START for name =", name) log(logInfo, "LoadToolkit() START for name =", name)
plug := initPlugin(name) plug := initPlugin(name)

View File

@ -118,7 +118,7 @@ func searchPaths(name string) *aplug {
// attempt to write out the file from the internal resource // attempt to write out the file from the internal resource
filename = "toolkit/" + name + ".so" filename = "toolkit/" + name + ".so"
p := tryfile(name, filename) p := initToolkit(name, filename)
if (p != nil) { if (p != nil) {
return p return p
} }
@ -128,20 +128,20 @@ func searchPaths(name string) *aplug {
log(logError, "searchPaths() error. exiting here?") log(logError, "searchPaths() error. exiting here?")
} else { } else {
filename = homeDir + "/go/src/git.wit.org/wit/gui/toolkit/" + name + ".so" filename = homeDir + "/go/src/git.wit.org/wit/gui/toolkit/" + name + ".so"
p = tryfile(name, filename) p = initToolkit(name, filename)
if (p != nil) { if (p != nil) {
return p return p
} }
} }
filename = "/usr/lib/go-gui/" + name + ".so" filename = "/usr/lib/go-gui/" + name + ".so"
p = tryfile(name, filename) p = initToolkit(name, filename)
if (p != nil) { if (p != nil) {
return p return p
} }
filename = "/usr/local/lib/" + name + ".so" filename = "/usr/local/lib/" + name + ".so"
p = tryfile(name, filename) p = initToolkit(name, filename)
if (p != nil) { if (p != nil) {
return p return p
} }
@ -150,13 +150,13 @@ func searchPaths(name string) *aplug {
// load module // load module
// 1. open the shared object file to load the symbols // 1. open the shared object file to load the symbols
func tryfile(name string, filename string) *aplug { func initToolkit(name string, filename string) *aplug {
plug, err := plugin.Open(filename) plug, err := plugin.Open(filename)
if err != nil { if err != nil {
log(debugGui, "plugin FAILED =", filename, err) log(debugGui, "plugin FAILED =", filename, err)
return nil return nil
} }
log(debugGui, "tryfile() loading plugin =", filename) log(debugGui, "initToolkit() loading plugin =", filename)
var newPlug *aplug var newPlug *aplug
newPlug = new(aplug) newPlug = new(aplug)
@ -180,13 +180,13 @@ func tryfile(name string, filename string) *aplug {
// set the communication to the plugins // set the communication to the plugins
newPlug.pluginChan = newPlug.PluginChannel() newPlug.pluginChan = newPlug.PluginChannel()
if (newPlug.pluginChan == nil) { if (newPlug.pluginChan == nil) {
log(debugError, "tryfile() ERROR PluginChannel() returned nil for plugin:", newPlug.name, filename) log(debugError, "initToolkit() ERROR PluginChannel() returned nil for plugin:", newPlug.name, filename)
return nil return nil
} }
newPlug.Callback(Config.guiChan) newPlug.Callback(Config.guiChan)
newPlug.InitOk = true newPlug.InitOk = true
log(debugPlugin, "tryfile() END", newPlug.name, filename) log(debugPlugin, "initToolkit() END", newPlug.name, filename)
return newPlug return newPlug
} }

View File

@ -1,8 +1,9 @@
package gui package gui
import ( import (
"git.wit.org/wit/gui/toolkit"
"sync" "sync"
"embed"
"git.wit.org/wit/gui/toolkit"
) )
// //
@ -52,6 +53,9 @@ type GuiConfig struct {
// sets the chan for the plugins to call back too // sets the chan for the plugins to call back too
guiChan chan toolkit.Action guiChan chan toolkit.Action
// option to pass in compiled plugins as embedded files
resFS embed.FS
} }
// 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

View File

@ -16,6 +16,7 @@ func (n *node) doWidgetClick() {
// rootNode.dumpTree(true) // rootNode.dumpTree(true)
case toolkit.Window: case toolkit.Window:
// setCurrentWindow(w) // setCurrentWindow(w)
n.doUserEvent()
case toolkit.Tab: case toolkit.Tab:
// setCurrentTab(w) // setCurrentTab(w)
case toolkit.Group: case toolkit.Group:

View File

@ -22,12 +22,16 @@ func simpleStdin() {
case "b": case "b":
log(true, "show buttons") log(true, "show buttons")
rootNode.showButtons() rootNode.showButtons()
case "d":
var a toolkit.Action
a.ActionType = toolkit.EnableDebug
callback <- a
case "": case "":
fmt.Println("") fmt.Println("")
fmt.Println("Enter:") fmt.Println("Enter:")
fmt.Println("'l': list all widgets") fmt.Println("'l': list all widgets")
fmt.Println("'b': for buttons") fmt.Println("'b': for buttons")
fmt.Println("") fmt.Println("'d': enable debugging")
default: default:
i, _ := strconv.Atoi(s) i, _ := strconv.Atoi(s)
log(true, "got input:", i) log(true, "got input:", i)