From 44f9a7d8c4442e10e73231ac230937fc32385ed3 Mon Sep 17 00:00:00 2001 From: emmettbicker Date: Wed, 4 Dec 2024 20:13:11 -0500 Subject: [PATCH 1/2] Add View Argument to Window.run() --- arcade/application.py | 9 +++++++-- arcade/window_commands.py | 12 +++++++++--- tests/unit/window/test_window.py | 17 +++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/arcade/application.py b/arcade/application.py index 6a748d750..6779af70a 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -402,15 +402,20 @@ def rect(self) -> Rect: """Return a Rect describing the size of the window.""" return LBWH(0, 0, self.width, self.height) - def run(self) -> None: + def run(self, view: View | None = None) -> None: """ - Run the event loop. + Run the event loop. Optionally start with a specified view. After the window has been set up, and the event hooks are in place, this is usually one of the last commands on the main program. This is a blocking function starting pyglet's event loop meaning it will start to dispatch events such as ``on_draw`` and ``on_update``. + + Args: + view: The view to display when starting the run. Defaults to None. """ + if view is not None: + self.show_view(view) arcade.run() def close(self) -> None: diff --git a/arcade/window_commands.py b/arcade/window_commands.py index f4d1c2271..badfbdc11 100644 --- a/arcade/window_commands.py +++ b/arcade/window_commands.py @@ -16,7 +16,7 @@ if TYPE_CHECKING: from arcade import Window - + from arcade.application import View _window: Window | None = None @@ -97,17 +97,23 @@ def close_window() -> None: gc.collect() -def run(): +def run(view: View | None = None): """ - Run the main loop. + Run the main loop. Optionally start with a specified view. After the window has been set up, and the event hooks are in place, this is usually one of the last commands on the main program. This is a blocking function starting pyglet's event loop meaning it will start to dispatch events such as ``on_draw`` and ``on_update``. + + Args: + view: The view to display when starting the run. Defaults to None. """ window = get_window() + if view is not None: + window.show_view(view) + # Used in some unit test if os.environ.get("ARCADE_TEST"): window.on_update(1.0 / 60.0) diff --git a/tests/unit/window/test_window.py b/tests/unit/window/test_window.py index ba41d7ee2..4635e5163 100644 --- a/tests/unit/window/test_window.py +++ b/tests/unit/window/test_window.py @@ -41,7 +41,7 @@ def test_window(window: arcade.Window): assert v[3] == height factor = window.get_pixel_ratio() - assert isinstance(factor, float) + assert isinstance(factor, float) assert factor > 0 def f(): @@ -52,6 +52,19 @@ def f(): arcade.unschedule(f) window.test() +def test_window_with_view_arg(window: arcade.Window): + class TestView(arcade.View): + def __init__(self): + super().__init__() + self.on_show_called = False + + def on_show_view(self): + self.on_show_called = True + v = TestView() + window.run(view=v) + + assert v.on_show_called + assert window.current_view is v def test_start_finish_render(window): """Test start and finish render""" @@ -68,7 +81,7 @@ def test_start_finish_render(window): # Only allowed to call start_render once with pytest.raises(RuntimeError): arcade.start_render() - + arcade.finish_render() # Make sure we rendered something to the screen From c72a6114d5b9e0048a97977e6582f6c438af1c9b Mon Sep 17 00:00:00 2001 From: emmettbicker Date: Wed, 4 Dec 2024 21:01:09 -0500 Subject: [PATCH 2/2] Revision --- arcade/window_commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arcade/window_commands.py b/arcade/window_commands.py index badfbdc11..9bb6302fc 100644 --- a/arcade/window_commands.py +++ b/arcade/window_commands.py @@ -97,7 +97,7 @@ def close_window() -> None: gc.collect() -def run(view: View | None = None): +def run(view: View | None = None) -> None: """ Run the main loop. Optionally start with a specified view. @@ -107,7 +107,7 @@ def run(view: View | None = None): it will start to dispatch events such as ``on_draw`` and ``on_update``. Args: - view: The view to display when starting the run. Defaults to None. + view: The view to display when starting the run. Defaults to None. """ window = get_window()