CollectionNavigatorTester.cs 6.2 KB

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