Program.cs 4.5 KB

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