CollectionNavigatorTester.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  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 ObservableCollection<string> _items = new ObservableCollection<string> (new ObservableCollection<string> ()
  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. "apricot",
  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 Toplevel top;
  74. private ListView _listView;
  75. private TreeView _treeView;
  76. // Don't create a Window, just return the top-level view
  77. public override void Main ()
  78. {
  79. Application.Init ();
  80. top = new Toplevel { SchemeName = "Base" };
  81. var allowMarking = new MenuItem ("Allow _Marking", "", null)
  82. {
  83. CheckType = MenuItemCheckStyle.Checked, Checked = false
  84. };
  85. allowMarking.Action = () => allowMarking.Checked = _listView.AllowsMarking = !_listView.AllowsMarking;
  86. var allowMultiSelection = new MenuItem ("Allow Multi _Selection", "", null)
  87. {
  88. CheckType = MenuItemCheckStyle.Checked, Checked = false
  89. };
  90. allowMultiSelection.Action = () =>
  91. allowMultiSelection.Checked =
  92. _listView.AllowsMultipleSelection = !_listView.AllowsMultipleSelection;
  93. allowMultiSelection.CanExecute = () => (bool)allowMarking.Checked;
  94. var menu = new MenuBar
  95. {
  96. Menus =
  97. [
  98. new MenuBarItem (
  99. "_Configure",
  100. new []
  101. {
  102. allowMarking,
  103. allowMultiSelection,
  104. null,
  105. new (
  106. "_Quit",
  107. $"{Application.QuitKey}",
  108. () => Quit (),
  109. null,
  110. null,
  111. (KeyCode)Application.QuitKey
  112. )
  113. }
  114. ),
  115. new MenuBarItem ("_Quit", $"{Application.QuitKey}", () => Quit ())
  116. ]
  117. };
  118. top.Add (menu);
  119. _items = new (_items.OrderBy (i => i, StringComparer.OrdinalIgnoreCase));
  120. CreateListView ();
  121. var vsep = new Line { Orientation = Orientation.Vertical, X = Pos.Right (_listView), Y = 1, Height = Dim.Fill () };
  122. top.Add (vsep);
  123. CreateTreeView ();
  124. Application.Run (top);
  125. top.Dispose ();
  126. Application.Shutdown ();
  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. }