Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Oct 15, 2024
1 parent 7d19522 commit 798a9d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 48 deletions.
12 changes: 5 additions & 7 deletions wgpu/gui/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from ._gui_utils import log_exception

# todo: idea: a global loop proxy object that defers to any of the other loops
# would e.g. allow using glfw with qt together. Probably to weird a use-case for the added complexity.
# Note: technically, we could have a global loop proxy object that defers to any of the other loops.
# That would e.g. allow using glfw with qt together. Probably to too weird use-case for the added complexity.


class WgpuLoop:
Expand All @@ -21,7 +21,7 @@ def call_later(self, delay, callback, *args):
raise NotImplementedError()

def poll(self):
"""Poll the underlying GUI toolkit for events.
"""Poll the underlying GUI toolkit for window events.
Some event loops (e.g. asyncio) are just that and dont have a GUI to update.
"""
Expand Down Expand Up @@ -50,9 +50,6 @@ class AnimationScheduler:
# scheduler._event_emitter.submit_and_dispatch(event)


# todo: statistics on time spent doing what


class Scheduler:
"""Helper class to schedule event processing and drawing."""

Expand Down Expand Up @@ -218,8 +215,9 @@ def pseudo_tick():

def event_tick(self):
"""A lightweight tick that processes evets and animations."""

# Get events from the GUI into our event mechanism.
self._canvas._get_loop().poll() # todo: maybe self._process_gui_events()?
self._canvas._get_loop().poll()

# Flush our events, so downstream code can update stuff.
# Maybe that downstream code request a new draw.
Expand Down
4 changes: 4 additions & 0 deletions wgpu/gui/asyncio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"""Implements an asyncio event loop."""

# This is used for GUI backends that don't have an event loop by themselves, lik glfw.
# Would be nice to also allow a loop based on e.g. Trio. But we can likely fit that in
# when the time comes.

import asyncio

from .base import WgpuLoop
Expand Down
27 changes: 6 additions & 21 deletions wgpu/gui/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ def loop(self):
def request_draw(self, draw_function=None):
"""Schedule a new draw event.
This function does not perform a draw directly, but schedules
a draw at a suitable moment in time. At that time the
draw function is called, and the resulting rendered image
is presented to screen.
This function does not perform a draw directly, but schedules a draw at
a suitable moment in time. At that time the draw function is called, and
the resulting rendered image is presented to screen.
Only affects drawing with schedule-mode 'ondemand'.
Arguments:
draw_function (callable or None): The function to set as the new draw
Expand All @@ -169,28 +170,12 @@ def request_draw(self, draw_function=None):
self._scheduler.set_draw_func(draw_function)
self._scheduler.request_draw()

# We don't call self._request_draw() directly but let the scheduler do that based on the policy
# self._scheduler.request_draw()
# todo: maybe have set_draw_function() separately
# todo: maybe requesting a new draw can be done by setting a field in an event?
# todo: can we invoke the draw function via a draw event?

# We can assume that this function is called when we flush events.
# So we can also maybe replace this by letting downstream code set a flag on the event object.
# In any case, we only really have to do something in ondemand mode; in other modes we draw regardless.
# todo: can just make the draw_function a handler for the draw event?

def force_draw(self):
self._force_draw()

def _process_input(self):
"""This should process all GUI events.
In some GUI systems, like Qt, events are already processed because the
Qt event loop is running, so this can be a no-op. In other cases, like
glfw, this hook allows glfw to do a tick.
"""
raise NotImplementedError()

def _draw_frame_and_present(self):
"""Draw the frame and present the result.
Expand Down
1 change: 0 additions & 1 deletion wgpu/gui/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ def run(self):


loop = GlfwAsyncioWgpuLoop()
# todo: loop or app?


def poll_glfw_briefly(poll_time=0.1):
Expand Down
21 changes: 11 additions & 10 deletions wgpu/gui/qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,13 +549,7 @@ def _app(self):
return QtWidgets.QApplication.instance() or QtWidgets.QApplication([])

def poll(self):
# todo: can check if loop is running ....
pass
# Don't actually do anything, because we assume that when using a Qt canvas, the qt event loop is running.
# If necessary, we can have a different (kind of) call for users and for the scheduler.
# app = self._app
# app.sendPostedEvents()
# app.process_events()
self._app.process_events()

def call_later(self, delay, callback, *args):
func = callback
Expand All @@ -566,12 +560,19 @@ def call_later(self, delay, callback, *args):
QtCore.QTimer.singleShot(int(delay * 1000), func)

def run(self):
# Note: we could detect if asyncio is running (interactive session) and wheter
# we can use QtAsyncio. However, there's no point because that's up for the
# end-user to decide.

# Note: its possible, and perfectly ok, if the application is started from user
# code. This works fine because the application object is global. This means
# though, that we cannot assume anything based on whether this method is called
# or not.

if already_had_app_on_import:
return # Likely in an interactive session or larger application that will start the Qt app.
app = self._app

# todo: we could detect if asyncio is running (interactive session) and wheter we can use QtAsyncio.
# But let's wait how things look with new scheduler etc.
app = self._app
app.exec() if hasattr(app, "exec") else app.exec_()

def stop(self):
Expand Down
9 changes: 0 additions & 9 deletions wgpu/gui/trio.py

This file was deleted.

0 comments on commit 798a9d1

Please sign in to comment.