Skip to content

Commit

Permalink
UI: add fit_content to UIBoxLayout (#1811)
Browse files Browse the repository at this point in the history
* UI: add fit_content to UIBoxLayout
* use math.inf instead ignoring type
  • Loading branch information
eruvanos committed Jun 5, 2023
1 parent 8c5fde5 commit f28eade
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions arcade/gui/widgets/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ def min_size(child: UIWidget) -> Tuple[float, float]:
base_height = self._padding_top + self._padding_bottom + 2 * self._border_width
self.size_hint_min = base_width + width, base_height + height

def fit_content(self):
"""
Resize the layout to fit the content. This will take the minimal required size into account.
"""
self._update_size_hints()
self.rect = self.rect.resize(self.size_hint_min[0], self.size_hint_min[1])

def do_layout(self):
start_y = self.content_rect.top
start_x = self.content_rect.left
Expand Down
5 changes: 3 additions & 2 deletions arcade/paths.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Classic A-star algorithm for path finding.
"""
import math
from typing import (
cast,
List,
Expand Down Expand Up @@ -167,9 +168,9 @@ def _AStarSearch(start: Point, end: Point, graph: _AStarGraph) -> Optional[List[
break
# Get the vertex in the open list with the lowest F score
current = None
current_fscore = None
current_fscore = math.inf
for pos in sorted(open_vertices):
if current is None or F[pos] < current_fscore: # type: ignore
if current is None or F[pos] < current_fscore:
current_fscore = F[pos]
current = pos

Expand Down
26 changes: 25 additions & 1 deletion tests/unit/gui/test_layouting_boxlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ def test_vertical_resize_child_according_size_hint_full(window):
assert box.size == (200, 200)
assert dummy_1.size == (200, 200)


def test_vertical_resize_child_according_size_hint_half(window):
box = UIBoxLayout(width=200, height=200, vertical=True)
dummy_1 = box.add(UIDummy(width=100, height=100, size_hint=(0.5, 0.5)))
Expand All @@ -315,6 +316,7 @@ def test_vertical_resize_children_according_size_hint(window):
assert dummy_1.size == (300, approx(100 + 200 / 3 * 2))
assert dummy_2.size == (150, approx(100 + 200 / 3 * 1))


def test_vertical_ignores_size_hint_none(window):
box = UIBoxLayout(width=300, height=400, vertical=True)
dummy_1 = box.add(UIDummy(width=100, height=100, size_hint=(1, None)))
Expand All @@ -327,6 +329,16 @@ def test_vertical_ignores_size_hint_none(window):
assert dummy_2.size == (100, 300)


def test_vertical_fit_content(window):
box = UIBoxLayout(width=100, height=100, vertical=True)
_ = box.add(UIDummy(width=100, height=50))
_ = box.add(UIDummy(width=20, height=100))

box.fit_content()

assert box.size == (100, 150)


def test_horizontal_resize_child_according_size_hint_full(window):
box = UIBoxLayout(width=200, height=200, vertical=False)
dummy_1 = box.add(UIDummy(width=100, height=100, size_hint=(1, 1)))
Expand All @@ -336,6 +348,7 @@ def test_horizontal_resize_child_according_size_hint_full(window):
assert box.size == (200, 200)
assert dummy_1.size == (200, 200)


def test_horizontal_resize_child_according_size_hint_half(window):
box = UIBoxLayout(width=200, height=200, vertical=False)
dummy_1 = box.add(UIDummy(width=100, height=100, size_hint=(0.5, 0.5)))
Expand All @@ -345,6 +358,7 @@ def test_horizontal_resize_child_according_size_hint_half(window):
assert box.size == (200, 200)
assert dummy_1.size == (100, 100)


def test_horizontal_resize_children_according_size_hint(window):
box = UIBoxLayout(width=300, height=400, vertical=False)
dummy_1 = box.add(UIDummy(size_hint_min=(100, 100), size_hint=(1, 1)))
Expand All @@ -368,4 +382,14 @@ def test_horizontal_ignores_size_hint_none(window):
assert dummy_1.size == (200, 100)
assert dummy_2.size == (100, 400)

# TODO test size hint < 1 (do not take full width)

def test_horizontal_fit_content(window):
box = UIBoxLayout(width=100, height=100, vertical=False)
_ = box.add(UIDummy(width=100, height=50))
_ = box.add(UIDummy(width=20, height=100))

box.fit_content()

assert box.size == (120, 100)

# TODO test size hint < 1 (do not take full width)

0 comments on commit f28eade

Please sign in to comment.