diff --git a/arcade/application.py b/arcade/application.py index 6a748d7505..6779af70ad 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 f4d1c22718..9bb6302fc1 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) -> 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 ba41d7ee25..4635e51632 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