CollectionNavigatorTester.cs 6.1 KB

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