Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Nov 8, 2024
1 parent ffabdbb commit b11190d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
30 changes: 15 additions & 15 deletions rendercanvas/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, *args, **kwargs):
# If this is a wrapper, it should pass the canvas kwargs to the subwidget.
if isinstance(self, WrapperRenderCanvas):
self._rc_init(**canvas_kwargs)
self.__events = self._subwidget.__events
self._events = self._subwidget._events
return

# The vsync is not-so-elegantly strored on the canvas, and picked up by wgou's canvas contex.
Expand All @@ -78,13 +78,13 @@ def __init__(self, *args, **kwargs):
}

# Events and scheduler
self.__events = EventEmitter()
self._events = EventEmitter()
self.__scheduler = None
loop = self._rc_get_loop()
if loop is not None:
self.__scheduler = Scheduler(
self,
self.__events,
self._events,
self._rc_get_loop(),
min_fps=canvas_kwargs["min_fps"],
max_fps=canvas_kwargs["max_fps"],
Expand Down Expand Up @@ -184,16 +184,16 @@ def present_image(self, image, **kwargs):
# %% Events

def add_event_handler(self, *args, **kwargs):
return self.__events.add_handler(*args, **kwargs)
return self._events.add_handler(*args, **kwargs)

def remove_event_handler(self, *args, **kwargs):
return self.__events.remove_handler(*args, **kwargs)
return self._events.remove_handler(*args, **kwargs)

def submit_event(self, event):
# Not strictly necessary for normal use-cases, but this allows
# the ._event to be an implementation detail to subclasses, and it
# allows users to e.g. emulate events in tests.
return self.__events.submit(event)
return self._events.submit(event)

add_event_handler.__doc__ = EventEmitter.add_handler.__doc__
remove_event_handler.__doc__ = EventEmitter.remove_handler.__doc__
Expand All @@ -215,7 +215,7 @@ def _process_events(self):

# Flush our events, so downstream code can update stuff.
# Maybe that downstream code request a new draw.
self.__events.flush()
self._events.flush()

# TODO: implement later (this is a start but is not tested)
# Schedule animation events until the lag is gone
Expand All @@ -224,16 +224,16 @@ def _process_events(self):
# animation_iters = 0
# while self._animation_time > time.perf_counter() - step:
# self._animation_time += step
# self.__events.submit({"event_type": "animate", "step": step, "catch_up": 0})
# self._events.submit({"event_type": "animate", "step": step, "catch_up": 0})
# # Do the animations. This costs time.
# self.__events.flush()
# self._events.flush()
# # Abort when we cannot keep up
# # todo: test this
# animation_iters += 1
# if animation_iters > 20:
# n = (time.perf_counter() - self._animation_time) // step
# self._animation_time += step * n
# self.__events.submit(
# self._events.submit(
# {"event_type": "animate", "step": step * n, "catch_up": n}
# )

Expand Down Expand Up @@ -301,7 +301,7 @@ def _draw_frame_and_present(self):
# Process special events
# Note that we must not process normal events here, since these can do stuff
# with the canvas (resize/close/etc) and most GUI systems don't like that.
self.__events.emit({"event_type": "before_draw"})
self._events.emit({"event_type": "before_draw"})

# Notify the scheduler
if self.__scheduler is not None:
Expand Down Expand Up @@ -366,7 +366,7 @@ def set_title(self, title):

# %% Methods for the subclass to implement

def _rc_init(self, *, present_method):
def _rc_init(self, **canvas_kwargs):
"""Method to initialize the canvas.
This method is called near the end of the initialization
Expand Down Expand Up @@ -441,13 +441,13 @@ def _rc_close(self):
Note that ``BaseRenderCanvas`` implements the ``close()`` method, which
is a rather common name; it may be necessary to re-implement that too.
"""
raise NotImplementedError()
pass

def _rc_is_closed(self):
"""Get whether the canvas is closed."""
raise NotImplementedError()
return False

def _rc_set_title(self):
def _rc_set_title(self, title):
"""Set the canvas title. May be ignored when it makes no sense.
The default implementation does nothing.
Expand Down
18 changes: 9 additions & 9 deletions tests/test_scheduling.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ def __init__(self, *args, **kwargs):
self.draw_count = 0
self.events_count = 0

def _get_loop(self):
def _rc_get_loop(self):
return self._loop

def _rc_close(self):
self._closed = True

def _rc_is_closed(self):
return self._closed

def _process_events(self):
super()._process_events()
self.events_count += 1
Expand All @@ -57,22 +63,16 @@ def _draw_frame_and_present(self):
super()._draw_frame_and_present()
self.draw_count += 1

def _request_draw(self):
def _rc_request_draw(self):
self._gui_draw_requested = True

def draw_if_necessary(self):
if self._gui_draw_requested:
self._gui_draw_requested = False
self._draw_frame_and_present()

def close(self):
self._closed = True

def is_closed(self):
return self._closed

def active_sleep(self, delay):
loop = self._get_loop()
loop = self._rc_get_loop()
etime = time.perf_counter() + delay
while time.perf_counter() < etime:
time.sleep(0.001)
Expand Down

0 comments on commit b11190d

Please sign in to comment.