Program.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Fluent API example demonstrating IRunnable with automatic disposal and result extraction
  2. using Terminal.Gui.App;
  3. using Terminal.Gui.Drawing;
  4. using Terminal.Gui.ViewBase;
  5. using Terminal.Gui.Views;
  6. #if POST_4148
  7. // Run the application with fluent API - automatically creates, runs, and disposes the runnable
  8. // Display the result
  9. if (Application.Create ()
  10. .Init ()
  11. .Run<ColorPickerView> ()
  12. .Shutdown () is Color { } result)
  13. {
  14. Console.WriteLine (@$"Selected Color: {(Color?)result}");
  15. }
  16. else
  17. {
  18. Console.WriteLine (@"No color selected");
  19. }
  20. #else
  21. // Run using traditional approach
  22. IApplication app = Application.Create ();
  23. app.Init ();
  24. var colorPicker = new ColorPickerView ();
  25. app.Run (colorPicker);
  26. Color? resultColor = colorPicker.Result;
  27. colorPicker.Dispose ();
  28. app.Shutdown ();
  29. if (resultColor is { } result)
  30. {
  31. Console.WriteLine (@$"Selected Color: {(Color?)result}");
  32. }
  33. else
  34. {
  35. Console.WriteLine (@"No color selected");
  36. }
  37. #endif
  38. #if POST_4148
  39. /// <summary>
  40. /// A runnable view that allows the user to select a color.
  41. /// Demonstrates IRunnable<TResult> pattern with automatic disposal.
  42. /// </summary>
  43. public class ColorPickerView : Runnable<Color?>
  44. {
  45. #else
  46. /// <summary>
  47. /// A runnable view that allows the user to select a color.
  48. /// Uses the traditional approach without automatic disposal/Fluent API.
  49. /// </summary>
  50. public class ColorPickerView : Toplevel
  51. {
  52. public Color? Result { get; set; }
  53. #endif
  54. public ColorPickerView ()
  55. {
  56. Title = "Select a Color (Esc to quit)";
  57. BorderStyle = LineStyle.Single;
  58. Height = Dim.Auto ();
  59. Width = Dim.Auto ();
  60. // Add instructions
  61. var instructions = new Label
  62. {
  63. Text = "Use arrow keys to select a color, Enter to accept",
  64. X = Pos.Center (),
  65. Y = 0
  66. };
  67. // Create color picker
  68. ColorPicker colorPicker = new ()
  69. {
  70. X = Pos.Center (),
  71. Y = Pos.Bottom (instructions),
  72. Style = new ColorPickerStyle ()
  73. {
  74. ShowColorName = true,
  75. ShowTextFields = true
  76. }
  77. };
  78. colorPicker.ApplyStyleChanges ();
  79. // Create OK button
  80. Button okButton = new ()
  81. {
  82. Title = "_OK",
  83. X = Pos.Align (Alignment.Center),
  84. Y = Pos.AnchorEnd (),
  85. IsDefault = true
  86. };
  87. okButton.Accepting += (s, e) =>
  88. {
  89. // Extract result before stopping
  90. Result = colorPicker.SelectedColor;
  91. RequestStop ();
  92. e.Handled = true;
  93. };
  94. // Create Cancel button
  95. Button cancelButton = new ()
  96. {
  97. Title = "_Cancel",
  98. X = Pos.Align (Alignment.Center),
  99. Y = Pos.AnchorEnd ()
  100. };
  101. cancelButton.Accepting += (s, e) =>
  102. {
  103. // Don't set result - leave as null
  104. RequestStop ();
  105. e.Handled = true;
  106. };
  107. // Add views
  108. Add (instructions, colorPicker, okButton, cancelButton);
  109. }
  110. #if POST_4148
  111. protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
  112. {
  113. // Alternative place to extract result before stopping
  114. // This is called before the view is removed from the stack
  115. if (!newIsRunning && Result is null)
  116. {
  117. // User pressed Esc - could extract current selection here
  118. // Result = _colorPicker.SelectedColor;
  119. }
  120. return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
  121. }
  122. #endif
  123. }