mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Refactor newinv2.md deep dive doc Terminal.Gui v2 introduces a transformative redesign, simplifying the library's architecture and improving maintainability. Key changes include: - Added TrueColor support with 24-bit RGB handling. - Introduced `Adornment` framework for borders, padding, and margins. - Enhanced Unicode and wide character support for internationalization. - Added `LineCanvas` for drawing lines and shapes with box-drawing characters. - Simplified API by consolidating redundant methods and aligning with modern .NET standards. - Introduced `ConfigurationManager` for user-customizable themes and text styles. - Improved scrolling with `Viewport` and integrated `ScrollBar`. - Added new layout features like `Dim.Auto`, `Pos.AnchorEnd`, and `Pos.Align`. - Overhauled keyboard and mouse APIs for better input handling. - Introduced new views (e.g., `DatePicker`, `ColorPicker`, `GraphView`) and enhanced existing ones. - Added Sixel image support for rendering graphics in compatible terminals. - Ensured AOT compatibility for improved deployment and performance. This update lays the foundation for building modern, user-friendly terminal applications. * Fixes #4368 - Clarify and Fix CWPPropertyHelper Property Change Workflow Refactor ChangeProperty method for clarity and flexibility Updated the `<param>` documentation for `currentValue` to clarify its behavior. Changed the `currentValue` parameter to be passed by reference (`ref`) to allow direct updates to the caller's variable. Made the `onChanging` delegate nullable and added a null check to prevent potential exceptions. Moved the `cancelled` variable inside the null-check block for `onChanging` to simplify logic. Explicitly updated `currentValue` to `finalValue` after the `doWork` action to ensure consistency. These changes improve the method's robustness, flexibility, and clarity. * Refactor ChangeProperty calls to use ref parameters Updated `CWPPropertyHelper.ChangeProperty` calls in `View.Drawing.Scheme.cs` and `View.Layout.cs` to pass fields as `ref` parameters. This ensures direct modification of fields, improving property update handling. Affected properties: - `_schemeName` and `_scheme` in `View.Drawing.Scheme.cs` - `_height` and `_width` in `View.Layout.cs` Property change notification logic remains unchanged. This refactor enhances maintainability and correctness without introducing new functionality.
117 lines
4.8 KiB
C#
117 lines
4.8 KiB
C#
namespace Terminal.Gui.App;
|
|
|
|
#nullable enable
|
|
|
|
/// <summary>
|
|
/// Provides helper methods for executing property change workflows in the Cancellable Work Pattern (CWP).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para>
|
|
/// Used for workflows where a property value is modified, such as in <see cref="OrientationHelper"/> or
|
|
/// <see cref="View.SchemeName"/>, allowing pre- and post-change events to customize or cancel the change.
|
|
/// </para>
|
|
/// </remarks>
|
|
/// <seealso cref="ValueChangingEventArgs{T}"/>
|
|
/// <seealso cref="ValueChangedEventArgs{T}"/>
|
|
public static class CWPPropertyHelper
|
|
{
|
|
/// <summary>
|
|
/// Executes a CWP workflow for a property change, with pre- and post-change events.
|
|
/// </summary>
|
|
/// <typeparam name="T">
|
|
/// The type of the property value, which may be a nullable reference type (e.g., <see cref="string"/>
|
|
/// ?).
|
|
/// </typeparam>
|
|
/// <param name="currentValue">
|
|
/// Reference to the current property value, which may be null for nullable types. If the change is not cancelled, this
|
|
/// will be set to <paramref name="finalValue"/>.
|
|
/// </param>
|
|
/// <param name="newValue">The proposed new property value, which may be null for nullable types.</param>
|
|
/// <param name="onChanging">The virtual method invoked before the change, returning true to cancel.</param>
|
|
/// <param name="changingEvent">The pre-change event raised to allow modification or cancellation.</param>
|
|
/// <param name="doWork">The action that performs the actual work of setting the property (e.g., updating backing field, calling related methods).</param>
|
|
/// <param name="onChanged">The virtual method invoked after the change.</param>
|
|
/// <param name="changedEvent">The post-change event raised to notify of the completed change.</param>
|
|
/// <param name="finalValue">
|
|
/// The final value after the workflow, reflecting any modifications, which may be null for
|
|
/// nullable types.
|
|
/// </param>
|
|
/// <returns>True if the property was changed, false if cancelled.</returns>
|
|
/// <exception cref="InvalidOperationException">
|
|
/// Thrown if <see cref="ValueChangingEventArgs{T}.NewValue"/> is null for non-nullable reference types after the
|
|
/// workflow.
|
|
/// </exception>
|
|
/// <example>
|
|
/// <code>
|
|
/// string? current = _schemeName;
|
|
/// string? proposed = "Base";
|
|
/// Func<ValueChangingEventArgs<string?>, bool> onChanging = OnSchemeNameChanging;
|
|
/// EventHandler<ValueChangingEventArgs<string?>>? changingEvent = SchemeNameChanging;
|
|
/// Action<string?> doWork = value => _schemeName = value;
|
|
/// Action<ValueChangedEventArgs<string?>>? onChanged = OnSchemeNameChanged;
|
|
/// EventHandler<ValueChangedEventArgs<string?>>? changedEvent = SchemeNameChanged;
|
|
/// bool changed = CWPPropertyHelper.ChangeProperty(
|
|
/// current, proposed, onChanging, changingEvent, doWork, onChanged, changedEvent, out string? final);
|
|
/// </code>
|
|
/// </example>
|
|
public static bool ChangeProperty<T> (
|
|
ref T currentValue,
|
|
T newValue,
|
|
Func<ValueChangingEventArgs<T>, bool>? onChanging,
|
|
EventHandler<ValueChangingEventArgs<T>>? changingEvent,
|
|
Action<T> doWork,
|
|
Action<ValueChangedEventArgs<T>>? onChanged,
|
|
EventHandler<ValueChangedEventArgs<T>>? changedEvent,
|
|
out T finalValue
|
|
)
|
|
{
|
|
if (EqualityComparer<T>.Default.Equals (currentValue, newValue))
|
|
{
|
|
finalValue = currentValue;
|
|
|
|
return false;
|
|
}
|
|
|
|
ValueChangingEventArgs<T> args = new (currentValue, newValue);
|
|
|
|
if (onChanging is { })
|
|
{
|
|
bool cancelled = onChanging (args) || args.Handled;
|
|
|
|
if (cancelled)
|
|
{
|
|
finalValue = currentValue;
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
changingEvent?.Invoke (null, args);
|
|
|
|
if (args.Handled)
|
|
{
|
|
finalValue = currentValue;
|
|
|
|
return false;
|
|
}
|
|
|
|
// Validate NewValue for non-nullable reference types
|
|
if (args.NewValue is null && !typeof (T).IsValueType && !Nullable.GetUnderlyingType (typeof (T))?.IsValueType == true)
|
|
{
|
|
throw new InvalidOperationException ("NewValue cannot be null for non-nullable reference types.");
|
|
}
|
|
|
|
finalValue = args.NewValue;
|
|
|
|
// Do the work (set backing field, update related properties, etc.) BEFORE raising Changed events
|
|
doWork (finalValue);
|
|
|
|
ValueChangedEventArgs<T> changedArgs = new (currentValue, finalValue);
|
|
currentValue = finalValue;
|
|
onChanged?.Invoke (changedArgs);
|
|
changedEvent?.Invoke (null, changedArgs);
|
|
|
|
return true;
|
|
}
|
|
}
|