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

Added on_resize handler on toga.Window #2364

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions android/src/toga_android/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def onGlobalLayout(self):
"""
native_parent = self.window.native_content.getParent()
self.window.resize_content(native_parent.getWidth(), native_parent.getHeight())
self.window.interface.on_resize()


class Window(Container):
Expand Down
1 change: 1 addition & 0 deletions changes/2304.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Toga Windows now supports calling user functions on resize events.
1 change: 1 addition & 0 deletions cocoa/src/toga_cocoa/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def windowDidResize_(self, notification) -> None:
if self.interface.content:
# Set the window to the new size
self.interface.content.refresh()
self.impl.interface.on_resize()

######################################################################
# Toolbar delegate methods
Expand Down
22 changes: 22 additions & 0 deletions core/src/toga/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ def __call__(self, window: Window, **kwargs: Any) -> bool:
...


class OnResizeHandler(Protocol):
def __call__(self, window: Window, **kwargs: Any) -> None:
"""A handler to invoke when a window resizes.

:param window: The window instance that resizes.
:param kwargs: Ensures compatibility with additional arguments introduced in
future ver
"""
...


T = TypeVar("T")


Expand Down Expand Up @@ -126,6 +137,7 @@ def __init__(
resizable: bool = True,
closable: bool = True,
minimizable: bool = True,
on_resize: OnResizeHandler | None = None,
on_close: OnCloseHandler | None = None,
resizeable=None, # DEPRECATED
closeable=None, # DEPRECATED
Expand Down Expand Up @@ -197,6 +209,7 @@ def __init__(
self._toolbar = CommandSet(on_change=self._impl.create_toolbar, app=self._app)

self.on_close = on_close
self.on_resize = on_resize

def __lt__(self, other) -> bool:
return self.id < other.id
Expand Down Expand Up @@ -468,6 +481,15 @@ def as_image(self, format: type[ImageT] = Image) -> ImageT:
# Window events
######################################################################

@property
def on_resize(self) -> OnResizeHandler:
"""The handler to invoke when the window resizes."""
return self._on_resize

@on_resize.setter
def on_resize(self, handler):
self._on_resize = wrapped_handler(self, handler)

@property
def on_close(self) -> OnCloseHandler:
"""The handler to invoke if the user attempts to close the window."""
Expand Down
13 changes: 13 additions & 0 deletions core/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,19 @@ def test_as_image(window):
assert image.size == (318, 346)


def test_on_resize(window):
assert window.on_resize._raw is None

on_resize_handler = Mock()
window.on_resize = on_resize_handler

assert window.on_resize._raw == on_resize_handler

window._impl.simulate_on_resize()

on_resize_handler.assert_called_once_with(window)


def test_screen(window, app):
"""A window can be moved to a different screen."""
# Cannot actually change window.screen, so just check
Expand Down
3 changes: 3 additions & 0 deletions dummy/src/toga_dummy/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ def simulate_close(self):
def get_current_screen(self):
# `window.screen` will return `Secondary Screen`
return ScreenImpl(native=("Secondary Screen", (-1366, -768), (1366, 768)))

def simulate_on_resize(self):
self.interface.on_resize()
4 changes: 4 additions & 0 deletions gtk/src/toga_gtk/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, interface, title, position, size):
self.create()
self.native._impl = self

self.native.connect("configure-event", self.gtk_configure_event)
self.native.connect("delete-event", self.gtk_delete_event)

self.native.set_default_size(size[0], size[1])
Expand Down Expand Up @@ -57,6 +58,9 @@ def create(self):
# Native event handlers
######################################################################

def gtk_configure_event(self, widget, data):
self.interface.on_resize()

def gtk_delete_event(self, widget, data):
if self._is_closing:
should_close = True
Expand Down
2 changes: 2 additions & 0 deletions iOS/src/toga_iOS/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def application_didChangeStatusBarOrientation_(
"""This callback is invoked when rotating the device from landscape to portrait
and vice versa."""
App.app.interface.main_window.content.refresh()
for window in App.app.interface.windows:
window.on_resize()


class App:
Expand Down
22 changes: 22 additions & 0 deletions testbed/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,28 @@ async def test_screen(second_window, second_window_probe):
)


async def test_on_resize(main_window, main_window_probe):
if toga.platform.current_platform in {"android", "iOS", "web"}:
pytest.xfail("Window.on_resize is non functional on current platform.")
main_window_on_resize_handler = Mock()
main_window.on_resize = main_window_on_resize_handler

main_window.show()
await main_window_probe.wait_for_window("Main window has been shown")

initial_size = main_window.size

main_window.size = (200, 150)
await main_window_probe.wait_for_window("Main window has been resized")
assert main_window.size == (200, 150)

main_window_on_resize_handler.assert_called_with(main_window)

main_window.size = initial_size
await main_window_probe.wait_for_window("Main window has been resized")
assert main_window.size == initial_size


async def test_as_image(main_window, main_window_probe):
"""The window can be captured as a screenshot"""

Expand Down
1 change: 1 addition & 0 deletions textual/src/toga_textual/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def on_mount(self) -> None:

def on_resize(self, event) -> None:
self.interface.content.refresh()
self.interface.on_resize()


class Window:
Expand Down
1 change: 1 addition & 0 deletions winforms/src/toga_winforms/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self, interface, title, position, size):

def winforms_Resize(self, sender, event):
self.resize_content()
self.interface.on_resize()

def winforms_FormClosing(self, sender, event):
# If the app is exiting, or a manual close has been requested, don't get
Expand Down
Loading