diff --git a/src/Titano.Ui/Visuals/AspectRatioDecorator.cs b/src/Titano.Ui/Visuals/AspectRatioDecorator.cs new file mode 100644 index 00000000..97023853 --- /dev/null +++ b/src/Titano.Ui/Visuals/AspectRatioDecorator.cs @@ -0,0 +1,36 @@ +using System.Windows; +using System.Windows.Controls; + +namespace Titano.Ui.Visuals; + +/// Sizes its child to the available width and a height of width ÷ (card thumbnails are 16:9). +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); + + /// Width ÷ height. + 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); + } +} diff --git a/src/Titano.Ui/Visuals/IThumbnailSource.cs b/src/Titano.Ui/Visuals/IThumbnailSource.cs new file mode 100644 index 00000000..09634142 --- /dev/null +++ b/src/Titano.Ui/Visuals/IThumbnailSource.cs @@ -0,0 +1,17 @@ +using System.Windows.Media; + +namespace Titano.Ui.Visuals; + +/// +/// 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. +/// +public interface IThumbnailSource +{ + /// Number of frames. + int Count { get; } + + /// Thumbnail of frame (0…Count−1) at least device pixels wide; null when unavailable. + ValueTask GetAsync(int index, int pixelWidth, CancellationToken cancellationToken); +} diff --git a/src/Titano.Ui/Visuals/SequenceCard.cs b/src/Titano.Ui/Visuals/SequenceCard.cs new file mode 100644 index 00000000..636fdf8a --- /dev/null +++ b/src/Titano.Ui/Visuals/SequenceCard.cs @@ -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; + +/// One entry of a card's status row: glyph in the status colour, optional text (a count) and tooltip. +public sealed record CardStatus(StatusKind Kind, string? Text = null, string? ToolTip = null); + +/// +/// 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 , which is loaded asynchronously; stale requests +/// are cancelled and the last frame stays until the next one arrives. +/// +[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(); + } + + /// User title or "{date} {camera}". + public string? Title { get => (string?)GetValue(TitleProperty); set => SetValue(TitleProperty, value); } + + /// Secondary line: count · camera · span. + public string? Subtitle { get => (string?)GetValue(SubtitleProperty); set => SetValue(SubtitleProperty, value); } + + /// Count badge text (top-right of the thumbnail); hidden when empty. + public string? Badge { get => (string?)GetValue(BadgeProperty); set => SetValue(BadgeProperty, value); } + + /// entries of the status row. + public IEnumerable? Statuses { get => (IEnumerable?)GetValue(StatusesProperty); set => SetValue(StatusesProperty, value); } + + /// Tooltip of the selection check box. + public object? SelectToolTip { get => GetValue(SelectToolTipProperty); set => SetValue(SelectToolTipProperty, value); } + + /// Quick actions shown bottom-right of the thumbnail on hover (Open, Analyse, Render, Sync). + public object? ActionsContent { get => GetValue(ActionsContentProperty); set => SetValue(ActionsContentProperty, value); } + + /// Selection state; bind two-way to the container's IsSelected so the check box toggles it. + public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); } + + /// Hero thumbnail; may arrive later (asynchronous loading), a placeholder shows until then. + public ImageSource? Thumbnail { get => (ImageSource?)GetValue(ThumbnailProperty); set => SetValue(ThumbnailProperty, value); } + + /// Frames for hover scrubbing; null disables scrubbing. + public IThumbnailSource? ScrubSource { get => (IThumbnailSource?)GetValue(ScrubSourceProperty); set => SetValue(ScrubSourceProperty, value); } + + /// Image on screen: the scrubbed frame while scrubbing, otherwise . + public ImageSource? DisplayedThumbnail { get => (ImageSource?)GetValue(DisplayedThumbnailProperty); private set => SetValue(DisplayedThumbnailKey, value); } + + /// True while the mouse scrubs over the thumbnail. + public bool IsScrubbing { get => (bool)GetValue(IsScrubbingProperty); private set => SetValue(IsScrubbingKey, value); } + + /// Scrubbed position 0…1 along the sequence (drives the progress hairline). + 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; + } +}