Program.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Example demonstrating how to make ANY View runnable without implementing IRunnable
  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. app.Init ();
  8. // Example 1: Use extension method with result extraction
  9. var textField = new TextField { Width = 40, Text = "Default text" };
  10. textField.Title = "Enter your name";
  11. textField.BorderStyle = LineStyle.Single;
  12. var textRunnable = textField.AsRunnable (tf => tf.Text);
  13. app.Run (textRunnable);
  14. if (textRunnable.Result is { } name)
  15. {
  16. MessageBox.Query ("Result", $"You entered: {name}", "OK");
  17. }
  18. else
  19. {
  20. MessageBox.Query ("Result", "Canceled", "OK");
  21. }
  22. textRunnable.Dispose ();
  23. // Example 2: Use IApplication.RunView() for one-liner
  24. var selectedColor = app.RunView (
  25. new ColorPicker
  26. {
  27. Title = "Pick a Color",
  28. BorderStyle = LineStyle.Single
  29. },
  30. cp => cp.SelectedColor);
  31. MessageBox.Query ("Result", $"Selected color: {selectedColor}", "OK");
  32. // Example 3: FlagSelector with typed enum result
  33. var flagSelector = new FlagSelector<SelectorStyles>
  34. {
  35. Title = "Choose Styles",
  36. BorderStyle = LineStyle.Single
  37. };
  38. var flagsRunnable = flagSelector.AsRunnable (fs => fs.Value);
  39. app.Run (flagsRunnable);
  40. MessageBox.Query ("Result", $"Selected styles: {flagsRunnable.Result}", "OK");
  41. flagsRunnable.Dispose ();
  42. // Example 4: Any View without result extraction
  43. var label = new Label
  44. {
  45. Text = "Press Esc to continue...",
  46. X = Pos.Center (),
  47. Y = Pos.Center ()
  48. };
  49. var labelRunnable = label.AsRunnable ();
  50. app.Run (labelRunnable);
  51. // Can still access the wrapped view
  52. MessageBox.Query ("Result", $"Label text was: {labelRunnable.WrappedView.Text}", "OK");
  53. labelRunnable.Dispose ();
  54. // Example 5: Complex custom View made runnable
  55. var formView = CreateCustomForm ();
  56. var formRunnable = formView.AsRunnable (ExtractFormData);
  57. app.Run (formRunnable);
  58. if (formRunnable.Result is { } formData)
  59. {
  60. MessageBox.Query (
  61. "Form Results",
  62. $"Name: {formData.Name}\nAge: {formData.Age}\nAgreed: {formData.Agreed}",
  63. "OK");
  64. }
  65. formRunnable.Dispose ();
  66. app.Shutdown ();
  67. // Helper method to create a custom form
  68. View CreateCustomForm ()
  69. {
  70. var form = new View
  71. {
  72. Title = "User Information",
  73. BorderStyle = LineStyle.Single,
  74. Width = 50,
  75. Height = 10
  76. };
  77. var nameField = new TextField
  78. {
  79. Id = "nameField",
  80. X = 10,
  81. Y = 1,
  82. Width = 30
  83. };
  84. var ageField = new TextField
  85. {
  86. Id = "ageField",
  87. X = 10,
  88. Y = 3,
  89. Width = 10
  90. };
  91. var agreeCheckbox = new CheckBox
  92. {
  93. Id = "agreeCheckbox",
  94. Title = "I agree to terms",
  95. X = 10,
  96. Y = 5
  97. };
  98. var okButton = new Button
  99. {
  100. Title = "OK",
  101. X = Pos.Center (),
  102. Y = 7,
  103. IsDefault = true
  104. };
  105. okButton.Accepting += (s, e) =>
  106. {
  107. form.App?.RequestStop ();
  108. e.Handled = true;
  109. };
  110. form.Add (new Label { Text = "Name:", X = 2, Y = 1 });
  111. form.Add (nameField);
  112. form.Add (new Label { Text = "Age:", X = 2, Y = 3 });
  113. form.Add (ageField);
  114. form.Add (agreeCheckbox);
  115. form.Add (okButton);
  116. return form;
  117. }
  118. // Helper method to extract data from the custom form
  119. FormData ExtractFormData (View form)
  120. {
  121. var nameField = form.SubViews.FirstOrDefault (v => v.Id == "nameField") as TextField;
  122. var ageField = form.SubViews.FirstOrDefault (v => v.Id == "ageField") as TextField;
  123. var agreeCheckbox = form.SubViews.FirstOrDefault (v => v.Id == "agreeCheckbox") as CheckBox;
  124. return new FormData
  125. {
  126. Name = nameField?.Text ?? string.Empty,
  127. Age = int.TryParse (ageField?.Text, out int age) ? age : 0,
  128. Agreed = agreeCheckbox?.CheckedState == CheckState.Checked
  129. };
  130. }
  131. // Result type for custom form
  132. record FormData
  133. {
  134. public string Name { get; init; } = string.Empty;
  135. public int Age { get; init; }
  136. public bool Agreed { get; init; }
  137. }