CWPPropertyHelper.cs 4.8 KB

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