Skip to content

Commit

Permalink
Refactor initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Nov 7, 2024
1 parent 6107796 commit 9d9400c
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 600 deletions.
2 changes: 1 addition & 1 deletion examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from rendercanvas.auto import RenderCanvas, loop

from cube import setup_drawing_sync
from rendercanvas.utils.cube import setup_drawing_sync


canvas = RenderCanvas(
Expand Down
2 changes: 1 addition & 1 deletion examples/qt_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self):
splitter = QtWidgets.QSplitter()

self.button = QtWidgets.QPushButton("Hello world", self)
self.canvas = QRenderWidget(splitter)
self.canvas = QRenderWidget(splitter, update_mode="continuous")
self.output = QtWidgets.QTextEdit(splitter)

self.button.clicked.connect(self.whenButtonClicked)
Expand Down
2 changes: 1 addition & 1 deletion examples/qt_app_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self):

# todo: use update_mode = 'continuous' when that feature has arrived
self.button = QtWidgets.QPushButton("Hello world", self)
self.canvas = QRenderWidget(splitter)
self.canvas = QRenderWidget(splitter, update_mode="continuous")
self.output = QtWidgets.QTextEdit(splitter)

# self.button.clicked.connect(self.whenButtonClicked) # see above :(
Expand Down
16 changes: 8 additions & 8 deletions rendercanvas/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,14 @@ class Scheduler:
# Note that any extra draws, e.g. via force_draw() or due to window resizes,
# don't affect the scheduling loop; they are just extra draws.

def __init__(self, canvas, loop, *, mode="ondemand", min_fps=1, max_fps=30):
def __init__(self, canvas, events, loop, *, mode="ondemand", min_fps=1, max_fps=30):
assert loop is not None

# We don't keep a ref to the canvas to help gc. This scheduler object can be
# referenced via a callback in an event loop, but it won't prevent the canvas
# from being deleted!
self._canvas_ref = weakref.ref(canvas)
self._events = canvas._events
self._events = events
# ... = canvas.get_context() -> No, context creation should be lazy!

# Scheduling variables
Expand All @@ -329,8 +331,6 @@ def __init__(self, canvas, loop, *, mode="ondemand", min_fps=1, max_fps=30):
# Keep track of fps
self._draw_stats = 0, time.perf_counter()

assert loop is not None

# Initialise the timer that runs our scheduling loop.
# Note that the backend may do a first draw earlier, starting the loop, and that's fine.
self._last_tick_time = -0.1
Expand Down Expand Up @@ -390,22 +390,22 @@ def _tick(self):

if self._mode == "fastest":
# fastest: draw continuously as fast as possible, ignoring fps settings.
canvas._request_draw()
canvas._rc_request_draw()

elif self._mode == "continuous":
# continuous: draw continuously, aiming for a steady max framerate.
canvas._request_draw()
canvas._rc_request_draw()

elif self._mode == "ondemand":
# ondemand: draw when needed (detected by calls to request_draw).
# Aim for max_fps when drawing is needed, otherwise min_fps.
if self._draw_requested:
canvas._request_draw()
canvas._rc_request_draw()
elif (
self._min_fps > 0
and time.perf_counter() - self._last_draw_time > 1 / self._min_fps
):
canvas._request_draw()
canvas._rc_request_draw()
else:
self._schedule_next_tick()

Expand Down
Loading

0 comments on commit 9d9400c

Please sign in to comment.