From 7ea694b0865b3fead15c0ad5d0192d43badb5fd4 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 22 May 2019 19:11:43 -0700 Subject: [PATCH] more moving of all variables into a common structure Signed-off-by: Jeff Carr --- area.go | 32 ++++++++++++++----------------- examples.go | 27 +++++++++++---------------- gui.go | 54 +++++++++++++++++++++++++++-------------------------- 3 files changed, 53 insertions(+), 60 deletions(-) diff --git a/area.go b/area.go index 225cefb..2c48cc9 100644 --- a/area.go +++ b/area.go @@ -7,10 +7,6 @@ import _ "github.com/andlabs/ui/winmanifest" import "github.com/davecgh/go-spew/spew" -var fontButton *ui.FontButton -var attrstr *ui.AttributedString -var splashArea *ui.Area - func areaClick(a int, b string) { log.Println("GOT areaClick(a,b) =", a, b) } @@ -18,26 +14,26 @@ func areaClick(a int, b string) { func makeSplashArea(custom func(int, string)) *ui.Area { // make this button just to get the default font (but don't display the button) // There should be another way to do this (?) - fontButton = CreateFontButton("SplashFont", "CLOSE", custom) + Data.fontButton = CreateFontButton("SplashFont", "CLOSE", custom) makeAttributedString() - splashArea = ui.NewArea(myAH) + Data.splashArea = ui.NewArea(myAH) - spew.Dump(splashArea) - return splashArea + spew.Dump(Data.splashArea) + return Data.splashArea } func appendWithAttributes(what string, attrs ...ui.Attribute) { - start := len(attrstr.String()) + start := len(Data.attrstr.String()) end := start + len(what) - attrstr.AppendUnattributed(what) + Data.attrstr.AppendUnattributed(what) for _, a := range attrs { - attrstr.SetAttribute(a, start, end) + Data.attrstr.SetAttribute(a, start, end) } } func makeAttributedString() { - attrstr = ui.NewAttributedString("") + Data.attrstr = ui.NewAttributedString("") appendWithAttributes("Welcome to the Cloud Control Panel\n", ui.TextSize(16), ui.TextColor{0.0, 0.0, 0.8, .8}) // "RGBT" @@ -46,13 +42,13 @@ func makeAttributedString() { appendWithAttributes("This control panel was designed to be an interface to your 'private' cloud. ", ui.TextWeightBold) appendWithAttributes("The concept of a private cloud means that you can use a providers system, or, seemlessly, use your own hardware in your own datacenter. ", ui.TextWeightBold) - attrstr.AppendUnattributed("\n") - attrstr.AppendUnattributed("\n") + Data.attrstr.AppendUnattributed("\n") + Data.attrstr.AppendUnattributed("\n") appendWithAttributes("This control panel requires:\n") - attrstr.AppendUnattributed("\n") + Data.attrstr.AppendUnattributed("\n") appendWithAttributes("IPv6\n") appendWithAttributes("Your hostname in DNS\n") - attrstr.AppendUnattributed("\n\n\n\n\n") + Data.attrstr.AppendUnattributed("\n\n\n\n\n") appendWithAttributes("\n", ui.TextSize(10)) } @@ -66,8 +62,8 @@ var myAH areaHandler func (ah areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) { tl := ui.DrawNewTextLayout(&ui.DrawTextLayoutParams{ - String: attrstr, - DefaultFont: fontButton.Font(), + String: Data.attrstr, + DefaultFont: Data.fontButton.Font(), Width: p.AreaWidth, Align: ui.DrawTextAlign(1), }) diff --git a/examples.go b/examples.go index 3d6c904..8ee9731 100644 --- a/examples.go +++ b/examples.go @@ -8,9 +8,6 @@ import _ "github.com/andlabs/ui/winmanifest" // import "github.com/davecgh/go-spew/spew" -var jcarrButton *ui.Button -var jcarrEntry *ui.MultilineEntry - func hostnameButton(hostname string) ui.Control { tmpbox := ui.NewHorizontalBox() tmpbox.SetPadded(true) @@ -19,8 +16,6 @@ func hostnameButton(hostname string) ui.Control { tmpbox.Append(tmpButton, false) tmpButton.OnClicked(defaultButtonClick) - jcarrButton = tmpButton - return tmpbox } @@ -34,7 +29,7 @@ func makeGroupEntries() ui.Control { entryForm.SetPadded(true) group.SetChild(entryForm) - jcarrEntry = ui.NewMultilineEntry() + jcarrEntry := ui.NewMultilineEntry() entryForm.Append("Entry", ui.NewEntry(), false) entryForm.Append("Password Entry", ui.NewPasswordEntry(), false) entryForm.Append("Search Entry", ui.NewSearchEntry(), false) @@ -134,7 +129,7 @@ func makeDataChoosersPage() ui.Control { entry := ui.NewEntry() entry.SetReadOnly(true) button.OnClicked(func(*ui.Button) { - filename := ui.OpenFile(mainwin) + filename := ui.OpenFile(Data.mainwin) if filename == "" { filename = "(cancelled)" } @@ -151,7 +146,7 @@ func makeDataChoosersPage() ui.Control { entry2 := ui.NewEntry() entry2.SetReadOnly(true) button.OnClicked(func(*ui.Button) { - filename := ui.SaveFile(mainwin) + filename := ui.SaveFile(Data.mainwin) if filename == "" { filename = "(cancelled)" } @@ -172,7 +167,7 @@ func makeDataChoosersPage() ui.Control { button = ui.NewButton("Message Box") button.OnClicked(func(*ui.Button) { - ui.MsgBox(mainwin, + ui.MsgBox(Data.mainwin, "This is a normal message box.", "More detailed information can be shown here.") }) @@ -181,7 +176,7 @@ func makeDataChoosersPage() ui.Control { false, ui.AlignFill, false, ui.AlignFill) button = ui.NewButton("Error Box") button.OnClicked(func(*ui.Button) { - ui.MsgBoxError(mainwin, + ui.MsgBoxError(Data.mainwin, "This message box describes an error.", "More detailed information can be shown here.") }) @@ -193,14 +188,14 @@ func makeDataChoosersPage() ui.Control { } func AddChoosersDemo() { - maintab.Append("Choosers examples", makeDataChoosersPage()) - maintab.SetMargined(tabcount, true) - tabcount += 1 + Data.maintab.Append("Choosers examples", makeDataChoosersPage()) + Data.maintab.SetMargined(Data.tabcount, true) + Data.tabcount += 1 } // This hangs on GTK func AddEntriesDemo() { - maintab.Append("Group examples", makeGroupEntries()) - tabcount += 1 - maintab.SetMargined(tabcount, true) + Data.maintab.Append("Group examples", makeGroupEntries()) + Data.tabcount += 1 + Data.maintab.SetMargined(Data.tabcount, true) } diff --git a/gui.go b/gui.go index 0795258..9a6f148 100644 --- a/gui.go +++ b/gui.go @@ -8,15 +8,6 @@ import _ "github.com/andlabs/ui/winmanifest" import "github.com/gookit/config" import "github.com/davecgh/go-spew/spew" -var mainwin *ui.Window -var maintab *ui.Tab -var tabcount int - -var Height int - -var allButtons []ButtonMap - - // // All GUI Data Structures and functions that are external // If you need cross platform support, these might only @@ -28,8 +19,19 @@ type GuiDataStructure struct { State string MainWindow *ui.Window Width int + Height int ButtonClick func(int, string) + cloudWindow *ui.Window + mainwin *ui.Window + maintab *ui.Tab + tabcount int + allButtons []ButtonMap + + // stuff for the 'area' + fontButton *ui.FontButton + attrstr *ui.AttributedString + splashArea *ui.Area } type TableColumnData struct { @@ -50,22 +52,22 @@ type ButtonMap struct { } func setupUI() { - mainwin = ui.NewWindow("Cloud Control Panel", Data.Width, Height, false) - mainwin.OnClosing(func(*ui.Window) bool { + Data.mainwin = ui.NewWindow("Cloud Control Panel", Data.Width, Data.Height, false) + Data.mainwin.OnClosing(func(*ui.Window) bool { ui.Quit() return true }) ui.OnShouldQuit(func() bool { - mainwin.Destroy() + Data.mainwin.Destroy() return true }) - maintab = ui.NewTab() - mainwin.SetChild(maintab) - mainwin.SetMargined(true) + Data.maintab = ui.NewTab() + Data.mainwin.SetChild(Data.maintab) + Data.mainwin.SetMargined(true) - tabcount = 0 - mainwin.Show() + Data.tabcount = 0 + Data.mainwin.Show() } func AddNewTab(mytab *ui.Tab, newbox ui.Control, tabOffset int) { @@ -176,12 +178,12 @@ func DoGUI() { func defaultButtonClick(button *ui.Button) { log.Println("defaultButtonClick() button =", button) - for key, foo := range allButtons { - log.Println("allButtons =", key, foo) - if allButtons[key].B == button { + for key, foo := range Data.allButtons { + log.Println("Data.allButtons =", key, foo) + if Data.allButtons[key].B == button { log.Println("\tBUTTON MATCHED") - if allButtons[key].custom != nil { - allButtons[key].custom(42, "something foo") + if Data.allButtons[key].custom != nil { + Data.allButtons[key].custom(42, "something foo") } } } @@ -189,8 +191,8 @@ func defaultButtonClick(button *ui.Button) { func defaultFontButtonClick(button *ui.FontButton) { log.Println("defaultButtonClick() button =", button) - for key, foo := range allButtons { - log.Println("allButtons =", key, foo) + for key, foo := range Data.allButtons { + log.Println("Data.allButtons =", key, foo) } } @@ -204,7 +206,7 @@ func CreateButton(name string, note string, custom func(int, string)) *ui.Button newmap.note = note newmap.name = name newmap.custom = custom - allButtons = append(allButtons, newmap) + Data.allButtons = append(Data.allButtons, newmap) return newB } @@ -219,7 +221,7 @@ func CreateFontButton(name string, note string, custom func(int, string)) *ui.Fo newmap.note = note newmap.name = name newmap.custom = custom - allButtons = append(allButtons, newmap) + Data.allButtons = append(Data.allButtons, newmap) return newB }