| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DialogResult.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | namespace MyNet.UI.Dialogs.ContentDialogs; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides non-generic factory helpers for the common <see cref="DialogResult{T}"/> of <see cref="bool"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class DialogResult |
| | | 13 | | { |
| | | 14 | | /// <summary>Returns a successful <c>bool</c> result (Value = <see langword="true"/>).</summary> |
| | 36 | 15 | | public static DialogResult<bool> Ok() => DialogResult<bool>.Success(true); |
| | | 16 | | |
| | | 17 | | /// <summary>Returns a cancelled <c>bool</c> result.</summary> |
| | 6 | 18 | | public static DialogResult<bool> Cancel() => DialogResult<bool>.Cancelled(); |
| | | 19 | | |
| | | 20 | | /// <summary>Returns a dismissed <c>bool</c> result.</summary> |
| | 3 | 21 | | public static DialogResult<bool> Dismiss() => DialogResult<bool>.Dismissed(); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Represents the result of a dialog operation, indicating whether the dialog was successful, canceled, dismissed, or r |
| | | 26 | | /// </summary> |
| | | 27 | | /// <typeparam name="T">The type of the value returned by the dialog.</typeparam> |
| | | 28 | | public sealed record DialogResult<T> |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the outcome of the dialog operation (Success, Canceled, or Dismissed). |
| | | 32 | | /// </summary> |
| | | 33 | | public DialogOutcome Outcome { get; init; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets a value indicating whether the dialog was confirmed (user clicked OK/Accept). |
| | | 37 | | /// </summary> |
| | | 38 | | public bool IsSuccess => Outcome == DialogOutcome.Success; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets a value indicating whether the dialog was explicitly canceled (user clicked Cancel). |
| | | 42 | | /// </summary> |
| | | 43 | | public bool IsCancelled => Outcome == DialogOutcome.Cancelled; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets a value indicating whether the dialog was dismissed without an explicit action (e.g. overlay click, Escape |
| | | 47 | | /// </summary> |
| | | 48 | | public bool IsDismissed => Outcome == DialogOutcome.Dismissed; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets the value returned by the dialog. Only meaningful when <see cref="IsSuccess"/> is <see langword="true"/>. |
| | | 52 | | /// </summary> |
| | | 53 | | public T? Value { get; init; } |
| | | 54 | | |
| | | 55 | | /// <summary>Creates a successful result carrying <paramref name="value"/>.</summary> |
| | | 56 | | public static DialogResult<T> Success(T value) => new() { Outcome = DialogOutcome.Success, Value = value }; |
| | | 57 | | |
| | | 58 | | /// <summary>Creates a canceled result (user explicitly canceled).</summary> |
| | | 59 | | public static DialogResult<T> Cancelled() => new() { Outcome = DialogOutcome.Cancelled }; |
| | | 60 | | |
| | | 61 | | /// <summary>Creates a dismissed result (closed without explicit action).</summary> |
| | | 62 | | public static DialogResult<T> Dismissed() => new() { Outcome = DialogOutcome.Dismissed }; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Defines the possible outcomes of a dialog operation. |
| | | 67 | | /// </summary> |
| | | 68 | | public enum DialogOutcome |
| | | 69 | | { |
| | | 70 | | /// <summary>The user confirmed the dialog (OK / Accept).</summary> |
| | | 71 | | Success, |
| | | 72 | | |
| | | 73 | | /// <summary>The user explicitly canceled the dialog (Cancel button).</summary> |
| | | 74 | | Cancelled, |
| | | 75 | | |
| | | 76 | | /// <summary>The dialog was closed without an explicit action (overlay click, Escape, forced close).</summary> |
| | | 77 | | Dismissed |
| | | 78 | | } |
| | | 79 | | |