feat(ui-visuals): add SequenceCard with asynchronous hover scrubbing

Card control for the Library (16:9 thumbnail, badge, selection box, title,
secondary line, status glyphs) whose thumbnail scrubs through the sequence
from an IThumbnailSource, cancelling stale requests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-02 10:26:50 +00:00
1 parent 0e2f9fd4f4
commit 0ac73793c7
3 files changed
+238

No files matched your search

@@ -0,0 +1,36 @@
using System.Windows;
using System.Windows.Controls;
namespace Titano.Ui.Visuals;
/// <summary>Sizes its child to the available width and a height of width ÷ <see cref="Ratio"/> (card thumbnails are 16:9).</summary>
public sealed class AspectRatioDecorator : Decorator
{
public static readonly DependencyProperty RatioProperty = DependencyProperty.Register(
nameof(Ratio), typeof(double), typeof(AspectRatioDecorator),
new FrameworkPropertyMetadata(16.0 / 9.0, FrameworkPropertyMetadataOptions.AffectsMeasure),
v => v is double d && double.IsFinite(d) && d > 0);
/// <summary>Width ÷ height.</summary>
public double Ratio { get => (double)GetValue(RatioProperty); set => SetValue(RatioProperty, value); }
protected override Size MeasureOverride(Size constraint)
{
var size = Fit(constraint);
Child?.Measure(size);
return size;
}
protected override Size ArrangeOverride(Size arrangeSize)
{
var size = Fit(arrangeSize);
Child?.Arrange(new Rect(size));
return size;
}
private Size Fit(Size available)
{
var width = double.IsInfinity(available.Width) ? 0 : available.Width;
return new Size(width, width / Ratio);
}
}
+17
View File
@@ -0,0 +1,17 @@
using System.Windows.Media;
namespace Titano.Ui.Visuals;
/// <summary>
/// Frames of a sequence as decoded thumbnails, loaded off the UI thread (filmstrip cells, card hover scrubbing).
/// Implementations return frozen images (or images created on the UI thread) and honour cancellation: a request
/// is cancelled as soon as the control no longer needs it.
/// </summary>
public interface IThumbnailSource
{
/// <summary>Number of frames.</summary>
int Count { get; }
/// <summary>Thumbnail of frame <paramref name="index"/> (0…Count−1) at least <paramref name="pixelWidth"/> device pixels wide; null when unavailable.</summary>
ValueTask<ImageSource?> GetAsync(int index, int pixelWidth, CancellationToken cancellationToken);
}
+185
View File
@@ -0,0 +1,185 @@
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using Titano.Core.Diagnostics;
using Titano.Ui.Controls;
namespace Titano.Ui.Visuals;
/// <summary>One entry of a card's status row: glyph in the status colour, optional text (a count) and tooltip.</summary>
public sealed record CardStatus(StatusKind Kind, string? Text = null, string? ToolTip = null);
/// <summary>
/// Library card (DESIGN §4 Cards): 236 px wide, 16:9 thumbnail with a count badge top-right and a selection check
/// box top-left (shown on hover or when selected), title, secondary line and status glyph row. Selected = accent
/// border with an AccentSelected glow. Moving the mouse across the thumbnail scrubs through the sequence: the
/// horizontal position picks a frame of <see cref="ScrubSource"/>, which is loaded asynchronously; stale requests
/// are cancelled and the last frame stays until the next one arrives.
/// </summary>
[TemplatePart(Name = PartThumbnail, Type = typeof(FrameworkElement))]
public class SequenceCard : Control
{
public const string PartThumbnail = "PART_Thumbnail";
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(SequenceCard));
public static readonly DependencyProperty SubtitleProperty = DependencyProperty.Register(nameof(Subtitle), typeof(string), typeof(SequenceCard));
public static readonly DependencyProperty BadgeProperty = DependencyProperty.Register(nameof(Badge), typeof(string), typeof(SequenceCard));
public static readonly DependencyProperty StatusesProperty = DependencyProperty.Register(nameof(Statuses), typeof(IEnumerable), typeof(SequenceCard));
public static readonly DependencyProperty SelectToolTipProperty = DependencyProperty.Register(nameof(SelectToolTip), typeof(object), typeof(SequenceCard));
public static readonly DependencyProperty ActionsContentProperty = DependencyProperty.Register(nameof(ActionsContent), typeof(object), typeof(SequenceCard));
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register(
nameof(IsSelected), typeof(bool), typeof(SequenceCard),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static readonly DependencyProperty ThumbnailProperty = DependencyProperty.Register(
nameof(Thumbnail), typeof(ImageSource), typeof(SequenceCard), new PropertyMetadata(null, OnThumbnailChanged));
public static readonly DependencyProperty ScrubSourceProperty = DependencyProperty.Register(
nameof(ScrubSource), typeof(IThumbnailSource), typeof(SequenceCard), new PropertyMetadata(null, OnScrubSourceChanged));
private static readonly DependencyPropertyKey DisplayedThumbnailKey = DependencyProperty.RegisterReadOnly(
nameof(DisplayedThumbnail), typeof(ImageSource), typeof(SequenceCard), new PropertyMetadata(null));
private static readonly DependencyPropertyKey IsScrubbingKey = DependencyProperty.RegisterReadOnly(
nameof(IsScrubbing), typeof(bool), typeof(SequenceCard), new PropertyMetadata(false));
private static readonly DependencyPropertyKey ScrubPositionKey = DependencyProperty.RegisterReadOnly(
nameof(ScrubPosition), typeof(double), typeof(SequenceCard), new PropertyMetadata(0.0));
public static readonly DependencyProperty DisplayedThumbnailProperty = DisplayedThumbnailKey.DependencyProperty;
public static readonly DependencyProperty IsScrubbingProperty = IsScrubbingKey.DependencyProperty;
public static readonly DependencyProperty ScrubPositionProperty = ScrubPositionKey.DependencyProperty;
private FrameworkElement? _thumbnailPart;
private CancellationTokenSource? _scrubCancellation;
private int _scrubIndex = -1;
static SequenceCard()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SequenceCard), new FrameworkPropertyMetadata(typeof(SequenceCard)));
}
public SequenceCard()
{
DataContextChanged += (_, _) => StopScrub();
}
/// <summary>User title or "{date} {camera}".</summary>
public string? Title { get => (string?)GetValue(TitleProperty); set => SetValue(TitleProperty, value); }
/// <summary>Secondary line: count · camera · span.</summary>
public string? Subtitle { get => (string?)GetValue(SubtitleProperty); set => SetValue(SubtitleProperty, value); }
/// <summary>Count badge text (top-right of the thumbnail); hidden when empty.</summary>
public string? Badge { get => (string?)GetValue(BadgeProperty); set => SetValue(BadgeProperty, value); }
/// <summary><see cref="CardStatus"/> entries of the status row.</summary>
public IEnumerable? Statuses { get => (IEnumerable?)GetValue(StatusesProperty); set => SetValue(StatusesProperty, value); }
/// <summary>Tooltip of the selection check box.</summary>
public object? SelectToolTip { get => GetValue(SelectToolTipProperty); set => SetValue(SelectToolTipProperty, value); }
/// <summary>Quick actions shown bottom-right of the thumbnail on hover (Open, Analyse, Render, Sync).</summary>
public object? ActionsContent { get => GetValue(ActionsContentProperty); set => SetValue(ActionsContentProperty, value); }
/// <summary>Selection state; bind two-way to the container's IsSelected so the check box toggles it.</summary>
public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); }
/// <summary>Hero thumbnail; may arrive later (asynchronous loading), a placeholder shows until then.</summary>
public ImageSource? Thumbnail { get => (ImageSource?)GetValue(ThumbnailProperty); set => SetValue(ThumbnailProperty, value); }
/// <summary>Frames for hover scrubbing; null disables scrubbing.</summary>
public IThumbnailSource? ScrubSource { get => (IThumbnailSource?)GetValue(ScrubSourceProperty); set => SetValue(ScrubSourceProperty, value); }
/// <summary>Image on screen: the scrubbed frame while scrubbing, otherwise <see cref="Thumbnail"/>.</summary>
public ImageSource? DisplayedThumbnail { get => (ImageSource?)GetValue(DisplayedThumbnailProperty); private set => SetValue(DisplayedThumbnailKey, value); }
/// <summary>True while the mouse scrubs over the thumbnail.</summary>
public bool IsScrubbing { get => (bool)GetValue(IsScrubbingProperty); private set => SetValue(IsScrubbingKey, value); }
/// <summary>Scrubbed position 0…1 along the sequence (drives the progress hairline).</summary>
public double ScrubPosition { get => (double)GetValue(ScrubPositionProperty); private set => SetValue(ScrubPositionKey, value); }
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_thumbnailPart = GetTemplateChild(PartThumbnail) as FrameworkElement;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (ScrubSource is not { Count: > 0 } source || _thumbnailPart is not { ActualWidth: > 0 } part)
{
StopScrub();
return;
}
var p = e.GetPosition(part);
if (p.X < 0 || p.Y < 0 || p.X >= part.ActualWidth || p.Y >= part.ActualHeight)
{
StopScrub();
return;
}
var count = source.Count;
var index = Math.Clamp((int)(p.X / part.ActualWidth * count), 0, count - 1);
IsScrubbing = true;
ScrubPosition = (index + 1.0) / count;
if (index == _scrubIndex) return;
_scrubIndex = index;
var pixelWidth = (int)Math.Ceiling(part.ActualWidth * VisualTreeHelper.GetDpi(this).DpiScaleX);
_ = LoadScrubFrameAsync(source, index, pixelWidth);
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
StopScrub();
}
private static void OnThumbnailChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var card = (SequenceCard)d;
if (!card.IsScrubbing) card.DisplayedThumbnail = (ImageSource?)e.NewValue;
}
private static void OnScrubSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((SequenceCard)d).StopScrub();
private async Task LoadScrubFrameAsync(IThumbnailSource source, int index, int pixelWidth)
{
_scrubCancellation?.Cancel();
var cancellation = new CancellationTokenSource();
_scrubCancellation = cancellation;
try
{
// Continues on the UI thread: the result is applied only if this is still the frame under the mouse.
var image = await source.GetAsync(index, pixelWidth, cancellation.Token);
if (!cancellation.IsCancellationRequested && index == _scrubIndex && image is not null) DisplayedThumbnail = image;
}
catch (OperationCanceledException)
{
}
catch (Exception ex)
{
Log.Warning("ui.card", $"Scrub frame {index} failed", ex);
}
finally
{
if (ReferenceEquals(_scrubCancellation, cancellation)) _scrubCancellation = null;
cancellation.Dispose();
}
}
private void StopScrub()
{
_scrubCancellation?.Cancel();
_scrubCancellation = null;
_scrubIndex = -1;
if (!IsScrubbing) return;
IsScrubbing = false;
ScrubPosition = 0;
DisplayedThumbnail = Thumbnail;
}
}