WPF-free geometry behind the visual controls: wrapped-grid layout with O(1) index/offset mapping and keyboard targets, nice axis ticks, min/max decimation, zoom/pan transforms, monotone curve editing rules, filmstrip and heatmap cell layout, an LRU cache and device-pixel snapping, with tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
145 lines
5.4 KiB
C#
145 lines
5.4 KiB
C#
using Titano.Testing;
|
||
using Titano.Ui.Visuals;
|
||
|
||
namespace Titano.App.Tests.Visuals;
|
||
|
||
public sealed class WrapLayoutTests
|
||
{
|
||
[Theory]
|
||
[Case(0.0, 1)]
|
||
[Case(235.0, 1)]
|
||
[Case(236.0, 1)]
|
||
[Case(487.0, 1)]
|
||
[Case(488.0, 2)]
|
||
[Case(1252.0, 5)]
|
||
[Case(1255.0, 5)]
|
||
public void ColumnsCountWholeItemsWithGapsBetween(double width, int expected)
|
||
{
|
||
// 236-px cards with 16-px gaps: n columns need n·236 + (n−1)·16 px.
|
||
Assert.Equal(expected, WrapLayout.ColumnsFor(width, 236, 16));
|
||
}
|
||
|
||
[Test]
|
||
public void RowsAndExtentLeaveNoTrailingGap()
|
||
{
|
||
var layout = new WrapLayout(11, 1252, 236, 208, 16);
|
||
Assert.Equal(5, layout.Columns);
|
||
Assert.Equal(3, layout.Rows);
|
||
Assert.Approximately(3 * 224 - 16, layout.ExtentHeight, 1e-9);
|
||
Assert.Approximately(5 * 236 + 4 * 16, layout.ExtentWidth, 1e-9);
|
||
Assert.Approximately(0, new WrapLayout(0, 1252, 236, 208, 16).ExtentHeight, 1e-9);
|
||
}
|
||
|
||
[Test]
|
||
public void IndexMapsToRowColumnAndPixelOrigin()
|
||
{
|
||
var layout = new WrapLayout(500_000, 1252, 236, 208, 16);
|
||
Assert.Equal(1234, layout.RowOf(6172));
|
||
Assert.Equal(2, layout.ColumnOf(6172));
|
||
Assert.Approximately(2 * 252, layout.LeftOf(6172), 1e-9);
|
||
Assert.Approximately(1234 * 224, layout.TopOf(6172), 1e-6);
|
||
Assert.Equal(100_000, layout.Rows);
|
||
}
|
||
|
||
[Test]
|
||
public void VisibleRangeIsConstantTimeAndCoversPartialRows()
|
||
{
|
||
var layout = new WrapLayout(500_000, 1252, 236, 208, 16);
|
||
// Offset in the middle of row 50 000's gap: row 50 000 has ended, row 50 001 starts at 50 001·224.
|
||
var offset = 50_000 * 224.0 + 210;
|
||
var (first, last) = layout.VisibleRange(offset, 600);
|
||
Assert.Equal(50_001 * 5, first);
|
||
// Bottom edge at offset + 600 = 50 000·224 + 810 → rows up to 50 003 (starts at +672) are visible.
|
||
Assert.Equal(50_004 * 5 - 1, last);
|
||
|
||
var (cachedFirst, cachedLast) = layout.VisibleRange(offset, 600, cacheRows: 1);
|
||
Assert.Equal(first - 5, cachedFirst);
|
||
Assert.Equal(last + 5, cachedLast);
|
||
}
|
||
|
||
[Test]
|
||
public void VisibleRangeExcludesRowStartingAtBottomEdge()
|
||
{
|
||
var layout = new WrapLayout(100, 1252, 236, 208, 16);
|
||
var (first, last) = layout.VisibleRange(0, 224);
|
||
Assert.Equal(0, first);
|
||
Assert.Equal(4, last);
|
||
}
|
||
|
||
[Test]
|
||
public void VisibleRangeClampsToLastPartialRow()
|
||
{
|
||
var layout = new WrapLayout(12, 1252, 236, 208, 16);
|
||
var (first, last) = layout.VisibleRange(0, 10_000, cacheRows: 2);
|
||
Assert.Equal(0, first);
|
||
Assert.Equal(11, last);
|
||
Assert.Equal((0, -1), new WrapLayout(0, 1252, 236, 208, 16).VisibleRange(0, 500));
|
||
}
|
||
|
||
[Test]
|
||
public void OffsetToRevealScrollsTheLeastAmount()
|
||
{
|
||
var layout = new WrapLayout(100, 1252, 236, 208, 16);
|
||
// Row 3 spans [672, 880): below a viewport [0, 600) → bottom-aligned.
|
||
Assert.Approximately(880 - 600, layout.OffsetToReveal(15, 0, 600), 1e-9);
|
||
// Already visible → unchanged.
|
||
Assert.Approximately(300, layout.OffsetToReveal(15, 300, 600), 1e-9);
|
||
// Above the viewport → top-aligned.
|
||
Assert.Approximately(224, layout.OffsetToReveal(5, 1000, 600), 1e-9);
|
||
// Taller than the viewport → top-aligned.
|
||
Assert.Approximately(672, WrapLayout.OffsetToReveal(672, 208, 0, 100), 1e-9);
|
||
}
|
||
|
||
[Test]
|
||
public void ClampOffsetKeepsViewportInsideExtent()
|
||
{
|
||
var layout = new WrapLayout(10, 1252, 236, 208, 16);
|
||
Assert.Approximately(432 - 300, layout.ClampOffset(5000, 300), 1e-9);
|
||
Assert.Approximately(0, layout.ClampOffset(-20, 300), 1e-9);
|
||
Assert.Approximately(0, layout.ClampOffset(50, 1000), 1e-9);
|
||
}
|
||
|
||
[Test]
|
||
public void JustifySpreadsLeftoverWidthOverGaps()
|
||
{
|
||
var layout = new WrapLayout(10, 1300, 236, 208, 16, justify: true);
|
||
Assert.Equal(5, layout.Columns);
|
||
Assert.Approximately(1300, layout.LeftOf(4) + 236, 1e-9);
|
||
Assert.Approximately(1300, layout.ExtentWidth, 1e-9);
|
||
}
|
||
|
||
[Theory]
|
||
[Case(7, WrapStep.Left, 6)]
|
||
[Case(5, WrapStep.Left, 4)]
|
||
[Case(0, WrapStep.Left, 0)]
|
||
[Case(4, WrapStep.Right, 5)]
|
||
[Case(11, WrapStep.Right, 11)]
|
||
[Case(7, WrapStep.Up, 2)]
|
||
[Case(2, WrapStep.Up, 2)]
|
||
[Case(2, WrapStep.Down, 7)]
|
||
[Case(9, WrapStep.Down, 11)]
|
||
[Case(11, WrapStep.Down, 11)]
|
||
[Case(7, WrapStep.First, 0)]
|
||
[Case(7, WrapStep.Last, 11)]
|
||
public void KeyboardStepsFollowReadingOrderAndColumns(int from, WrapStep step, int expected)
|
||
{
|
||
// 12 items in rows of 5: 0–4, 5–9, 10–11.
|
||
var layout = new WrapLayout(12, 1252, 236, 208, 16);
|
||
Assert.Equal(expected, layout.Navigate(from, step, pageRows: 2));
|
||
}
|
||
|
||
[Test]
|
||
public void PageStepsMoveWholeViewportRowsInTheSameColumn()
|
||
{
|
||
var layout = new WrapLayout(1000, 1252, 236, 208, 16);
|
||
var pageRows = layout.PageRows(700);
|
||
Assert.Equal(3, pageRows);
|
||
Assert.Equal(2 + 3 * 5, layout.Navigate(2, WrapStep.PageDown, pageRows));
|
||
Assert.Equal(2, layout.Navigate(2 + 3 * 5, WrapStep.PageUp, pageRows));
|
||
Assert.Equal(3, layout.Navigate(8, WrapStep.PageUp, pageRows));
|
||
// From row 198 a page down is clamped to the last row, same column.
|
||
Assert.Equal(999, layout.Navigate(994, WrapStep.PageDown, pageRows));
|
||
Assert.Equal(-1, new WrapLayout(0, 1252, 236, 208, 16).Navigate(0, WrapStep.Down, 1));
|
||
}
|
||
}
|