using System.Windows; using System.Windows.Input; using System.Windows.Media; using Titano.Core.Localization; namespace Titano.Ui.Visuals; /// /// Library timeline mode (BRIEF §12.2): one band per year with its label on the left and a density bar per slot /// (month) on a scale shared by all bands, slot labels underneath and a divider between bands. The current slot /// (, ) is outlined in Accent; clicking a slot makes it current /// and raises . From the keyboard, the arrow keys move a dashed focus ring from month to /// month (Left and Right run across years, Up and Down change year, Home and End go to the year's ends) with its /// readout, and Enter or Space activates the slot under it. Desired height = bands × , so the /// control scrolls inside a ScrollViewer and brings the focused band into sight. /// public sealed class TimelineBands : DrawingElement { public static readonly DependencyProperty BandsProperty = DependencyProperty.Register( nameof(Bands), typeof(IReadOnlyList), typeof(TimelineBands), new PropertyMetadata(null, OnDataPropertyChanged)); public static readonly DependencyProperty SlotLabelsProperty = DependencyProperty.Register( nameof(SlotLabels), typeof(IReadOnlyList), typeof(TimelineBands), new PropertyMetadata(null, OnDataPropertyChanged)); public static readonly DependencyProperty BrushKeyProperty = DependencyProperty.Register( nameof(BrushKey), typeof(string), typeof(TimelineBands), new FrameworkPropertyMetadata("Brush.Accent", FrameworkPropertyMetadataOptions.AffectsRender)); public static readonly DependencyProperty BandHeightProperty = DependencyProperty.Register( nameof(BandHeight), typeof(double), typeof(TimelineBands), new FrameworkPropertyMetadata(64.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), v => v is double d && d >= 24); public static readonly DependencyProperty CurrentBandProperty = DependencyProperty.Register( nameof(CurrentBand), typeof(int), typeof(TimelineBands), new FrameworkPropertyMetadata(-1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender)); public static readonly DependencyProperty CurrentSlotProperty = DependencyProperty.Register( nameof(CurrentSlot), typeof(int), typeof(TimelineBands), new FrameworkPropertyMetadata(-1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.AffectsRender)); public static readonly DependencyProperty ValueFormatterProperty = DependencyProperty.Register( nameof(ValueFormatter), typeof(Func), typeof(TimelineBands), new PropertyMetadata(null)); public static readonly RoutedEvent SlotActivatedEvent = EventManager.RegisterRoutedEvent( nameof(SlotActivated), RoutingStrategy.Bubble, typeof(ChartItemEventHandler), typeof(TimelineBands)); private const double LabelColumn = 64; private const double LabelSize = 11; private (int Band, int Slot) _hover = (-1, -1); /// The slot the keyboard is on; (−1, −1) until the control first takes keyboard focus. private (int Band, int Slot) _keyboard = (-1, -1); static TimelineBands() { FocusableProperty.OverrideMetadata(typeof(TimelineBands), new FrameworkPropertyMetadata(true)); // The focused slot is outlined on the drawing itself. FocusVisualStyleProperty.OverrideMetadata(typeof(TimelineBands), new FrameworkPropertyMetadata(null)); } /// One band per year: label + one value per slot. public IReadOnlyList? Bands { get => (IReadOnlyList?)GetValue(BandsProperty); set => SetValue(BandsProperty, value); } /// Slot captions (culture-aware month initials); also fixes the slot count. public IReadOnlyList? SlotLabels { get => (IReadOnlyList?)GetValue(SlotLabelsProperty); set => SetValue(SlotLabelsProperty, value); } public string BrushKey { get => (string)GetValue(BrushKeyProperty); set => SetValue(BrushKeyProperty, value); } public double BandHeight { get => (double)GetValue(BandHeightProperty); set => SetValue(BandHeightProperty, value); } /// Band of the current slot (two-way); −1 = none. public int CurrentBand { get => (int)GetValue(CurrentBandProperty); set => SetValue(CurrentBandProperty, value); } /// Current slot inside (two-way); −1 = none. public int CurrentSlot { get => (int)GetValue(CurrentSlotProperty); set => SetValue(CurrentSlotProperty, value); } /// Culture-aware value formatting for readouts. public Func? ValueFormatter { get => (Func?)GetValue(ValueFormatterProperty); set => SetValue(ValueFormatterProperty, value); } public event ChartItemEventHandler SlotActivated { add => AddHandler(SlotActivatedEvent, value); remove => RemoveHandler(SlotActivatedEvent, value); } private int SlotCount { get { if (SlotLabels is { Count: > 0 } labels) return labels.Count; var max = 0; if (Bands is { } bands) for (var b = 0; b < bands.Count; b++) max = Math.Max(max, bands[b].Values.Count); return max; } } private double SlotPitch => SlotCount == 0 ? 0 : Math.Max(0, ActualWidth - LabelColumn) / SlotCount; protected override Size MeasureOverride(Size availableSize) => new(0, (Bands?.Count ?? 0) * BandHeight); protected override void OnDataChanged() { _hover = (-1, -1); if (_keyboard.Band >= (Bands?.Count ?? 0) || _keyboard.Slot >= SlotCount) _keyboard = (-1, -1); base.OnDataChanged(); } protected override void OnRender(DrawingContext dc) { DrawHitArea(dc); if (Bands is not { Count: > 0 } bands || SlotCount == 0) return; var slots = SlotCount; var pitch = SlotPitch; var scale = PixelScale; var max = 0.0; for (var b = 0; b < bands.Count; b++) for (var s = 0; s < bands[b].Values.Count; s++) if (double.IsFinite(bands[b].Values[s])) max = Math.Max(max, bands[b].Values[s]); if (max <= 0) max = 1; var text = GetBrush("Brush.Text"); var muted = GetBrush("Brush.TextMuted"); var fill = GetBrush(BrushKey); var divider = GetHairline("Brush.Stroke"); var captionHeight = Math.Ceiling(CreateText("0", LabelSize, muted).Height); var barWidth = Math.Max(1, Math.Min(pitch * 0.6, 28)); for (var b = 0; b < bands.Count; b++) { var band = bands[b]; var top = b * BandHeight; var baseline = top + BandHeight - captionHeight - 8; var barSpace = baseline - top - 8; var title = CreateLabel(band.Label, 13, text, semiBold: true); DrawTextAt(dc, title, new Point(0, top + (BandHeight - title.Height) / 2)); for (var s = 0; s < slots; s++) { var cx = LabelColumn + pitch * (s + 0.5); var value = s < band.Values.Count ? band.Values[s] : double.NaN; if (double.IsFinite(value) && value > 0) { var h = Math.Max(1, barSpace * Math.Clamp(value / max, 0, 1)); dc.DrawRoundedRectangle(fill, null, new Rect(cx - barWidth / 2, baseline - h, barWidth, h), 2, 2); } if (SlotLabels is { } labels && s < labels.Count) { var caption = CreateLabel(labels[s], LabelSize, muted); DrawTextAt(dc, caption, new Point(cx - caption.Width / 2, baseline + 4)); } if (b == CurrentBand && s == CurrentSlot) dc.DrawRoundedRectangle(null, GetPen("Brush.Accent", 1.5), new Rect(cx - pitch / 2 + 2, top + 4, pitch - 4, BandHeight - 8), 4, 4); if (IsKeyboardFocused && b == _keyboard.Band && s == _keyboard.Slot) dc.DrawRoundedRectangle(null, GetPen("Brush.Accent", 2, LineDash.Dashed), new Rect(cx - pitch / 2 + 0.5, top + 1, pitch - 1, BandHeight - 2), 5, 5); } if (b < bands.Count - 1) { var y = PixelGrid.SnapLine(top + BandHeight, divider.Thickness, scale); dc.DrawLine(divider, new Point(0, y), new Point(ActualWidth, y)); } } } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); var p = e.GetPosition(this); _hover = HitTest(p); Readout.Show(_hover.Band >= 0 ? ReadoutText(_hover.Band, _hover.Slot) : null, p); } protected override void OnMouseLeave(MouseEventArgs e) { base.OnMouseLeave(e); _hover = (-1, -1); Readout.Hide(); } protected override void OnQueryCursor(QueryCursorEventArgs e) { base.OnQueryCursor(e); if (e.Handled || _hover.Band < 0) return; e.Cursor = Cursors.Hand; e.Handled = true; } protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) { base.OnMouseLeftButtonUp(e); var (band, slot) = HitTest(e.GetPosition(this)); if (band < 0) return; _keyboard = (band, slot); Activate(band, slot); e.Handled = true; } protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e) { base.OnGotKeyboardFocus(e); if (_keyboard.Band < 0 && Bands is { Count: > 0 } && SlotCount > 0) _keyboard = CurrentBand >= 0 && CurrentSlot >= 0 ? (CurrentBand, CurrentSlot) : (0, 0); InvalidateVisual(); } protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e) { base.OnLostKeyboardFocus(e); Readout.Hide(); InvalidateVisual(); } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); var bands = Bands?.Count ?? 0; var slots = SlotCount; if (e.Handled || bands == 0 || slots == 0) return; var (band, slot) = _keyboard.Band >= 0 ? _keyboard : (0, 0); switch (e.Key) { case Key.Enter or Key.Space: Activate(band, slot); e.Handled = true; return; // Bands run newest first: Left goes back in time, into the year below after January. case Key.Left: (band, slot) = slot > 0 ? (band, slot - 1) : band < bands - 1 ? (band + 1, slots - 1) : (band, slot); break; case Key.Right: (band, slot) = slot < slots - 1 ? (band, slot + 1) : band > 0 ? (band - 1, 0) : (band, slot); break; case Key.Up: band = Math.Max(0, band - 1); break; case Key.Down: band = Math.Min(bands - 1, band + 1); break; case Key.Home: slot = 0; break; case Key.End: slot = slots - 1; break; default: return; } _keyboard = (band, slot); var pitch = SlotPitch; var top = band * BandHeight; BringIntoView(new Rect(0, top, ActualWidth, BandHeight)); Readout.Show(ReadoutText(band, slot), new Point(LabelColumn + pitch * (slot + 0.5), top + BandHeight / 2)); InvalidateVisual(); e.Handled = true; } private void Activate(int band, int slot) { SetCurrentValue(CurrentBandProperty, band); SetCurrentValue(CurrentSlotProperty, slot); RaiseEvent(new ChartItemEventArgs(SlotActivatedEvent, band, slot)); } private (int Band, int Slot) HitTest(Point p) { var count = Bands?.Count ?? 0; var pitch = SlotPitch; if (count == 0 || pitch <= 0 || p.X < LabelColumn) return (-1, -1); var band = (int)Math.Floor(p.Y / BandHeight); var slot = (int)Math.Floor((p.X - LabelColumn) / pitch); return band >= 0 && band < count && slot >= 0 && slot < SlotCount ? (band, slot) : (-1, -1); } private string ReadoutText(int band, int slot) { var row = Bands![band]; if (row.ToolTips is { } tips && slot < tips.Count && !string.IsNullOrEmpty(tips[slot])) return tips[slot]!; var value = slot < row.Values.Count ? row.Values[slot] : double.NaN; var caption = SlotLabels is { } labels && slot < labels.Count ? " " + labels[slot] : string.Empty; var format = ValueFormatter ?? (v => v.ToString("N0", Loc.Culture)); return row.Label + caption + (double.IsFinite(value) ? Loc.Separator + format(value) : string.Empty); } }