Add RemoveChild to IParentableElement, only resize Container if children overflow it.

This commit is contained in:
2025-06-20 19:14:29 +02:00
parent 3154b3fa10
commit a9a8113dd9
4 changed files with 28 additions and 9 deletions

View File

@@ -137,6 +137,9 @@ namespace Voile.Input
AddInputMapping("accept", [
new KeyInputAction(KeyboardKey.Enter),
]);
AddInputMapping("cancel", [
new KeyInputAction(KeyboardKey.Backspace),
]);
}
protected bool TryGetInputMappings(string forAction, [NotNullWhen(true)] out List<IInputAction>? inputActions)

View File

@@ -12,7 +12,7 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
public Vector2 Position { get; set; }
public Rect MinimumRect { get; set; } = Rect.Zero;
public Rect Size { get; set; } = Rect.Zero;
public bool Visible { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public bool Visible { get; set; } = true;
public Container()
{
@@ -67,13 +67,15 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
maxY = MathF.Max(maxY, pos.Y + size.Height);
}
// Optionally apply padding
float padding = 0f; // or make this configurable
float padding = 0f;
Size = new Rect(
maxX - minX + padding * 2,
maxY - minY + padding * 2
);
float occupiedWidth = (maxX - minX) + padding * 2;
float occupiedHeight = (maxY - minY) + padding * 2;
float finalWidth = MathF.Max(occupiedWidth, MinimumRect.Width);
float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height);
Size = new Rect(finalWidth, finalHeight);
}
public void AddChild(IElement child)
@@ -82,6 +84,12 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
Update();
}
public void RemoveChild(IElement child)
{
_children.Remove(child);
Update();
}
public void Render(RenderSystem renderer, Style style)
{
foreach (var child in Children)

View File

@@ -14,6 +14,7 @@ public interface IParentableElement
{
public IReadOnlyList<IElement> Children { get; }
public void AddChild(IElement child);
public void RemoveChild(IElement child);
}
public interface IResizeableElement