CollectionNavigatorTester.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata (
  7. "Collection Navigator",
  8. "Demonstrates keyboard navigation in ListView & TreeView (CollectionNavigator)."
  9. )]
  10. [ScenarioCategory ("Controls")]
  11. [ScenarioCategory ("ListView")]
  12. [ScenarioCategory ("TreeView")]
  13. [ScenarioCategory ("Text and Formatting")]
  14. [ScenarioCategory ("Mouse and Keyboard")]
  15. public class CollectionNavigatorTester : Scenario
  16. {
  17. private readonly List<string> _items = new []
  18. {
  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 ();
  73. private ListView _listView;
  74. private TreeView _treeView;
  75. // Don't create a Window, just return the top-level view
  76. public override void Init ()
  77. {
  78. Application.Init ();
  79. Top = new ();
  80. Top.ColorScheme = Colors.ColorSchemes ["Base"];
  81. }
  82. public override void Setup ()
  83. {
  84. var allowMarking = new MenuItem ("Allow _Marking", "", null)
  85. {
  86. CheckType = MenuItemCheckStyle.Checked, Checked = false
  87. };
  88. allowMarking.Action = () => allowMarking.Checked = _listView.AllowsMarking = !_listView.AllowsMarking;
  89. var allowMultiSelection = new MenuItem ("Allow Multi _Selection", "", null)
  90. {
  91. CheckType = MenuItemCheckStyle.Checked, Checked = false
  92. };
  93. allowMultiSelection.Action = () =>
  94. allowMultiSelection.Checked =
  95. _listView.AllowsMultipleSelection = !_listView.AllowsMultipleSelection;
  96. allowMultiSelection.CanExecute = () => (bool)allowMarking.Checked;
  97. var menu = new MenuBar
  98. {
  99. Menus =
  100. [
  101. new MenuBarItem (
  102. "_Configure",
  103. new []
  104. {
  105. allowMarking,
  106. allowMultiSelection,
  107. null,
  108. new (
  109. "_Quit",
  110. $"{Application.QuitKey}",
  111. () => Quit (),
  112. null,
  113. null,
  114. (KeyCode)Application.QuitKey
  115. )
  116. }
  117. ),
  118. new MenuBarItem ("_Quit", $"{Application.QuitKey}", () => Quit ())
  119. ]
  120. };
  121. Top.Add (menu);
  122. _items.Sort (StringComparer.OrdinalIgnoreCase);
  123. CreateListView ();
  124. var vsep = new LineView (Orientation.Vertical) { X = Pos.Right (_listView), Y = 1, Height = Dim.Fill () };
  125. Top.Add (vsep);
  126. CreateTreeView ();
  127. }
  128. private void CreateListView ()
  129. {
  130. var label = new Label
  131. {
  132. Text = "ListView",
  133. TextAlignment = Alignment.Center,
  134. X = 0,
  135. Y = 1, // for menu
  136. Width = Dim.Percent (50),
  137. Height = 1
  138. };
  139. Top.Add (label);
  140. _listView = new ListView
  141. {
  142. X = 0,
  143. Y = Pos.Bottom (label),
  144. Width = Dim.Percent (50) - 1,
  145. Height = Dim.Fill (),
  146. AllowsMarking = false,
  147. AllowsMultipleSelection = false
  148. };
  149. Top.Add (_listView);
  150. _listView.SetSource (_items);
  151. _listView.KeystrokeNavigator.SearchStringChanged += (s, e) => { label.Text = $"ListView: {e.SearchString}"; };
  152. }
  153. private void CreateTreeView ()
  154. {
  155. var label = new Label
  156. {
  157. Text = "TreeView",
  158. TextAlignment = Alignment.Center,
  159. X = Pos.Right (_listView) + 2,
  160. Y = 1, // for menu
  161. Width = Dim.Percent (50),
  162. Height = 1
  163. };
  164. Top.Add (label);
  165. _treeView = new TreeView
  166. {
  167. X = Pos.Right (_listView) + 1, Y = Pos.Bottom (label), Width = Dim.Fill (), Height = Dim.Fill ()
  168. };
  169. _treeView.Style.HighlightModelTextOnly = true;
  170. Top.Add (_treeView);
  171. var root = new TreeNode ("IsLetterOrDigit examples");
  172. root.Children = _items.Where (i => char.IsLetterOrDigit (i [0]))
  173. .Select (i => new TreeNode (i))
  174. .Cast<ITreeNode> ()
  175. .ToList ();
  176. _treeView.AddObject (root);
  177. root = new TreeNode ("Non-IsLetterOrDigit examples");
  178. root.Children = _items.Where (i => !char.IsLetterOrDigit (i [0]))
  179. .Select (i => new TreeNode (i))
  180. .Cast<ITreeNode> ()
  181. .ToList ();
  182. _treeView.AddObject (root);
  183. _treeView.ExpandAll ();
  184. _treeView.GoToFirst ();
  185. _treeView.KeystrokeNavigator.SearchStringChanged += (s, e) => { label.Text = $"TreeView: {e.SearchString}"; };
  186. }
  187. private void Quit () { Application.RequestStop (); }
  188. }