| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="HeadlessDialogStrategy.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using MyNet.UI.Dialogs.MessageBox; |
| | | 10 | | |
| | | 11 | | namespace MyNet.UI.Dialogs.ContentDialogs; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Fallback strategy for headless environments (unit tests, services without UI). |
| | | 15 | | /// Message boxes complete immediately with <see cref="IMessageBox.DefaultResult"/>. |
| | | 16 | | /// Other dialogs complete with a dismissed outcome. |
| | | 17 | | /// </summary> |
| | | 18 | | public sealed class HeadlessDialogStrategy : IDialogStrategy |
| | | 19 | | { |
| | | 20 | | /// <summary>Priority used so platform presenters override this fallback.</summary> |
| | | 21 | | public const int FallbackPriority = -1000; |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | 9 | 24 | | public int Priority => FallbackPriority; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | 9 | 27 | | public bool CanHandle(IDialog dialog, DialogOptions? options) => true; |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public Task<DialogResult<bool>> ShowAsync(IDialog dialog, DialogOptions options, CancellationToken ct = default) |
| | | 31 | | { |
| | 6 | 32 | | ct.ThrowIfCancellationRequested(); |
| | | 33 | | |
| | 6 | 34 | | if (dialog is MessageBoxViewModel messageBox) |
| | | 35 | | { |
| | 6 | 36 | | messageBox.ApplyResult(messageBox.DefaultResult); |
| | 6 | 37 | | return Task.FromResult(DialogResult.Ok()); |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | return Task.FromResult(DialogResult.Dismiss()); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | 0 | 44 | | public Task CloseAsync(IDialog dialog) => Task.CompletedTask; |
| | | 45 | | } |
| | | 46 | | |