Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,14 @@ No abbreviations in names.
and does nothing, and it also forces an exemption into `AppMenuBarTests`, which walks the declared
`Items` and requires every leaf to carry one or the other. The chart's two marker menus, keyed
`MenuFlyout`s in `TrendChartView.axaml`, are no placeholders: each item runs a `Click` handler that acts
on the moment and the marker under the pointer.
on the moment and the marker under the pointer. Every `Go` item carries a command and its `InputGesture`
from `Navigation/WindowGestures`, the one gesture table; `git grep -n "InputGesture=" -- SemiPlot/SemiPlot.UI`
prints only lines that read `WindowGestures`. A tooltip that names a gesture prints it through
`WindowGestures.TextOf`, the text the menu prints (`docs/architecture/ui-text.md#the-gesture-text`).
- A window gesture runs from `MainWindow.OnKeyDown`, which runs the `Go` item whose `InputGesture` matches
the key (`AppMenuBar.GoItemFor`), only on a key no control handled and never on one typed into a `TextBox`. No window `KeyBinding` carries one, because a `KeyBinding` runs
ahead of the focused `TextBox`; `git grep -n "KeyBinding" -- SemiPlot/SemiPlot.UI/MainWindow` prints nothing
(`docs/architecture/trend-interaction.md#the-go-menu-and-the-gestures`).
- The sidebar row is read-only but for its visibility box, and `TrendLegendViewTests` gates it as an
allowlist over the realised row template: every control in it is a `Border`, `Grid`, `TextBlock` or
`CheckBox`, and exactly one `CheckBox` is present. `ThePanel_RealisesNoTextEditor` gates the panel's
Expand Down Expand Up @@ -402,6 +409,16 @@ No abbreviations in names.
re-reads `IMarkerStore.ReadAsync` every 5 s and on `ReadNow()` after each marker write, and
`Chart/ChartMarkers` reconciles each read into `Core/Trends/MarkerSet`, which keeps a marker
with a write in flight as the window shows it (`docs/architecture/overview.md#the-live-markers`).
The run and marker steps and the marker list read `ChartMarkers.All` in
`Navigation/NavigationBarViewModel` and move the chart with `Navigation.SetWindow` and nothing else; the
one marker list is the bar's `MarkersButton` flyout, which the `Go` menu and Ctrl+M open too, and the one
run list is the bar's `RecipeRunsButton` flyout, which the `Go` menu and Ctrl+R open too; both are
`Navigation/NavigationListView` over one row policy, `NavigationListRows`
(`docs/architecture/trend-interaction.md#the-marker-and-run-lists`).
- The minimap moves the chart only through `Minimap/MinimapPager`, which lands every page step and track
press with `Navigation.SetWindow` and nothing else. The page has no writer but its derivation,
`MinimapPage.Holding` over the chart window clipped to the newest sample, so no control picks a Day, Week or
Month unit and no indicator names the page (`docs/architecture/trend-interaction.md#archive-overview-minimap`).
- Every chart history request comes from `Chart/ChartHistoryPlanner.Next` and names the visible pens
only; no caller builds a `HistoryRequest` of its own
(`docs/architecture/data-integration.md#what-one-history-query-covers`).
Expand Down Expand Up @@ -479,7 +496,7 @@ No abbreviations in names.
`ArchiveExceptionMapper`, which stays synchronous, pure and unit-testable, and nothing opens a second
connection to enrich the error. Each read supplies every relation its statement touches, which the
detail line names: one relation for the history and realtime reads, `semiplot_tags, trends` for the
archive extent and the archive days, all three of `semiplot_tags`, `semiplot_groups` and
archive extent, the archive days and the archive day spans, all three of `semiplot_tags`, `semiplot_groups` and
`semiplot_pen_groups` for the pen catalogue, and `semiplot_markers` for every marker read and write.

---
Expand Down
4 changes: 4 additions & 0 deletions SemiPlot/SemiPlot.Core/Data/ArchiveDaySpan.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace SemiPlot.Core.Data;

/// <summary>An archive wall-clock day that holds raw rows, with its earliest and latest raw sample in UTC.</summary>
public sealed record ArchiveDaySpan(DateOnly Day, DateTime FirstUtc, DateTime LastUtc);
6 changes: 6 additions & 0 deletions SemiPlot/SemiPlot.Core/Data/IDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ Task<Result<IReadOnlyList<DateOnly>>> QueryArchiveDaysAsync(
DateOnly firstDay,
DateOnly lastDay,
CancellationToken cancellationToken = default);

// One span per archive wall-clock day in [firstDay, lastDay] that holds a raw row of a catalogue pen, ascending.
Task<Result<IReadOnlyList<ArchiveDaySpan>>> QueryArchiveDaySpansAsync(
DateOnly firstDay,
DateOnly lastDay,
CancellationToken cancellationToken = default);
}
214 changes: 214 additions & 0 deletions SemiPlot/SemiPlot.Core/Trends/DayCalendar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
using SemiPlot.Core.Data;

namespace SemiPlot.Core.Trends;

/// <summary>A horizontal span of the day calendar, in pixels from its left end.</summary>
public readonly record struct TrackSpan(double Left, double Width);

/// <summary>How much of the common working hours a day's span covers; a day without rows has no cell.</summary>
public enum DayCoverage
{
Low,
Medium,
Full
}

/// <summary>One day cell of the calendar that holds rows, graded by its coverage.</summary>
public readonly record struct CalendarCell(double Left, double Width, DayCoverage Coverage);

/// <summary>
/// The minimap's day calendar: one cell per archive wall-clock day of the extent, the last day's cell ending at
/// <see cref="Right"/> (docs/architecture/trend-interaction.md#archive-overview-minimap).
/// </summary>
public readonly record struct DayCalendar(DateOnly FirstDay, int DayCount, double Pitch, double Gap, double Right)
{
/// <summary>The page outline's stroke, drawn outside the cells; the calendar keeps it free at its left end.</summary>
public const double OutlineStroke = 2.0;

/// <summary>The month separator's width, in the gap before a month's first cell.</summary>
public const double SeparatorWidth = 1.0;

private const double MaximumPitch = 20.0;
private const int LongestMondayLabelledArchiveDays = 42;
private const double NarrowestGappedPitch = 4.0;
private const double CellGap = 1.0;
private const double LowCoverageCeiling = 0.25;
private const double MediumCoverageCeiling = 0.75;

/// <summary>A cell's drawn width; at a pitch below one pixel a cell is one pixel column wide.</summary>
public double CellWidth => Math.Max(Pitch - Gap, 1.0);

/// <summary>True on an archive of six weeks or less, whose label row names its Mondays beside its months.</summary>
public bool ShowsMondayLabels => DayCount <= LongestMondayLabelledArchiveDays;

/// <summary>
/// The calendar over the days from first to last, the last ending at the row's right end, at a whole pitch of
/// at most 20 px, a 1 px gap from a 4 px pitch; a pitch below 1 px is fractional; null when no room is left.
/// </summary>
public static DayCalendar? Over(DateOnly firstDay, DateOnly lastDay, double width)
{
var dayCount = Math.Max(lastDay.DayNumber - firstDay.DayNumber + 1, 1);
var available = width - OutlineStroke;
if (available <= 0.0)
{
return null;
}

var wholePitch = Math.Min(MaximumPitch, Math.Floor(available / dayCount));
var pitch = wholePitch >= 1.0 ? wholePitch : available / dayCount;

return new DayCalendar(firstDay, dayCount, pitch, pitch >= NarrowestGappedPitch ? CellGap : 0.0, width);
}

/// <summary>The grade of a day's span against the common working hours of the archive's wall clock.</summary>
private static DayCoverage CoverageOf(ArchiveDaySpan span, DayHours hours, TimeZoneInfo zone)
{
var midnight = ArchiveClock.Midnight(span.Day);
var (hoursStart, hoursEnd) = (midnight.AddHours(hours.StartHour), midnight.AddHours(hours.EndHour));
var first = Later(ArchiveClock.WallClockOf(span.FirstUtc, zone), hoursStart);
var last = Earlier(ArchiveClock.WallClockOf(span.LastUtc, zone), hoursEnd);
var covered = last > first ? (last - first) / (hoursEnd - hoursStart) : 0.0;

return covered switch
{
< LowCoverageCeiling => DayCoverage.Low,
< MediumCoverageCeiling => DayCoverage.Medium,
_ => DayCoverage.Full
};
}

/// <summary>A cell's left edge; a pitch below one pixel puts a day in the whole pixel column it falls in.</summary>
public double CellLeft(int index)
{
return Right - CellWidth - Math.Floor((DayCount - 1 - index) * Pitch);
}

public TrackSpan Cell(int index)
{
return new TrackSpan(CellLeft(index), CellWidth);
}

/// <summary>The cell index of the day; negative before the calendar, the count or more past it.</summary>
public int IndexOf(DateOnly day)
{
return day.DayNumber - FirstDay.DayNumber;
}

/// <summary>The day whose pitch holds x, its leading gap included; null outside the cells.</summary>
public DateOnly? DayAt(double x)
{
if (x > Right)
{
return null;
}

var index = DayCount - 1 - (int)Math.Floor((Right - x) / Pitch);

return index >= 0 ? FirstDay.AddDays(index) : null;
}

/// <summary>
/// One cell per ascending span on the calendar, graded by <see cref="CoverageOf"/>; days that share a pixel
/// column share one cell of their highest grade.
/// </summary>
public IReadOnlyList<CalendarCell> Cells(IReadOnlyList<ArchiveDaySpan> spans, DayHours hours, TimeZoneInfo zone)
{
var cells = new List<CalendarCell>(spans.Count);
foreach (var span in spans)
{
var index = IndexOf(span.Day);
if (index < 0 || index >= DayCount)
{
continue;
}

var cell = new CalendarCell(CellLeft(index), CellWidth, CoverageOf(span, hours, zone));
if (cells.Count > 0 && cells[^1].Left == cell.Left)
{
var shared = cells[^1].Coverage;
cells[^1] = cell with { Coverage = shared > cell.Coverage ? shared : cell.Coverage };
}
else
{
cells.Add(cell);
}
}

return cells;
}

/// <summary>The page's cells on the calendar with the stroke around them; a week stops at the last day.</summary>
public TrackSpan? PageOutline(MinimapPage page)
{
var first = Math.Max(IndexOf(page.FirstDay), 0);
var last = Math.Min(IndexOf(page.EndDay) - 1, DayCount - 1);
if (first > last)
{
return null;
}

var left = CellLeft(first) - OutlineStroke;
var right = CellLeft(last) + CellWidth + OutlineStroke;

return new TrackSpan(left, right - left);
}

/// <summary>The count of the markers on each archive wall-clock day that holds one.</summary>
public static IReadOnlyDictionary<DateOnly, int> MarkerCountsByDay(
IReadOnlyList<DateTime> markerTimesUtc,
TimeZoneInfo zone)
{
var counts = new Dictionary<DateOnly, int>();
foreach (var markerTimeUtc in markerTimesUtc)
{
var day = ArchiveClock.DayOf(markerTimeUtc, zone);
counts[day] = counts.GetValueOrDefault(day) + 1;
}

return counts;
}

/// <summary>One cell-wide tick per calendar day that holds at least one of the markers, ascending.</summary>
public IReadOnlyList<TrackSpan> MarkerTicks(IReadOnlyList<DateTime> markerTimesUtc, TimeZoneInfo zone)
{
var ticks = new List<TrackSpan>();
foreach (var day in MarkerCountsByDay(markerTimesUtc, zone).Keys.Order())
{
var index = IndexOf(day);
if (index >= 0 && index < DayCount)
{
ticks.Add(Cell(index));
}
}

return ticks;
}

/// <summary>The x of a 1 px line before each month's first cell; none while the Mondays are named.</summary>
public IReadOnlyList<double> MonthSeparators()
{
if (ShowsMondayLabels)
{
return [];
}

var separators = new List<double>();
var month = new DateOnly(FirstDay.Year, FirstDay.Month, 1).AddMonths(1);
for (; IndexOf(month) < DayCount; month = month.AddMonths(1))
{
separators.Add(CellLeft(IndexOf(month)) - SeparatorWidth);
}

return separators;
}

private static DateTime Later(DateTime first, DateTime second)
{
return first > second ? first : second;
}

private static DateTime Earlier(DateTime first, DateTime second)
{
return first < second ? first : second;
}
}
80 changes: 80 additions & 0 deletions SemiPlot/SemiPlot.Core/Trends/DayCalendarLabels.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace SemiPlot.Core.Trends;

public enum CalendarLabelKind
{
Day,
Month
}

/// <summary>A label under the calendar at <see cref="X"/>, naming its day or that day's month by its kind.</summary>
public readonly record struct CalendarLabel(double X, DateOnly Day, CalendarLabelKind Kind);

/// <summary>A shown candidate by its index, at the left edge the row gives it.</summary>
public readonly record struct PlacedLabel(int Index, double Left);

/// <summary>
/// The label row under the day calendar (docs/architecture/trend-interaction.md#archive-overview-minimap).
/// </summary>
public static class DayCalendarLabels
{
// A month label stands clear of the separator before its first cell.
private const double MonthLabelOffset = 2.0;
private const double LabelGap = 4.0;

/// <summary>
/// The first cell's month, each month's first day, and each Monday on an archive that names its Mondays.
/// </summary>
public static IReadOnlyList<CalendarLabel> Candidates(DayCalendar calendar)
{
var labels = new List<CalendarLabel>();
for (var index = 0; index < calendar.DayCount; index++)
{
var day = calendar.FirstDay.AddDays(index);
var left = calendar.CellLeft(index);
if (index == 0 || day.Day == 1)
{
labels.Add(new CalendarLabel(left + MonthLabelOffset, day, CalendarLabelKind.Month));
}
else if (calendar.ShowsMondayLabels && day.DayOfWeek == DayOfWeek.Monday)
{
labels.Add(new CalendarLabel(left, day, CalendarLabelKind.Day));
}
}

return labels;
}

/// <summary>
/// The candidates that show, by index: the first cell's label, then each month, then each Monday, keeps its
/// place when it starts inside the row 4 px clear of every label kept before it; one that would run past the
/// row ends at its right end.
/// </summary>
public static IReadOnlyList<PlacedLabel> Fitted(
IReadOnlyList<CalendarLabel> candidates,
IReadOnlyList<double> widths,
double rowWidth)
{
var fitted = new List<PlacedLabel>();
var byPriority = Enumerable.Range(0, candidates.Count).OrderBy(index => PriorityOf(candidates, index));
foreach (var index in byPriority)
{
var left = Math.Min(candidates[index].X, rowWidth - widths[index]);
var right = left + widths[index];
var isClear = fitted.TrueForAll(kept =>
left >= kept.Left + widths[kept.Index] + LabelGap || right + LabelGap <= kept.Left);
if (left >= 0.0 && isClear)
{
fitted.Add(new PlacedLabel(index, left));
}
}

fitted.Sort((first, second) => first.Index.CompareTo(second.Index));

return fitted;
}

private static int PriorityOf(IReadOnlyList<CalendarLabel> candidates, int index)
{
return index == 0 ? 0 : candidates[index].Kind == CalendarLabelKind.Month ? 1 : 2;
}
}
Loading
Loading