tree/plugin.go

72 lines
1.5 KiB
Go
Raw Permalink Normal View History

package tree
/*
These code should be common to all gui plugins
There are some helper functions that are probably going to be
the same everywhere. Mostly due to handling the binary tree structure
and the channel communication
*/
import (
2025-02-12 17:00:44 -06:00
"time"
"go.wit.com/widget"
)
// searches the binary tree for a WidgetId
func FindWidgetId(id int) *Node {
return treeRoot.FindWidgetId(id)
}
// searches the binary tree for a WidgetId
func (n *Node) FindWidgetId(id int) *Node {
if n == nil {
return nil
}
if n.WidgetId == id {
return n
}
for _, child := range n.children {
newN := child.FindWidgetId(id)
if newN != nil {
return newN
}
}
return nil
}
2025-02-12 17:00:44 -06:00
func (me *TreeInfo) InitOK() {
me.ok = true
}
// this hack is to wait for the application to send something
// before trying to do anything. todo: rethink this someday
func (me *TreeInfo) WaitOK() {
2025-02-12 17:00:44 -06:00
for {
if me.ok {
return
}
time.Sleep(10 * time.Millisecond)
}
}
// Other goroutines must use this to access the GUI
//
// You can not acess / process the GUI thread directly from
// other goroutines. This is due to the nature of how
// Linux, MacOS and Windows work (they all work differently. suprise. surprise.)
//
// this sets the channel to send user events back from the plugin
func (me *TreeInfo) Callback(guiCallback chan widget.Action) {
me.callback = guiCallback
}
2025-02-12 17:00:44 -06:00
// this is the function that receives things from the application
func (me *TreeInfo) PluginChannel() chan widget.Action {
me.WaitOK()
return me.pluginChan
}