ClassExplorer.cs 7.2 KB

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