ui: Stop the game when the window is deactivated (#230)

This commit is contained in:
Hajime Hoshi 2016-06-30 01:07:54 +09:00
parent ff30f01c1b
commit 3ebc5cc15a

View File

@ -30,6 +30,7 @@ type userInterface struct {
deviceScale float64 deviceScale float64
sizeChanged bool sizeChanged bool
contextRestored chan struct{} contextRestored chan struct{}
windowFocus chan struct{}
} }
var currentUI = &userInterface{ var currentUI = &userInterface{
@ -81,6 +82,9 @@ func (u *userInterface) ActualScreenScale() float64 {
} }
func (u *userInterface) Update() (interface{}, error) { func (u *userInterface) Update() (interface{}, error) {
if u.windowFocus != nil {
<-u.windowFocus
}
if u.contextRestored != nil { if u.contextRestored != nil {
<-u.contextRestored <-u.contextRestored
} }
@ -151,6 +155,16 @@ func initialize() (*opengl.Context, error) {
}) })
<-ch <-ch
} }
window.Call("addEventListener", "focus", func() {
if currentUI.windowFocus == nil {
return
}
close(currentUI.windowFocus)
currentUI.windowFocus = nil
})
window.Call("addEventListener", "blur", func() {
currentUI.windowFocus = make(chan struct{})
})
canvas = doc.Call("createElement", "canvas") canvas = doc.Call("createElement", "canvas")
canvas.Set("width", 16) canvas.Set("width", 16)