acdream/src/AcDream.App/UI/UiScrollablePanel.cs
Erik a59e077a66 fix #371: straddling rows clip at the viewport edge instead of vanishing whole
Gate-3 screenshot review (user): 'the chat tab looks like it is missing
per window config' — Chat Window 1's header rendered over a void at the
DEFAULT scroll offset because its 260px self-sized filter block
straddled the viewport's bottom edge and UiScrollablePanel hid
straddling rows WHOLE (AP-201's predicted symptom, now user-observed at
scroll position zero, upgrading it from polish to blocking).

By fix time the UI renderer HAD everything needed: UiRenderContext's
clip stack (PushClip/PopClip with rect intersection + per-draw quad
clipping) and UiElement's ClipsChildren hook, already honored by both
the generic draw walk and hit-testing. The fix is therefore exactly the
shape the filing asked for, in the panel itself:
- ClipsChildren => true: children draw and hit-test clipped to the
  viewport rect.
- The layout cull keeps any INTERSECTING row Visible (was: fully-inside
  only), with a half-pixel margin excluding zero-overlap edge rows;
  fully-outside rows stay hidden as the cheap skip.

AP-201 retired in this commit (AP actives 142 -> 141); #371 closed; the
gate script's Chat-tab steps re-written to expect clean edge clipping
and to treat any whole-block vanish as a regression. Pinned by
StraddlingRow_StaysVisible_AndClipsInsteadOfVanishing (the exact gate-3
geometry: header + 260px straddler in a 430px viewport) and
ViewportClipsChildDrawingAndHitTesting (the clipped slice is not
clickable).

Full Release suite: 13,089 passed / 4 skipped / 0 failed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-11 17:08:21 +02:00

122 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace AcDream.App.UI;
/// <summary>
/// Simple vertical viewport for controller-built row lists. It shares the same
/// pixel scroll model as chat text and item grids. A row that straddles the
/// viewport edge stays visible and is CLIPPED to the viewport (#371 —
/// <see cref="ClipsChildren"/> routes the draw walk and hit-testing through
/// <see cref="UiRenderContext.PushClip"/>); before that fix this class hid a
/// straddling row WHOLE, which made the Chat tab's 260px per-window filter
/// blocks vanish into a void at the default scroll offset (AP-201's predicted
/// symptom, user-observed at Campaign OP gate 3).
/// </summary>
public sealed class UiScrollablePanel : UiPanel
{
/// <inheritdoc/>
protected override bool ClipsChildren => true;
private readonly Dictionary<UiElement, float> _baseTops = new(ReferenceEqualityComparer.Instance);
public UiScrollable Scroll { get; } = new();
public int LineHeight { get; set; } = 16;
public int ContentHeight { get; private set; }
public UiScrollablePanel()
{
BackgroundColor = Vector4.Zero;
BorderColor = Vector4.Zero;
}
public override void AddChild(UiElement child)
{
base.AddChild(child);
Track(child);
}
public override bool RemoveChild(UiElement child)
{
bool removed = base.RemoveChild(child);
if (removed)
{
_baseTops.Remove(child);
RecomputeContentHeight();
}
return removed;
}
public void ClearContent()
{
foreach (var child in ChildrenBackToFrontSnapshot())
RemoveChild(child);
_baseTops.Clear();
ContentHeight = 0;
Scroll.SetScrollY(0);
}
internal void LayoutScrollableChildren()
{
Scroll.LineHeight = Math.Max(1, LineHeight);
Scroll.ContentHeight = ContentHeight;
Scroll.ViewHeight = Math.Max(0, (int)MathF.Floor(Height));
Scroll.SetScrollY(Scroll.ScrollY);
foreach (var child in Children)
{
if (!_baseTops.TryGetValue(child, out float baseTop))
continue;
float top = baseTop - Scroll.ScrollY;
child.Top = top;
// #371: INTERSECTION, not full containment — a straddling row stays
// visible and ClipsChildren trims it to the viewport at draw and
// hit-test time. Rows entirely outside stay hidden (cheap skip);
// the half-pixel margin excludes zero-overlap rows sitting exactly
// on either edge.
child.Visible = top + child.Height > 0.5f && top < Height - 0.5f;
}
}
public override bool OnEvent(in UiEvent e)
{
if (e.Type == UiEventType.Scroll)
{
Scroll.ScrollByLines(-e.Data0);
LayoutScrollableChildren();
return true;
}
return base.OnEvent(e);
}
protected override void OnDraw(UiRenderContext ctx)
{
LayoutScrollableChildren();
base.OnDraw(ctx);
}
private void Track(UiElement child)
{
child.Anchors = AnchorEdges.None;
_baseTops[child] = child.Top;
RecomputeContentHeight();
}
private void RecomputeContentHeight()
{
float bottom = 0f;
foreach (var child in Children)
{
if (!_baseTops.TryGetValue(child, out float top))
continue;
bottom = MathF.Max(bottom, top + child.Height);
}
ContentHeight = (int)MathF.Ceiling(bottom);
Scroll.SetScrollY(Scroll.ScrollY);
}
}