using Avalonia; using Avalonia.Controls; using Avalonia.Media; using FinalToken.Core; namespace FinalToken.App; /// D12: real instances share a time axis; their burn-ups are never connected. public sealed class ResetTimeline(ResetHistory history, TimeProvider clock) : Control { private Rect plot; private DateTimeOffset from, to; private readonly List<(Rect Bounds, ResetInstance Instance)> hits = []; protected override void OnInitialized() { base.OnInitialized(); Height = 210; ToolTip.SetShowDelay(this, 0); PointerMoved += (_, e) => { var point = e.GetPosition(this); if (!plot.Contains(point) || to <= from) { ToolTip.SetTip(this, null); return; } var time = from.AddTicks((long)((to - from).Ticks * (point.X - plot.Left) / plot.Width)); var instance = hits.Where(h => h.Bounds.Contains(point)).OrderBy(h => Math.Abs(h.Bounds.Center.X - point.X)) .Select(h => h.Instance).FirstOrDefault() ?? history.Instances.FirstOrDefault(r => r.Early && r.End <= time && r.Reset >= time); ToolTip.SetTip(this, instance is null ? null : DataTooltip.Reset(instance, clock)); }; PointerExited += (_, _) => ToolTip.SetTip(this, null); } public override void Render(DrawingContext context) { base.Render(context); context.DrawRectangle(Brushes.Transparent, null, new Rect(Bounds.Size)); hits.Clear(); if (history.Instances.Count == 0) return; var dark = Ui.Dark(this); var colour = Ui.ProviderColour(history.Window.Provider, dark); var muted = Ui.Colour("Muted", dark); plot = new Rect(42, 12, Math.Max(1, Bounds.Width - 58), Math.Max(1, Bounds.Height - 48)); from = history.Instances.Min(r => r.Start); to = history.Instances.Max(r => r.Early ? r.Reset : r.End); if (to <= from) to = from.AddHours(1); double X(DateTimeOffset time) => plot.Left + (time - from).TotalSeconds / (to - from).TotalSeconds * plot.Width; double Y(double used) => plot.Bottom - used / 100 * plot.Height; foreach (var tick in new[] { 0, 25, 50, 75, 100 }) { context.DrawLine(new Pen(Ui.Colour("Grid", dark)), new(plot.Left, Y(tick)), new(plot.Right, Y(tick))); ChartText.Draw(context, tick + "%", new(0, Y(tick) - 7), muted); } using (context.PushClip(plot)) { foreach (var row in history.Instances.Where(r => r.Early)) { var band = new Rect(X(row.End), plot.Top, Math.Max(0, X(row.Reset) - X(row.End)), plot.Height); using (context.PushClip(band)) { using (context.PushOpacity(.035)) context.DrawRectangle(colour, null, band); using (context.PushOpacity(.14)) for (var x = band.Left - band.Height; x < band.Right; x += 8) context.DrawLine(new Pen(colour), new(x, band.Bottom), new(x + band.Height, band.Top)); } } foreach (var row in history.Instances.Reverse()) { var hitWidth = Math.Max(8, X(row.End) - X(row.Start)); hits.Add((new Rect((X(row.Start) + X(row.End) - hitWidth) / 2, plot.Top, hitWidth, plot.Height), row)); if (!row.Running) context.DrawLine(new Pen(Ui.Colour("Axis", dark)), new(X(row.End), plot.Top), new(X(row.End), plot.Bottom)); if (row.ReachedLimit is { } reached) { using (context.PushOpacity(.16)) context.DrawRectangle(colour, null, new Rect(X(reached), plot.Top, Math.Max(1, X(row.End) - X(reached)), 7)); context.DrawLine(new Pen(colour, 3), new(X(reached), plot.Top + 1), new(X(row.End), plot.Top + 1)); } var points = row.BurnUp.Points; for (var i = 1; i < points.Count; i++) { var a = points[i - 1]; var b = points[i]; if (i == 1 && a.Time == row.Start && row.BurnUp.Scale is null && b.Origin == UseOrigin.Measured && b.Time > row.Start) continue; var estimated = a.Origin == UseOrigin.Estimated || b.Origin == UseOrigin.Estimated; using (context.PushOpacity(estimated ? .55 : 1)) context.DrawLine(new Pen(colour, 2, estimated ? new DashStyle([4, 3], 0) : null), new(X(a.Time), Y(a.Used)), new(X(b.Time), Y(b.Used))); } } } for (var i = 0; i < 5; i++) { var time = from.AddTicks((to - from).Ticks * i / 4); ChartText.Draw(context, Display.ResetDate(time, clock), new(Math.Clamp(X(time) - 36, plot.Left, Math.Max(plot.Left, plot.Right - 86)), plot.Bottom + 12), muted); } } } public sealed class ResetHistogram(IReadOnlyList counts, Provider provider, bool weekdays) : Control { private readonly List<(Rect Bounds, int Index)> hits = []; private static readonly string[] Weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]; protected override void OnInitialized() { base.OnInitialized(); Height = 104; ToolTip.SetShowDelay(this, 0); PointerMoved += (_, e) => { var hit = hits.Where(h => h.Bounds.Contains(e.GetPosition(this))).Select(h => (int?)h.Index).FirstOrDefault(); ToolTip.SetTip(this, hit is { } index ? new DataTooltip(weekdays ? Weekdays[index] : $"{index:00}:00–{index:00}:59 local", new TooltipRow("Early resets", counts[index].ToString())) : null); }; PointerExited += (_, _) => ToolTip.SetTip(this, null); } public override void Render(DrawingContext context) { base.Render(context); context.DrawRectangle(Brushes.Transparent, null, new Rect(Bounds.Size)); hits.Clear(); if (counts.Count == 0) return; var dark = Ui.Dark(this); var colour = Ui.ProviderColour(provider, dark); var muted = Ui.Colour("Muted", dark); var plot = new Rect(26, 10, Math.Max(1, Bounds.Width - 34), Math.Max(1, Bounds.Height - 34)); var maximum = Math.Max(1, counts.Max()); var width = plot.Width / counts.Count; ChartText.Draw(context, maximum.ToString(), new(0, plot.Top - 6), muted); ChartText.Draw(context, "0", new(8, plot.Bottom - 6), muted); context.DrawLine(new Pen(Ui.Colour("Grid", dark)), plot.TopLeft, plot.TopRight); context.DrawLine(new Pen(Ui.Colour("Axis", dark)), plot.BottomLeft, plot.BottomRight); for (var i = 0; i < counts.Count; i++) { var height = plot.Height * counts[i] / maximum; var left = plot.Left + i * width; var bar = UsageChart.BarBounds(left, plot.Bottom, width, height, TopLevel.GetTopLevel(this)?.RenderScaling ?? 1); if (height > 0) context.DrawRectangle(colour, null, bar); hits.Add((new(left, plot.Top, Math.Max(8, width), plot.Height), i)); if (weekdays || i % 3 == 0) ChartText.Draw(context, weekdays ? Weekdays[i] : i.ToString("00"), new(left + width / 2 - (weekdays ? 10 : 6), plot.Bottom + 8), muted, 11); } } }