ファイル

93 行
3.0 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 double NodesMinimum => UndoTreeSettings.NodesMinimum;
public double NodesMaximum => UndoTreeSettings.NodesMaximum;
public double RetentionDaysMinimum => UndoTreeSettings.RetentionDaysMinimum;
public double RetentionDaysMaximum => UndoTreeSettings.RetentionDaysMaximum;
public double TotalSizeMinimumMegabytes => UndoTreeSettings.TotalSizeMegabytesMinimum;
public double TotalSizeMaximumMegabytes => UndoTreeSettings.TotalSizeMegabytesMaximum;
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
{
< UndoTreeRecordStore.BytesPerKilobyte => string.Create(CultureInfo.InvariantCulture, $"{bytes} B"),
< UndoTreeRecordStore.BytesPerMegabyte => string.Create(CultureInfo.InvariantCulture, $"{(double)bytes / UndoTreeRecordStore.BytesPerKilobyte:F1} KB"),
_ => string.Create(CultureInfo.InvariantCulture, $"{(double)bytes / UndoTreeRecordStore.BytesPerMegabyte: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(UndoTreeToolPlugin.Id, exception);
}
}
}