using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia.Threading;
using FluentAvalonia.UI.Controls;
using StabilityMatrix.Avalonia.Languages;
namespace StabilityMatrix.Avalonia.ViewModels.Base;
///
/// Base class for view models that are used in
///
public abstract class TaskDialogViewModelBase : ViewModelBase
{
private TaskDialog? dialog;
protected static TaskDialogCommand GetCommandButton(string text, ICommand command)
{
return new TaskDialogCommand
{
Text = text,
DialogResult = TaskDialogStandardResult.None,
Command = command,
IsDefault = true,
ClosesOnInvoked = false
};
}
protected static TaskDialogButton GetCloseButton()
{
return new TaskDialogButton
{
Text = Resources.Action_Close,
DialogResult = TaskDialogStandardResult.Close
};
}
///
/// Return a that uses this view model as its content
///
public virtual TaskDialog GetDialog()
{
Dispatcher.UIThread.VerifyAccess();
dialog = new TaskDialog
{
Content = this,
XamlRoot = App.VisualRoot,
Buttons = { GetCloseButton() }
};
dialog.AttachedToVisualTree += (s, _) =>
{
((TaskDialog)s!).Closing += OnDialogClosing;
};
dialog.DetachedFromVisualTree += (s, _) =>
{
((TaskDialog)s!).Closing -= OnDialogClosing;
};
return dialog;
}
///
/// Show the dialog from and return the result
///
public async Task ShowDialogAsync()
{
return (TaskDialogStandardResult)await GetDialog().ShowAsync(true);
}
protected void CloseDialog(TaskDialogStandardResult result)
{
dialog?.Hide(result);
}
protected virtual async void OnDialogClosing(object? sender, TaskDialogClosingEventArgs e) { }
}