Program.cs 4.0 KB

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