Text.cs 8.7 KB

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