CollectionNavigatorTester.cs 4.6 KB

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