Handle quitting during scene more gracefully

This commit is contained in:
Grant Sanderson 2022-04-27 11:19:44 -07:00
parent 52259af5df
commit e83ad785ca

View file

@ -100,6 +100,7 @@ class Scene(object):
self.mouse_drag_point = Point() self.mouse_drag_point = Point()
self.hold_on_wait = self.presenter_mode self.hold_on_wait = self.presenter_mode
self.inside_embed = False self.inside_embed = False
self.quit_interaction = False
# Much nicer to work with deterministic scenes # Much nicer to work with deterministic scenes
if self.random_seed is not None: if self.random_seed is not None:
@ -117,8 +118,8 @@ class Scene(object):
self.setup() self.setup()
try: try:
self.construct() self.construct()
except EndSceneEarlyException: except (EndSceneEarlyException, KeyboardInterrupt):
pass self.linger_after_completion = False
self.tear_down() self.tear_down()
def setup(self) -> None: def setup(self) -> None:
@ -137,8 +138,12 @@ class Scene(object):
def tear_down(self) -> None: def tear_down(self) -> None:
self.stop_skipping() self.stop_skipping()
self.file_writer.finish() self.file_writer.finish()
if self.window and self.linger_after_completion: if self.window:
if self.linger_after_completion:
self.interact() self.interact()
else:
self.window.destroy()
self.window = None
def interact(self) -> None: def interact(self) -> None:
# If there is a window, enter a loop # If there is a window, enter a loop
@ -149,12 +154,12 @@ class Scene(object):
" and the mouse to interact with the scene. Just press `command + q` or `esc`" " and the mouse to interact with the scene. Just press `command + q` or `esc`"
" if you want to quit." " if you want to quit."
) )
self.quit_interaction = False
self.refresh_static_mobjects() self.refresh_static_mobjects()
while not (self.window.is_closing or self.quit_interaction): try:
while True:
self.update_frame(1 / self.camera.frame_rate) self.update_frame(1 / self.camera.frame_rate)
if self.window.is_closing: except (EndSceneEarlyException, KeyboardInterrupt):
self.window.destroy() return
def embed(self, close_scene_on_exit: bool = True) -> None: def embed(self, close_scene_on_exit: bool = True) -> None:
if not self.preview: if not self.preview:
@ -251,6 +256,9 @@ class Scene(object):
if self.skip_animations and not ignore_skipping: if self.skip_animations and not ignore_skipping:
return return
if self.window.is_closing or self.quit_interaction:
raise EndSceneEarlyException()
if self.window: if self.window:
self.window.clear() self.window.clear()
self.camera.clear() self.camera.clear()