Skip to content

Commit f7fa4be

Browse files
authored
Better glfw import error message (#221)
* Better glfw import error message * glfw support on rpi
1 parent ddc5f59 commit f7fa4be

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

wgpu/gui/auto.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
for e.g. Qt later. Or we might decide to stick with these two.
66
"""
77

8+
__all__ = ["WgpuCanvas", "run", "call_later"]
9+
10+
import sys
11+
812

913
def is_jupyter():
1014
"""Determine whether the user is executing in a Jupyter Notebook / Lab."""
@@ -24,6 +28,8 @@ def is_jupyter():
2428
try:
2529
from .glfw import WgpuCanvas, run, call_later # noqa
2630
except ImportError as err:
27-
raise ImportError(
28-
str(err) + "\n\n Install glfw using e.g. ``pip install -U glfw``"
29-
)
31+
msg = str(err)
32+
msg += "\n\n Install glfw using e.g. ``pip install -U glfw``."
33+
if sys.platform.startswith("linux"):
34+
msg += "\n You may also need to run the equivalent of ``apt install libglfw3``."
35+
raise ImportError(msg) from None

wgpu/gui/glfw.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@
3333
+ "Did you apt install libglfw3-wayland?"
3434
)
3535

36+
# Some glfw functions are not always available
37+
set_window_content_scale_callback = lambda *args: None # noqa: E731
38+
set_window_maximize_callback = lambda *args: None # noqa: E731
39+
get_window_content_scale = lambda *args: (1, 1) # noqa: E731
40+
41+
if hasattr(glfw, "set_window_content_scale_callback"):
42+
set_window_content_scale_callback = glfw.set_window_content_scale_callback
43+
if hasattr(glfw, "set_window_maximize_callback"):
44+
set_window_maximize_callback = glfw.set_window_maximize_callback
45+
if hasattr(glfw, "get_window_content_scale"):
46+
get_window_content_scale = glfw.get_window_content_scale
47+
3648

3749
all_glfw_canvases = weakref.WeakSet()
3850

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

133145
# Register callbacks. We may get notified too often, but that's
134146
# ok, they'll result in a single draw.
135-
glfw.set_window_content_scale_callback(self._window, self._on_pixelratio_change)
136147
glfw.set_framebuffer_size_callback(self._window, self._on_size_change)
137148
glfw.set_window_close_callback(self._window, self._on_close)
138149
glfw.set_window_refresh_callback(self._window, self._on_window_dirty)
139150
glfw.set_window_focus_callback(self._window, self._on_window_dirty)
140-
glfw.set_window_maximize_callback(self._window, self._on_window_dirty)
151+
set_window_content_scale_callback(self._window, self._on_pixelratio_change)
152+
set_window_maximize_callback(self._window, self._on_window_dirty)
141153

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

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

215227
# The current screen size and physical size, and its ratio
216-
pixel_ratio = glfw.get_window_content_scale(self._window)[0]
228+
pixel_ratio = get_window_content_scale(self._window)[0]
217229
ssize = glfw.get_window_size(self._window)
218230
psize = glfw.get_framebuffer_size(self._window)
219231

0 commit comments

Comments
 (0)