Adapt to available space
Put a sketch and its notes beside each other when room permits, then stack them when the page narrows. A width threshold makes the behavior predictable in a resized desktop window as well as a phone.
using Xui.Core.Canvas;
using Xui.Core.Math2D;
using Xui.Core.UI;
using NFloat = System.Runtime.InteropServices.NFloat;
public sealed class ResponsiveExample : ViewCollection
{
private readonly Border first = Panel("The sketch", 0xD6B8F4FF);
private readonly Border second = Panel("The notes", 0xEADCFBFF);
public ResponsiveExample()
{
Add(first);
Add(second);
}
protected override Size MeasureCore(Size available, IMeasureContext context)
{
NFloat width = NFloat.IsFinite(available.Width) ? available.Width : 400;
NFloat height = NFloat.IsFinite(available.Height) ? available.Height : 320;
var wide = width >= 560;
var childSize = wide ? new Size((width - 16) / 2, height) : new Size(width, (height - 16) / 2);
first.Measure(childSize, context);
second.Measure(childSize, context);
return new Size(width, height);
}
protected override void ArrangeCore(Rect rect, IMeasureContext context)
{
if (rect.Width >= 560)
{
var width = NFloat.Max(0, (rect.Width - 16) / 2);
first.Arrange(new Rect(rect.X, rect.Y, width, rect.Height), context);
second.Arrange(new Rect(rect.X + width + 16, rect.Y, width, rect.Height), context);
}
else
{
var height = NFloat.Max(0, (rect.Height - 16) / 2);
first.Arrange(new Rect(rect.X, rect.Y, rect.Width, height), context);
second.Arrange(new Rect(rect.X, rect.Y + height + 16, rect.Width, height), context);
}
}
private static Border Panel(string title, uint color) => new()
{
Padding = 24,
BackgroundColor = new Color(color),
Content = new Label
{
Text = title,
FontFamily = "Inter",
FontSize = 20
}
};
}
Make one decision from the allocation
The example uses 560 logical units as its threshold. Below it, each panel gets half the available height with a gap. At or above it, each gets half the available width. The view keeps the same child instances, so a real editor's state would not disappear during a resize.
Both measure and arrange use the same threshold. Measure supplies an appropriate child constraint; arrange uses the final rectangle and includes its X and Y origin. That keeps the container correct when it sits inside another page instead of at window origin.
The fallback sizes in MeasureCore handle an unconstrained parent. They are desired dimensions, not a promise that every parent gives the view 400 by 320. The final rectangle still controls arrangement.
Prefer reflow before scaling
Wrapping, changing columns, and scrolling preserve readable text and useful input targets. Scaling an entire application down can make everything technically fit while making buttons difficult to use. Give content a chance to reflow first.
Use IDeviceInfo for actual input and preference questions, such as a touch-oriented design preset. Use available width for a layout question. A desktop window can be narrow; a tablet can be wide; a browser frame can be smaller than either.
The fixed article preview shows the narrow arrangement. To inspect the wide arrangement, run the example in a desktop window and resize past the threshold:
dotnet run --project Xui/Apps/DemoApp/DemoApp.Desktop.csproj -- --demo layout/responsive
Run that command from a checkout of the Xui repository.
Keep the breakpoint understandable
Choose a threshold from the minimum useful content widths plus the gap. Test just below and above it. Avoid adding several nearly identical thresholds that cause navigation and content to jump at unrelated sizes.
For simple collections, FlexBox can wrap naturally without a custom breakpoint. Use a custom container when the structure itself changes and the layout needs an explicit rule.