Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add View Argument to Window.run() #2468

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 9 additions & 3 deletions arcade/window_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

if TYPE_CHECKING:
from arcade import Window

from arcade.application import View
einarf marked this conversation as resolved.
Show resolved Hide resolved

_window: Window | None = None

Expand Down Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/window/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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"""
Expand All @@ -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
Expand Down
Loading