From 3cdbc285e3810627aee3d1e63a7e93cc2dac92be Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 27 Apr 2023 09:06:57 -0500 Subject: [PATCH] andlabs: window and tab now in binary tree Signed-off-by: Jeff Carr --- toolkit/andlabs/add.go | 32 +++++--------------- toolkit/andlabs/common.go | 61 ++++++++++++++++++++++++++++++++++++++ toolkit/andlabs/main.go | 3 ++ toolkit/andlabs/structs.go | 29 +++++++++++++++++- toolkit/andlabs/tab.go | 44 +++++++++++++-------------- toolkit/andlabs/window.go | 10 +++---- 6 files changed, 124 insertions(+), 55 deletions(-) diff --git a/toolkit/andlabs/add.go b/toolkit/andlabs/add.go index 6d4c7cd..9c4fe54 100644 --- a/toolkit/andlabs/add.go +++ b/toolkit/andlabs/add.go @@ -21,36 +21,18 @@ func add(a toolkit.Action) { actionDump(debugError, &a) return } - if (a.WidgetId == 0) { - log(debugError, "add() error. w.WidgetId == 0") - actionDump(debugError, &a) + if (a.WidgetType == toolkit.Root) { + rootNode = addWidget(&a, nil) return } + n := addWidget(&a, nil) - // for now, window gets handled without checking where == nil) - if (a.WidgetType == toolkit.Window) { - newWindow(a) - return - } - - if (andlabs[a.ParentId] == nil) { - // listMap(debugError) // memory corruption? - log(debugError, "add() Widget.Name =", a.Name) - log(debugError, "add() Widget.Type =", a.WidgetType) - log(debugError, "ERROR add() ERROR a.Parent map to t == nil. WidgetId =", a.WidgetId, "ParentId =", a.ParentId) - exit("can not add()") - return - } - - switch a.WidgetType { + switch n.WidgetType { case toolkit.Window: - newWindow(a) + newWindow(n) return case toolkit.Tab: - log(debugError, "add() CAME AT THIS FROM add() =", a.Name) - log(debugError, "add() CAME AT THIS FROM add() =", a.Name) - log(debugError, "add() CAME AT THIS FROM add() =", a.Name) - newTab(a) + newTab(n) return case toolkit.Label: newLabel(&a) @@ -89,7 +71,7 @@ func add(a toolkit.Action) { newImage(&a) return default: - log(debugError, "add() error TODO: ", a.WidgetType, a.Name) + log(debugError, "add() error TODO: ", n.WidgetType, n.Name) } } diff --git a/toolkit/andlabs/common.go b/toolkit/andlabs/common.go index cd8e9c8..630c6a9 100644 --- a/toolkit/andlabs/common.go +++ b/toolkit/andlabs/common.go @@ -4,6 +4,67 @@ import ( "git.wit.org/wit/gui/toolkit" ) +// 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 +} + +func addWidget(a *toolkit.Action, tk *andlabsT) *node { + n := new(node) + n.WidgetType = a.WidgetType + n.WidgetId = a.WidgetId + n.ParentId = a.ParentId + + // copy the data from the action message + n.Name = a.Name + n.Text = a.Text + n.I = a.I + n.S = a.S + n.B = a.B + n.X = a.X + n.Y = a.Y + + // store the internal toolkit information + n.tk = tk + + if (a.WidgetType == toolkit.Root) { + log(logInfo, "addWidget() Root") + return n + } + + if (rootNode.findWidgetId(a.WidgetId) != nil) { + log(logError, "addWidget() WidgetId already exists", a.WidgetId) + return rootNode.findWidgetId(a.WidgetId) + } + + // add this new widget on the binary tree + n.parent = rootNode.findWidgetId(a.ParentId) + if n.parent != nil { + n.parent.children = append(n.parent.children, n) + } + + // deprecate this when this toolkit uses the binary tree instead + if (andlabs[a.WidgetId] == nil) { + andlabs[a.WidgetId] = tk + } + + return n +} + func (t *andlabsT) doUserEvent() { if (callback == nil) { log(debugError, "doUserEvent() callback == nil", t.wId) diff --git a/toolkit/andlabs/main.go b/toolkit/andlabs/main.go index f9a7e68..e54da30 100644 --- a/toolkit/andlabs/main.go +++ b/toolkit/andlabs/main.go @@ -12,6 +12,9 @@ import ( // this is the channel we get requests to make widgets var pluginChan chan toolkit.Action +// the starting point of the binary tree +var rootNode *node + var uiMainUndef bool = true var uiMain sync.Once var muAction sync.Mutex diff --git a/toolkit/andlabs/structs.go b/toolkit/andlabs/structs.go index 60047ef..c4618b9 100644 --- a/toolkit/andlabs/structs.go +++ b/toolkit/andlabs/structs.go @@ -9,6 +9,33 @@ var andlabs map[int]*andlabsT // var callback func(int) bool var callback chan toolkit.Action +type node struct { + parent *node + children []*node + + WidgetId int // widget ID + WidgetType toolkit.WidgetType + ParentId int // parent ID + + Name string + Text string + + // This is how the values are passed back and forth + // values from things like checkboxes & dropdown's + B bool + I int + S string + + A any // switch to this or deprecate this? pros/cons? + + // This is used for things like a slider(0,100) + X int + Y int + + // the internal plugin toolkit structure + tk *andlabsT +} + // stores the raw toolkit internals type andlabsT struct { wId int // widget ID @@ -22,7 +49,7 @@ type andlabsT struct { // tw *toolkit.Widget parent *andlabsT - a toolkit.Action + children []*andlabsT uiControl ui.Control diff --git a/toolkit/andlabs/tab.go b/toolkit/andlabs/tab.go index 6936df6..42743de 100644 --- a/toolkit/andlabs/tab.go +++ b/toolkit/andlabs/tab.go @@ -1,7 +1,7 @@ package main import ( - "git.wit.org/wit/gui/toolkit" +// "git.wit.org/wit/gui/toolkit" "github.com/andlabs/ui" _ "github.com/andlabs/ui/winmanifest" @@ -19,38 +19,40 @@ import ( once there is one. If you send a Window here, it will replace any existing tabs rather than adding a new one */ -func (t *andlabsT) newTab(a toolkit.Action) { +func (p *node) newTab(n *node) { // var w *ui.Window var newt *andlabsT - log(debugToolkit, "newTab() START", a.WidgetId, a.ParentId) + t := p.tk + + log(debugToolkit, "newTab() START", n.WidgetId, n.ParentId) if (t.uiTab == nil) { if (t.uiWindow == nil) { - log(debugToolkit, "newTab() uiWindow == nil. I can't add a toolbar without window", a.WidgetId, a.ParentId) + log(debugToolkit, "newTab() uiWindow == nil. I can't add a toolbar without window", n.WidgetId, n.ParentId) return } // this means you have to make a new tab - log(debugToolkit, "newTab() GOOD. This should be the first tab:", a.WidgetId, a.ParentId) - newt = rawTab(t.uiWindow, a.Text) + log(debugToolkit, "newTab() GOOD. This should be the first tab:", n.WidgetId, n.ParentId) + newt = rawTab(t.uiWindow, n.Text) t.uiTab = newt.uiTab } else { // this means you have to append a tab - log(debugToolkit, "newTab() GOOD. This should be an additional tab:", a.WidgetId, a.ParentId) - newt = t.appendTab(a.Text) + log(debugToolkit, "newTab() GOOD. This should be an additional tab:", n.WidgetId, n.ParentId) + newt = t.appendTab(n.Text) } // add the structure to the array - if (andlabs[a.WidgetId] == nil) { - log(logInfo, "newTab() MAPPED", a.WidgetId, a.ParentId) - andlabs[a.WidgetId] = newt - newt.WidgetType = a.WidgetType + if (andlabs[n.WidgetId] == nil) { + log(logInfo, "newTab() MAPPED", n.WidgetId, n.ParentId) + andlabs[n.WidgetId] = newt + newt.WidgetType = n.WidgetType } else { - log(debugError, "newTab() DO WHAT?", a.WidgetId, a.ParentId) + log(debugError, "newTab() DO WHAT?", n.WidgetId, n.ParentId) log(debugError, "THIS IS BAD") } - newt.Name = a.Name + newt.Name = n.Name log(debugToolkit, "t:") t.Dump(debugToolkit) @@ -118,15 +120,9 @@ func (t *andlabsT) appendTab(name string) *andlabsT { return &newT } -func newTab(a toolkit.Action) { - // w := a.Widget - log(debugToolkit, "newTab()", a.ParentId) +func newTab(n *node) { + log(logInfo, "newTab() add to parent id:", n.ParentId) - t := andlabs[a.ParentId] - if (t == nil) { - log(debugToolkit, "newTab() parent toolkit == nil. new tab can not be made =", a.ParentId) - log(debugToolkit, "look for a window? check for an existing tab?") - return - } - t.newTab(a) + p := n.parent + p.newTab(n) } diff --git a/toolkit/andlabs/window.go b/toolkit/andlabs/window.go index 42a2baa..3e87b2c 100644 --- a/toolkit/andlabs/window.go +++ b/toolkit/andlabs/window.go @@ -15,15 +15,15 @@ func (t *andlabsT) ErrorWindow(msg1 string, msg2 string) { ui.MsgBoxError(t.uiWindow, msg1, msg2) } -func newWindow(a toolkit.Action) { +func newWindow(n *node) { var newt *andlabsT newt = new(andlabsT) newt.WidgetType = toolkit.Window - newt.wId = a.WidgetId + newt.wId = n.WidgetId // menubar bool is if the OS defined border on the window should be used - win := ui.NewWindow(a.Name, a.X, a.Y, menubar) + win := ui.NewWindow(n.Name, n.X, n.Y, menubar) win.SetBorderless(canvas) win.SetMargined(margin) win.OnClosing(func(*ui.Window) bool { @@ -32,9 +32,9 @@ func newWindow(a toolkit.Action) { }) newt.uiWindow = win newt.uiControl = win - newt.Name = a.Name + newt.Name = n.Name - andlabs[a.WidgetId] = newt + n.tk = newt win.Show() return }