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
186 lines
9.3 KiB
C#
186 lines
9.3 KiB
C#
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;
|
|
}
|
|
}
|