Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3345.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Window visibility and focus events in the web backend no longer raise errors when the browser window loses focus
7 changes: 2 additions & 5 deletions web/src/toga_web/libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
except ModuleNotFoundError:
# To ensure the code can be imported, provide a js symbol as a fallback
js = None


try:
# Try to import pyodide from the PyScript namespace
import pyodide
except ModuleNotFoundError:
# To ensure the code can be imported, provide a pyodide symbol as a fallback
pyodide = None

from pyscript.web import document

create_proxy = pyodide.ffi.create_proxy if pyodide else lambda f: f

Expand Down Expand Up @@ -43,7 +40,7 @@ def create_element(
events or methods.
:returns: A newly created DOM element.
"""
element = js.document.createElement(tag)
element = document.createElement(tag)

if id:
element.id = id
Expand Down
23 changes: 14 additions & 9 deletions web/src/toga_web/window.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from toga.command import Group, Separator
from toga.constants import WindowState
from toga.types import Position, Size
from toga_web.libs import create_element, js
from toga_web.libs import create_element, create_proxy, js

from .screens import Screen as ScreenImpl

Expand All @@ -21,9 +21,15 @@ def __init__(self, interface, title, position, size):
app_placeholder = js.document.getElementById("app-placeholder")
app_placeholder.appendChild(self.native)

js.document.body.onfocus = self.dom_on_gain_focus
js.document.body.onblur = self.dom_on_lose_focus
js.document.addEventListener("visibilitychange", self.dom_on_visibility_change)
js.document.body.addEventListener(
"focus", create_proxy(self.dom_on_gain_focus), True
)
js.document.body.addEventListener(
"blur", create_proxy(self.dom_on_lose_focus), True
)
js.document.addEventListener(
"visibilitychange", create_proxy(self.dom_on_visibility_change)
)

self.set_title(title)

Expand All @@ -44,11 +50,10 @@ def dom_on_lose_focus(self, event):
self.interface.on_lose_focus()

def dom_on_visibility_change(self, event):
if hasattr(js.document, "hidden"):
if js.document.visibilityState == "visible":
self.interface.on_show()
else:
self.interface.on_hide()
if js.document.visibilityState == "visible":
self.interface.on_show()
else:
self.interface.on_hide()

######################################################################
# Window properties
Expand Down