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 13 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.
5 changes: 5 additions & 0 deletions src/travertino/declaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ def apply(self, property, value):
"Style must define an apply method"
) # pragma: no cover

def layout(self, viewport):
raise NotImplementedError(
"Style must define a layout method"
) # pragma: no cover

######################################################################
# Provide a dict-like interface
######################################################################
Expand Down
39 changes: 38 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,10 +172,44 @@ def refresh(self, viewport):
if self._root:
self._root.refresh(viewport)
else:
self.style.layout(self, viewport)
if self.applicator:
######################################################################
# 2024-12: Backwards compatibility for Toga <= 0.4.8
######################################################################
# (See below)
if self._old_layout_args():
self.style.layout(self, viewport)
else:
self.style.layout(viewport)
######################################################################
# End backwards compatibility
######################################################################

self.applicator.set_bounds()

######################################################################
# 2024-12: Backwards compatibility for Toga <= 0.4.8
######################################################################

# Accommodate the earlier signature of layout(), which included the node as a
# parameter. This needs to be called on the *instance* -- to have access to the
# style -- but it needs to be cached on the *class*, so all instances have access
# to it.

def _old_layout_args(self):
try:
return self.__class__._cached_old_layout_args
except AttributeError:
self.__class__._cached_old_layout_args = (
"node" in signature(self.style.layout).parameters
)

return self.__class__._cached_old_layout_args

######################################################################
# End backwards compatibility
######################################################################

def _set_root(self, node, root):
# Propagate a root node change through a tree.
node._root = root
Expand Down
56 changes: 48 additions & 8 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ 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)


@prep_style_class
Expand All @@ -38,10 +44,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 +136,41 @@ def test_create_node():
assert child3.root == new_node


def test_refresh():
@pytest.mark.parametrize(
"StyleClass, cached_value",
[
(Style, False),
(OldStyle, True),
],
)
def test_layout_signature_check(StyleClass, cached_value):
"""After the first call to refresh(), node class should cache args version."""

class Applicator:
def set_bounds(self):
pass

class TestNode(Node):
# So we don't change the actual Node class
pass

# Before refresh() is called, the cached value isn't set.
assert not hasattr(TestNode, "_cached_old_layout_args")

# Check again, just to be sure nothing has changed from creating an instance.
node = TestNode(style=StyleClass(), applicator=Applicator())
assert not hasattr(TestNode, "_cached_old_layout_args")

# Refresh for the first time.
node.refresh(Viewport(width=10, height=20))

# After refreshing, which signature to use for layout() should be cached on the
# class, and thus accessible to both instances.
assert TestNode._cached_old_layout_args == cached_value


@pytest.mark.parametrize("StyleClass", [Style, OldStyle])
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 +195,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