From 3ebc5cc15aca98f04f185949f738dde17663c964 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 30 Jun 2016 01:07:54 +0900 Subject: [PATCH] ui: Stop the game when the window is deactivated (#230) --- internal/ui/ui_js.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index ea4e3dabe..7a634f02a 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -30,6 +30,7 @@ type userInterface struct { deviceScale float64 sizeChanged bool contextRestored chan struct{} + windowFocus chan struct{} } var currentUI = &userInterface{ @@ -81,6 +82,9 @@ func (u *userInterface) ActualScreenScale() float64 { } func (u *userInterface) Update() (interface{}, error) { + if u.windowFocus != nil { + <-u.windowFocus + } if u.contextRestored != nil { <-u.contextRestored } @@ -151,6 +155,16 @@ func initialize() (*opengl.Context, error) { }) <-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.Set("width", 16)