Program.cs 4.0 KB

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