Program.cs 5.5 KB

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