未使用のMFインターロップファイルを削除しました。
MfProxyEncoderのトランスコード進捗報告処理をTranscodeProgressReporterクラスに抽出しました。
キャンセル登録の型をIAsyncOperationWithProgressからIAsyncActionWithProgressに修正しました。
MfProxyEncoderFactory・MemoryBudget・PooledMemoryStream・CacheEntryViewModel等をプライマリコンストラクタ構文に変換しました。
ProxyCacheManagerのCacheKeyをrecord structに変換し、Equalsのオーバーライドを整理しました。
PooledBufferをmutableなstructに変更し、Interlocked.Exchangeによるスレッドセーフなディスポーズを実装しました。
FallbackStreamにRead(Span<byte>)・Write(ReadOnlySpan<byte>)・WriteAsyncの各オーバーロードを追加しました。
MemoryBudgetのTickCountを32ビットuintにキャストし、ラッピング演算の整合性を修正しました。
GetAllSnapshotsをList<T>ベースに変更し、配列の事前確保とArray.Resizeを除去しました。
ZeroDiskProxyVideoSourceのUpgrade適用ロジックをTryApplyUpgradeメソッドとして抽出しました。
ProxyGenerationItem・ByteFormatter・CacheEntryViewModelの文字列生成をstring.Createと補間文字列に統一しました。
PluginHostからResourceRegistryへの依存を取り除き、関連するDispose呼び出しを削除しました。
CheckExportWindowにDebug.Assertを追加し、スレッドプールの非同期継続から呼ばれた場合の前提条件違反を検出できるようにしました。
ClearAllのforeachでデコンストラクション構文をkeyプロパティの明示的な参照に変更しました。
CreateCoreメソッドをインスタンスメンバーからstaticメソッドに変更しました。
パターンマッチングをis { IsLoaded: true }・is { IsValid: true }形式に統一しました。
148 行
5.3 KiB
C#
148 行
5.3 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Windows.Input;
|
|
using YukkuriMovieMaker.Commons;
|
|
using ZeroDiskProxy.Core;
|
|
using ZeroDiskProxy.Interfaces;
|
|
using ZeroDiskProxy.Localization;
|
|
using ZeroDiskProxy.Memory;
|
|
using ZeroDiskProxy.Utilities;
|
|
|
|
namespace ZeroDiskProxy.ViewModels;
|
|
|
|
internal sealed class SettingsViewModel : Bindable
|
|
{
|
|
private readonly ProxyCacheManager? _cacheManager;
|
|
private readonly MemoryBudget? _budget;
|
|
private readonly IDialogService _dialogService;
|
|
|
|
private string _cacheSummaryText = Translate.CacheSummaryLoading;
|
|
public string CacheSummaryText
|
|
{
|
|
get => _cacheSummaryText;
|
|
set => Set(ref _cacheSummaryText, value);
|
|
}
|
|
|
|
private string _memoryUsageText = "";
|
|
public string MemoryUsageText
|
|
{
|
|
get => _memoryUsageText;
|
|
set => Set(ref _memoryUsageText, value);
|
|
}
|
|
|
|
private bool _hasCacheEntries;
|
|
public bool HasCacheEntries
|
|
{
|
|
get => _hasCacheEntries;
|
|
set => Set(ref _hasCacheEntries, value);
|
|
}
|
|
|
|
public ObservableCollection<CacheEntryViewModel> CacheEntries { get; } = [];
|
|
|
|
public ICommand ClearCacheCommand { get; }
|
|
public ICommand RefreshCacheInfoCommand { get; }
|
|
public ICommand RemoveCacheEntryCommand { get; }
|
|
|
|
internal SettingsViewModel(ProxyCacheManager? cacheManager, MemoryBudget? budget, IDialogService dialogService)
|
|
{
|
|
_cacheManager = cacheManager;
|
|
_budget = budget;
|
|
_dialogService = dialogService;
|
|
ClearCacheCommand = new ActionCommand(ExecuteClearCache);
|
|
RefreshCacheInfoCommand = new ActionCommand(ExecuteRefreshCacheInfo);
|
|
RemoveCacheEntryCommand = new ActionCommand<CacheEntryViewModel>(ExecuteRemoveEntry);
|
|
ExecuteRefreshCacheInfo();
|
|
}
|
|
|
|
private void ExecuteClearCache()
|
|
{
|
|
if (_cacheManager is null)
|
|
return;
|
|
|
|
var (count, totalSize) = _cacheManager.ClearAll();
|
|
if (count == 0)
|
|
{
|
|
_dialogService.ShowInformation(Translate.CacheNoEntriesToDelete, Translate.AppName);
|
|
}
|
|
else
|
|
{
|
|
_dialogService.ShowInformation(
|
|
string.Format(Translate.CacheDeleteSummaryFormat, count.ToString("N0"), ByteFormatter.Format(totalSize)),
|
|
Translate.AppName);
|
|
}
|
|
|
|
ExecuteRefreshCacheInfo();
|
|
}
|
|
|
|
private void ExecuteRefreshCacheInfo()
|
|
{
|
|
if (_cacheManager is null)
|
|
{
|
|
CacheSummaryText = Translate.CacheSummaryPluginNotInitialized;
|
|
MemoryUsageText = "";
|
|
HasCacheEntries = false;
|
|
return;
|
|
}
|
|
|
|
var (count, memSize, diskSize, memCount, diskCount) = _cacheManager.GetCacheInfo();
|
|
CacheSummaryText = string.Format(Translate.CacheSummaryCountFormat, count.ToString("N0"));
|
|
|
|
var usageText = string.Format(
|
|
Translate.CacheUsageMemoryAndDiskFormat,
|
|
memCount.ToString("N0"),
|
|
ByteFormatter.Format(memSize),
|
|
diskCount.ToString("N0"),
|
|
ByteFormatter.Format(diskSize));
|
|
|
|
if (_budget is not null)
|
|
usageText = string.Concat(
|
|
usageText,
|
|
string.Format(Translate.CacheUsageAllocatedFormat, ByteFormatter.Format(_budget.AllocatedBytes)));
|
|
|
|
MemoryUsageText = usageText;
|
|
|
|
CacheEntries.Clear();
|
|
var snapshots = _cacheManager.GetAllSnapshots();
|
|
foreach (var snap in snapshots)
|
|
CacheEntries.Add(new CacheEntryViewModel(snap));
|
|
|
|
HasCacheEntries = CacheEntries.Count > 0;
|
|
}
|
|
|
|
private void ExecuteRemoveEntry(CacheEntryViewModel? entry)
|
|
{
|
|
if (entry is null || _cacheManager is null)
|
|
return;
|
|
|
|
_cacheManager.RemoveProxy(entry.OriginalPath, entry.Scale);
|
|
ExecuteRefreshCacheInfo();
|
|
}
|
|
}
|
|
|
|
internal sealed class CacheEntryViewModel(CacheEntrySnapshot snapshot) : Bindable
|
|
{
|
|
public string FileName { get; } = snapshot.FileName;
|
|
public string OriginalPath { get; } = snapshot.OriginalPath;
|
|
public float Scale { get; } = snapshot.Scale;
|
|
public string Resolution { get; } = string.Create(null, stackalloc char[24], $"{snapshot.ProxyWidth}\u00d7{snapshot.ProxyHeight}");
|
|
public bool IsInMemory { get; } = snapshot.IsInMemory;
|
|
public string StorageType { get; } = snapshot.IsInMemory ? Translate.StorageMemory : Translate.StorageDisk;
|
|
public long DataSize { get; } = snapshot.DataSize;
|
|
public string DataSizeText { get; } = ByteFormatter.Format(snapshot.DataSize);
|
|
public string CreatedAtText { get; } = snapshot.CreatedAt.ToLocalTime().ToString("HH:mm:ss");
|
|
public string LastAccessedText { get; } = snapshot.LastAccessedAt.ToLocalTime().ToString("HH:mm:ss");
|
|
public string ScaleText { get; } = string.Create(null, stackalloc char[8], $"{snapshot.Scale * 100:F0}%");
|
|
}
|
|
|
|
internal sealed class ActionCommand(Action execute) : ICommand
|
|
{
|
|
public event EventHandler? CanExecuteChanged { add { } remove { } }
|
|
public bool CanExecute(object? parameter) => true;
|
|
public void Execute(object? parameter) => execute();
|
|
}
|
|
|
|
internal sealed class ActionCommand<T>(Action<T?> execute) : ICommand where T : class
|
|
{
|
|
public event EventHandler? CanExecuteChanged { add { } remove { } }
|
|
public bool CanExecute(object? parameter) => true;
|
|
public void Execute(object? parameter) => execute(parameter as T);
|
|
} |