From 1274211f20ad142392d306387272b6fae6355af5 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 2 Jun 2019 15:19:53 -0700 Subject: [PATCH] almost complete 'CLEAN'. yet more code refactoring Signed-off-by: Jeff Carr --- area.go | 10 ++++------ box.go | 25 ------------------------ button.go | 41 ++++++++++++++++++++-------------------- debug.go | 1 - entry.go | 3 ++- gui.go | 48 ++--------------------------------------------- structs.go | 7 +++---- table.go | 2 -- tableCallbacks.go | 7 ++----- 9 files changed, 33 insertions(+), 111 deletions(-) diff --git a/area.go b/area.go index 5e6fb9b..3b3430a 100644 --- a/area.go +++ b/area.go @@ -7,16 +7,14 @@ import _ "github.com/andlabs/ui/winmanifest" import "github.com/davecgh/go-spew/spew" -// THIS IS CLEAN - func makeGenericArea(gb *GuiBox, newText *ui.AttributedString, custom func(*GuiButton)) { // make this button just to get the default font (but don't display the button) // There should be another way to do this (?) var newB *GuiButton newB = CreateFontButton(gb, "AREA") newB.Box = gb - newB.custom = custom - newB.GW = gb.Window + newB.Custom = custom +// newB.GW = gb.Window gw := gb.Window // initialize the GuiArea{} @@ -77,8 +75,8 @@ func (ah GuiArea) MouseEvent(a *ui.Area, me *ui.AreaMouseEvent) { log.Println("GOT MOUSE UP") log.Println("GOT MOUSE UP ah.Button =", ah.Button) log.Println("GOT MOUSE UP ah.Button.FB =", ah.Button.FB) - if (ah.Button.custom != nil) { - ah.Button.custom(ah.Button) + if (ah.Button.Custom != nil) { + ah.Button.Custom(ah.Button) } else if (Data.MouseClick != nil) { Data.MouseClick(ah.Button) } diff --git a/box.go b/box.go index 259d7fa..9cffdf5 100644 --- a/box.go +++ b/box.go @@ -8,8 +8,6 @@ import _ "github.com/andlabs/ui/winmanifest" // import pb "git.wit.com/wit/witProtobuf" // import "github.com/davecgh/go-spew/spew" -// THIS IS CLEAN - // add(nil, newbox, "") // use this when the Window is created. Always called 'MAINBOX' // add(gw.BoxMap["MAINBOX"], newbox, name) // use this to add a box off the main box // add(gw.BoxMap["BUTTONBOX"], newbox, name) // use this to add something to the box called 'BUTTONBOX' @@ -150,26 +148,3 @@ func AddGenericBox(gw *GuiWindow, name string) *GuiBox { return newbox } - -/* -func CreateGenericBox(gw *GuiWindow, name string) *GuiBox { - log.Println("CreateAddVmBox() START name =", name) - - var box *GuiBox - box = new(GuiBox) - - vbox := ui.NewVerticalBox() - vbox.SetPadded(true) - box.UiBox = vbox - box.Window = gw - gw.BoxMap["ADD VM" + name] = box - - hbox := ui.NewHorizontalBox() - hbox.SetPadded(true) - vbox.Append(hbox, false) - - gw.UiTab.Append(name, vbox) - gw.UiTab.SetMargined(0, true) - return box -} -*/ diff --git a/button.go b/button.go index 5211042..298c312 100644 --- a/button.go +++ b/button.go @@ -1,16 +1,11 @@ package gui import "log" - import "github.com/andlabs/ui" import _ "github.com/andlabs/ui/winmanifest" - import pb "git.wit.com/wit/witProtobuf" - // import "github.com/davecgh/go-spew/spew" -// THIS IS CLEAN - // This is the default mouse click handler // Every mouse click that hasn't been assigned to // something specific will fall into this routine @@ -32,23 +27,27 @@ func defaultButtonClick(button *ui.Button) { } if Data.AllButtons[key].B == button { log.Println("\tgui.defaultButtonClick() BUTTON MATCHED") - log.Println("\tgui.defaultButtonClick() Data.AllButtons[key].Action =", Data.AllButtons[key].Action) - if Data.AllButtons[key].custom != nil { - log.Println("\tgui.defaultButtonClick() DOING CUSTOM FUNCTION") - Data.AllButtons[key].custom(Data.AllButtons[key]) - return - } - if (Data.MouseClick != nil) { - Data.MouseClick(Data.AllButtons[key]) - } else { - log.Println("\tgui.defaultButtonClick() IGNORING BUTTON. MouseClick() is nil") - } + guiButtonClick(Data.AllButtons[key]) return } } - log.Println("\tgui.defaultButtonClick() BUTTON NOT FOUND") + log.Println("\tgui.defaultButtonClick() ERROR: BUTTON NOT FOUND") if (Data.Debug) { - panic("gui.defaultButtonClick() SHOULD NOT HAVE UNMAPPED BUTTONS") + panic("gui.defaultButtonClick() ERROR: UNMAPPED ui.Button") + } +} + +func guiButtonClick(button *GuiButton) { + log.Println("\tgui.guiButtonClick() button.Action =", button.Action) + if button.Custom != nil { + log.Println("\tgui.guiButtonClick() DOING CUSTOM FUNCTION") + button.Custom(button) + return + } + if (Data.MouseClick != nil) { + Data.MouseClick(button) + } else { + log.Println("\tgui.guiButtonClick() IGNORING BUTTON. MouseClick() is nil") } } @@ -63,12 +62,12 @@ func CreateButton(box *GuiBox, a *pb.Account, vm *pb.Event_VM, name string, acti log.Println("CreateButton() box.Window == nil") panic("crap") } - newB.GW = box.Window newB.Account = a newB.VM = vm newB.Box = box +// newB.GW = box.Window newB.Action = action - newB.custom = custom + newB.Custom = custom Data.AllButtons = append(Data.AllButtons, newB) box.UiBox.Append(newB.B, false) @@ -76,9 +75,9 @@ func CreateButton(box *GuiBox, a *pb.Account, vm *pb.Event_VM, name string, acti } func CreateFontButton(box *GuiBox, action string) *GuiButton { - // create a 'fake' button entry for the mouse clicks var newGB GuiButton + newGB.Name = "FONT" newGB.Action = action newGB.FB = ui.NewFontButton() newGB.Box = box diff --git a/debug.go b/debug.go index 8920b5e..03b1d9f 100644 --- a/debug.go +++ b/debug.go @@ -10,7 +10,6 @@ import "fmt" // import "github.com/davecgh/go-spew/spew" // import pb "git.wit.com/wit/witProtobuf" -// THIS IS NOT CLEAN // // this watches the GUI primarily to process protobuf's // this is pointless or wrong but I use it for debugging diff --git a/entry.go b/entry.go index 83e4ec3..b8282e3 100644 --- a/entry.go +++ b/entry.go @@ -8,8 +8,9 @@ import _ "github.com/andlabs/ui/winmanifest" import "github.com/davecgh/go-spew/spew" // import pb "git.wit.com/wit/witProtobuf" +// THIS IS CLEAN (except the 'Memory' normalization example) -// THIS IS CLEAN +// functions for handling text entry boxes func NewLabel(box *GuiBox, text string) { box.UiBox.Append(ui.NewLabel(text), false) diff --git a/gui.go b/gui.go index b412e20..eccc6e8 100644 --- a/gui.go +++ b/gui.go @@ -12,8 +12,6 @@ import pb "git.wit.com/wit/witProtobuf" // import "github.com/davecgh/go-spew/spew" -// THIS IS NOT CLEAN (except the Memory normalization example) - const Xaxis = 0 const Yaxis = 1 @@ -73,10 +71,12 @@ func InitTabWindow(gw *GuiWindow) { gw.UiWindow.SetBorderless(false) // create a 'fake' button entry for the mouse clicks + /* var newBM GuiButton newBM.Action = "QUIT" newBM.GW = gw Data.AllButtons = append(Data.AllButtons, &newBM) + */ gw.UiWindow.OnClosing(func(*ui.Window) bool { log.Println("InitTabWindow() OnClosing() THIS WINDOW IS CLOSING gw=", gw) @@ -128,50 +128,6 @@ func normalizeInt(s string) string { return clean } -/* -func defaultEntryChange(e *ui.Entry) { - for key, em := range Data.AllEntries { - if (Data.Debug) { - log.Println("\tdefaultEntryChange() Data.AllEntries =", key, em) - } - if Data.AllEntries[key].UiEntry == e { - log.Println("defaultEntryChange() FOUND", - "action =", Data.AllEntries[key].Action, - "Last =", Data.AllEntries[key].Last, - "e.Text() =", e.Text()) - Data.AllEntries[key].Last = e.Text() - if Data.AllEntries[key].Normalize != nil { - fixed := Data.AllEntries[key].Normalize(e.Text()) - e.SetText(fixed) - } - return - } - } - log.Println("defaultEntryChange() ERROR. MISSING ENTRY MAP. e.Text() =", e.Text()) -} - -func defaultMakeEntry(startValue string, edit bool, action string) *GuiEntry { - e := ui.NewEntry() - e.SetText(startValue) - if (edit == false) { - e.SetReadOnly(true) - } - e.OnChanged(defaultEntryChange) - - // add the entry field to the global map - var newEntry GuiEntry - newEntry.UiEntry = e - newEntry.Edit = edit - newEntry.Action = action - if (action == "Memory") { - newEntry.Normalize = normalizeInt - } - Data.AllEntries = append(Data.AllEntries, &newEntry) - - return &newEntry -} -*/ - func MessageWindow(gw *GuiWindow, msg1 string, msg2 string) { ui.MsgBox(gw.UiWindow, msg1, msg2) } diff --git a/structs.go b/structs.go index 8ad240e..982b6a0 100644 --- a/structs.go +++ b/structs.go @@ -8,8 +8,6 @@ import _ "github.com/andlabs/ui/winmanifest" import pb "git.wit.com/wit/witProtobuf" -// THIS IS CLEAN - // // All GUI Data Structures and functions that are external // If you need cross platform support, these might only @@ -110,15 +108,16 @@ type GuiBox struct { // the user clicks it. You could probably // call this 'GuiMouseClick' type GuiButton struct { + Name string // field for human readable name Action string // what type of button Box *GuiBox // what box the button click was in - GW *GuiWindow // what window the button click was in (redundant?) +// GW *GuiWindow // what window the button click was in (redundant?) Account *pb.Account // associated with what account? VM *pb.Event_VM // associated with which VM? // a callback function for the main application - custom func (*GuiButton) + Custom func (*GuiButton) // andlabs/ui abstraction mapping B *ui.Button diff --git a/table.go b/table.go index 67a64b7..d3e5f20 100644 --- a/table.go +++ b/table.go @@ -9,8 +9,6 @@ import _ "github.com/andlabs/ui/winmanifest" // import "github.com/davecgh/go-spew/spew" -// THIS IS CLEAN - func initRowBTcolor(mh *TableData, intBG int, cell TableColumnData) { humanInt := cell.Index diff --git a/tableCallbacks.go b/tableCallbacks.go index f71cabc..c4ab738 100644 --- a/tableCallbacks.go +++ b/tableCallbacks.go @@ -14,8 +14,6 @@ import "runtime" import "github.com/andlabs/ui" import _ "github.com/andlabs/ui/winmanifest" -// THIS IS CLEAN - func (mh *TableData) NumRows(m *ui.TableModel) int { if (Data.Debug) { log.Println("NumRows = mh.RowCount = ", mh.RowCount, "(last Row & Column =", mh.lastRow, mh.lastColumn, ")") @@ -110,11 +108,10 @@ func defaultSetCellValue(mh *TableData, row int, column int) { button := mh.Rows[row].HumanData[humanID].Button if (button != nil) { - if (Data.MouseClick != nil) { - Data.MouseClick(button) - } + guiButtonClick(button) return } + log.Println("defaultSetCellValue() ERROR: UNKNOWN BUTTON IN TABLE") if (Data.Debug) { panic("defaultSetCellValue() GOT AN UNKNOWN BUTTON CLICK IN TABLE") }