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

Changed to updated Pack.layout() signature (without node) #244

Merged
merged 19 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from 6 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 changes/244.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Node.reresh() has been updated to use Toga 0.5.0's newer signature for style.layout, omitting the node parameter. It is still backwards-compatible with Toga 0.4.8.
15 changes: 14 additions & 1 deletion src/travertino/node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from inspect import signature


class Node:
def __init__(self, style, applicator=None, children=None):
# Parent needs to be primed before style is (potentially) applied with
Expand Down Expand Up @@ -169,7 +172,17 @@ def refresh(self, viewport):
if self._root:
self._root.refresh(viewport)
else:
self.style.layout(self, viewport)
######################################################################
# 2024-12: Backwards compatibility for Toga <= 0.4.8
######################################################################
params = signature(self.style.layout).parameters
if len(params) == 1 or "_deprecated_usage" in params:
self.style.layout(viewport)
else:
self.style.layout(self, viewport)
######################################################################
# End backwards compatibility
######################################################################
if self.applicator:
self.applicator.set_bounds()

Expand Down
30 changes: 22 additions & 8 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,23 @@ class IntrinsicSize(BaseIntrinsicSize):
class Box(BaseBox):
pass

def layout(self, root, viewport):
def layout(self, viewport):
# A simple layout scheme that allocates twice the viewport size.
root.layout.content_width = viewport.width * 2
root.layout.content_height = viewport.height * 2
self._applicator.node.layout.content_width = viewport.width * 2
self._applicator.node.layout.content_height = viewport.height * 2


class OldStyle(Style):
# Uses two-argument layout(), as in Toga <= 0.4.8
def layout(self, node, viewport):
super().layout(viewport)


class OldStyleDifferentName(Style):
# Just to be on the paranoid side, also test with a different parameter name, like
# this test used to have.
def layout(self, root, viewport):
super().layout(viewport)


@prep_style_class
Expand All @@ -38,10 +51,10 @@ class IntrinsicSize(BaseIntrinsicSize):
class Box(BaseBox):
pass

def layout(self, root, viewport):
def layout(self, viewport):
# A simple layout scheme that allocates twice the viewport size.
root.layout.content_width = viewport.width * 2
root.layout.content_height = viewport.height * 2
self._applicator.node.layout.content_width = viewport.width * 2
self._applicator.node.layout.content_height = viewport.height * 2


class AttributeTestStyle(BaseStyle):
Expand Down Expand Up @@ -130,7 +143,8 @@ def test_create_node():
assert child3.root == new_node


def test_refresh():
@pytest.mark.parametrize("StyleClass", [Style, OldStyle, OldStyleDifferentName])
def test_refresh(StyleClass):
"""The layout can be refreshed, and the applicator invoked"""

# Define an applicator that tracks the node being rendered and its size
Expand All @@ -155,7 +169,7 @@ def __init__(self, style, children=None):
)

# Define a simple 2 level tree of nodes.
style = Style()
style = StyleClass()
child1 = TestNode(style=style)
child2 = TestNode(style=style)
child3 = TestNode(style=style)
Expand Down
Loading