CollectionNavigatorTester.cs 4.6 KB

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