From 76b371b375fcd98f7d3f20363c17f62ebad1c985 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Mon, 25 Oct 2021 06:42:33 -0500 Subject: [PATCH] EXIT: more accurate window close & exit code Signed-off-by: Jeff Carr --- gui-example/main.go | 40 +++++++++++++++++++++++++++------------- structs.go | 4 +++- window.go | 43 ++++++++++++++++++++++++++++++++----------- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/gui-example/main.go b/gui-example/main.go index c9e8cbd..d391ed4 100644 --- a/gui-example/main.go +++ b/gui-example/main.go @@ -8,26 +8,25 @@ import ( "git.wit.org/wit/gui" ) -func customExit(gw *gui.GuiWindow) { - log.Println("Should Exit Here") - os.Exit(0) -} - func main() { - log.Println("starting my Control Panel") - - gui.Config.Width = 800 - gui.Config.Height = 300 - gui.Config.Exit = customExit + log.Println("Starting my Control Panel") + // This initializes the first window go gui.Main(initGUI) + // This starts a goroutine to demonstrate how to + // inject things into the GUI watchGUI() } func initGUI() { - n := gui.NewWindow("WIT GUI Example Window", 640, 480) - n.AddDemoTab("A Simple Tab Demo") + gui.Config.Title = "WIT GUI Window Demo" + gui.Config.Width = 640 + gui.Config.Height = 480 + gui.Config.Exit = myExit + + node := gui.NewWindow() + node.AddDemoTab("A Simple Tab Demo") } // This demonstrates how to properly interact with the GUI @@ -36,12 +35,27 @@ func initGUI() { func watchGUI() { var i = 1 for { - log.Println("Waiting for customExit()", i) + log.Println("Waiting", i, "seconds") i += 1 time.Sleep(1 * time.Second) if i == 4 { log.Println("Opening a Debug Window via the gui.Queue()") + gui.Config.Width = 800 + gui.Config.Height = 300 + gui.Config.Exit = myDebugExit gui.Queue(gui.DebugWindow) } } } + +func myExit(n *gui.Node) { + log.Println() + log.Println("Entered myExit() on node.Name =", n.Name) + log.Println() + os.Exit(0) +} + +func myDebugExit(n *gui.Node) { + log.Println("Entered myDebugExit() on node.Name =", n.Name) + log.Println("Don't actually os.Exit()") +} diff --git a/structs.go b/structs.go index 80c763d..cf84746 100644 --- a/structs.go +++ b/structs.go @@ -20,13 +20,15 @@ var Data GuiData var Config GuiConfig type GuiConfig struct { + Title string Width int Height int + Exit func(*Node) + Debug bool DebugNode bool DebugTabs bool DebugTable bool - Exit func(*GuiWindow) depth int counter int // used to make unique ID's diff --git a/window.go b/window.go index 4940e35..5a1e8d1 100644 --- a/window.go +++ b/window.go @@ -80,15 +80,17 @@ func InitWindow(parent *Node, gw *GuiWindow, name string, axis int) *Node { w := node.uiWindow newGuiWindow.UiWindow = w - // newGuiWindow.UiWindow.SetTitle("test") + f := Config.Exit w.OnClosing(func(*ui.Window) bool { - log.Println("gui.InitWindow() OnClosing() THIS WINDOW IS CLOSING newGuiWindow=", newGuiWindow) + if (Config.Debug) { + log.Println("gui.InitWindow() OnClosing()") + } // newGuiWindow.UiWindow.Destroy() - if Config.Exit == nil { + if f == nil { ui.Quit() } else { - // allow a custom exit function - Config.Exit(newGuiWindow) + // use a custom exit function + f(node) } return true }) @@ -299,8 +301,14 @@ func (parent *Node) makeNode(title string, x int, y int) *Node { func (n *Node) uiNewWindow(title string, x int, y int) { w := ui.NewWindow(title, x, y, false) w.SetBorderless(false) + f := Config.Exit w.OnClosing(func(*ui.Window) bool { - log.Println("ui.Window().OnClosing() IS EMPTY FOR window name =", title) + if (Config.Debug) { + log.Println("ui.Window().OnClosing()") + } + if (f != nil) { + f(n) + } return true }) w.SetMargined(true) @@ -390,18 +398,31 @@ func mapWindow(parent *Node, window *ui.Window, title string, x int, y int) *Nod return node } -func NewWindow(title string, x int, y int) *Node { +// This routine creates a blank window with a Title and size (W x H) +// +// This routine can not have any arguements due to the nature of how +// it can be passed via the 'andlabs/ui' queue which, because it is +// cross platform, must pass UI changes into the OS threads (that is +// my guess). +func NewWindow() *Node { + title := Config.Title + w := Config.Width + h := Config.Height + var node *Node - node = mapWindow(nil, nil, title, x, y) + node = mapWindow(nil, nil, title, w, h) box := node.box log.Println("gui.NewWindow() title = box.Name =", box.Name) - node.uiNewWindow(box.Name, x, y) + node.uiNewWindow(box.Name, w, h) window := node.uiWindow + f := Config.Exit ui.OnShouldQuit(func() bool { - log.Println("createWindow().Destroy()", box.Name) - window.Destroy() + log.Println("createWindow().Destroy() on node.Name =", node.Name) + if (f != nil) { + f(node) + } return true })