Program.cs 3.8 KB

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