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; /// /// Base class for view models that are used in /// 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 }; } /// /// Return a that uses this view model as its content /// 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; } /// /// 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) { } }