ClassExplorer.cs 11 KB

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