ClassExplorer.cs 7.3 KB

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