TAB: correctly lookup and add tabs to blank windows

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2021-10-06 13:23:00 -05:00
parent c58dee3d01
commit 908615c038
4 changed files with 178 additions and 135 deletions

View File

@ -33,8 +33,12 @@ func WatchGUI() {
} }
func DumpMap() { func DumpMap() {
for name, _ := range Data.WindowMap { for name, window := range Data.WindowMap {
log.Println("gui.DumpBoxes() MAP: ", name) log.Println("gui.DumpBoxes() MAP: ", name)
log.Println("gui.DumpBoxes() BOXES:", name)
for name, abox := range window.BoxMap {
log.Printf("gui.DumpBoxes() \tBOX mapname=%-12s abox.Name=%-12s", name, abox.Name)
}
} }
} }

View File

@ -1,36 +1,39 @@
package gui package gui
import "log" import (
import "image/color" "image/color"
import "golang.org/x/image/font" "log"
import "github.com/andlabs/ui" "github.com/andlabs/ui"
import _ "github.com/andlabs/ui/winmanifest" "golang.org/x/image/font"
_ "github.com/andlabs/ui/winmanifest"
)
// //
// All GUI Data Structures and functions that are external // All GUI Data Structures and functions that are external
// If you need cross platform support, these might only // If you need cross platform support, these might only
// be the safe way to interact with the GUI // be the safe way to interact with the GUI
// //
var Data GuiData var Data GuiData
var Config GuiConfig var Config GuiConfig
type GuiConfig struct { type GuiConfig struct {
Width int Width int
Height int Height int
Debug bool Debug bool
DebugTable bool DebugTable bool
Exit func(*GuiWindow) Exit func(*GuiWindow)
} }
type GuiData struct { type GuiData struct {
// a fallback default function to handle mouse events // a fallback default function to handle mouse events
// if nothing else is defined to handle them // if nothing else is defined to handle them
MouseClick func(*GuiButton) MouseClick func(*GuiButton)
// A map of all the entry boxes // A map of all the entry boxes
AllEntries []*GuiEntry AllEntries []*GuiEntry
WindowMap map[string]*GuiWindow WindowMap map[string]*GuiWindow
// Windows []*GuiWindow // Windows []*GuiWindow
@ -40,15 +43,15 @@ type GuiData struct {
// //
// This has to work this way because of how // This has to work this way because of how
// andlabs/ui & andlabs/libui work // andlabs/ui & andlabs/libui work
AllButtons []*GuiButton AllButtons []*GuiButton
buttonMap map[*ui.Button]*GuiButton buttonMap map[*ui.Button]*GuiButton
Nodes *Node Nodes *Node
} }
type GuiTab struct { type GuiTab struct {
Name string // field for human readable name Name string // field for human readable name
Number int // the andlabs/ui tab index Number int // the andlabs/ui tab index
Window *GuiWindow // the parent Window Window *GuiWindow // the parent Window
} }
// //
@ -70,42 +73,42 @@ type GuiTab struct {
// can destroy and replace it with something else // can destroy and replace it with something else
// //
type GuiWindow struct { type GuiWindow struct {
Name string // field for human readable name Name string // field for human readable name
Width int Width int
Height int Height int
Axis int // does it add items to the X or Y axis Axis int // does it add items to the X or Y axis
TabNumber *int // the andlabs/ui tab index TabNumber *int // the andlabs/ui tab index
// the callback function to make the window contents // the callback function to make the window contents
// MakeWindow func(*GuiBox) *GuiBox // MakeWindow func(*GuiBox) *GuiBox
// the components of the window // the components of the window
BoxMap map[string]*GuiBox BoxMap map[string]*GuiBox
EntryMap map[string]*GuiEntry EntryMap map[string]*GuiEntry
Area *GuiArea Area *GuiArea
// andlabs/ui abstraction mapping // andlabs/ui abstraction mapping
UiWindow *ui.Window UiWindow *ui.Window
UiTab *ui.Tab // if this != nil, the window is 'tabbed' UiTab *ui.Tab // if this != nil, the window is 'tabbed'
} }
// GuiBox is any type of ui.Hbox or ui.Vbox // GuiBox is any type of ui.Hbox or ui.Vbox
// There can be lots of these for each GuiWindow // There can be lots of these for each GuiWindow
type GuiBox struct { type GuiBox struct {
Name string // field for human readable name Name string // field for human readable name
Axis int // does it add items to the X or Y axis Axis int // does it add items to the X or Y axis
Window *GuiWindow // the parent Window Window *GuiWindow // the parent Window
// andlabs/ui abstraction mapping // andlabs/ui abstraction mapping
UiBox *ui.Box UiBox *ui.Box
} }
func (s GuiBox) SetTitle(title string) { func (s GuiBox) SetTitle(title string) {
log.Println("DID IT!", title) log.Println("DID IT!", title)
if (s.Window == nil) { if s.Window == nil {
return return
} }
if (s.Window.UiWindow == nil) { if s.Window.UiWindow == nil {
return return
} }
s.Window.UiWindow.SetTitle(title) s.Window.UiWindow.SetTitle(title)
@ -113,17 +116,33 @@ func (s GuiBox) SetTitle(title string) {
} }
func (s GuiBox) Append(child ui.Control, x bool) { func (s GuiBox) Append(child ui.Control, x bool) {
if (s.UiBox == nil) { if s.UiBox == nil {
return return
} }
s.UiBox.Append(child, x) s.UiBox.Append(child, x)
} }
func (s GuiBox) InitTab(title string) *ui.Tab { func (w GuiWindow) InitBox(title string) *GuiBox {
if (s.Window == nil) { if w.UiWindow == nil {
log.Println("gui.InitBox() THIS SHOULD NEVER HAPPEN. Window doesn't exist", w)
return nil return nil
} }
if (s.Window.UiWindow == nil) { tab := ui.NewTab()
w.UiWindow.SetChild(tab)
w.UiWindow.SetMargined(true)
tab.Append(title, initBlankWindow())
tab.SetMargined(0, true)
w.UiTab = tab
return nil
}
func (s GuiBox) InitTab(title string) *ui.Tab {
if s.Window == nil {
return nil
}
if s.Window.UiWindow == nil {
return nil return nil
} }
@ -141,10 +160,10 @@ func (s GuiBox) InitTab(title string) *ui.Tab {
} }
func (s GuiBox) AddTab(title string, custom ui.Control) *ui.Tab { func (s GuiBox) AddTab(title string, custom ui.Control) *ui.Tab {
if (s.Window == nil) { if s.Window == nil {
return nil return nil
} }
if (s.Window.UiTab == nil) { if s.Window.UiTab == nil {
return nil return nil
} }
@ -155,10 +174,10 @@ func (s GuiBox) AddTab(title string, custom ui.Control) *ui.Tab {
} }
func (s GuiBox) AddTab2(title string, custom ui.Control) *ui.Tab { func (s GuiBox) AddTab2(title string, custom ui.Control) *ui.Tab {
if (s.Window == nil) { if s.Window == nil {
return nil return nil
} }
if (s.Window.UiTab == nil) { if s.Window.UiTab == nil {
return nil return nil
} }
@ -200,32 +219,32 @@ func tabSetMargined(tab *ui.Tab) {
// the user clicks it. You could probably // the user clicks it. You could probably
// call this 'GuiMouseClick' // call this 'GuiMouseClick'
type GuiButton struct { type GuiButton struct {
Name string // field for human readable name Name string // field for human readable name
Box *GuiBox // what box the button click was in Box *GuiBox // what box the button click was in
// a callback function for the main application // a callback function for the main application
Custom func (*GuiButton) Custom func(*GuiButton)
Values interface {} Values interface{}
Color color.RGBA Color color.RGBA
// andlabs/ui abstraction mapping // andlabs/ui abstraction mapping
B *ui.Button B *ui.Button
FB *ui.FontButton FB *ui.FontButton
CB *ui.ColorButton CB *ui.ColorButton
} }
// text entry fields // text entry fields
type GuiEntry struct { type GuiEntry struct {
Name string // field for human readable name Name string // field for human readable name
Edit bool Edit bool
Last string // the last value Last string // the last value
Normalize func (string) string // function to 'normalize' the data Normalize func(string) string // function to 'normalize' the data
B *GuiButton B *GuiButton
Box *GuiBox Box *GuiBox
// andlabs/ui abstraction mapping // andlabs/ui abstraction mapping
UiEntry *ui.Entry UiEntry *ui.Entry
} }
// //
@ -233,20 +252,21 @@ type GuiEntry struct {
// AREA STRUCTURES START // AREA STRUCTURES START
// AREA STRUCTURES START // AREA STRUCTURES START
// //
type GuiArea struct{ type GuiArea struct {
Button *GuiButton // what button handles mouse events Button *GuiButton // what button handles mouse events
Box *GuiBox Box *GuiBox
UiAttrstr *ui.AttributedString UiAttrstr *ui.AttributedString
UiArea *ui.Area UiArea *ui.Area
} }
type FontString struct { type FontString struct {
S string S string
Size int Size int
F font.Face F font.Face
W font.Weight W font.Weight
} }
// //
// AREA STRUCTURES END // AREA STRUCTURES END
// AREA STRUCTURES END // AREA STRUCTURES END
@ -264,18 +284,18 @@ type FontString struct {
// to the GUI. This is the "authoritative" data. // to the GUI. This is the "authoritative" data.
// //
type TableData struct { type TableData struct {
RowCount int // This is the number of 'rows' which really means data elements not what the human sees RowCount int // This is the number of 'rows' which really means data elements not what the human sees
RowWidth int // This is how wide each row is RowWidth int // This is how wide each row is
Rows []RowData // This is all the table data by row Rows []RowData // This is all the table data by row
generatedColumnTypes []ui.TableValue // generate this dynamically generatedColumnTypes []ui.TableValue // generate this dynamically
Cells [20]CellData Cells [20]CellData
Human [20]HumanMap Human [20]HumanMap
Box *GuiBox Box *GuiBox
lastRow int lastRow int
lastColumn int lastColumn int
} }
// //
@ -294,44 +314,44 @@ type TableData struct {
// TODO: re-add images and the progress bar (works in andlabs/ui) // TODO: re-add images and the progress bar (works in andlabs/ui)
// //
type HumanCellData struct { type HumanCellData struct {
Name string // what kind of row is this? Name string // what kind of row is this?
Text string Text string
TextID int TextID int
Color color.RGBA Color color.RGBA
ColorID int ColorID int
Button *GuiButton Button *GuiButton
} }
type HumanMap struct { type HumanMap struct {
Name string // what kind of row is this? Name string // what kind of row is this?
TextID int TextID int
ColorID int ColorID int
} }
type TableColumnData struct { type TableColumnData struct {
Index int Index int
CellType string CellType string
Heading string Heading string
Color string Color string
} }
type CellData struct { type CellData struct {
Index int Index int
HumanID int HumanID int
Name string // what type of cell is this? Name string // what type of cell is this?
} }
// hmm. will this stand the test of time? // hmm. will this stand the test of time?
type RowData struct { type RowData struct {
Name string // what kind of row is this? Name string // what kind of row is this?
Status string // status of the row? Status string // status of the row?
/* /*
// TODO: These may or may not be implementable // TODO: These may or may not be implementable
// depending on if it's possible to detect the bgcolor or what row is selected // depending on if it's possible to detect the bgcolor or what row is selected
click func() // what function to call if the user clicks on it click func() // what function to call if the user clicks on it
doubleclick func() // what function to call if the user double clicks on it doubleclick func() // what function to call if the user double clicks on it
*/ */
HumanData [20]HumanCellData HumanData [20]HumanCellData
} }
// //

View File

@ -20,7 +20,7 @@ func makeWindowDebug() ui.Control {
vbox.Append(pbar, false) vbox.Append(pbar, false)
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
vbox = addGroup(hbox, "WindowMap 2") vbox = addGroup(hbox, "Window")
cbox := ui.NewCombobox() cbox := ui.NewCombobox()
for name, _ := range Data.WindowMap { for name, _ := range Data.WindowMap {
@ -39,9 +39,9 @@ func makeWindowDebug() ui.Control {
}) })
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
vbox = addGroup(hbox, "Buttons") vbox = addGroup(hbox, "Debug Window")
b1 := addButton(vbox, "dumpBox(name)") b1 := addButton(vbox, "dumpBox(window)")
b1.OnClicked(func(*ui.Button) { b1.OnClicked(func(*ui.Button) {
x := cbox.Selected() x := cbox.Selected()
log.Println("x =", x) log.Println("x =", x)
@ -49,23 +49,12 @@ func makeWindowDebug() ui.Control {
dumpBox(names[x]) dumpBox(names[x])
}) })
dump2 := addButton(vbox, "Dump Boxes") b2 := addButton(vbox, "SetMargined(tab)")
dump2.OnClicked(func(*ui.Button) {
DumpBoxes()
})
dump1 := addButton(vbox, "Dump MAP")
dump1.OnClicked(func(*ui.Button) {
DumpMap()
})
b2 := addButton(vbox, "SetMargined()")
b2.OnClicked(func(*ui.Button) { b2.OnClicked(func(*ui.Button) {
x := cbox.Selected() x := cbox.Selected()
log.Println("x =", x) log.Println("x =", x)
log.Println("findBox; names[x] =", names[x]) log.Println("FindWindow; names[x] =", names[x])
findBox(names[x]) gw := FindWindow(names[x])
gw := findBox(names[x])
if gw == nil { if gw == nil {
return return
} }
@ -82,13 +71,12 @@ func makeWindowDebug() ui.Control {
gw.UiTab.SetMargined(*gw.TabNumber, true) gw.UiTab.SetMargined(*gw.TabNumber, true)
}) })
b3 := addButton(vbox, "Hide()") b3 := addButton(vbox, "Hide(tab)")
b3.OnClicked(func(*ui.Button) { b3.OnClicked(func(*ui.Button) {
x := cbox.Selected() x := cbox.Selected()
log.Println("x =", x) log.Println("x =", x)
log.Println("findBox; names[x] =", names[x]) log.Println("FindWindow; names[x] =", names[x])
findBox(names[x]) gw := FindWindow(names[x])
gw := findBox(names[x])
if gw == nil { if gw == nil {
return return
} }
@ -98,13 +86,12 @@ func makeWindowDebug() ui.Control {
gw.UiTab.Hide() gw.UiTab.Hide()
}) })
b4 := addButton(vbox, "Show()") b4 := addButton(vbox, "Show(tab)")
b4.OnClicked(func(*ui.Button) { b4.OnClicked(func(*ui.Button) {
x := cbox.Selected() x := cbox.Selected()
log.Println("x =", x) log.Println("x =", x)
log.Println("findBox; names[x] =", names[x]) log.Println("FindWindow; names[x] =", names[x])
findBox(names[x]) gw := FindWindow(names[x])
gw := findBox(names[x])
if gw == nil { if gw == nil {
return return
} }
@ -114,13 +101,12 @@ func makeWindowDebug() ui.Control {
gw.UiTab.Show() gw.UiTab.Show()
}) })
b5 := addButton(vbox, "Delete()") b5 := addButton(vbox, "Delete(tab)")
b5.OnClicked(func(*ui.Button) { b5.OnClicked(func(*ui.Button) {
x := cbox.Selected() x := cbox.Selected()
log.Println("x =", x) log.Println("x =", x)
log.Println("findBox; names[x] =", names[x]) log.Println("FindWindow; names[x] =", names[x])
findBox(names[x]) gw := FindWindow(names[x])
gw := findBox(names[x])
if gw == nil { if gw == nil {
return return
} }
@ -133,6 +119,19 @@ func makeWindowDebug() ui.Control {
gw.UiTab.Delete(*gw.TabNumber) gw.UiTab.Delete(*gw.TabNumber)
}) })
/////////////////////////////////////////////////////
vbox = addGroup(hbox, "Global Debug")
dump2 := addButton(vbox, "Dump Boxes")
dump2.OnClicked(func(*ui.Button) {
DumpBoxes()
})
dump1 := addButton(vbox, "Dump MAP")
dump1.OnClicked(func(*ui.Button) {
DumpMap()
})
return hbox return hbox
} }
@ -156,12 +155,28 @@ func addGroup(b *ui.Box, name string) *ui.Box {
return vbox return vbox
} }
func findBox(s string) *GuiWindow { func FindWindow(s string) *GuiWindow {
for name, window := range Data.WindowMap { for name, window := range Data.WindowMap {
if name == s { if name == s {
return window return window
} }
} }
log.Printf("COULD NOT FIND WINDOW", s)
return nil
}
func FindBox(s string) *GuiBox {
for name, window := range Data.WindowMap {
if name != s {
continue
}
for name, abox := range window.BoxMap {
log.Printf("gui.DumpBoxes() \tBOX mapname=%-12s abox.Name=%-12s", name, abox.Name)
return abox
}
log.Println("gui.FindBox() NEED TO INIT WINDOW name =", name)
}
log.Println("gui.FindBox() COULD NOT FIND BOX", s)
return nil return nil
} }
@ -180,6 +195,7 @@ func dumpBox(s string) {
// log.Println("gui.DumpBoxes()\tWindow.UiWindow type =", reflect.TypeOf(window.UiWindow)) // log.Println("gui.DumpBoxes()\tWindow.UiWindow type =", reflect.TypeOf(window.UiWindow))
log.Println("gui.DumpBoxes()\tWindow.UiWindow =", window.UiWindow) log.Println("gui.DumpBoxes()\tWindow.UiWindow =", window.UiWindow)
log.Println("gui.DumpBoxes()\tWindow.UiTab =", window.UiTab) log.Println("gui.DumpBoxes()\tWindow.UiTab =", window.UiTab)
log.Println("gui.dumpBox() BoxMap START")
for name, abox := range window.BoxMap { for name, abox := range window.BoxMap {
log.Printf("gui.DumpBoxes() \tBOX mapname=%-12s abox.Name=%-12s", name, abox.Name) log.Printf("gui.DumpBoxes() \tBOX mapname=%-12s abox.Name=%-12s", name, abox.Name)
if name == "MAINBOX" { if name == "MAINBOX" {
@ -189,6 +205,7 @@ func dumpBox(s string) {
} }
} }
} }
log.Println("gui.dumpBox() BoxMap END")
if window.UiTab != nil { if window.UiTab != nil {
pages := window.UiTab.NumPages() pages := window.UiTab.NumPages()
log.Println("gui.DumpBoxes()\tWindow.UiTab.NumPages() =", pages) log.Println("gui.DumpBoxes()\tWindow.UiTab.NumPages() =", pages)

View File

@ -210,6 +210,8 @@ func mapWindow(window *ui.Window, title string, x int, y int) *GuiBox {
box.Window = &newGuiWindow box.Window = &newGuiWindow
box.Name = title box.Name = title
newGuiWindow.BoxMap["jcarrInitTest"] = &box
return &box return &box
} }