CollectionNavigatorTester.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Linq;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios {
  5. [ScenarioMetadata (Name: "Collection Navigator", Description: "Demonstrates keyboard navigation in ListView & TreeView (CollectionNavigator).")]
  6. [ScenarioCategory ("Controls"),
  7. ScenarioCategory ("ListView"),
  8. ScenarioCategory ("TreeView"),
  9. ScenarioCategory ("Text and Formatting"),
  10. ScenarioCategory ("Mouse and Keyboard")]
  11. public class CollectionNavigatorTester : Scenario {
  12. // Don't create a Window, just return the top-level view
  13. public override void Init ()
  14. {
  15. Application.Init ();
  16. Application.Top.ColorScheme = Colors.ColorSchemes ["Base"];
  17. }
  18. System.Collections.Generic.List<string> _items = new string [] {
  19. "a",
  20. "b",
  21. "bb",
  22. "c",
  23. "ccc",
  24. "ccc",
  25. "cccc",
  26. "ddd",
  27. "dddd",
  28. "dddd",
  29. "ddddd",
  30. "dddddd",
  31. "ddddddd",
  32. "this",
  33. "this is a test",
  34. "this was a test",
  35. "this and",
  36. "that and that",
  37. "the",
  38. "think",
  39. "thunk",
  40. "thunks",
  41. "zip",
  42. "zap",
  43. "zoo",
  44. "@jack",
  45. "@sign",
  46. "@at",
  47. "@ateme",
  48. "n@",
  49. "n@brown",
  50. ".net",
  51. "$100.00",
  52. "$101.00",
  53. "$101.10",
  54. "$101.11",
  55. "$200.00",
  56. "$210.99",
  57. "$$",
  58. "appricot",
  59. "arm",
  60. "丗丙业丞",
  61. "丗丙丛",
  62. "text",
  63. "egg",
  64. "candle",
  65. " <- space",
  66. "\t<- tab",
  67. "\n<- newline",
  68. "\r<- formfeed",
  69. "q",
  70. "quit",
  71. "quitter"
  72. }.ToList<string> ();
  73. public override void Setup ()
  74. {
  75. var allowMarking = new MenuItem ("Allow _Marking", "", null) {
  76. CheckType = MenuItemCheckStyle.Checked,
  77. Checked = false
  78. };
  79. allowMarking.Action = () => allowMarking.Checked = _listView.AllowsMarking = !_listView.AllowsMarking;
  80. var allowMultiSelection = new MenuItem ("Allow Multi _Selection", "", null) {
  81. CheckType = MenuItemCheckStyle.Checked,
  82. Checked = false
  83. };
  84. allowMultiSelection.Action = () => allowMultiSelection.Checked = _listView.AllowsMultipleSelection = !_listView.AllowsMultipleSelection;
  85. allowMultiSelection.CanExecute = () => (bool)allowMarking.Checked;
  86. var menu = new MenuBar (new MenuBarItem [] {
  87. new MenuBarItem ("_Configure", new MenuItem [] {
  88. allowMarking,
  89. allowMultiSelection,
  90. null,
  91. new MenuItem ("_Quit", $"{Application.QuitKey}", () => Quit(), null, null, (KeyCode)Application.QuitKey),
  92. }),
  93. new MenuBarItem("_Quit", $"{Application.QuitKey}", () => Quit()),
  94. });
  95. Application.Top.Add (menu);
  96. _items.Sort (StringComparer.OrdinalIgnoreCase);
  97. CreateListView ();
  98. var vsep = new LineView (Orientation.Vertical) {
  99. X = Pos.Right (_listView),
  100. Y = 1,
  101. Height = Dim.Fill ()
  102. };
  103. Application.Top.Add (vsep);
  104. CreateTreeView ();
  105. }
  106. ListView _listView = null;
  107. private void CreateListView ()
  108. {
  109. var label = new Label () {
  110. Text = "ListView",
  111. TextAlignment = TextAlignment.Centered,
  112. X = 0,
  113. Y = 1, // for menu
  114. Width = Dim.Percent (50),
  115. Height = 1,
  116. };
  117. Application.Top.Add (label);
  118. _listView = new ListView () {
  119. X = 0,
  120. Y = Pos.Bottom (label),
  121. Width = Dim.Percent (50) - 1,
  122. Height = Dim.Fill (),
  123. AllowsMarking = false,
  124. AllowsMultipleSelection = false,
  125. };
  126. Application.Top.Add (_listView);
  127. _listView.SetSource (_items);
  128. _listView.KeystrokeNavigator.SearchStringChanged += (s,e) => {
  129. label.Text = $"ListView: {e.SearchString}";
  130. };
  131. }
  132. TreeView _treeView = null;
  133. private void CreateTreeView ()
  134. {
  135. var label = new Label () {
  136. Text = "TreeView",
  137. TextAlignment = TextAlignment.Centered,
  138. X = Pos.Right (_listView) + 2,
  139. Y = 1, // for menu
  140. Width = Dim.Percent (50),
  141. Height = 1,
  142. };
  143. Application.Top.Add (label);
  144. _treeView = new TreeView () {
  145. X = Pos.Right (_listView) + 1,
  146. Y = Pos.Bottom (label),
  147. Width = Dim.Fill (),
  148. Height = Dim.Fill ()
  149. };
  150. _treeView.Style.HighlightModelTextOnly = true;
  151. Application.Top.Add (_treeView);
  152. var root = new TreeNode ("IsLetterOrDigit examples");
  153. root.Children = _items.Where (i => char.IsLetterOrDigit (i [0])).Select (i => new TreeNode (i)).Cast<ITreeNode> ().ToList ();
  154. _treeView.AddObject (root);
  155. root = new TreeNode ("Non-IsLetterOrDigit examples");
  156. root.Children = _items.Where (i => !char.IsLetterOrDigit (i [0])).Select (i => new TreeNode (i)).Cast<ITreeNode> ().ToList ();
  157. _treeView.AddObject (root);
  158. _treeView.ExpandAll ();
  159. _treeView.GoToFirst ();
  160. _treeView.KeystrokeNavigator.SearchStringChanged += (s,e) => {
  161. label.Text = $"TreeView: {e.SearchString}";
  162. };
  163. }
  164. private void Quit ()
  165. {
  166. Application.RequestStop ();
  167. }
  168. }
  169. }