ClassExplorer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("Class Explorer", "Tree view explorer for classes by namespace based on TreeView.")]
  9. [ScenarioCategory ("Controls")]
  10. [ScenarioCategory ("TreeView")]
  11. public class ClassExplorer : Scenario
  12. {
  13. private MenuItem _highlightModelTextOnly;
  14. private MenuItem _miShowPrivate;
  15. private TextView _textView;
  16. private TreeView<object> _treeView;
  17. public override void Main ()
  18. {
  19. Application.Init ();
  20. var top = new Toplevel ();
  21. var win = new Window
  22. {
  23. Title = GetName ()
  24. };
  25. var menu = new MenuBar
  26. {
  27. Menus =
  28. [
  29. new MenuBarItem ("_File", new MenuItem [] { new ("_Quit", "", () => Quit ()) }),
  30. new MenuBarItem (
  31. "_View",
  32. new []
  33. {
  34. _miShowPrivate =
  35. new MenuItem (
  36. "_Include Private",
  37. "",
  38. () => ShowPrivate ()
  39. ) { Checked = false, CheckType = MenuItemCheckStyle.Checked },
  40. new ("_Expand All", "", () => _treeView.ExpandAll ()),
  41. new ("_Collapse All", "", () => _treeView.CollapseAll ())
  42. }
  43. ),
  44. new MenuBarItem (
  45. "_Style",
  46. new []
  47. {
  48. _highlightModelTextOnly = new MenuItem (
  49. "_Highlight Model Text Only",
  50. "",
  51. () => OnCheckHighlightModelTextOnly ()
  52. ) { CheckType = MenuItemCheckStyle.Checked }
  53. }
  54. )
  55. ]
  56. };
  57. top.Add (menu);
  58. _treeView = new TreeView<object> { X = 0, Y = 1, Width = Dim.Percent (50), Height = Dim.Fill () };
  59. var lblSearch = new Label { Text = "Search" };
  60. var tfSearch = new TextField { Width = 20, X = Pos.Right (lblSearch) };
  61. win.Add (lblSearch);
  62. win.Add (tfSearch);
  63. TreeViewTextFilter<object> filter = new (_treeView);
  64. _treeView.Filter = filter;
  65. tfSearch.TextChanged += (s, e) =>
  66. {
  67. filter.Text = tfSearch.Text;
  68. if (_treeView.SelectedObject != null)
  69. {
  70. _treeView.EnsureVisible (_treeView.SelectedObject);
  71. }
  72. };
  73. _treeView.AddObjects (AppDomain.CurrentDomain.GetAssemblies ());
  74. _treeView.AspectGetter = GetRepresentation;
  75. _treeView.TreeBuilder = new DelegateTreeBuilder<object> (ChildGetter, CanExpand);
  76. _treeView.SelectionChanged += TreeView_SelectionChanged;
  77. win.Add (_treeView);
  78. _textView = new TextView { X = Pos.Right (_treeView), Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };
  79. win.Add (_textView);
  80. top.Add (win);
  81. Application.Run (top);
  82. top.Dispose ();
  83. Application.Shutdown ();
  84. }
  85. private bool CanExpand (object arg) { return arg is Assembly || arg is Type || arg is ShowForType; }
  86. private IEnumerable<object> ChildGetter (object arg)
  87. {
  88. try
  89. {
  90. if (arg is Assembly a)
  91. {
  92. return a.GetTypes ();
  93. }
  94. if (arg is Type t)
  95. {
  96. // Note that here we cannot simply return the enum values as the same object cannot appear under multiple branches
  97. return Enum.GetValues (typeof (Showable))
  98. .Cast<Showable> ()
  99. // Although we new the Type every time the delegate is called state is preserved because the class has appropriate equality members
  100. .Select (v => new ShowForType (v, t));
  101. }
  102. if (arg is ShowForType show)
  103. {
  104. switch (show.ToShow)
  105. {
  106. case Showable.Properties:
  107. return show.Type.GetProperties (GetFlags ());
  108. case Showable.Constructors:
  109. return show.Type.GetConstructors (GetFlags ());
  110. case Showable.Events:
  111. return show.Type.GetEvents (GetFlags ());
  112. case Showable.Fields:
  113. return show.Type.GetFields (GetFlags ());
  114. case Showable.Methods:
  115. return show.Type.GetMethods (GetFlags ());
  116. }
  117. }
  118. }
  119. catch (Exception)
  120. {
  121. return Enumerable.Empty<object> ();
  122. }
  123. return Enumerable.Empty<object> ();
  124. }
  125. private BindingFlags GetFlags ()
  126. {
  127. if (_miShowPrivate.Checked == true)
  128. {
  129. return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
  130. }
  131. return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  132. }
  133. private string GetRepresentation (object model)
  134. {
  135. try
  136. {
  137. if (model is Assembly ass)
  138. {
  139. return ass.GetName ().Name;
  140. }
  141. if (model is PropertyInfo p)
  142. {
  143. return p.Name;
  144. }
  145. if (model is FieldInfo f)
  146. {
  147. return f.Name;
  148. }
  149. if (model is EventInfo ei)
  150. {
  151. return ei.Name;
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. return ex.Message;
  157. }
  158. return model.ToString ();
  159. }
  160. private void OnCheckHighlightModelTextOnly ()
  161. {
  162. _treeView.Style.HighlightModelTextOnly = !_treeView.Style.HighlightModelTextOnly;
  163. _highlightModelTextOnly.Checked = _treeView.Style.HighlightModelTextOnly;
  164. _treeView.SetNeedsDisplay ();
  165. }
  166. private void Quit () { Application.RequestStop (); }
  167. private void ShowPrivate ()
  168. {
  169. _miShowPrivate.Checked = !_miShowPrivate.Checked;
  170. _treeView.RebuildTree ();
  171. _treeView.SetFocus ();
  172. }
  173. private void TreeView_SelectionChanged (object sender, SelectionChangedEventArgs<object> e)
  174. {
  175. object val = e.NewValue;
  176. object [] all = _treeView.GetAllSelectedObjects ().ToArray ();
  177. if (val == null || val is ShowForType)
  178. {
  179. return;
  180. }
  181. try
  182. {
  183. if (all.Length > 1)
  184. {
  185. _textView.Text = all.Length + " Objects";
  186. }
  187. else
  188. {
  189. var sb = new StringBuilder ();
  190. // tell the user about the currently selected tree node
  191. sb.AppendLine (e.NewValue.GetType ().Name);
  192. if (val is Assembly ass)
  193. {
  194. sb.AppendLine ($"Location:{ass.Location}");
  195. sb.AppendLine ($"FullName:{ass.FullName}");
  196. }
  197. if (val is PropertyInfo p)
  198. {
  199. sb.AppendLine ($"Name:{p.Name}");
  200. sb.AppendLine ($"Type:{p.PropertyType}");
  201. sb.AppendLine ($"CanWrite:{p.CanWrite}");
  202. sb.AppendLine ($"CanRead:{p.CanRead}");
  203. }
  204. if (val is FieldInfo f)
  205. {
  206. sb.AppendLine ($"Name:{f.Name}");
  207. sb.AppendLine ($"Type:{f.FieldType}");
  208. }
  209. if (val is EventInfo ev)
  210. {
  211. sb.AppendLine ($"Name:{ev.Name}");
  212. sb.AppendLine ("Parameters:");
  213. foreach (ParameterInfo parameter in ev.EventHandlerType.GetMethod ("Invoke")
  214. .GetParameters ())
  215. {
  216. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  217. }
  218. }
  219. if (val is MethodInfo method)
  220. {
  221. sb.AppendLine ($"Name:{method.Name}");
  222. sb.AppendLine ($"IsPublic:{method.IsPublic}");
  223. sb.AppendLine ($"IsStatic:{method.IsStatic}");
  224. sb.AppendLine ($"Parameters:{(method.GetParameters ().Any () ? "" : "None")}");
  225. foreach (ParameterInfo parameter in method.GetParameters ())
  226. {
  227. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  228. }
  229. }
  230. if (val is ConstructorInfo ctor)
  231. {
  232. sb.AppendLine ($"Name:{ctor.Name}");
  233. sb.AppendLine ($"Parameters:{(ctor.GetParameters ().Any () ? "" : "None")}");
  234. foreach (ParameterInfo parameter in ctor.GetParameters ())
  235. {
  236. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  237. }
  238. }
  239. _textView.Text = sb.ToString ().Replace ("\r\n", "\n");
  240. }
  241. }
  242. catch (Exception ex)
  243. {
  244. _textView.Text = ex.Message;
  245. }
  246. _textView.SetNeedsDisplay ();
  247. }
  248. private enum Showable
  249. {
  250. Properties,
  251. Fields,
  252. Events,
  253. Constructors,
  254. Methods
  255. }
  256. private class ShowForType
  257. {
  258. public ShowForType (Showable toShow, Type type)
  259. {
  260. ToShow = toShow;
  261. Type = type;
  262. }
  263. public Showable ToShow { get; }
  264. public Type Type { get; }
  265. // Make sure to implement Equals methods on your objects if you intend to return new instances every time in ChildGetter
  266. public override bool Equals (object obj)
  267. {
  268. return obj is ShowForType type && EqualityComparer<Type>.Default.Equals (Type, type.Type) && ToShow == type.ToShow;
  269. }
  270. public override int GetHashCode () { return HashCode.Combine (Type, ToShow); }
  271. public override string ToString () { return ToShow.ToString (); }
  272. }
  273. }