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
12 changes: 9 additions & 3 deletions wgpu/gui/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
for e.g. Qt later. Or we might decide to stick with these two.
"""

__all__ = ["WgpuCanvas", "run", "call_later"]

import sys


def is_jupyter():
"""Determine whether the user is executing in a Jupyter Notebook / Lab."""
Expand All @@ -24,6 +28,8 @@ def is_jupyter():
try:
from .glfw import WgpuCanvas, run, call_later # noqa
except ImportError as err:
raise ImportError(
str(err) + "\n\n Install glfw using e.g. ``pip install -U glfw``"
)
msg = str(err)
msg += "\n\n Install glfw using e.g. ``pip install -U glfw``."
if sys.platform.startswith("linux"):
msg += "\n You may also need to run the equivalent of ``apt install libglfw3``."
raise ImportError(msg) from None
20 changes: 16 additions & 4 deletions wgpu/gui/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
+ "Did you apt install libglfw3-wayland?"
)

# Some glfw functions are not always available
set_window_content_scale_callback = lambda *args: None # noqa: E731
set_window_maximize_callback = lambda *args: None # noqa: E731
get_window_content_scale = lambda *args: (1, 1) # noqa: E731

if hasattr(glfw, "set_window_content_scale_callback"):
set_window_content_scale_callback = glfw.set_window_content_scale_callback
if hasattr(glfw, "set_window_maximize_callback"):
set_window_maximize_callback = glfw.set_window_maximize_callback
if hasattr(glfw, "get_window_content_scale"):
get_window_content_scale = glfw.get_window_content_scale


all_glfw_canvases = weakref.WeakSet()

Expand Down Expand Up @@ -132,12 +144,12 @@ def __init__(self, *, size=None, title=None, **kwargs):

# Register callbacks. We may get notified too often, but that's
# ok, they'll result in a single draw.
glfw.set_window_content_scale_callback(self._window, self._on_pixelratio_change)
glfw.set_framebuffer_size_callback(self._window, self._on_size_change)
glfw.set_window_close_callback(self._window, self._on_close)
glfw.set_window_refresh_callback(self._window, self._on_window_dirty)
glfw.set_window_focus_callback(self._window, self._on_window_dirty)
glfw.set_window_maximize_callback(self._window, self._on_window_dirty)
set_window_content_scale_callback(self._window, self._on_pixelratio_change)
set_window_maximize_callback(self._window, self._on_window_dirty)

# User input
self._key_modifiers = set()
Expand Down Expand Up @@ -189,7 +201,7 @@ def _determine_size(self):
# Because the value of get_window_size is in physical-pixels
# on some systems and in logical-pixels on other, we use the
# framebuffer size and pixel ratio to derive the logical size.
pixel_ratio = glfw.get_window_content_scale(self._window)[0]
pixel_ratio = get_window_content_scale(self._window)[0]
psize = glfw.get_framebuffer_size(self._window)
psize = int(psize[0]), int(psize[1])

Expand All @@ -213,7 +225,7 @@ def _set_logical_size(self, new_logical_size):
# Here, we simply do a quick test so we can compensate.

# The current screen size and physical size, and its ratio
pixel_ratio = glfw.get_window_content_scale(self._window)[0]
pixel_ratio = get_window_content_scale(self._window)[0]
ssize = glfw.get_window_size(self._window)
psize = glfw.get_framebuffer_size(self._window)

Expand Down