CollectionNavigatorTester.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #nullable enable
  2. using System.Collections.ObjectModel;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata (
  5. "Collection Navigator",
  6. "Demonstrates keyboard navigation in ListView & TreeView (CollectionNavigator)."
  7. )]
  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. {
  15. private ObservableCollection<string> _items = new (
  16. [
  17. "a",
  18. "b",
  19. "bb",
  20. "c",
  21. "ccc",
  22. "ccc",
  23. "cccc",
  24. "ddd",
  25. "dddd",
  26. "dddd",
  27. "ddddd",
  28. "dddddd",
  29. "ddddddd",
  30. "this",
  31. "this is a test",
  32. "this was a test",
  33. "this and",
  34. "that and that",
  35. "the",
  36. "think",
  37. "thunk",
  38. "thunks",
  39. "zip",
  40. "zap",
  41. "zoo",
  42. "@jack",
  43. "@sign",
  44. "@at",
  45. "@ateme",
  46. "n@",
  47. "n@brown",
  48. ".net",
  49. "$100.00",
  50. "$101.00",
  51. "$101.10",
  52. "$101.11",
  53. "$200.00",
  54. "$210.99",
  55. "$$",
  56. "apricot",
  57. "arm",
  58. "丗丙业丞",
  59. "丗丙丛",
  60. "text",
  61. "egg",
  62. "candle",
  63. " <- space",
  64. "\t<- tab",
  65. "\n<- newline",
  66. "\r<- formfeed",
  67. "q",
  68. "quit",
  69. "quitter"
  70. ]
  71. );
  72. private Window? _top;
  73. private ListView? _listView;
  74. private TreeView? _treeView;
  75. private CheckBox? _allowMarkingCheckBox;
  76. private CheckBox? _allowMultiSelectionCheckBox;
  77. public override void Main ()
  78. {
  79. Application.Init ();
  80. Window top = new ()
  81. {
  82. SchemeName = "Base"
  83. };
  84. _top = top;
  85. // MenuBar
  86. MenuBar menu = new ();
  87. _allowMarkingCheckBox = new ()
  88. {
  89. Title = "Allow _Marking"
  90. };
  91. _allowMarkingCheckBox.CheckedStateChanged += (s, e) =>
  92. {
  93. if (_listView is { })
  94. {
  95. _listView.AllowsMarking = _allowMarkingCheckBox.CheckedState == CheckState.Checked;
  96. }
  97. if (_allowMultiSelectionCheckBox is { })
  98. {
  99. _allowMultiSelectionCheckBox.Enabled = _allowMarkingCheckBox.CheckedState == CheckState.Checked;
  100. }
  101. };
  102. _allowMultiSelectionCheckBox = new ()
  103. {
  104. Title = "Allow Multi _Selection",
  105. Enabled = false
  106. };
  107. _allowMultiSelectionCheckBox.CheckedStateChanged += (s, e) =>
  108. {
  109. if (_listView is { })
  110. {
  111. _listView.AllowsMultipleSelection =
  112. _allowMultiSelectionCheckBox.CheckedState == CheckState.Checked;
  113. }
  114. };
  115. menu.Add (
  116. new MenuBarItem (
  117. "_Configure",
  118. [
  119. new MenuItem
  120. {
  121. CommandView = _allowMarkingCheckBox
  122. },
  123. new MenuItem
  124. {
  125. CommandView = _allowMultiSelectionCheckBox
  126. },
  127. new MenuItem
  128. {
  129. Title = "_Quit",
  130. Key = Application.QuitKey,
  131. Action = Quit
  132. }
  133. ]
  134. )
  135. );
  136. menu.Add (
  137. new MenuBarItem (
  138. "_Quit",
  139. [
  140. new MenuItem
  141. {
  142. Title = "_Quit",
  143. Key = Application.QuitKey,
  144. Action = Quit
  145. }
  146. ]
  147. )
  148. );
  149. top.Add (menu);
  150. _items = new (_items.OrderBy (i => i, StringComparer.OrdinalIgnoreCase));
  151. CreateListView ();
  152. Line vsep = new ()
  153. {
  154. Orientation = Orientation.Vertical,
  155. X = Pos.Right (_listView!),
  156. Y = 1,
  157. Height = Dim.Fill ()
  158. };
  159. top.Add (vsep);
  160. CreateTreeView ();
  161. Application.Run (top);
  162. top.Dispose ();
  163. Application.Shutdown ();
  164. }
  165. private void CreateListView ()
  166. {
  167. if (_top is null)
  168. {
  169. return;
  170. }
  171. Label label = new ()
  172. {
  173. Text = "ListView",
  174. TextAlignment = Alignment.Center,
  175. X = 0,
  176. Y = 1, // for menu
  177. Width = Dim.Percent (50),
  178. Height = 1
  179. };
  180. _top.Add (label);
  181. _listView = new ()
  182. {
  183. X = 0,
  184. Y = Pos.Bottom (label),
  185. Width = Dim.Percent (50) - 1,
  186. Height = Dim.Fill (),
  187. AllowsMarking = false,
  188. AllowsMultipleSelection = false
  189. };
  190. _top.Add (_listView);
  191. _listView.SetSource (_items);
  192. _listView.KeystrokeNavigator.SearchStringChanged += (s, e) => { label.Text = $"ListView: {e.SearchString}"; };
  193. }
  194. private void CreateTreeView ()
  195. {
  196. if (_top is null || _listView is null)
  197. {
  198. return;
  199. }
  200. Label label = new ()
  201. {
  202. Text = "TreeView",
  203. TextAlignment = Alignment.Center,
  204. X = Pos.Right (_listView) + 2,
  205. Y = 1, // for menu
  206. Width = Dim.Percent (50),
  207. Height = 1
  208. };
  209. _top.Add (label);
  210. _treeView = new ()
  211. {
  212. X = Pos.Right (_listView) + 1,
  213. Y = Pos.Bottom (label),
  214. Width = Dim.Fill (),
  215. Height = Dim.Fill ()
  216. };
  217. _treeView.Style.HighlightModelTextOnly = true;
  218. _top.Add (_treeView);
  219. TreeNode root = new ("IsLetterOrDigit examples");
  220. root.Children = _items.Where (i => char.IsLetterOrDigit (i [0]))
  221. .Select (i => new TreeNode (i))
  222. .Cast<ITreeNode> ()
  223. .ToList ();
  224. _treeView.AddObject (root);
  225. root = new ("Non-IsLetterOrDigit examples");
  226. root.Children = _items.Where (i => !char.IsLetterOrDigit (i [0]))
  227. .Select (i => new TreeNode (i))
  228. .Cast<ITreeNode> ()
  229. .ToList ();
  230. _treeView.AddObject (root);
  231. _treeView.ExpandAll ();
  232. _treeView.GoToFirst ();
  233. _treeView.KeystrokeNavigator.SearchStringChanged += (s, e) => { label.Text = $"TreeView: {e.SearchString}"; };
  234. }
  235. private void Quit () { Application.RequestStop (); }
  236. }