From e7ed5dec7d1416e812836ed6c5924f2d526c582d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 12 May 2026 14:32:41 +0100 Subject: [PATCH] Align text editing vertical metrics --- src/Aquarium.Engine/Render/Ui/DebugUi.cs | 39 +++++++++++++++--------- state/evidence.jsonl | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Aquarium.Engine/Render/Ui/DebugUi.cs b/src/Aquarium.Engine/Render/Ui/DebugUi.cs index 1119bcf..dd65927 100644 --- a/src/Aquarium.Engine/Render/Ui/DebugUi.cs +++ b/src/Aquarium.Engine/Render/Ui/DebugUi.cs @@ -1281,14 +1281,17 @@ internal sealed class DebugUi if (endColumn > startColumn) { using var layout = directWriteFactory.CreateTextLayout(displayLines[line], format, bounds.Width, TextLineHeight); - var startX = TextPositionX(layout, startColumn, displayLines[line].Length); - var endX = TextPositionX(layout, endColumn, displayLines[line].Length); + var startHit = TextPositionHit(layout, startColumn, displayLines[line].Length); + var endHit = TextPositionHit(layout, endColumn, displayLines[line].Length); + var vertical = TextVerticalHit(layout, displayLines[line].Length); + var selectionTop = top + line * TextLineHeight + vertical.Top; + var selectionBottom = selectionTop + vertical.Height; target.FillRectangle( RectFromEdges( - bounds.Left + startX, - top + line * TextLineHeight + 2.0f, - bounds.Left + endX, - top + (line + 1) * TextLineHeight), + bounds.Left + startHit.X, + selectionTop, + bounds.Left + endHit.X, + selectionBottom), activeRowBrush); } @@ -1326,9 +1329,10 @@ internal sealed class DebugUi { var column = Math.Clamp(caretIndex - lineStart, 0, displayLines[line].Length); using var layout = directWriteFactory.CreateTextLayout(displayLines[line], format, bounds.Width, TextLineHeight); - var x = bounds.Left + TextPositionX(layout, column, displayLines[line].Length); - var y = top + line * TextLineHeight + 2.0f; - target.DrawLine(new Vector2(x, y), new Vector2(x, y + TextLineHeight - 2.0f), accentBrush, 1.25f); + var hit = TextPositionHit(layout, column, displayLines[line].Length); + var x = bounds.Left + hit.X; + var y = top + line * TextLineHeight + hit.Top; + target.DrawLine(new Vector2(x, y), new Vector2(x, y + hit.Height), accentBrush, 1.25f); return; } @@ -1374,7 +1378,7 @@ internal sealed class DebugUi var edges = new float[displayLines[line].Length + 1]; for (var index = 0; index < edges.Length; index++) { - edges[index] = TextPositionX(layout, index, displayLines[line].Length); + edges[index] = TextPositionHit(layout, index, displayLines[line].Length).X; } lineHitCache.Add(new LineHitCache(globalStart, bounds.Left, top + line * TextLineHeight, edges)); @@ -1382,11 +1386,11 @@ internal sealed class DebugUi } } - private static float TextPositionX(IDWriteTextLayout layout, int position, int length) + private static TextHit TextPositionHit(IDWriteTextLayout layout, int position, int length) { if (length == 0) { - return 0.0f; + return new TextHit(0.0f, 2.0f, TextLineHeight - 2.0f); } var clamped = Math.Clamp(position, 0, length); @@ -1403,8 +1407,13 @@ internal sealed class DebugUi trailing = new RawBool(false); } - layout.HitTestTextPosition(textPosition, trailing, out var x, out _, out _); - return x; + layout.HitTestTextPosition(textPosition, trailing, out var x, out _, out var metrics); + return new TextHit(x, metrics.Top, metrics.Height); + } + + private static TextHit TextVerticalHit(IDWriteTextLayout layout, int length) + { + return TextPositionHit(layout, Math.Min(1, length), length); } private bool HasLabel => !string.IsNullOrWhiteSpace(Label); @@ -1423,6 +1432,8 @@ internal sealed class DebugUi private readonly record struct LineHitCache(int GlobalStart, float OriginX, float OriginY, float[] Edges); + private readonly record struct TextHit(float X, float Top, float Height); + private static bool IsEmptyPrompt(string value) { var normalized = value.Replace("\r\n", "\n", StringComparison.Ordinal).Replace('\r', '\n'); diff --git a/state/evidence.jsonl b/state/evidence.jsonl index 293fed3..3c3415c 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -66,3 +66,4 @@ {"timestamp":"2026-05-12T14:05:00+01:00","topic":"terminal-bottom-line-layout","lesson":"DirectWrite multiline layout can still make a terminal body feel vertically floating even when the containing bounds are bottom-biased. For terminal-style text boxes, draw visible lines individually from the bottom of the text bounds using explicit line height; do not rely on padded blank lines or paragraph layout for bottom alignment.","files":["src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/evidence.jsonl"],"source":"user screenshot correction"} {"timestamp":"2026-05-12T14:25:00+01:00","topic":"debug-text-editing-capture","lesson":"Debug text boxes need real editing ownership: persistent caret index, selection anchor, mouse placement, drag selection, arrow/Home/End movement, Backspace/Delete, selected-text replacement, and input capture while focused. When a text box owns the keyboard, host-level debug hotkeys and client camera controls must receive a non-interactive input snapshot instead of the live key/mouse state.","files":["src/Aquarium.Engine.Contracts/Input/InputState.cs","src/Aquarium.Engine/AquariumHost.cs","src/Aquarium.Engine/Platform/Win32Window.cs","src/Aquarium.Engine/Render/IAquariumRenderer.cs","src/Aquarium.Engine/Render/D3D12Renderer.cs","src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/memory.json"],"source":"user text editing requirement"} {"timestamp":"2026-05-12T14:35:00+01:00","topic":"directwrite-text-editing-metrics","lesson":"Do not position text-box caret or selection with guessed monospace character widths. Use DirectWrite text layout hit-test positions for the same font and layout used to draw text, cache those positions for mouse placement, and keep text boxes focus-only visually rather than filling like active buttons on click.","files":["src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/evidence.jsonl"],"source":"user screenshot correction"} +{"timestamp":"2026-05-12T14:42:00+01:00","topic":"directwrite-text-editing-vertical-metrics","lesson":"Horizontal caret alignment is not enough. Caret height and selection top/bottom should also come from DirectWrite hit-test metrics for the active glyph run; using the UI line box makes editing visuals droop below the text even when x positions are correct.","files":["src/Aquarium.Engine/Render/Ui/DebugUi.cs","state/evidence.jsonl"],"source":"user screenshot correction"}