Program.cs 5.5 KB

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