Skip to content

Commit

Permalink
\Add optional 'view' argument to run() methods in Window and application
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomarcalst committed Nov 13, 2024
1 parent 575d662 commit bea6b0c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
14 changes: 8 additions & 6 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion arcade/window_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def close_window() -> None:
gc.collect()


def run():
def run(view = None):
"""
Run the main loop.
Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/window/test_window_new.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit bea6b0c

Please sign in to comment.