Multi-Platform Package Manager for Stable Diffusion
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.2 KiB

using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia.Threading;
using AvaloniaEdit.Rendering;
using FluentAvalonia.UI.Controls;
using StabilityMatrix.Avalonia.Languages;
namespace StabilityMatrix.Avalonia.ViewModels.Base;
/// <summary>
/// Base class for view models that are used in <see cref="FluentAvalonia.UI.Controls.TaskDialog"/>
/// </summary>
public abstract class TaskDialogViewModelBase : ViewModelBase
{
private TaskDialog? dialog;
public virtual string? Title { get; set; }
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 };
}
/// <summary>
/// Return a <see cref="TaskDialog"/> that uses this view model as its content
/// </summary>
public virtual TaskDialog GetDialog()
{
Dispatcher.UIThread.VerifyAccess();
dialog = new TaskDialog
{
Header = Title,
Content = this,
XamlRoot = App.VisualRoot,
Buttons = { GetCloseButton() }
};
dialog.AttachedToVisualTree += (s, _) =>
{
((TaskDialog)s!).Closing += OnDialogClosing;
};
dialog.DetachedFromVisualTree += (s, _) =>
{
((TaskDialog)s!).Closing -= OnDialogClosing;
};
return dialog;
}
/// <summary>
/// Show the dialog from <see cref="GetDialog"/> and return the result
/// </summary>
public async Task<TaskDialogStandardResult> ShowDialogAsync()
{
return (TaskDialogStandardResult)await GetDialog().ShowAsync(true);
}
protected void CloseDialog(TaskDialogStandardResult result)
{
dialog?.Hide(result);
}
protected virtual async void OnDialogClosing(object? sender, TaskDialogClosingEventArgs e) { }
}