From 64ec626ca653e679f1fadeef3ef687d038ecf029 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 4 Sep 2021 19:20:24 +0900 Subject: [PATCH] internal/uidriver/js: Optimize isFocused() by using bind --- internal/uidriver/js/ui_js.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/uidriver/js/ui_js.go b/internal/uidriver/js/ui_js.go index e3c269f1a..1bf77eacc 100644 --- a/internal/uidriver/js/ui_js.go +++ b/internal/uidriver/js/ui_js.go @@ -89,6 +89,19 @@ var ( go2cpp = js.Global().Get("go2cpp") ) +var ( + documentHasFocus js.Value + documentHidden js.Value +) + +func init() { + if go2cpp.Truthy() { + return + } + documentHasFocus = document.Get("hasFocus").Call("bind", document) + documentHidden = js.Global().Get("Object").Call("getOwnPropertyDescriptor", js.Global().Get("Document").Get("prototype"), "hidden").Get("get").Call("bind", document) +} + func (u *UserInterface) ScreenSizeInFullscreen() (int, int) { return window.Get("innerWidth").Int(), window.Get("innerHeight").Int() } @@ -252,10 +265,10 @@ func (u *UserInterface) isFocused() bool { return true } - if !document.Call("hasFocus").Bool() { + if !documentHasFocus.Invoke().Bool() { return false } - if document.Get("hidden").Bool() { + if documentHidden.Invoke().Bool() { return false } return true