ViewRunnableExtensions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. namespace Terminal.Gui.Views;
  2. /// <summary>
  3. /// Extension methods for making any <see cref="View"/> runnable with typed results.
  4. /// </summary>
  5. /// <remarks>
  6. /// These extensions provide a fluent API for wrapping views in <see cref="RunnableWrapper{TView, TResult}"/>,
  7. /// enabling any View to be run as a blocking session without implementing <see cref="IRunnable{TResult}"/>.
  8. /// </remarks>
  9. public static class ViewRunnableExtensions
  10. {
  11. /// <summary>
  12. /// Converts any View into a runnable with typed result extraction.
  13. /// </summary>
  14. /// <typeparam name="TView">The type of view to make runnable.</typeparam>
  15. /// <typeparam name="TResult">The type of result data to extract.</typeparam>
  16. /// <param name="view">The view to wrap. Cannot be null.</param>
  17. /// <param name="resultExtractor">
  18. /// Function that extracts the result from the view when stopping.
  19. /// Called automatically when the runnable session ends.
  20. /// </param>
  21. /// <returns>A <see cref="RunnableWrapper{TView, TResult}"/> that wraps the view.</returns>
  22. /// <exception cref="ArgumentNullException">Thrown if <paramref name="view"/> or <paramref name="resultExtractor"/> is null.</exception>
  23. /// <remarks>
  24. /// <para>
  25. /// This method wraps the view in a <see cref="RunnableWrapper{TView, TResult}"/> and automatically
  26. /// subscribes to <see cref="IRunnable.IsRunningChanging"/> to extract the result when the session stops.
  27. /// </para>
  28. /// <para>
  29. /// The result is extracted before the view is disposed, ensuring all data is still accessible.
  30. /// </para>
  31. /// </remarks>
  32. /// <example>
  33. /// <code>
  34. /// // Make a TextField runnable with string result
  35. /// var runnable = new TextField { Width = 40 }
  36. /// .AsRunnable(tf =&gt; tf.Text);
  37. ///
  38. /// app.Run(runnable);
  39. /// Console.WriteLine($"User entered: {runnable.Result}");
  40. /// runnable.Dispose();
  41. ///
  42. /// // Make a ColorPicker runnable with Color? result
  43. /// var colorRunnable = new ColorPicker()
  44. /// .AsRunnable(cp =&gt; cp.SelectedColor);
  45. ///
  46. /// app.Run(colorRunnable);
  47. /// Console.WriteLine($"Selected: {colorRunnable.Result}");
  48. /// colorRunnable.Dispose();
  49. ///
  50. /// // Make a FlagSelector runnable with enum result
  51. /// var flagsRunnable = new FlagSelector&lt;SelectorStyles&gt;()
  52. /// .AsRunnable(fs =&gt; fs.Value);
  53. ///
  54. /// app.Run(flagsRunnable);
  55. /// Console.WriteLine($"Selected styles: {flagsRunnable.Result}");
  56. /// flagsRunnable.Dispose();
  57. /// </code>
  58. /// </example>
  59. public static RunnableWrapper<TView, TResult> AsRunnable<TView, TResult> (
  60. this TView view,
  61. Func<TView, TResult?> resultExtractor)
  62. where TView : View
  63. {
  64. if (view is null)
  65. {
  66. throw new ArgumentNullException (nameof (view));
  67. }
  68. if (resultExtractor is null)
  69. {
  70. throw new ArgumentNullException (nameof (resultExtractor));
  71. }
  72. var wrapper = new RunnableWrapper<TView, TResult> { WrappedView = view };
  73. // Subscribe to IsRunningChanging to extract result when stopping
  74. wrapper.IsRunningChanging += (s, e) =>
  75. {
  76. if (!e.NewValue) // Stopping
  77. {
  78. wrapper.Result = resultExtractor (view);
  79. }
  80. };
  81. return wrapper;
  82. }
  83. /// <summary>
  84. /// Converts any View into a runnable without result extraction.
  85. /// </summary>
  86. /// <typeparam name="TView">The type of view to make runnable.</typeparam>
  87. /// <param name="view">The view to wrap. Cannot be null.</param>
  88. /// <returns>A <see cref="RunnableWrapper{TView, Object}"/> that wraps the view.</returns>
  89. /// <exception cref="ArgumentNullException">Thrown if <paramref name="view"/> is null.</exception>
  90. /// <remarks>
  91. /// <para>
  92. /// Use this overload when you don't need to extract a typed result, but still want to
  93. /// run the view as a blocking session. The wrapped view can still be accessed via
  94. /// <see cref="RunnableWrapper{TView, TResult}.WrappedView"/> after running.
  95. /// </para>
  96. /// </remarks>
  97. /// <example>
  98. /// <code>
  99. /// // Make a view runnable without result extraction
  100. /// var colorPicker = new ColorPicker();
  101. /// var runnable = colorPicker.AsRunnable();
  102. ///
  103. /// app.Run(runnable);
  104. ///
  105. /// // Access the wrapped view directly to get the result
  106. /// Console.WriteLine($"Selected: {runnable.WrappedView.SelectedColor}");
  107. /// runnable.Dispose();
  108. /// </code>
  109. /// </example>
  110. public static RunnableWrapper<TView, object> AsRunnable<TView> (this TView view)
  111. where TView : View
  112. {
  113. if (view is null)
  114. {
  115. throw new ArgumentNullException (nameof (view));
  116. }
  117. return new RunnableWrapper<TView, object> { WrappedView = view };
  118. }
  119. }