Refine portable dashboard layout lowering

This commit is contained in:
Meta Vens 2026-06-01 14:53:56 +01:00
commit fb5b492c04
5 changed files with 73 additions and 27 deletions

View file

@ -33,6 +33,21 @@ public sealed class AquariumUiDocument
}
public AquariumUiDocument Surface(string id, string title, float left, float top, float width, float height, Action<AquariumUiSurfaceBuilder> compose)
{
return Surface(id, title, left, top, width, height, contentPadding: 8.0f, rootGap: 8.0f, rootPadding: 8.0f, compose);
}
public AquariumUiDocument Surface(
string id,
string title,
float left,
float top,
float width,
float height,
float contentPadding,
float rootGap,
float rootPadding,
Action<AquariumUiSurfaceBuilder> compose)
{
var children = new List<AquariumUiElement>();
compose(new AquariumUiSurfaceBuilder(children));
@ -41,7 +56,8 @@ public sealed class AquariumUiDocument
id,
title,
new AquariumUiRect(left, top, width, height),
new AquariumUiElement(id + ".root", "dashboard", title, Role: "root", Layout: AquariumUiLayout.Vertical(8.0f, 8.0f), Children: children)));
new AquariumUiElement(id + ".root", "dashboard", title, Role: "root", Layout: AquariumUiLayout.Vertical(rootGap, rootPadding), Children: children),
Math.Max(0.0f, contentPadding)));
return this;
}
@ -65,7 +81,8 @@ public sealed record AquariumUiSurface(
string Id,
string Title,
AquariumUiRect Bounds,
AquariumUiElement Root);
AquariumUiElement Root,
float ContentPadding = 8.0f);
public sealed record AquariumUiRect(float Left, float Top, float Width, float Height);
@ -138,6 +155,7 @@ public sealed record AquariumUiElement(
Action? Invoke = null,
Func<bool>? IsVisible = null,
float Weight = 1.0f,
float? FixedExtent = null,
IReadOnlyList<AquariumUiElement>? Children = null)
{
public bool Visible => IsVisible?.Invoke() ?? true;
@ -145,19 +163,19 @@ public sealed record AquariumUiElement(
public sealed class AquariumUiSurfaceBuilder(List<AquariumUiElement> children)
{
public AquariumUiSurfaceBuilder Vertical(string id, Action<AquariumUiSurfaceBuilder> compose, float weight = 1.0f, float gap = 8.0f, float padding = 0.0f, Func<bool>? isVisible = null)
public AquariumUiSurfaceBuilder Vertical(string id, Action<AquariumUiSurfaceBuilder> compose, float weight = 1.0f, float gap = 8.0f, float padding = 0.0f, Func<bool>? isVisible = null, float? fixedExtent = null)
{
var scopeChildren = new List<AquariumUiElement>();
compose(new AquariumUiSurfaceBuilder(scopeChildren));
children.Add(new AquariumUiElement(id, "group", Layout: AquariumUiLayout.Vertical(gap, padding), IsVisible: isVisible, Weight: Math.Max(0.001f, weight), Children: scopeChildren));
children.Add(new AquariumUiElement(id, "group", Layout: AquariumUiLayout.Vertical(gap, padding), IsVisible: isVisible, Weight: Math.Max(0.001f, weight), FixedExtent: fixedExtent, Children: scopeChildren));
return this;
}
public AquariumUiSurfaceBuilder Horizontal(string id, Action<AquariumUiSurfaceBuilder> compose, float weight = 1.0f, float gap = 8.0f, float padding = 0.0f, Func<bool>? isVisible = null)
public AquariumUiSurfaceBuilder Horizontal(string id, Action<AquariumUiSurfaceBuilder> compose, float weight = 1.0f, float gap = 8.0f, float padding = 0.0f, Func<bool>? isVisible = null, float? fixedExtent = null)
{
var scopeChildren = new List<AquariumUiElement>();
compose(new AquariumUiSurfaceBuilder(scopeChildren));
children.Add(new AquariumUiElement(id, "group", Layout: AquariumUiLayout.Horizontal(gap, padding), IsVisible: isVisible, Weight: Math.Max(0.001f, weight), Children: scopeChildren));
children.Add(new AquariumUiElement(id, "group", Layout: AquariumUiLayout.Horizontal(gap, padding), IsVisible: isVisible, Weight: Math.Max(0.001f, weight), FixedExtent: fixedExtent, Children: scopeChildren));
return this;
}
@ -166,11 +184,11 @@ public sealed class AquariumUiSurfaceBuilder(List<AquariumUiElement> children)
return Horizontal(id, compose, weight, isVisible: isVisible);
}
public AquariumUiSurfaceBuilder Pane(string id, string title, Action<AquariumUiSurfaceBuilder> compose, string tone = "neutral", float weight = 1.0f)
public AquariumUiSurfaceBuilder Pane(string id, string title, Action<AquariumUiSurfaceBuilder> compose, string tone = "neutral", float weight = 1.0f, float? fixedExtent = null)
{
var paneChildren = new List<AquariumUiElement>();
compose(new AquariumUiSurfaceBuilder(paneChildren));
children.Add(new AquariumUiElement(id, "pane", title, Style: new AquariumUiStyle("panel", tone), Layout: AquariumUiLayout.Vertical(8.0f, 10.0f), Weight: Math.Max(0.001f, weight), Children: paneChildren));
children.Add(new AquariumUiElement(id, "pane", title, Style: new AquariumUiStyle("panel", tone), Layout: AquariumUiLayout.Vertical(8.0f, 10.0f), Weight: Math.Max(0.001f, weight), FixedExtent: fixedExtent, Children: paneChildren));
return this;
}

View file

@ -671,8 +671,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
var surface = clientUiSurfaces[surfaceIndex];
var surfaceBounds = SurfaceBounds(surface);
var hasTitle = !string.IsNullOrWhiteSpace(surface.Title);
var contentTop = hasTitle ? surfaceBounds.Top + 42.0f : surfaceBounds.Top + 8.0f;
var content = RectFromEdges(surfaceBounds.Left + 8.0f, contentTop, surfaceBounds.Right - 8.0f, surfaceBounds.Bottom - 8.0f);
var contentTop = hasTitle ? surfaceBounds.Top + 42.0f : surfaceBounds.Top + surface.ContentPadding;
var content = RectFromEdges(surfaceBounds.Left + surface.ContentPadding, contentTop, surfaceBounds.Right - surface.ContentPadding, surfaceBounds.Bottom - surface.ContentPadding);
if (TryHitPreviewChildren(surface.Root.Children ?? [], content, surface.Root.Layout ?? AquariumUiLayout.Vertical(), point, out hit))
{
return true;
@ -738,7 +738,10 @@ public sealed class D3D12Renderer : IAquariumRenderer
case "group":
return TryHitPreviewChildren(element.Children ?? [], bounds, element.Layout ?? AquariumUiLayout.Vertical(), point, out hit);
case "pane":
return TryHitPreviewChildren(element.Children ?? [], RectFromEdges(bounds.Left + 6.0f, bounds.Top + 32.0f, bounds.Right - 6.0f, bounds.Bottom - 6.0f), element.Layout ?? AquariumUiLayout.Vertical(), point, out hit);
var paneContent = !string.IsNullOrWhiteSpace(element.Text)
? RectFromEdges(bounds.Left + 6.0f, bounds.Top + 32.0f, bounds.Right - 6.0f, bounds.Bottom - 6.0f)
: bounds;
return TryHitPreviewChildren(element.Children ?? [], paneContent, element.Layout ?? AquariumUiLayout.Vertical(), point, out hit);
case "card":
return TryHitPreviewChildren(element.Children ?? [], bounds, element.Layout ?? AquariumUiLayout.Vertical(4.0f, 8.0f), point, out hit);
case "preview":
@ -820,12 +823,15 @@ public sealed class D3D12Renderer : IAquariumRenderer
return true;
}
private Rect SurfaceBounds(AquariumUiSurface surface) =>
RectFromEdges(
Math.Clamp(surface.Bounds.Left, 8.0f, Math.Max(8.0f, width - 80.0f)),
Math.Clamp(surface.Bounds.Top, 8.0f, Math.Max(8.0f, height - 48.0f)),
Math.Clamp(surface.Bounds.Left + surface.Bounds.Width, 88.0f, width - 8.0f),
Math.Clamp(surface.Bounds.Top + surface.Bounds.Height, 56.0f, height - 8.0f));
private Rect SurfaceBounds(AquariumUiSurface surface)
{
var edgePadding = surface.ContentPadding <= 0.0f ? 0.0f : 8.0f;
return RectFromEdges(
Math.Clamp(surface.Bounds.Left, edgePadding, Math.Max(edgePadding, width - 80.0f)),
Math.Clamp(surface.Bounds.Top, edgePadding, Math.Max(edgePadding, height - 48.0f)),
Math.Clamp(surface.Bounds.Left + surface.Bounds.Width, 80.0f + edgePadding, width - edgePadding),
Math.Clamp(surface.Bounds.Top + surface.Bounds.Height, 48.0f + edgePadding, height - edgePadding));
}
private static string HitPreviewHandle(Rect itemBounds, Vector2 point)
{
@ -855,6 +861,11 @@ public sealed class D3D12Renderer : IAquariumRenderer
private static float? PreferredUiExtent(AquariumUiElement element, bool horizontal)
{
if (element.FixedExtent is { } fixedExtent)
{
return Math.Max(0.0f, fixedExtent);
}
if (horizontal)
{
return null;
@ -899,7 +910,8 @@ public sealed class D3D12Renderer : IAquariumRenderer
return element.Kind switch
{
"pane" => contentExtent + 38.0f,
"pane" when !string.IsNullOrWhiteSpace(element.Text) => contentExtent + 38.0f,
"pane" => contentExtent,
"card" => contentExtent,
_ => contentExtent,
};

View file

@ -194,11 +194,12 @@ internal sealed class DirectWriteOverlay : IDisposable
private void DrawSurface(AquariumUiSurface surface)
{
var edgePadding = surface.ContentPadding <= 0.0f ? 0.0f : 8.0f;
var bounds = RectFromEdges(
Math.Clamp(surface.Bounds.Left, 8.0f, Math.Max(8.0f, width - 80.0f)),
Math.Clamp(surface.Bounds.Top, 8.0f, Math.Max(8.0f, height - 48.0f)),
Math.Clamp(surface.Bounds.Left + surface.Bounds.Width, 88.0f, width - 8.0f),
Math.Clamp(surface.Bounds.Top + surface.Bounds.Height, 56.0f, height - 8.0f));
Math.Clamp(surface.Bounds.Left, edgePadding, Math.Max(edgePadding, width - 80.0f)),
Math.Clamp(surface.Bounds.Top, edgePadding, Math.Max(edgePadding, height - 48.0f)),
Math.Clamp(surface.Bounds.Left + surface.Bounds.Width, 80.0f + edgePadding, width - edgePadding),
Math.Clamp(surface.Bounds.Top + surface.Bounds.Height, 48.0f + edgePadding, height - edgePadding));
renderTarget.FillRectangle(bounds, panelBrush);
renderTarget.DrawRectangle(bounds, outlineBrush, 1.0f);
var hasTitle = !string.IsNullOrWhiteSpace(surface.Title);
@ -207,8 +208,8 @@ internal sealed class DirectWriteOverlay : IDisposable
DrawHeader(surface.Title, titleFormat, RectFromEdges(bounds.Left + 12.0f, bounds.Top + 8.0f, bounds.Right - 12.0f, bounds.Top + 34.0f), primaryTextBrush);
}
var contentTop = hasTitle ? bounds.Top + 42.0f : bounds.Top + 8.0f;
var content = RectFromEdges(bounds.Left + 8.0f, contentTop, bounds.Right - 8.0f, bounds.Bottom - 8.0f);
var contentTop = hasTitle ? bounds.Top + 42.0f : bounds.Top + surface.ContentPadding;
var content = RectFromEdges(bounds.Left + surface.ContentPadding, contentTop, bounds.Right - surface.ContentPadding, bounds.Bottom - surface.ContentPadding);
DrawSurfaceChildren(surface.Root.Children ?? [], content, surface.Root.Layout ?? AquariumUiLayout.Vertical());
}
@ -256,8 +257,12 @@ internal sealed class DirectWriteOverlay : IDisposable
DrawSurfaceChildren(element.Children ?? [], bounds, element.Layout ?? AquariumUiLayout.Vertical());
break;
case "pane":
DrawSurfaceFrame(bounds, element.Text ?? element.Id, element.Style?.Tone ?? "neutral", drawTitle: true);
DrawSurfaceChildren(element.Children ?? [], RectFromEdges(bounds.Left + 6.0f, bounds.Top + 32.0f, bounds.Right - 6.0f, bounds.Bottom - 6.0f), element.Layout ?? AquariumUiLayout.Vertical());
var hasPaneTitle = !string.IsNullOrWhiteSpace(element.Text);
DrawSurfaceFrame(bounds, element.Text, element.Style?.Tone ?? "neutral", drawTitle: hasPaneTitle);
var paneContent = hasPaneTitle
? RectFromEdges(bounds.Left + 6.0f, bounds.Top + 32.0f, bounds.Right - 6.0f, bounds.Bottom - 6.0f)
: bounds;
DrawSurfaceChildren(element.Children ?? [], paneContent, element.Layout ?? AquariumUiLayout.Vertical());
break;
case "card":
DrawSurfaceFrame(bounds, null, element.Style?.Tone ?? "neutral", drawTitle: false);
@ -512,6 +517,11 @@ internal sealed class DirectWriteOverlay : IDisposable
private static float? PreferredExtent(AquariumUiElement element, bool horizontal)
{
if (element.FixedExtent is { } fixedExtent)
{
return Math.Max(0.0f, fixedExtent);
}
if (horizontal)
{
return null;
@ -556,7 +566,8 @@ internal sealed class DirectWriteOverlay : IDisposable
return element.Kind switch
{
"pane" => contentExtent + 38.0f,
"pane" when !string.IsNullOrWhiteSpace(element.Text) => contentExtent + 38.0f,
"pane" => contentExtent,
"card" => contentExtent,
_ => contentExtent,
};