Text.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 = true
  80. };
  81. chxMultiline.Toggled += (s,e) => textView.Multiline = (bool)e.OldValue;
  82. Win.Add (chxMultiline);
  83. var chxWordWrap = new CheckBox ("Word Wrap") {
  84. X = Pos.Right (chxMultiline) + 2,
  85. Y = Pos.Top (chxMultiline)
  86. };
  87. chxWordWrap.Toggled += (s,e) => textView.WordWrap = (bool)e.OldValue;
  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 = true
  96. };
  97. Key keyTab = textView.GetKeyFromCommand (Command.Tab);
  98. Key keyBackTab = textView.GetKeyFromCommand (Command.BackTab);
  99. chxCaptureTabs.Toggled += (s,e) => {
  100. if (e.OldValue == true) {
  101. textView.AddKeyBinding (keyTab, Command.Tab);
  102. textView.AddKeyBinding (keyBackTab, Command.BackTab);
  103. } else {
  104. textView.ClearKeybinding (keyTab);
  105. textView.ClearKeybinding (keyBackTab);
  106. }
  107. textView.WordWrap = (bool)e.OldValue;
  108. };
  109. Win.Add (chxCaptureTabs);
  110. var hexEditor = new HexView (new MemoryStream (Encoding.UTF8.GetBytes ("HexEditor Unicode that shouldn't 𝔹Aℝ𝔽!"))) {
  111. X = 1,
  112. Y = Pos.Bottom (chxMultiline) + 1,
  113. Width = Dim.Percent (50) - 1,
  114. Height = Dim.Percent (30),
  115. };
  116. Win.Add (hexEditor);
  117. var labelMirroringHexEditor = new Label () {
  118. X = Pos.Right (hexEditor) + 1,
  119. Y = Pos.Top (hexEditor),
  120. Width = Dim.Fill (1) - 1,
  121. Height = Dim.Height (hexEditor) - 1,
  122. };
  123. var array = ((MemoryStream)hexEditor.Source).ToArray ();
  124. labelMirroringHexEditor.Text = Encoding.UTF8.GetString (array, 0, array.Length);
  125. hexEditor.Edited += (s,kv) => {
  126. hexEditor.ApplyEdits ();
  127. var array = ((MemoryStream)hexEditor.Source).ToArray ();
  128. labelMirroringHexEditor.Text = Encoding.UTF8.GetString (array, 0, array.Length);
  129. };
  130. Win.Add (labelMirroringHexEditor);
  131. var dateField = new DateField (System.DateTime.Now) {
  132. X = 1,
  133. Y = Pos.Bottom (hexEditor) + 1,
  134. Width = 20,
  135. IsShortFormat = false
  136. };
  137. Win.Add (dateField);
  138. var labelMirroringDateField = new Label (dateField.Text) {
  139. X = Pos.Right (dateField) + 1,
  140. Y = Pos.Top (dateField),
  141. Width = Dim.Width (dateField),
  142. Height = Dim.Height (dateField),
  143. };
  144. Win.Add (labelMirroringDateField);
  145. dateField.TextChanged += (s, prev) => {
  146. labelMirroringDateField.Text = dateField.Text;
  147. };
  148. _timeField = new TimeField (DateTime.Now.TimeOfDay) {
  149. X = Pos.Right (labelMirroringDateField) + 5,
  150. Y = Pos.Bottom (hexEditor) + 1,
  151. Width = 20,
  152. IsShortFormat = false
  153. };
  154. Win.Add (_timeField);
  155. _labelMirroringTimeField = new Label (_timeField.Text) {
  156. X = Pos.Right (_timeField) + 1,
  157. Y = Pos.Top (_timeField),
  158. Width = Dim.Width (_timeField),
  159. Height = Dim.Height (_timeField),
  160. };
  161. Win.Add (_labelMirroringTimeField);
  162. _timeField.TimeChanged += TimeChanged;
  163. // MaskedTextProvider - uses .NET MaskedTextProvider
  164. var netProviderLabel = new Label ("NetMaskedTextProvider [ 999 000 LLL >LLL| AAA aaa ]") {
  165. X = Pos.Left (dateField),
  166. Y = Pos.Bottom (dateField) + 1
  167. };
  168. Win.Add (netProviderLabel);
  169. var netProvider = new NetMaskedTextProvider ("999 000 LLL > LLL | AAA aaa");
  170. var netProviderField = new TextValidateField (netProvider) {
  171. X = Pos.Right (netProviderLabel) + 1,
  172. Y = Pos.Y (netProviderLabel),
  173. };
  174. Win.Add (netProviderField);
  175. // TextRegexProvider - Regex provider implemented by Terminal.Gui
  176. var regexProvider = new Label ("TextRegexProvider [ ^([0-9]?[0-9]?[0-9]|1000)$ ]") {
  177. X = Pos.Left (netProviderLabel),
  178. Y = Pos.Bottom (netProviderLabel) + 1
  179. };
  180. Win.Add (regexProvider);
  181. var provider2 = new TextRegexProvider ("^([0-9]?[0-9]?[0-9]|1000)$") { ValidateOnInput = false };
  182. var regexProviderField = new TextValidateField (provider2) {
  183. X = Pos.Right (regexProvider) + 1,
  184. Y = Pos.Y (regexProvider),
  185. Width = 30,
  186. TextAlignment = TextAlignment.Centered
  187. };
  188. Win.Add (regexProviderField);
  189. var labelAppendAutocomplete = new Label ("Append Autocomplete:") {
  190. Y = Pos.Y (regexProviderField) + 2,
  191. X = 1
  192. };
  193. var appendAutocompleteTextField = new TextField () {
  194. X = Pos.Right(labelAppendAutocomplete),
  195. Y = labelAppendAutocomplete.Y,
  196. Width = Dim.Fill()
  197. };
  198. appendAutocompleteTextField.Autocomplete = new AppendAutocomplete (appendAutocompleteTextField);
  199. appendAutocompleteTextField.Autocomplete.SuggestionGenerator = new SingleWordSuggestionGenerator {
  200. AllSuggestions = new System.Collections.Generic.List<string>{
  201. "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"
  202. }
  203. };
  204. Win.Add (labelAppendAutocomplete);
  205. Win.Add (appendAutocompleteTextField);
  206. }
  207. TimeField _timeField;
  208. Label _labelMirroringTimeField;
  209. private void TimeChanged (object sender, DateTimeEventArgs<TimeSpan> e)
  210. {
  211. _labelMirroringTimeField.Text = _timeField.Text;
  212. }
  213. }
  214. }