Skip to content

Commit a3916d8

Browse files
committed
gui/fix division by zero error, when container size ==0
1 parent 12b6472 commit a3916d8

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

arcade/gui/widgets/layout.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import warnings
34
from dataclasses import dataclass
45
from typing import Dict, Iterable, List, Optional, Tuple, TypeVar
56

@@ -900,6 +901,13 @@ def _box_axis_algorithm(constraints: list[_C], container_size: float) -> List[fl
900901
Returns:
901902
List of tuples with the sizes of each element
902903
"""
904+
905+
# if there is no space, return the min value of each constraint
906+
# this will cause a overflow, so we give a warning
907+
if container_size <= 0:
908+
warnings.warn("Container size is 0, cannot calculate sizes for children.")
909+
return [c.min for c in constraints]
910+
903911
# adjust hint value based on min and max values
904912
for c in constraints:
905913
c.hint = max(c.min / container_size, c.hint)

tests/unit/gui/test_layouting_box_main_algorithm.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ def test_shw_smaller_1(window):
3131
assert sizes == [10, 10, 50]
3232

3333

34+
def test_container_size_zero(window):
35+
# GIVEN
36+
entries = [
37+
_C(hint=0.1, min=50, max=None),
38+
_C(hint=0.1, min=50, max=None),
39+
_C(hint=0.5, min=50, max=None),
40+
]
41+
42+
# WHEN
43+
sizes = _box_axis_algorithm(entries, 0)
44+
45+
# THEN
46+
assert sizes == [50, 50, 50]
47+
48+
3449
def test_complex_example_with_max_value():
3550
# GIVEN
3651
entries = [

0 commit comments

Comments
 (0)