Skip to content

Commit a6994b8

Browse files
committed
fix: be more tolerant when checking for alignment errors
1 parent 59bb9dd commit a6994b8

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

glooey/drawing/alignment.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ def fill(child_rect, parent_rect):
1515
@alignment
1616
def fill_horz(child_rect, parent_rect):
1717
child_rect.width = parent_rect.width
18-
child_rect.center = parent_rect.center
18+
child_rect.center_left = parent_rect.center_left
1919

2020
@alignment
2121
def fill_vert(child_rect, parent_rect):
2222
child_rect.height = parent_rect.height
23-
child_rect.center = parent_rect.center
23+
child_rect.bottom_center = parent_rect.bottom_center
2424

2525
@alignment
2626
def fill_top(child_rect, parent_rect):
@@ -105,7 +105,8 @@ def align(key_or_function, child_rect, parent_rect, outside_ok=False):
105105
if __debug__:
106106
if parent_rect != parent_copy:
107107
raise RuntimeError(f"{repr(key_or_function)} changed the parent rectangle (second argument) from {parent_copy} to {parent_rect}. Alignment functions should only modify the child rectangle (first argument).")
108-
if not outside_ok and not child_rect.inside(parent_rect):
108+
# Grow the parent rectangle by 1 px to be resilient to rounding errors.
109+
if not outside_ok and not child_rect.inside(parent_rect.get_grown(1)):
109110
raise RuntimeError(f"{repr(key_or_function)} placed the child rectangle outside the parent rectangle. This most likely indicates a bug in '{alignment_func.__qualname__}()'.\nchild: {child_rect}\nparent: {parent_rect}")
110111

111112
def fixed_size_align(key_or_function, child_rect, parent_rect, outside_ok=False):

tests/drawing/test_alignment.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def change_parent(child_rect, parent_rect):
1818
glooey.drawing.align(change_parent, child, parent)
1919

2020
def test_child_outside_parent():
21-
child = Rect.from_square(5)
21+
child = Rect.from_square(4.5)
2222
parent = Rect.from_square(6)
2323

2424
def move_1px_right(child_rect, parent_rect):
@@ -27,7 +27,11 @@ def move_1px_right(child_rect, parent_rect):
2727
# This should be fine the first time...
2828
glooey.drawing.align(move_1px_right, child, parent)
2929

30-
# ...but out-of-bounds the second time.
30+
# ...and also fine the second time, because the child is allowed to exceed
31+
# its parent by 1 px to account for rounding errors...
32+
glooey.drawing.align(move_1px_right, child, parent)
33+
34+
# ...but out-of-bounds the third time.
3135
with pytest.raises(RuntimeError, match='move_1px_right'):
3236
glooey.drawing.align(move_1px_right, child, parent)
3337

0 commit comments

Comments
 (0)