From bea6b0c1965605b593c52315651167029b936d89 Mon Sep 17 00:00:00 2001 From: joaomarcalst Date: Thu, 14 Nov 2024 00:54:12 +0100 Subject: [PATCH] \Add optional 'view' argument to run() methods in Window and application --- arcade/application.py | 14 ++++++---- arcade/window_commands.py | 6 +++- tests/unit/window/test_window_new.py | 41 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 tests/unit/window/test_window_new.py diff --git a/arcade/application.py b/arcade/application.py index 6a748d750..a7c1ac8a3 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -402,15 +402,17 @@ 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: + from typing import Optional + + def run(self, view: Optional[View] = 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 (Optional[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..8425462de 100644 --- a/arcade/window_commands.py +++ b/arcade/window_commands.py @@ -97,7 +97,7 @@ def close_window() -> None: gc.collect() -def run(): +def run(view = None): """ Run the main loop. @@ -108,6 +108,10 @@ def run(): """ window = get_window() + # Show the specific view if provided + 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_new.py b/tests/unit/window/test_window_new.py new file mode 100644 index 000000000..3c9f39ff0 --- /dev/null +++ b/tests/unit/window/test_window_new.py @@ -0,0 +1,41 @@ +import pytest +import arcade + + +class TestView(arcade.View): + def __init__(self): + super().__init__() + self.on_show_called = False + + def on_show_view(self): + self.on_show_called = True + + +def test_window_with_view(): + # Cria uma janela e uma view para o teste + window = arcade.Window(width=800, height=600, title="Window Test with View") + view = TestView() + + # Executa o método run com a view + window.run(view=view) + + # Verifica se a view foi mostrada automaticamente + assert window.current_view == view + assert view.on_show_called is True + + # Fecha a janela ao finalizar o teste + window.close() + + +def test_run_without_view(): + # Cria uma janela e chama run sem uma view + window = arcade.Window(width=800, height=600, title="Window Test without View") + + # Executa o método run sem uma view + window.run() + + # Verifica que não há uma view ativa + assert window.current_view is None + + # Fecha a janela ao finalizar o teste + window.close() \ No newline at end of file