CWPPropertyHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace Terminal.Gui.App;
  2. #nullable enable
  3. /// <summary>
  4. /// Provides helper methods for executing property change workflows in the Cancellable Work Pattern (CWP).
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// Used for workflows where a property value is modified, such as in <see cref="OrientationHelper"/> or
  9. /// <see cref="View.SchemeName"/>, allowing pre- and post-change events to customize or cancel the change.
  10. /// </para>
  11. /// </remarks>
  12. /// <seealso cref="ValueChangingEventArgs{T}"/>
  13. /// <seealso cref="ValueChangedEventArgs{T}"/>
  14. public static class CWPPropertyHelper
  15. {
  16. /// <summary>
  17. /// Executes a CWP workflow for a property change, with pre- and post-change events.
  18. /// </summary>
  19. /// <typeparam name="T">
  20. /// The type of the property value, which may be a nullable reference type (e.g., <see cref="string"/>
  21. /// ?).
  22. /// </typeparam>
  23. /// <param name="currentValue">The current property value, which may be null for nullable types.</param>
  24. /// <param name="newValue">The proposed new property value, which may be null for nullable types.</param>
  25. /// <param name="onChanging">The virtual method invoked before the change, returning true to cancel.</param>
  26. /// <param name="changingEvent">The pre-change event raised to allow modification or cancellation.</param>
  27. /// <param name="onChanged">The virtual method invoked after the change.</param>
  28. /// <param name="changedEvent">The post-change event raised to notify of the completed change.</param>
  29. /// <param name="finalValue">
  30. /// The final value after the workflow, reflecting any modifications, which may be null for
  31. /// nullable types.
  32. /// </param>
  33. /// <returns>True if the property was changed, false if cancelled.</returns>
  34. /// <exception cref="InvalidOperationException">
  35. /// Thrown if <see cref="ValueChangingEventArgs{T}.NewValue"/> is null for non-nullable reference types after the
  36. /// workflow.
  37. /// </exception>
  38. /// <example>
  39. /// <code>
  40. /// string? current = null;
  41. /// string? proposed = "Base";
  42. /// Func&lt;ValueChangingEventArgs&lt;string?&gt;, bool&gt; onChanging = args =&gt; false;
  43. /// EventHandler&lt;ValueChangingEventArgs&lt;string?&gt;&gt;? changingEvent = null;
  44. /// Action&lt;ValueChangedEventArgs&lt;string?&gt;&gt;? onChanged = args =&gt;
  45. /// Console.WriteLine($"SchemeName changed to {args.NewValue ?? "none"}.");
  46. /// EventHandler&lt;ValueChangedEventArgs&lt;string?&gt;&gt;? changedEvent = null;
  47. /// bool changed = CWPPropertyHelper.ChangeProperty(
  48. /// current, proposed, onChanging, changingEvent, onChanged, changedEvent, out string? final);
  49. /// </code>
  50. /// </example>
  51. public static bool ChangeProperty<T> (
  52. T currentValue,
  53. T newValue,
  54. Func<ValueChangingEventArgs<T>, bool> onChanging,
  55. EventHandler<ValueChangingEventArgs<T>>? changingEvent,
  56. Action<ValueChangedEventArgs<T>>? onChanged,
  57. EventHandler<ValueChangedEventArgs<T>>? changedEvent,
  58. out T finalValue
  59. )
  60. {
  61. if (EqualityComparer<T>.Default.Equals (currentValue, newValue))
  62. {
  63. finalValue = currentValue;
  64. return false;
  65. }
  66. ValueChangingEventArgs<T> args = new (currentValue, newValue);
  67. bool cancelled = onChanging (args) || args.Handled;
  68. if (cancelled)
  69. {
  70. finalValue = currentValue;
  71. return false;
  72. }
  73. changingEvent?.Invoke (null, args);
  74. if (args.Handled)
  75. {
  76. finalValue = currentValue;
  77. return false;
  78. }
  79. // Validate NewValue for non-nullable reference types
  80. if (args.NewValue is null && !typeof (T).IsValueType && !Nullable.GetUnderlyingType (typeof (T))?.IsValueType == true)
  81. {
  82. throw new InvalidOperationException ("NewValue cannot be null for non-nullable reference types.");
  83. }
  84. finalValue = args.NewValue;
  85. ValueChangedEventArgs<T> changedArgs = new (currentValue, finalValue);
  86. onChanged?.Invoke (changedArgs);
  87. changedEvent?.Invoke (null, changedArgs);
  88. return true;
  89. }
  90. }