Text.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using Terminal.Gui;
  8. using Terminal.Gui.TextValidateProviders;
  9. namespace UICatalog.Scenarios;
  10. [ScenarioMetadata ("Text Input Controls", "Tests all text input controls")]
  11. [ScenarioCategory ("Controls")]
  12. [ScenarioCategory ("Mouse and Keyboard")]
  13. [ScenarioCategory ("Text and Formatting")]
  14. public class Text : Scenario {
  15. Label _labelMirroringTimeField;
  16. TimeField _timeField;
  17. public override void Setup ()
  18. {
  19. // TextField is a simple, single-line text input control
  20. var textField = new TextField ("TextField with test text. Unicode shouldn't 𝔹Aℝ𝔽!") {
  21. X = 1,
  22. Y = 0,
  23. Width = Dim.Percent (50) - 1,
  24. // Height will be replaced with 1
  25. Height = 2
  26. };
  27. var singleWordGenerator = new SingleWordSuggestionGenerator ();
  28. textField.Autocomplete.SuggestionGenerator = singleWordGenerator;
  29. textField.TextChanging += TextField_TextChanging;
  30. void TextField_TextChanging (object sender, TextChangingEventArgs e)
  31. {
  32. singleWordGenerator.AllSuggestions = Regex.Matches (e.NewText, "\\w+")
  33. .Select (s => s.Value)
  34. .Distinct ().ToList ();
  35. }
  36. Win.Add (textField);
  37. var labelMirroringTextField = new Label (textField.Text) {
  38. X = Pos.Right (textField) + 1,
  39. Y = Pos.Top (textField),
  40. Width = Dim.Fill (1) - 1
  41. };
  42. Win.Add (labelMirroringTextField);
  43. textField.TextChanged += (s, prev) => {
  44. labelMirroringTextField.Text = textField.Text;
  45. };
  46. // TextView is a rich (as in functionality, not formatting) text editing control
  47. var textView = new TextView {
  48. X = 1,
  49. Y = Pos.Bottom (textField) + 1,
  50. Width = Dim.Percent (50) - 1,
  51. Height = Dim.Percent (30)
  52. };
  53. textView.Text = "TextView with some more test text. Unicode shouldn't 𝔹Aℝ𝔽!";
  54. textView.DrawContent += TextView_DrawContent;
  55. // This shows how to enable autocomplete in TextView.
  56. void TextView_DrawContent (object sender, DrawEventArgs e)
  57. {
  58. singleWordGenerator.AllSuggestions = Regex.Matches (textView.Text, "\\w+")
  59. .Select (s => s.Value)
  60. .Distinct ().ToList ();
  61. }
  62. Win.Add (textView);
  63. var labelMirroringTextView = new Label {
  64. X = Pos.Right (textView) + 1,
  65. Y = Pos.Top (textView),
  66. Width = Dim.Fill (1) - 1,
  67. Height = Dim.Height (textView) - 1
  68. };
  69. Win.Add (labelMirroringTextView);
  70. // Use ContentChanged to detect if the user has typed something in a TextView.
  71. // The TextChanged property is only fired if the TextView.Text property is
  72. // explicitly set
  73. textView.ContentsChanged += (s, a) => {
  74. labelMirroringTextView.Enabled = !labelMirroringTextView.Enabled;
  75. labelMirroringTextView.Text = textView.Text;
  76. };
  77. // By default TextView is a multi-line control. It can be forced to
  78. // single-line mode.
  79. var chxMultiline = new CheckBox ("Multiline") {
  80. X = Pos.Left (textView),
  81. Y = Pos.Bottom (textView),
  82. Checked = textView.Multiline
  83. };
  84. Win.Add (chxMultiline);
  85. var chxWordWrap = new CheckBox ("Word Wrap") {
  86. X = Pos.Right (chxMultiline) + 2,
  87. Y = Pos.Top (chxMultiline),
  88. Checked = textView.WordWrap
  89. };
  90. chxWordWrap.Toggled += (s, e) => textView.WordWrap = (bool)e.NewValue;
  91. Win.Add (chxWordWrap);
  92. // TextView captures Tabs (so users can enter /t into text) by default;
  93. // This means using Tab to navigate doesn't work by default. This shows
  94. // how to turn tab capture off.
  95. var chxCaptureTabs = new CheckBox ("Capture Tabs") {
  96. X = Pos.Right (chxWordWrap) + 2,
  97. Y = Pos.Top (chxWordWrap),
  98. Checked = textView.AllowsTab
  99. };
  100. chxMultiline.Toggled += (s, e) => {
  101. textView.Multiline = (bool)e.NewValue;
  102. if (!textView.Multiline && (bool)chxWordWrap.Checked) {
  103. chxWordWrap.Checked = false;
  104. }
  105. if (!textView.Multiline && (bool)chxCaptureTabs.Checked) {
  106. chxCaptureTabs.Checked = false;
  107. }
  108. };
  109. var keyTab = textView.KeyBindings.GetKeyFromCommands (Command.Tab);
  110. var keyBackTab = textView.KeyBindings.GetKeyFromCommands (Command.BackTab);
  111. chxCaptureTabs.Toggled += (s, e) => {
  112. if (e.NewValue == true) {
  113. textView.KeyBindings.Add (keyTab, Command.Tab);
  114. textView.KeyBindings.Add (keyBackTab, Command.BackTab);
  115. } else {
  116. textView.KeyBindings.Remove (keyTab);
  117. textView.KeyBindings.Remove (keyBackTab);
  118. }
  119. textView.AllowsTab = (bool)e.NewValue;
  120. };
  121. Win.Add (chxCaptureTabs);
  122. var hexEditor = new HexView (new MemoryStream (Encoding.UTF8.GetBytes ("HexEditor Unicode that shouldn't 𝔹Aℝ𝔽!"))) {
  123. X = 1,
  124. Y = Pos.Bottom (chxMultiline) + 1,
  125. Width = Dim.Percent (50) - 1,
  126. Height = Dim.Percent (30)
  127. };
  128. Win.Add (hexEditor);
  129. var labelMirroringHexEditor = new Label {
  130. X = Pos.Right (hexEditor) + 1,
  131. Y = Pos.Top (hexEditor),
  132. Width = Dim.Fill (1) - 1,
  133. Height = Dim.Height (hexEditor) - 1
  134. };
  135. var array = ((MemoryStream)hexEditor.Source).ToArray ();
  136. labelMirroringHexEditor.Text = Encoding.UTF8.GetString (array, 0, array.Length);
  137. hexEditor.Edited += (s, kv) => {
  138. hexEditor.ApplyEdits ();
  139. var array = ((MemoryStream)hexEditor.Source).ToArray ();
  140. labelMirroringHexEditor.Text = Encoding.UTF8.GetString (array, 0, array.Length);
  141. };
  142. Win.Add (labelMirroringHexEditor);
  143. var dateField = new DateField (DateTime.Now) {
  144. X = 1,
  145. Y = Pos.Bottom (hexEditor) + 1,
  146. Width = 20,
  147. IsShortFormat = false
  148. };
  149. Win.Add (dateField);
  150. var labelMirroringDateField = new Label (dateField.Text) {
  151. X = Pos.Right (dateField) + 1,
  152. Y = Pos.Top (dateField),
  153. Width = Dim.Width (dateField),
  154. Height = Dim.Height (dateField)
  155. };
  156. Win.Add (labelMirroringDateField);
  157. dateField.TextChanged += (s, prev) => {
  158. labelMirroringDateField.Text = dateField.Text;
  159. };
  160. _timeField = new TimeField (DateTime.Now.TimeOfDay) {
  161. X = Pos.Right (labelMirroringDateField) + 5,
  162. Y = Pos.Bottom (hexEditor) + 1,
  163. Width = 20,
  164. IsShortFormat = false
  165. };
  166. Win.Add (_timeField);
  167. _labelMirroringTimeField = new Label (_timeField.Text) {
  168. X = Pos.Right (_timeField) + 1,
  169. Y = Pos.Top (_timeField),
  170. Width = Dim.Width (_timeField),
  171. Height = Dim.Height (_timeField)
  172. };
  173. Win.Add (_labelMirroringTimeField);
  174. _timeField.TimeChanged += TimeChanged;
  175. // MaskedTextProvider - uses .NET MaskedTextProvider
  176. var netProviderLabel = new Label ("NetMaskedTextProvider [ 999 000 LLL >LLL| AAA aaa ]") {
  177. X = Pos.Left (dateField),
  178. Y = Pos.Bottom (dateField) + 1
  179. };
  180. Win.Add (netProviderLabel);
  181. var netProvider = new NetMaskedTextProvider ("999 000 LLL > LLL | AAA aaa");
  182. var netProviderField = new TextValidateField (netProvider) {
  183. X = Pos.Right (netProviderLabel) + 1,
  184. Y = Pos.Y (netProviderLabel)
  185. };
  186. Win.Add (netProviderField);
  187. // TextRegexProvider - Regex provider implemented by Terminal.Gui
  188. var regexProvider = new Label ("TextRegexProvider [ ^([0-9]?[0-9]?[0-9]|1000)$ ]") {
  189. X = Pos.Left (netProviderLabel),
  190. Y = Pos.Bottom (netProviderLabel) + 1
  191. };
  192. Win.Add (regexProvider);
  193. var provider2 = new TextRegexProvider ("^([0-9]?[0-9]?[0-9]|1000)$") { ValidateOnInput = false };
  194. var regexProviderField = new TextValidateField (provider2) {
  195. X = Pos.Right (regexProvider) + 1,
  196. Y = Pos.Y (regexProvider),
  197. Width = 30,
  198. TextAlignment = TextAlignment.Centered
  199. };
  200. Win.Add (regexProviderField);
  201. var labelAppendAutocomplete = new Label ("Append Autocomplete:") {
  202. Y = Pos.Y (regexProviderField) + 2,
  203. X = 1
  204. };
  205. var appendAutocompleteTextField = new TextField {
  206. X = Pos.Right (labelAppendAutocomplete),
  207. Y = Pos.Bottom (labelAppendAutocomplete),
  208. Width = Dim.Fill ()
  209. };
  210. appendAutocompleteTextField.Autocomplete = new AppendAutocomplete (appendAutocompleteTextField);
  211. appendAutocompleteTextField.Autocomplete.SuggestionGenerator = new SingleWordSuggestionGenerator {
  212. AllSuggestions = new List<string> {
  213. "fish", "flipper", "fin", "fun", "the", "at", "there", "some", "my", "of", "be", "use", "her", "than", "and", "this", "an", "would", "first", "have", "each", "make", "water", "to", "from", "which", "like", "been", "in", "or", "she", "him", "call", "is", "one", "do", "into", "who", "you", "had", "how", "time", "oil", "that", "by", "their", "has", "its", "it", "word", "if", "look", "now", "he", "but", "will", "two", "find", "was", "not", "up", "more", "long", "for", "what", "other", "write",
  214. "down", "on", "all", "about", "go", "day", "are", "were", "out", "see", "did", "as", "we", "many", "number", "get", "with", "when", "then", "no", "come", "his", "your", "them", "way", "made", "they", "can", "these", "could", "may", "said", "so", "people", "part"
  215. }
  216. };
  217. Win.Add (labelAppendAutocomplete);
  218. Win.Add (appendAutocompleteTextField);
  219. }
  220. void TimeChanged (object sender, DateTimeEventArgs<TimeSpan> e) => _labelMirroringTimeField.Text = _timeField.Text;
  221. }