fix(ui): kit follow-ups for lists, chart contrast, activation and RTL bars
- ListBox (and the Library card grid) get a theme template that keeps their fill when disabled; ListView keeps the system GridView template. - BarChart, HeatmapStrip, SeriesChart and TimelineBands draw their labels in TextMuted; heatmap cells without data take the Stroke tone. - Category charts show the hand cursor and take the keyboard (arrows, Home, End, Enter, Space) only while an activation handler is attached to them; the keyboard item is outlined, read out and exposed as ItemStatus. - Horizontal bar charts and stacked bars start on the reading side in right-to-left languages; vertical bars, heatmaps and time axes stay LTR. - Archive drops its local list template, Focus.Inset copy, chart brush aliases and the redundant FlowDirection on the template box. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
10 files changed
+350
-197
No files matched your search
@@ -8,10 +8,11 @@ namespace Titano.Ui.Visuals;
|
||||
|
||||
/// <summary>
|
||||
/// Bars for labelled values (Archive: storage by year, by camera, by format; uploads per day). Horizontal bars put
|
||||
/// the category labels on the left and the value labels after each bar, as in the Archive mockup; vertical bars
|
||||
/// put the categories underneath (skipping labels that would collide). Optional value gridlines use nice ticks.
|
||||
/// A bar's opacity can follow its value (<see cref="MinimumOpacity"/>). Hovering reads out the bar; clicking raises
|
||||
/// <see cref="ItemActivatedEvent"/>.
|
||||
/// the category labels on the reading side and the value labels after each bar, as in the Archive mockup, and
|
||||
/// mirror in a right-to-left language; vertical bars put the categories underneath (skipping labels that would
|
||||
/// collide) and keep them left to right. Optional value gridlines use nice ticks. A bar's opacity can follow its
|
||||
/// value (<see cref="MinimumOpacity"/>). Hovering reads out the bar; clicking raises <see cref="ItemActivatedEvent"/>,
|
||||
/// and a handler attached to the chart also gives it the hand cursor and the keyboard.
|
||||
/// </summary>
|
||||
public sealed class BarChart : DrawingElement
|
||||
{
|
||||
@@ -52,7 +53,6 @@ public sealed class BarChart : DrawingElement
|
||||
|
||||
private Rect _area;
|
||||
private double _pitch;
|
||||
private int _hover = -1;
|
||||
|
||||
/// <summary>Bars in display order.</summary>
|
||||
public IReadOnlyList<ChartItem>? Items { get => (IReadOnlyList<ChartItem>?)GetValue(ItemsProperty); set => SetValue(ItemsProperty, value); }
|
||||
@@ -79,27 +79,32 @@ public sealed class BarChart : DrawingElement
|
||||
/// <summary>Culture-aware value formatting for value labels, ticks and readouts.</summary>
|
||||
public Func<double, string>? ValueFormatter { get => (Func<double, string>?)GetValue(ValueFormatterProperty); set => SetValue(ValueFormatterProperty, value); }
|
||||
|
||||
/// <summary>Raised when a bar is clicked, or chosen with the keyboard and Enter; attach it to the chart itself to make the bars interactive.</summary>
|
||||
public event ChartItemEventHandler ItemActivated
|
||||
{
|
||||
add => AddHandler(ItemActivatedEvent, value);
|
||||
remove => RemoveHandler(ItemActivatedEvent, value);
|
||||
add
|
||||
{
|
||||
AddHandler(ItemActivatedEvent, value);
|
||||
TrackActivation(value, added: true);
|
||||
}
|
||||
remove
|
||||
{
|
||||
RemoveHandler(ItemActivatedEvent, value);
|
||||
TrackActivation(value, added: false);
|
||||
}
|
||||
}
|
||||
|
||||
protected internal override string? AutomationDescription =>
|
||||
Items is { Count: > 0 } items ? string.Join(Environment.NewLine, items.Select(ReadoutText)) : null;
|
||||
|
||||
protected override int ItemCount => Items?.Count ?? 0;
|
||||
|
||||
protected override Size MeasureOverride(Size availableSize)
|
||||
{
|
||||
var count = Items?.Count ?? 0;
|
||||
return Orientation == Orientation.Horizontal ? new Size(0, count * (BarThickness + RowGap)) : new Size(0, 120);
|
||||
}
|
||||
|
||||
protected override void OnDataChanged()
|
||||
{
|
||||
_hover = -1;
|
||||
base.OnDataChanged();
|
||||
}
|
||||
|
||||
protected override void OnRender(DrawingContext dc)
|
||||
{
|
||||
DrawHitArea(dc);
|
||||
@@ -108,39 +113,40 @@ public sealed class BarChart : DrawingElement
|
||||
var max = ScaleMaximum(items);
|
||||
if (Orientation == Orientation.Horizontal) RenderHorizontal(dc, items, max);
|
||||
else RenderVertical(dc, items, max);
|
||||
DrawKeyboardItem(dc);
|
||||
}
|
||||
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
protected override int ItemAt(Point point)
|
||||
{
|
||||
base.OnMouseMove(e);
|
||||
var p = e.GetPosition(this);
|
||||
_hover = HitTest(p);
|
||||
Readout.Show(_hover >= 0 && Items is { } items ? ReadoutText(items[_hover]) : null, p);
|
||||
var count = ItemCount;
|
||||
if (count == 0 || _area.IsEmpty || !(_pitch > 0)) return -1;
|
||||
var index = Orientation == Orientation.Horizontal
|
||||
? (int)Math.Floor((point.Y - _area.Top) / _pitch)
|
||||
: (int)Math.Floor((point.X - _area.Left) / _pitch);
|
||||
var inside = Orientation == Orientation.Horizontal ? point.X >= 0 && point.X <= ActualWidth : point.Y >= 0 && point.Y <= ActualHeight;
|
||||
return inside && index >= 0 && index < count ? index : -1;
|
||||
}
|
||||
|
||||
protected override void OnQueryCursor(QueryCursorEventArgs e)
|
||||
/// <summary>A horizontal bar's whole row (label, bar and value), a vertical bar's whole column.</summary>
|
||||
protected override Rect ItemBounds(int item)
|
||||
{
|
||||
base.OnQueryCursor(e);
|
||||
if (e.Handled || _hover < 0) return;
|
||||
e.Cursor = Cursors.Hand;
|
||||
e.Handled = true;
|
||||
if (_area.IsEmpty || !(_pitch > 0)) return Rect.Empty;
|
||||
return Orientation == Orientation.Horizontal
|
||||
? new Rect(1, _area.Top + _pitch * item + 1, Math.Max(0, ActualWidth - 2), Math.Max(0, _pitch - 2))
|
||||
: new Rect(_area.Left + _pitch * item + 1, 1, Math.Max(0, _pitch - 2), Math.Max(0, ActualHeight - 2));
|
||||
}
|
||||
|
||||
protected override void OnMouseLeave(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseLeave(e);
|
||||
_hover = -1;
|
||||
Readout.Hide();
|
||||
}
|
||||
protected override string? ItemText(int item) => Items is { } items && item < items.Count ? ReadoutText(items[item]) : null;
|
||||
|
||||
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
|
||||
/// <summary>Up/Down move along horizontal bars, Left/Right along vertical ones (left to right in every language).</summary>
|
||||
protected override int Navigate(int item, Key key) => (Orientation, key) switch
|
||||
{
|
||||
base.OnMouseLeftButtonUp(e);
|
||||
var index = HitTest(e.GetPosition(this));
|
||||
if (index < 0) return;
|
||||
RaiseEvent(new ChartItemEventArgs(ItemActivatedEvent, index, -1));
|
||||
e.Handled = true;
|
||||
}
|
||||
(Orientation.Horizontal, Key.Up) or (Orientation.Vertical, Key.Left) => Math.Max(0, item - 1),
|
||||
(Orientation.Horizontal, Key.Down) or (Orientation.Vertical, Key.Right) => item + 1,
|
||||
_ => -1,
|
||||
};
|
||||
|
||||
protected override void Activate(int item) => RaiseEvent(new ChartItemEventArgs(ItemActivatedEvent, item, -1));
|
||||
|
||||
private string Format(double value) => (ValueFormatter ?? DefaultFormat)(value);
|
||||
|
||||
@@ -160,24 +166,29 @@ public sealed class BarChart : DrawingElement
|
||||
private double OpacityOf(double value, double max) =>
|
||||
MinimumOpacity >= 1 ? 1 : MinimumOpacity + (1 - MinimumOpacity) * Math.Clamp(value / max, 0, 1);
|
||||
|
||||
/// <summary>
|
||||
/// Laid out from the left and mirrored in a right-to-left language (<see cref="DrawingElement.FromStart"/>): labels on
|
||||
/// the reading side, bars growing away from it, each value after its bar's end.
|
||||
/// </summary>
|
||||
private void RenderHorizontal(DrawingContext dc, IReadOnlyList<ChartItem> items, double max)
|
||||
{
|
||||
var faint = GetBrush("Brush.TextFaint");
|
||||
var muted = GetBrush("Brush.TextMuted");
|
||||
var text = GetBrush("Brush.Text");
|
||||
var labels = new FormattedText[items.Count];
|
||||
var values = new FormattedText?[items.Count];
|
||||
double labelWidth = 0, valueWidth = 0;
|
||||
for (var i = 0; i < items.Count; i++)
|
||||
{
|
||||
labels[i] = CreateLabel(items[i].Label, LabelSize, faint);
|
||||
labels[i] = CreateLabel(items[i].Label, LabelSize, muted);
|
||||
labelWidth = Math.Max(labelWidth, labels[i].Width);
|
||||
if (!ShowValues) continue;
|
||||
values[i] = CreateLabel(items[i].ValueText ?? Format(items[i].Value), LabelSize, text);
|
||||
valueWidth = Math.Max(valueWidth, values[i]!.Width);
|
||||
}
|
||||
var width = ActualWidth;
|
||||
var gridHeight = ShowGrid ? 16 : 0;
|
||||
var left = Math.Ceiling(labelWidth) + 8;
|
||||
var right = ActualWidth - (ShowValues ? valueWidth + 6 : 0);
|
||||
var right = width - (ShowValues ? valueWidth + 6 : 0);
|
||||
_area = new Rect(left, 0, Math.Max(0, right - left), Math.Max(0, ActualHeight - gridHeight));
|
||||
_pitch = Math.Min(_area.Height / items.Count, BarThickness + RowGap);
|
||||
var thickness = Math.Max(1, Math.Min(BarThickness, _pitch - 2));
|
||||
@@ -187,28 +198,29 @@ public sealed class BarChart : DrawingElement
|
||||
{
|
||||
var item = items[i];
|
||||
var cy = _area.Top + _pitch * (i + 0.5);
|
||||
DrawTextAt(dc, labels[i], new Point(left - 8 - labels[i].Width, cy - labels[i].Height / 2));
|
||||
DrawTextAt(dc, labels[i], new Point(FromStart(left - 8 - labels[i].Width, labels[i].Width, width), cy - labels[i].Height / 2));
|
||||
var length = _area.Width * Math.Clamp(item.Value / max, 0, 1);
|
||||
if (length > 0)
|
||||
{
|
||||
dc.PushOpacity(OpacityOf(item.Value, max));
|
||||
dc.DrawRoundedRectangle(GetBrush(item.BrushKey ?? BrushKey), null, new Rect(left, cy - thickness / 2, length, thickness), 2, 2);
|
||||
dc.DrawRoundedRectangle(GetBrush(item.BrushKey ?? BrushKey), null, new Rect(FromStart(left, length, width), cy - thickness / 2, length, thickness), 2, 2);
|
||||
dc.Pop();
|
||||
}
|
||||
if (values[i] is { } value) DrawTextAt(dc, value, new Point(left + length + 6, cy - value.Height / 2));
|
||||
if (values[i] is { } value) DrawTextAt(dc, value, new Point(FromStart(left + length + 6, value.Width, width), cy - value.Height / 2));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Categories run left to right in every language: vertical bars often stand for days or years.</summary>
|
||||
private void RenderVertical(DrawingContext dc, IReadOnlyList<ChartItem> items, double max)
|
||||
{
|
||||
var faint = GetBrush("Brush.TextFaint");
|
||||
var sample = CreateText("0", LabelSize, faint);
|
||||
var muted = GetBrush("Brush.TextMuted");
|
||||
var sample = CreateText("0", LabelSize, muted);
|
||||
var labelHeight = Math.Ceiling(sample.Height) + 4;
|
||||
var valueHeight = ShowValues ? labelHeight : 0;
|
||||
var axisWidth = 0.0;
|
||||
if (ShowGrid)
|
||||
{
|
||||
var top = CreateText(Format(max), LabelSize, faint);
|
||||
var top = CreateText(Format(max), LabelSize, muted);
|
||||
axisWidth = Math.Ceiling(top.Width) + 8;
|
||||
}
|
||||
_area = new Rect(axisWidth, valueHeight, Math.Max(0, ActualWidth - axisWidth), Math.Max(0, ActualHeight - labelHeight - valueHeight));
|
||||
@@ -234,7 +246,7 @@ public sealed class BarChart : DrawingElement
|
||||
var value = CreateLabel(item.ValueText ?? Format(item.Value), LabelSize, text);
|
||||
if (value.Width <= _pitch) DrawTextAt(dc, value, new Point(cx - value.Width / 2, _area.Bottom - height - value.Height - 2));
|
||||
}
|
||||
var label = CreateLabel(item.Label, LabelSize, faint);
|
||||
var label = CreateLabel(item.Label, LabelSize, muted);
|
||||
var x = cx - label.Width / 2;
|
||||
if (x < lastLabelRight + 6) continue;
|
||||
DrawTextAt(dc, label, new Point(x, _area.Bottom + 4));
|
||||
@@ -242,21 +254,25 @@ public sealed class BarChart : DrawingElement
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Gridlines at nice ticks along the value axis, with labels below (horizontal bars) or left (vertical bars).</summary>
|
||||
/// <summary>
|
||||
/// Gridlines at nice ticks along the value axis, with labels below (horizontal bars, mirrored with them) or left
|
||||
/// (vertical bars).
|
||||
/// </summary>
|
||||
private void DrawValueGrid(DrawingContext dc, double max, bool horizontal)
|
||||
{
|
||||
var ticks = NiceScale.Inner(0, max, horizontal ? Math.Max(2, (int)(_area.Width / 80)) : Math.Max(2, (int)(_area.Height / 36)));
|
||||
var pen = GetHairline("Brush.Stroke");
|
||||
var faint = GetBrush("Brush.TextFaint");
|
||||
var muted = GetBrush("Brush.TextMuted");
|
||||
var scale = PixelScale;
|
||||
var width = ActualWidth;
|
||||
for (var i = 0; i < ticks.Count; i++)
|
||||
{
|
||||
var label = CreateText(Format(ticks[i]), LabelSize, faint);
|
||||
var label = CreateText(Format(ticks[i]), LabelSize, muted);
|
||||
if (horizontal)
|
||||
{
|
||||
var x = PixelGrid.SnapLine(_area.Left + _area.Width * ticks[i] / max, pen.Thickness, scale);
|
||||
var x = PixelGrid.SnapLine(FromStart(_area.Left + _area.Width * ticks[i] / max, 0, width), pen.Thickness, scale);
|
||||
dc.DrawLine(pen, new Point(x, _area.Top), new Point(x, _area.Bottom));
|
||||
dc.DrawText(label, new Point(Math.Min(x - label.Width / 2, ActualWidth - label.Width), _area.Bottom + 2));
|
||||
dc.DrawText(label, new Point(Math.Clamp(x - label.Width / 2, 0, Math.Max(0, width - label.Width)), _area.Bottom + 2));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -266,15 +282,4 @@ public sealed class BarChart : DrawingElement
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private int HitTest(Point p)
|
||||
{
|
||||
var count = Items?.Count ?? 0;
|
||||
if (count == 0 || _area.IsEmpty || !(_pitch > 0)) return -1;
|
||||
var index = Orientation == Orientation.Horizontal
|
||||
? (int)Math.Floor((p.Y - _area.Top) / _pitch)
|
||||
: (int)Math.Floor((p.X - _area.Left) / _pitch);
|
||||
var inside = Orientation == Orientation.Horizontal ? p.X >= 0 && p.X <= ActualWidth : p.Y >= 0 && p.Y <= ActualHeight;
|
||||
return inside && index >= 0 && index < count ? index : -1;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user