2
0

Program.cs 4.1 KB

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