| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MessageBoxViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Windows.Input; |
| | | 9 | | using MyNet.UI.Commands; |
| | | 10 | | using MyNet.UI.ViewModels.Dialog; |
| | | 11 | | |
| | | 12 | | namespace MyNet.UI.Dialogs.MessageBox; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// View model for a message box dialog. Button commands set a <see cref="MessageBoxResult"/> and request close. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class MessageBoxViewModel : DialogViewModel<MessageBoxResult>, IMessageBox |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Initializes a new instance of the <see cref="MessageBoxViewModel"/> class. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="options">Message box configuration.</param> |
| | | 23 | | /// <param name="commandFactory">Optional command factory.</param> |
| | | 24 | | public MessageBoxViewModel(MessageBoxOptions options, ICommandFactory? commandFactory = null) |
| | 9 | 25 | | : base(commandFactory) |
| | | 26 | | { |
| | 9 | 27 | | ArgumentNullException.ThrowIfNull(options); |
| | | 28 | | |
| | 9 | 29 | | Message = options.Message; |
| | 9 | 30 | | Title = options.Title; |
| | 9 | 31 | | Severity = options.Severity; |
| | 9 | 32 | | Buttons = options.Buttons; |
| | 9 | 33 | | DefaultResult = options.DefaultResult; |
| | | 34 | | |
| | 9 | 35 | | var commands = commandFactory.GetOrDefault(); |
| | | 36 | | OkCommand = commands.Create(() => Complete(MessageBoxResult.Ok), () => Supports(MessageBoxResult.Ok)); |
| | | 37 | | CancelCommand = commands.Create(() => Complete(MessageBoxResult.Cancel), () => Supports(MessageBoxResult.Cancel) |
| | | 38 | | YesCommand = commands.Create(() => Complete(MessageBoxResult.Yes), () => Supports(MessageBoxResult.Yes)); |
| | | 39 | | NoCommand = commands.Create(() => Complete(MessageBoxResult.No), () => Supports(MessageBoxResult.No)); |
| | 9 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | | 43 | | public string Message { get; } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public MessageSeverity Severity { get; } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public MessageBoxResultOption Buttons { get; } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public MessageBoxResult DefaultResult { get; } |
| | | 53 | | |
| | | 54 | | /// <summary>Gets the command bound to the OK button when visible.</summary> |
| | | 55 | | public ICommand OkCommand { get; } |
| | | 56 | | |
| | | 57 | | /// <summary>Gets the command bound to the Cancel button when visible.</summary> |
| | | 58 | | public ICommand CancelCommand { get; } |
| | | 59 | | |
| | | 60 | | /// <summary>Gets the command bound to the Yes button when visible.</summary> |
| | | 61 | | public ICommand YesCommand { get; } |
| | | 62 | | |
| | | 63 | | /// <summary>Gets the command bound to the No button when visible.</summary> |
| | | 64 | | public ICommand NoCommand { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Applies a result without user interaction (headless presenter / tests). |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="result">The result to apply.</param> |
| | 9 | 70 | | public void ApplyResult(MessageBoxResult result) => Complete(result); |
| | | 71 | | |
| | | 72 | | private void Complete(MessageBoxResult result) |
| | | 73 | | { |
| | 9 | 74 | | SetResult(result); |
| | 9 | 75 | | RequestClose(); |
| | 9 | 76 | | } |
| | | 77 | | |
| | | 78 | | private bool Supports(MessageBoxResult result) |
| | 0 | 79 | | => result switch |
| | 0 | 80 | | { |
| | 0 | 81 | | MessageBoxResult.Ok => Buttons is MessageBoxResultOption.Ok or MessageBoxResultOption.OkCancel, |
| | 0 | 82 | | MessageBoxResult.Cancel => Buttons is MessageBoxResultOption.OkCancel or MessageBoxResultOption.YesNoCancel, |
| | 0 | 83 | | MessageBoxResult.Yes => Buttons is MessageBoxResultOption.YesNo or MessageBoxResultOption.YesNoCancel, |
| | 0 | 84 | | MessageBoxResult.No => Buttons is MessageBoxResultOption.YesNo or MessageBoxResultOption.YesNoCancel, |
| | 0 | 85 | | _ => false |
| | 0 | 86 | | }; |
| | | 87 | | } |
| | | 88 | | |