Skip to content

Commit

Permalink
ui: Add IsForeground API to glfw driver (hajimehoshi#1058)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Burkett authored and hajimehoshi committed Jan 16, 2020
1 parent 6eb05a0 commit 6686044
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/driver/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type UI interface {
DeviceScaleFactor() float64
CursorMode() CursorMode
IsFullscreen() bool
IsForeground() bool
IsRunnableInBackground() bool
IsVsyncEnabled() bool
ScreenSizeInFullscreen() (int, int)
Expand Down
13 changes: 13 additions & 0 deletions internal/uidriver/glfw/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/uidriver/js/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions internal/uidriver/mobile/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down

0 comments on commit 6686044

Please sign in to comment.