From 669848bbe2f0e9faff0fed36f7f8aa02890e1fe3 Mon Sep 17 00:00:00 2001 From: routersys Date: Sun, 13 Sep 2026 11:26:33 +0900 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E3=81=AE=E9=80=B2=E6=8D=97?= =?UTF-8?q?=E3=81=AE=E3=83=93=E3=83=A5=E3=83=BC=E3=83=A2=E3=83=87=E3=83=AB?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/GenerationItemViewModel.cs | 62 +++++++++++++++++++ .../ViewModels/GenerationProgressViewModel.cs | 55 ++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 ProxyForge/ViewModels/GenerationItemViewModel.cs create mode 100644 ProxyForge/ViewModels/GenerationProgressViewModel.cs diff --git a/ProxyForge/ViewModels/GenerationItemViewModel.cs b/ProxyForge/ViewModels/GenerationItemViewModel.cs new file mode 100644 index 0000000..034826d --- /dev/null +++ b/ProxyForge/ViewModels/GenerationItemViewModel.cs @@ -0,0 +1,62 @@ +using System.ComponentModel; +using System.Windows; +using ProxyForge.Encoding; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.ViewModels; + +internal sealed class GenerationItemViewModel : Bindable +{ + public GenerationItemViewModel(ProxyGenerationItem item) + { + Item = item; + PropertyChangedEventManager.AddHandler(item, OnItemChanged, string.Empty); + } + + public ProxyGenerationItem Item { get; } + + public string FileName => Item.FileName; + + public string SourcePath => Item.SourcePath; + + public double Percentage => Item.Progress * 100d; + + public bool IsWaiting => Item.Status == ProxyGenerationStatus.Waiting; + + public string StatusText => Item.Status switch + { + ProxyGenerationStatus.Waiting => Texts.StatusWaiting, + ProxyGenerationStatus.Generating => Texts.StatusGenerating, + ProxyGenerationStatus.Completed => Texts.StatusCompleted, + ProxyGenerationStatus.Cancelled => Texts.StatusCancelled, + _ => Item.Failure is { } failure ? string.Concat(Texts.StatusFailed, ": ", DescribeFailure(failure)) : Texts.StatusFailed, + }; + + public static string DescribeFailure(ProxyEncodeFailure failure) => failure switch + { + ProxyEncodeFailure.FFmpegUnavailable => Texts.FailureFFmpegUnavailable, + ProxyEncodeFailure.SourceUnavailable => Texts.FailureSourceUnavailable, + ProxyEncodeFailure.NoFrames => Texts.FailureNoFrames, + ProxyEncodeFailure.UnusableSize => Texts.FailureUnusableSize, + ProxyEncodeFailure.Transparent => Texts.FailureTransparent, + ProxyEncodeFailure.FFmpegFailed => Texts.FailureFFmpegFailed, + ProxyEncodeFailure.NoOutput => Texts.FailureNoOutput, + ProxyEncodeFailure.SourceChanged => Texts.FailureSourceChanged, + _ => failure.ToString(), + }; + + void OnItemChanged(object? sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(ProxyGenerationItem.Progress): + OnPropertyChanged(nameof(Percentage)); + break; + case nameof(ProxyGenerationItem.Status): + case nameof(ProxyGenerationItem.Failure): + OnPropertyChanged(nameof(StatusText)); + OnPropertyChanged(nameof(IsWaiting)); + break; + } + } +} diff --git a/ProxyForge/ViewModels/GenerationProgressViewModel.cs b/ProxyForge/ViewModels/GenerationProgressViewModel.cs new file mode 100644 index 0000000..412818f --- /dev/null +++ b/ProxyForge/ViewModels/GenerationProgressViewModel.cs @@ -0,0 +1,55 @@ +using System.Collections.ObjectModel; +using System.Collections.Specialized; +using System.Windows; +using ProxyForge.Encoding; +using YukkuriMovieMaker.Commons; + +namespace ProxyForge.ViewModels; + +internal sealed class GenerationProgressViewModel : Bindable +{ + readonly ObservableCollection source; + + public GenerationProgressViewModel(ObservableCollection source) + { + this.source = source; + CollectionChangedEventManager.AddHandler(source, OnSourceChanged); + Rebuild(); + } + + public ObservableCollection Items { get; } = []; + + public bool HasItems => Items.Count > 0; + + void OnSourceChanged(object? sender, NotifyCollectionChangedEventArgs e) + { + switch (e.Action) + { + case NotifyCollectionChangedAction.Add when e.NewItems is not null: + var index = e.NewStartingIndex < 0 ? Items.Count : e.NewStartingIndex; + foreach (ProxyGenerationItem item in e.NewItems) + Items.Insert(index++, new GenerationItemViewModel(item)); + break; + case NotifyCollectionChangedAction.Remove when e.OldItems is not null: + foreach (ProxyGenerationItem item in e.OldItems) + { + var existing = Items.FirstOrDefault(candidate => ReferenceEquals(candidate.Item, item)); + if (existing is not null) + Items.Remove(existing); + } + break; + default: + Rebuild(); + break; + } + + OnPropertyChanged(nameof(HasItems)); + } + + void Rebuild() + { + Items.Clear(); + foreach (var item in source) + Items.Add(new GenerationItemViewModel(item)); + } +}