Build with XuiView Markdown

Measure and arrange a custom view

Build an inset panel: a container that leaves the same amount of space around its child. This is a useful first custom layout because it makes the difference between asking for space and assigning space visible.

For most pages, use the existing stacks, FlexBox, or Grid. Write a container when its sizing rule is a reusable part of your app.

Measure inward, report outward

using System;
using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using NFloat = System.Runtime.InteropServices.NFloat;

public sealed class InsetPanel : View
{
    private View? content;
    private NFloat inset = 24;
    public View? Content { get => content; set => SetProtectedChild(ref content, value); }
    public NFloat Inset { get => inset; set => SetViewProperty(ref inset, NFloat.Max(0, value)); }
    public override int Count => content is null ? 0 : 1;

    public override View this[int index] => index == 0 && content is not null ? content : throw new IndexOutOfRangeException();
    protected override Size MeasureCore(Size available, IMeasureContext context)
    {
        var inner = new Size(NFloat.Max(0, available.Width - Inset * 2), NFloat.Max(0, available.Height - Inset * 2));
        var desired = content?.Measure(inner, context) ?? Size.Empty;
        return new Size(desired.Width + Inset * 2, desired.Height + Inset * 2);
    }

    protected override void ArrangeCore(Rect rect, IMeasureContext context)
    {
        content?.Arrange(new Rect(rect.X + Inset, rect.Y + Inset, NFloat.Max(0, rect.Width - Inset * 2), NFloat.Max(0, rect.Height - Inset * 2)), context);
    }
}

MeasureCore receives the available border-edge size. Subtract the inset on both sides before measuring the child. Then add those sides back to the child's desired size. Clamp the inner size to zero so a tiny window never sends negative dimensions to the child.

An available dimension can be infinite when a parent measures unconstrained content. Subtracting a finite inset preserves that infinity; do not convert it to an integer or use it to allocate a pixel buffer. Let the child report a finite desired size.

ArrangeCore receives the actual rectangle. It gives the child the inner rectangle, including the parent's position. The child may apply its own alignment and margin inside that slot. Avoid measuring again just to compute values you already know from the final rectangle.

Make the child part of the tree

The Content setter calls SetProtectedChild. That establishes the parent relationship, detaches the old content, attaches the new content when appropriate, and invalidates the container. Count and the indexer expose that relationship to tree traversal.

The base RenderCore renders the child, and the base AnimateCore advances it. The panel does not need to reimplement those methods. If you later draw a background in RenderCore, call base.RenderCore(context) after the background to keep the child visible.

Do not put the same view instance in two panels. A view has one parent; create another instance or remove it from its previous parent first.

Change the inset safely

SetViewProperty invalidates measure, arrangement, and drawing when Inset changes. That matters because the inset affects both the requested size and the child's position. Calling only InvalidateRender would repaint the old layout.

As a small exercise, use an InsetPanel with a Label as its content, then change Inset from 24 to 48. The label should move inward and the panel should request more space. Resize until the available area is smaller than the insets: the child slot should become zero-sized rather than negative.

Keep measurement free of subscriptions, network requests, and state updates. Layout may run repeatedly with different constraints; a measurement that mutates its own constraints can create a feedback loop.