CWPPropertyHelper.cs 4.8 KB

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