This commit is contained in:
Hajime Hoshi 2023-02-21 02:06:38 +09:00
parent b08b740914
commit 9aef5746d8
2 changed files with 19 additions and 9 deletions

View File

@ -22,6 +22,7 @@ import (
"runtime/debug" "runtime/debug"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time"
"unicode" "unicode"
"golang.org/x/mobile/app" "golang.org/x/mobile/app"
@ -54,6 +55,7 @@ var (
func init() { func init() {
theUI.userInterfaceImpl = userInterfaceImpl{ theUI.userInterfaceImpl = userInterfaceImpl{
foreground: 1, foreground: 1,
runnableOnUnfocused: 0, // This is 0 for backward compatibility during v2 (#2576).
graphicsDriverInitCh: make(chan struct{}), graphicsDriverInitCh: make(chan struct{}),
errCh: make(chan error), errCh: make(chan error),
@ -73,7 +75,8 @@ func (u *userInterfaceImpl) Update() error {
default: default:
} }
if !u.IsFocused() { if !u.IsFocused() && !u.IsRunnableOnUnfocused() {
time.Sleep(time.Second / 60)
return nil return nil
} }
@ -101,8 +104,9 @@ type userInterfaceImpl struct {
outsideWidth float64 outsideWidth float64
outsideHeight float64 outsideHeight float64
foreground int32 foreground int32
errCh chan error runnableOnUnfocused int32
errCh chan error
// Used for gomobile-build // Used for gomobile-build
gbuildWidthPx int gbuildWidthPx int
@ -232,9 +236,11 @@ func (u *userInterfaceImpl) SetForeground(foreground bool) error {
if foreground { if foreground {
return hooks.ResumeAudio() return hooks.ResumeAudio()
} else { }
if !u.IsRunnableOnUnfocused() {
return hooks.SuspendAudio() return hooks.SuspendAudio()
} }
return nil
} }
func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error { func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error {
@ -395,11 +401,15 @@ func (u *userInterfaceImpl) IsFocused() bool {
} }
func (u *userInterfaceImpl) IsRunnableOnUnfocused() bool { func (u *userInterfaceImpl) IsRunnableOnUnfocused() bool {
return false return atomic.LoadInt32(&u.runnableOnUnfocused) != 0
} }
func (u *userInterfaceImpl) SetRunnableOnUnfocused(runnableOnUnfocused bool) { func (u *userInterfaceImpl) SetRunnableOnUnfocused(runnableOnUnfocused bool) {
// Do nothing var v int32
if runnableOnUnfocused {
v = 1
}
atomic.StoreInt32(&u.runnableOnUnfocused, v)
} }
func (u *userInterfaceImpl) SetFPSMode(mode FPSModeType) { func (u *userInterfaceImpl) SetFPSMode(mode FPSModeType) {

6
run.go
View File

@ -428,13 +428,13 @@ func IsRunnableOnUnfocused() bool {
// SetRunnableOnUnfocused sets the state if the game runs even in background. // SetRunnableOnUnfocused sets the state if the game runs even in background.
// //
// If the given value is true, the game runs even in background e.g. when losing focus. // If the given value is true, the game runs even in background e.g. when losing focus.
// The initial state is true. // The initial state is true on desktops, and false on mobiles.
// For mobiles, in addition to SetRunnableOnUnfocused, you might require some permission settings
// not to pause audio when the application goes to background.
// //
// Known issue: On browsers, even if the state is on, the game doesn't run in background tabs. // Known issue: On browsers, even if the state is on, the game doesn't run in background tabs.
// This is because browsers throttles background tabs not to often update. // This is because browsers throttles background tabs not to often update.
// //
// SetRunnableOnUnfocused does nothing on mobiles so far.
//
// SetRunnableOnUnfocused is concurrent-safe. // SetRunnableOnUnfocused is concurrent-safe.
func SetRunnableOnUnfocused(runnableOnUnfocused bool) { func SetRunnableOnUnfocused(runnableOnUnfocused bool) {
ui.Get().SetRunnableOnUnfocused(runnableOnUnfocused) ui.Get().SetRunnableOnUnfocused(runnableOnUnfocused)