| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="DialogBuilder.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 | | |
| | | 10 | | namespace MyNet.UI.Dialogs.ContentDialogs; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Default implementation of <see cref="IDialogBuilder"/> for non-typed dialogs. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="service">The content dialog service used to display the dialog.</param> |
| | | 16 | | /// <param name="dialog">The dialog view model to be displayed.</param> |
| | | 17 | | public sealed class DialogBuilder(IContentDialogService service, IDialog dialog) : IDialogBuilder |
| | | 18 | | { |
| | 0 | 19 | | private readonly DialogOptions _options = new() { Dialog = dialog }; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public IDialogBuilder AsModal() |
| | | 23 | | { |
| | 0 | 24 | | _options.IsModal = true; |
| | 0 | 25 | | return this; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public IDialogBuilder AsNonModal() |
| | | 30 | | { |
| | 0 | 31 | | _options.IsModal = false; |
| | 0 | 32 | | return this; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public IDialogBuilder WithTitle(string title) |
| | | 37 | | { |
| | 0 | 38 | | _options.Title = title; |
| | 0 | 39 | | return this; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public IDialogBuilder WithOwner(object owner) |
| | | 44 | | { |
| | 0 | 45 | | _options.Owner = owner; |
| | 0 | 46 | | return this; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | | 50 | | public IDialogBuilder CloseOnOverlayClick(bool value = true) |
| | | 51 | | { |
| | 0 | 52 | | _options.CloseOnOverlayClick = value; |
| | 0 | 53 | | return this; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public Task<DialogResult<bool>> ShowAsync(CancellationToken cancellationToken = default) |
| | 0 | 58 | | => service.ShowAsync(dialog, _options, cancellationToken); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Default implementation of <see cref="IDialogBuilder{TResult}"/> for typed dialogs. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <typeparam name="TResult">The type of the value returned by the dialog.</typeparam> |
| | | 65 | | /// <param name="service">The content dialog service used to display the dialog.</param> |
| | | 66 | | /// <param name="dialog">The typed dialog view model to be displayed.</param> |
| | | 67 | | public sealed class DialogBuilder<TResult>(IContentDialogService service, IDialog<TResult> dialog) : IDialogBuilder<TRes |
| | | 68 | | { |
| | | 69 | | private readonly DialogOptions _options = new() { Dialog = dialog }; |
| | | 70 | | |
| | | 71 | | /// <inheritdoc /> |
| | | 72 | | public IDialogBuilder<TResult> AsModal() |
| | | 73 | | { |
| | | 74 | | _options.IsModal = true; |
| | | 75 | | return this; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <inheritdoc /> |
| | | 79 | | IDialogBuilder IDialogBuilder.AsModal() => AsModal(); |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | | 82 | | public IDialogBuilder<TResult> AsNonModal() |
| | | 83 | | { |
| | | 84 | | _options.IsModal = false; |
| | | 85 | | return this; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <inheritdoc /> |
| | | 89 | | IDialogBuilder IDialogBuilder.AsNonModal() => AsNonModal(); |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | public IDialogBuilder<TResult> WithTitle(string title) |
| | | 93 | | { |
| | | 94 | | _options.Title = title; |
| | | 95 | | return this; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc /> |
| | | 99 | | IDialogBuilder IDialogBuilder.WithTitle(string title) => WithTitle(title); |
| | | 100 | | |
| | | 101 | | /// <inheritdoc /> |
| | | 102 | | public IDialogBuilder<TResult> WithOwner(object owner) |
| | | 103 | | { |
| | | 104 | | _options.Owner = owner; |
| | | 105 | | return this; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <inheritdoc /> |
| | | 109 | | IDialogBuilder IDialogBuilder.WithOwner(object owner) => WithOwner(owner); |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public IDialogBuilder<TResult> CloseOnOverlayClick(bool value = true) |
| | | 113 | | { |
| | | 114 | | _options.CloseOnOverlayClick = value; |
| | | 115 | | return this; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | IDialogBuilder IDialogBuilder.CloseOnOverlayClick(bool value) => CloseOnOverlayClick(value); |
| | | 120 | | |
| | | 121 | | /// <inheritdoc /> |
| | | 122 | | Task<DialogResult<bool>> IDialogBuilder.ShowAsync(CancellationToken cancellationToken) |
| | | 123 | | => service.ShowAsync((IDialog)dialog, _options, cancellationToken); |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | | 126 | | public Task<DialogResult<TResult>> ShowAsync(CancellationToken cancellationToken = default) |
| | | 127 | | => service.ShowAsync(dialog, _options, cancellationToken); |
| | | 128 | | } |
| | | 129 | | |