Skip to content

Commit

Permalink
More cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Oct 15, 2024
1 parent 798a9d1 commit 9ee8cf1
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ To render to the screen you can use a variety of GUI toolkits:

```py
# The auto backend selects either the glfw, qt or jupyter backend
from wgpu.gui.auto import WgpuCanvas, run, call_later
from wgpu.gui.auto import WgpuCanvas, loop

# Visualizations can be embedded as a widget in a Qt application.
# Import PySide6, PyQt6, PySide2 or PyQt5 before running the line below.
Expand Down
23 changes: 8 additions & 15 deletions docs/gui.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ The Canvas base classes

~WgpuCanvasInterface
~WgpuCanvasBase
~WgpuAutoGui


For each supported GUI toolkit there is a module that implements a ``WgpuCanvas`` class,
which inherits from :class:`WgpuCanvasBase`, providing a common API.
The GLFW, Qt, and Jupyter backends also inherit from :class:`WgpuAutoGui` to include
support for events (interactivity). In the next sections we demonstrates the different
canvas classes that you can use.


Events
------

To implement interaction with a ``WgpuCanvas``, use the :func:`WgpuCanvasBase.add_event_handler()` method.
Events come in the following flavours:

.. autoclass:: WgpuEventType
:members:

Expand All @@ -46,22 +45,16 @@ across different machines and environments. Using ``wgpu.gui.auto`` selects a
suitable backend depending on the environment and more. See
:ref:`interactive_use` for details.

To implement interaction, the ``canvas`` has a :func:`WgpuAutoGui.handle_event()` method
that can be overloaded. Alternatively you can use it's :func:`WgpuAutoGui.add_event_handler()`
method. See the `event spec <https://jupyter-rfb.readthedocs.io/en/stable/events.html>`_
for details about the event objects.

Also see the `triangle auto <https://github.com/pygfx/wgpu-py/blob/main/examples/triangle_auto.py>`_
and `cube <https://github.com/pygfx/wgpu-py/blob/main/examples/cube.py>`_ examples that demonstrate the auto gui.
Also see the e.g. the `gui_auto.py <https://github.com/pygfx/wgpu-py/blob/main/examples/gui_auto.py>`_ example.

.. code-block:: py
from wgpu.gui.auto import WgpuCanvas, run, call_later
from wgpu.gui.auto import WgpuCanvas, loop
canvas = WgpuCanvas(title="Example")
canvas.request_draw(your_draw_function)
run()
loop.run()
Support for GLFW
Expand All @@ -73,12 +66,12 @@ but you can replace ``from wgpu.gui.auto`` with ``from wgpu.gui.glfw`` to force

.. code-block:: py
from wgpu.gui.glfw import WgpuCanvas, run, call_later
from wgpu.gui.glfw import WgpuCanvas, loop
canvas = WgpuCanvas(title="Example")
canvas.request_draw(your_draw_function)
run()
loop()
Support for Qt
Expand Down
2 changes: 1 addition & 1 deletion docs/guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ GUI toolkits are supported, see the :doc:`gui`. In general, it's easiest to let

.. code-block:: py
from wgpu.gui.auto import WgpuCanvas, run
from wgpu.gui.auto import WgpuCanvas
canvas = WgpuCanvas(title="a wgpu example")
Expand Down
7 changes: 3 additions & 4 deletions tests/test_gui_auto_offscreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,22 @@ def test_canvas_class():

assert WgpuCanvas is WgpuManualOffscreenCanvas
assert issubclass(WgpuCanvas, wgpu.gui.WgpuCanvasBase)
assert issubclass(WgpuCanvas, wgpu.gui.WgpuAutoGui)


def test_event_loop():
"""Check that the event loop handles queued tasks and then returns."""
# Note: if this test fails, it may run forever, so it's a good idea to have a timeout on the CI job or something

from wgpu.gui.auto import run, call_later
from wgpu.gui.auto import loop

ran = False

def check():
nonlocal ran
ran = True

call_later(0, check)
run()
loop.call_later(0, check)
loop.run()

assert ran

Expand Down
10 changes: 5 additions & 5 deletions tests/test_gui_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def test_run_bare_canvas():

# This is (more or less) the equivalent of:
#
# from wgpu.gui.auto import WgpuCanvas, run
# from wgpu.gui.auto import WgpuCanvas, loop
# canvas = WgpuCanvas()
# run()
# loop.run()
#
# Note: run() calls _draw_frame_and_present() in event loop.
# Note: loop.run() calls _draw_frame_and_present() in event loop.

canvas = MyOffscreenCanvas()
canvas._draw_frame_and_present()
Expand Down Expand Up @@ -208,8 +208,8 @@ def draw_frame():
assert canvas.frame_count == 4


def test_autogui_mixin():
c = wgpu.gui.WgpuAutoGui()
def test_canvas_base_events():
c = wgpu.gui.WgpuCanvasBase()

# It's a mixin
assert not isinstance(c, wgpu.gui.WgpuCanvasBase)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_gui_glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ def teardown_module():
pass # Do not glfw.terminate() because other tests may still need glfw


def test_is_autogui():
def test_is_canvas_base():
from wgpu.gui.glfw import WgpuCanvas

assert issubclass(WgpuCanvas, wgpu.gui.WgpuCanvasBase)
assert issubclass(WgpuCanvas, wgpu.gui.WgpuAutoGui)


def test_glfw_canvas_basics():
Expand Down
2 changes: 1 addition & 1 deletion wgpu/gui/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ._gui_utils import logger, QT_MODULE_NAMES, get_imported_qt_lib, asyncio_is_running


# Note that wx is not in here, because it does not (yet) implement base.WgpuAutoGui
# Note that wx is not in here, because it does not (yet) fully implement base.WgpuCanvasBase
WGPU_GUI_BACKEND_NAMES = ["glfw", "qt", "jupyter", "offscreen"]


Expand Down

0 comments on commit 9ee8cf1

Please sign in to comment.