Skip to content

Commit

Permalink
feat(core/geometry): add validation on Box to check for valid margin/…
Browse files Browse the repository at this point in the history
…border/padding
  • Loading branch information
aravinda0 committed Jan 22, 2024
1 parent bb5a8bd commit e1cc05e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/qtile_bonsai/core/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ def __init__(
self.border = border
self.padding = padding

# TODO: Want to invoke this here, but need to redo how tab bars are hidden. The
# logic currently relies on being able to set `height = 0`.
# self.validate()

@property
def principal_rect(self) -> Rect:
return self._principal_rect
Expand Down Expand Up @@ -365,6 +369,13 @@ def content_rect(self) -> Rect:
h=padding_rect.h - (padding.top + padding.bottom),
)

def validate(self):
content_rect = self.content_rect
if content_rect.w <= 0 or content_rect.h <= 0:
raise ValueError(
"Invalid margin/border/padding values. No space left for content"
)

def as_dict(self) -> dict:
return {
"principal_rect": self.principal_rect.as_dict(),
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/core/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,23 @@ def test_padding_rect_includes_content_padding(self, layered_box: Box):

def test_content_rect_includes_content_only(self, layered_box: Box):
assert layered_box.content_rect == Rect(189, 126, 264, 206)

@pytest.mark.parametrize(
("margin", "border", "padding"),
[
(80, 0, 0),
(0, 80, 0),
(0, 0, 80),
(80, 80, 80),
],
)
def test_raises_error_on_invalid_perimeters(self, margin, border, padding):
box = Box(
Rect(100, 100, 100, 100),
margin=margin,
border=border,
padding=padding,
)
err_msg = "Invalid margin/border/padding values. No space left for content"
with pytest.raises(ValueError, match=err_msg):
box.validate()

0 comments on commit e1cc05e

Please sign in to comment.