Text.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 (System.DateTime.Now) {
  144. X = 1,
  145. Y = Pos.Bottom (hexEditor) + 1,
  146. Width = 20
  147. };
  148. Win.Add (dateField);
  149. var labelMirroringDateField = new Label (dateField.Text) {
  150. X = Pos.Right (dateField) + 1,
  151. Y = Pos.Top (dateField),
  152. Width = Dim.Width (dateField),
  153. Height = Dim.Height (dateField)
  154. };
  155. Win.Add (labelMirroringDateField);
  156. dateField.TextChanged += (s, prev) => {
  157. labelMirroringDateField.Text = dateField.Text;
  158. };
  159. _timeField = new TimeField (DateTime.Now.TimeOfDay) {
  160. X = Pos.Right (labelMirroringDateField) + 5,
  161. Y = Pos.Bottom (hexEditor) + 1,
  162. Width = 20,
  163. IsShortFormat = false
  164. };
  165. Win.Add (_timeField);
  166. _labelMirroringTimeField = new Label (_timeField.Text) {
  167. X = Pos.Right (_timeField) + 1,
  168. Y = Pos.Top (_timeField),
  169. Width = Dim.Width (_timeField),
  170. Height = Dim.Height (_timeField)
  171. };
  172. Win.Add (_labelMirroringTimeField);
  173. _timeField.TimeChanged += TimeChanged;
  174. // MaskedTextProvider - uses .NET MaskedTextProvider
  175. var netProviderLabel = new Label ("NetMaskedTextProvider [ 999 000 LLL >LLL |AAA aaa ]") {
  176. X = Pos.Left (dateField),
  177. Y = Pos.Bottom (dateField) + 1
  178. };
  179. Win.Add (netProviderLabel);
  180. var netProvider = new NetMaskedTextProvider ("999 000 LLL >LLL |AAA aaa");
  181. var netProviderField = new TextValidateField (netProvider) {
  182. X = Pos.Right (netProviderLabel) + 1,
  183. Y = Pos.Y (netProviderLabel)
  184. };
  185. Win.Add (netProviderField);
  186. var labelMirroringNetProviderField = new Label (netProviderField.Text) {
  187. X = Pos.Right (netProviderField) + 1,
  188. Y = Pos.Top (netProviderField),
  189. Width = Dim.Width (netProviderField),
  190. Height = Dim.Height (netProviderField)
  191. };
  192. Win.Add (labelMirroringNetProviderField);
  193. netProviderField.Provider.TextChanged += (s, prev) => {
  194. labelMirroringNetProviderField.Text = netProviderField.Text;
  195. };
  196. // TextRegexProvider - Regex provider implemented by Terminal.Gui
  197. var regexProvider = new Label ("TextRegexProvider [ ^([0-9]?[0-9]?[0-9]|1000)$ ]") {
  198. X = Pos.Left (netProviderLabel),
  199. Y = Pos.Bottom (netProviderLabel) + 1
  200. };
  201. Win.Add (regexProvider);
  202. var provider2 = new TextRegexProvider ("^([0-9]?[0-9]?[0-9]|1000)$") { ValidateOnInput = false };
  203. var regexProviderField = new TextValidateField (provider2) {
  204. X = Pos.Right (regexProvider) + 1,
  205. Y = Pos.Y (regexProvider),
  206. Width = 30,
  207. TextAlignment = TextAlignment.Centered
  208. };
  209. Win.Add (regexProviderField);
  210. var labelMirroringRegexProviderField = new Label (regexProviderField.Text) {
  211. X = Pos.Right (regexProviderField) + 1,
  212. Y = Pos.Top (regexProviderField),
  213. Width = Dim.Width (regexProviderField),
  214. Height = Dim.Height (regexProviderField)
  215. };
  216. Win.Add (labelMirroringRegexProviderField);
  217. regexProviderField.Provider.TextChanged += (s, prev) => {
  218. labelMirroringRegexProviderField.Text = regexProviderField.Text;
  219. };
  220. var labelAppendAutocomplete = new Label ("Append Autocomplete:") {
  221. Y = Pos.Y (regexProviderField) + 2,
  222. X = 1
  223. };
  224. var appendAutocompleteTextField = new TextField {
  225. X = Pos.Right (labelAppendAutocomplete),
  226. Y = Pos.Top (labelAppendAutocomplete),
  227. Width = Dim.Fill ()
  228. };
  229. appendAutocompleteTextField.Autocomplete = new AppendAutocomplete (appendAutocompleteTextField);
  230. appendAutocompleteTextField.Autocomplete.SuggestionGenerator = new SingleWordSuggestionGenerator {
  231. AllSuggestions = new List<string> {
  232. "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",
  233. "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"
  234. }
  235. };
  236. Win.Add (labelAppendAutocomplete);
  237. Win.Add (appendAutocompleteTextField);
  238. }
  239. void TimeChanged (object sender, DateTimeEventArgs<TimeSpan> e) => _labelMirroringTimeField.Text = _timeField.Text;
  240. }