CWPPropertyHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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="doWork">The action that performs the actual work of setting the property (e.g., updating backing field, calling related methods).</param>
  28. /// <param name="onChanged">The virtual method invoked after the change.</param>
  29. /// <param name="changedEvent">The post-change event raised to notify of the completed change.</param>
  30. /// <param name="finalValue">
  31. /// The final value after the workflow, reflecting any modifications, which may be null for
  32. /// nullable types.
  33. /// </param>
  34. /// <returns>True if the property was changed, false if cancelled.</returns>
  35. /// <exception cref="InvalidOperationException">
  36. /// Thrown if <see cref="ValueChangingEventArgs{T}.NewValue"/> is null for non-nullable reference types after the
  37. /// workflow.
  38. /// </exception>
  39. /// <example>
  40. /// <code>
  41. /// string? current = _schemeName;
  42. /// string? proposed = "Base";
  43. /// Func&lt;ValueChangingEventArgs&lt;string?&gt;, bool&gt; onChanging = OnSchemeNameChanging;
  44. /// EventHandler&lt;ValueChangingEventArgs&lt;string?&gt;&gt;? changingEvent = SchemeNameChanging;
  45. /// Action&lt;string?&gt; doWork = value => _schemeName = value;
  46. /// Action&lt;ValueChangedEventArgs&lt;string?&gt;&gt;? onChanged = OnSchemeNameChanged;
  47. /// EventHandler&lt;ValueChangedEventArgs&lt;string?&gt;&gt;? changedEvent = SchemeNameChanged;
  48. /// bool changed = CWPPropertyHelper.ChangeProperty(
  49. /// current, proposed, onChanging, changingEvent, doWork, onChanged, changedEvent, out string? final);
  50. /// </code>
  51. /// </example>
  52. public static bool ChangeProperty<T> (
  53. T currentValue,
  54. T newValue,
  55. Func<ValueChangingEventArgs<T>, bool> onChanging,
  56. EventHandler<ValueChangingEventArgs<T>>? changingEvent,
  57. Action<T> doWork,
  58. Action<ValueChangedEventArgs<T>>? onChanged,
  59. EventHandler<ValueChangedEventArgs<T>>? changedEvent,
  60. out T finalValue
  61. )
  62. {
  63. if (EqualityComparer<T>.Default.Equals (currentValue, newValue))
  64. {
  65. finalValue = currentValue;
  66. return false;
  67. }
  68. ValueChangingEventArgs<T> args = new (currentValue, newValue);
  69. bool cancelled = onChanging (args) || args.Handled;
  70. if (cancelled)
  71. {
  72. finalValue = currentValue;
  73. return false;
  74. }
  75. changingEvent?.Invoke (null, args);
  76. if (args.Handled)
  77. {
  78. finalValue = currentValue;
  79. return false;
  80. }
  81. // Validate NewValue for non-nullable reference types
  82. if (args.NewValue is null && !typeof (T).IsValueType && !Nullable.GetUnderlyingType (typeof (T))?.IsValueType == true)
  83. {
  84. throw new InvalidOperationException ("NewValue cannot be null for non-nullable reference types.");
  85. }
  86. finalValue = args.NewValue;
  87. // Do the work (set backing field, update related properties, etc.) BEFORE raising Changed events
  88. doWork (finalValue);
  89. ValueChangedEventArgs<T> changedArgs = new (currentValue, finalValue);
  90. onChanged?.Invoke (changedArgs);
  91. changedEvent?.Invoke (null, changedArgs);
  92. return true;
  93. }
  94. }