From 6686044452cc3aa39e3b8241905fe9cb11a46be0 Mon Sep 17 00:00:00 2001 From: Zachary Burkett Date: Wed, 15 Jan 2020 20:47:23 -0500 Subject: [PATCH] ui: Add IsForeground API to glfw driver (#1058) Updates #1037 --- internal/driver/ui.go | 1 + internal/uidriver/glfw/ui.go | 13 +++++++++++++ internal/uidriver/js/ui.go | 5 +++++ internal/uidriver/mobile/ui.go | 5 +++++ run.go | 10 ++++++++++ 5 files changed, 34 insertions(+) diff --git a/internal/driver/ui.go b/internal/driver/ui.go index 9ff6d001c..b42165e82 100644 --- a/internal/driver/ui.go +++ b/internal/driver/ui.go @@ -37,6 +37,7 @@ type UI interface { DeviceScaleFactor() float64 CursorMode() CursorMode IsFullscreen() bool + IsForeground() bool IsRunnableInBackground() bool IsVsyncEnabled() bool ScreenSizeInFullscreen() (int, int) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index ede4c5185..87eb37662 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -402,6 +402,19 @@ func (u *UserInterface) SetFullscreen(fullscreen bool) { u.setWindowSize(w, h, fullscreen, u.vsync) } +func (u *UserInterface) IsForeground() bool { + if !u.isRunning() { + return false + } + + var foreground bool + _ = u.t.Call(func() error { + foreground = u.window.GetAttrib(glfw.Focused) == glfw.True + return nil + }) + return foreground +} + func (u *UserInterface) SetRunnableInBackground(runnableInBackground bool) { u.setRunnableInBackground(runnableInBackground) } diff --git a/internal/uidriver/js/ui.go b/internal/uidriver/js/ui.go index f03d90cab..f781a65c1 100644 --- a/internal/uidriver/js/ui.go +++ b/internal/uidriver/js/ui.go @@ -76,6 +76,11 @@ func (u *UserInterface) IsFullscreen() bool { return false } +func (u *UserInterface) IsForeground() bool { + // TODO: implement this + return true +} + func (u *UserInterface) SetRunnableInBackground(runnableInBackground bool) { u.runnableInBackground = runnableInBackground } diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index 34d364584..f2f1753fc 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -369,6 +369,11 @@ func (u *UserInterface) SetFullscreen(fullscreen bool) { // Do nothing } +func (u *UserInterface) IsForeground() { + // TODO: implement this + return true +} + func (u *UserInterface) IsRunnableInBackground() bool { return false } diff --git a/run.go b/run.go index d4c094878..bca5063b4 100644 --- a/run.go +++ b/run.go @@ -346,6 +346,16 @@ func SetFullscreen(fullscreen bool) { uiDriver().SetFullscreen(fullscreen) } +// IsForeground returns a boolean value indicating whether +// the game is in focus or in the foreground. +// +// IsForeground will only return true if IsRunnableInBackground is false. +// +// IsForeground is concurrent-safe. +func IsForeground() bool { + return uiDriver().IsForeground() +} + // IsRunnableInBackground returns a boolean value indicating whether // the game runs even in background. //