Program.cs 3.2 KB

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