From 5cb040b7a8f6059e5490f7408054011b3a4d7a52 Mon Sep 17 00:00:00 2001 From: surtur Date: Mon, 23 May 2022 17:56:24 +0200 Subject: [PATCH] app(xkcdreader): init,get fyne.App from anywhere --- xkcdreader/app.go | 18 ++++++++++++++++-- xkcdreader/app_test.go | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/xkcdreader/app.go b/xkcdreader/app.go index a1ad83a..160e4e7 100644 --- a/xkcdreader/app.go +++ b/xkcdreader/app.go @@ -17,10 +17,15 @@ import ( const appGreeting = "welcome to go-xkcdreader" +var a fyne.App + // RunApp performs sets up and runs the main application func RunApp() { - a := app.New() - w := a.NewWindow(cmd.GetAppName()) + // initialize the fyne application + newApp() + + goxkcdreader := getApp() + w := goxkcdreader.NewWindow(cmd.GetAppName()) centered := container.New( layout.NewHBoxLayout(), @@ -40,6 +45,15 @@ func RunApp() { w.ShowAndRun() } +func newApp() { + a = app.New() + log.Println("Created a new fyne application") +} + +func getApp() fyne.App { + return a +} + // makeGreeting creates a greeting label func makeGreeting() *widget.Label { w := widget.NewLabel(appGreeting) diff --git a/xkcdreader/app_test.go b/xkcdreader/app_test.go index 987082b..d152bcc 100644 --- a/xkcdreader/app_test.go +++ b/xkcdreader/app_test.go @@ -92,3 +92,26 @@ func TestTabs(t *testing.T) { FAIL */ } + +func TestNewApp(t *testing.T) { + // init the application + newApp() + + // get the application from var a + testA := a + + if testA == nil { + t.Error("Failed to init application") + } +} + +func TestGetApp(t *testing.T) { + // init the application + newApp() + + got := getApp() + + if got == nil { + t.Error("Failed to get application pointer using getApp()") + } +}