ファイル
YMM4-UndoTree/UndoTree/UndoTreeSettingsViewModel.cs
T

81 行
2.4 KiB
C#

using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Globalization;
using YukkuriMovieMaker.Commons;
namespace UndoTree;
internal class UndoTreeSettingsViewModel : Bindable
{
readonly UndoTreeRecordStore store;
public UndoTreeSettingsViewModel(UndoTreeRecordStore? store = null)
{
this.store = store ?? UndoTreeRecordStore.Default;
OpenFolderCommand = new ActionCommand(_ => true, _ => OpenFolder());
ClearRecordsCommand = new ActionCommand(_ => Usage.Count > 0, _ => ClearRecords());
UpdateUsage();
}
public UndoTreeSettings Settings => UndoTreeSettings.Default;
public string RecordFolder => store.Directory;
public UndoTreeRecordUsage Usage { get; private set; } = new(0, 0);
public string UsageText { get; private set => Set(ref field, value); } = string.Empty;
public int ConflictPolicyIndex
{
get => (int)Settings.ConflictPolicy;
set
{
if (Enum.IsDefined((ConflictPolicy)value))
Settings.ConflictPolicy = (ConflictPolicy)value;
OnPropertyChanged(nameof(ConflictPolicyIndex));
}
}
public ActionCommand OpenFolderCommand { get; }
public ActionCommand ClearRecordsCommand { get; }
internal static string FormatBytes(long bytes) => bytes switch
{
< 1024 => string.Create(CultureInfo.InvariantCulture, $"{bytes} B"),
< 1024 * 1024 => string.Create(CultureInfo.InvariantCulture, $"{bytes / 1024.0:F1} KB"),
_ => string.Create(CultureInfo.InvariantCulture, $"{bytes / (1024.0 * 1024.0):F1} MB"),
};
public void UpdateUsage()
{
Usage = store.Usage();
UsageText = string.Format(Texts.RecordUsage, FormatBytes(Usage.Bytes), Usage.Count);
ClearRecordsCommand.RaiseCanExecuteChanged();
}
void ClearRecords()
{
if (!UndoTreeDispatch.Confirm(string.Format(Texts.ConfirmClearRecords, Usage.Count)))
return;
store.Clear();
UpdateUsage();
}
void OpenFolder()
{
try
{
Directory.CreateDirectory(store.Directory);
Process.Start(new ProcessStartInfo(store.Directory) { UseShellExecute = true });
}
catch (Exception exception) when (exception is IOException or UnauthorizedAccessException or Win32Exception or InvalidOperationException)
{
Log.Default.Write("UndoTree", exception);
}
}
}