Add ConfineToChildren to Container, containers will use MinimumRect as a size if a given size is smaller than minimum, update documentation for Container.

This commit is contained in:
2025-06-20 20:08:17 +02:00
parent bc95fff4a3
commit f0c721bb0f
3 changed files with 44 additions and 9 deletions

View File

@@ -21,6 +21,17 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
set
{
_size = value;
if (value.Width < MinimumRect.Width)
{
_size.Width = MinimumRect.Width;
}
if (value.Height < MinimumRect.Height)
{
_size.Height = MinimumRect.Height;
}
MarkDirty();
}
}
@@ -29,6 +40,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
/// <inheritdoc />
public bool Dirty => _isDirty;
/// <summary>
/// Specifies if this <see cref="Container"/>'s minimum size will be confined to contents.
/// </summary>
public bool ConfineToContents { get; set; } = false;
public Container()
{
MarkDirty();
@@ -62,7 +78,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
}
Arrange();
CalculateMinimumSize();
if (ConfineToContents)
{
RecalculateSizes();
}
}
public void MarkDirty() => _isDirty = true;
@@ -73,16 +93,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
public abstract void Arrange();
/// <summary>
/// Recalculates a minimum size for this <see cref="Container"/>.
/// Recalculates sizes for this <see cref="Container"/>.
/// This is done automatically if <see cref="ConfineToContents"/> is true.
/// </summary>
public void CalculateMinimumSize()
public void RecalculateSizes()
{
if (Children.Count == 0)
{
_size = MinimumRect;
return;
}
float minX = float.MaxValue;
float minY = float.MaxValue;
float maxX = float.MinValue;
@@ -108,6 +123,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height);
MinimumRect = new Rect(finalWidth, finalHeight);
if (MinimumRect > _size)
{
_size = MinimumRect;
}
}
public void AddChild(IElement child)

View File

@@ -5,5 +5,19 @@ namespace Voile.UI;
/// </summary>
public record Rect(float Width = 0.0f, float Height = 0.0f)
{
public float Width { get; set; } = Width;
public float Height { get; set; } = Height;
public static Rect Zero => new Rect(0.0f, 0.0f);
public float Area => Width * Height;
public int CompareTo(Rect? other)
{
if (other is null) return 1;
return Area.CompareTo(other.Area);
}
public static bool operator >(Rect left, Rect right) => left.CompareTo(right) > 0;
public static bool operator <(Rect left, Rect right) => left.CompareTo(right) < 0;
public static bool operator >=(Rect left, Rect right) => left.CompareTo(right) >= 0;
public static bool operator <=(Rect left, Rect right) => left.CompareTo(right) <= 0;
}