ClassExplorer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. Application.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. Application.Top.Add (menu);
  73. treeView = new TreeView<object> () {
  74. X = 0,
  75. Y = 1,
  76. Width = Dim.Percent (50),
  77. Height = Dim.Fill (),
  78. };
  79. var lblSearch = new Label("Search");
  80. var tfSearch = new TextField(){
  81. Width = 20,
  82. X = Pos.Right(lblSearch),
  83. };
  84. Win.Add(lblSearch);
  85. Win.Add(tfSearch);
  86. var filter = new TreeViewTextFilter<object>(treeView);
  87. treeView.Filter = filter;
  88. tfSearch.TextChanged += (s)=>{
  89. filter.Text = tfSearch.Text.ToString();
  90. if(treeView.SelectedObject != null)
  91. {
  92. treeView.EnsureVisible(treeView.SelectedObject);
  93. }
  94. };
  95. treeView.AddObjects (AppDomain.CurrentDomain.GetAssemblies ());
  96. treeView.AspectGetter = GetRepresentation;
  97. treeView.TreeBuilder = new DelegateTreeBuilder<object> (ChildGetter, CanExpand);
  98. treeView.SelectionChanged += TreeView_SelectionChanged;
  99. Win.Add (treeView);
  100. textView = new TextView () {
  101. X = Pos.Right (treeView),
  102. Y = 0,
  103. Width = Dim.Fill (),
  104. Height = Dim.Fill ()
  105. };
  106. Win.Add (textView);
  107. }
  108. private void OnCheckHighlightModelTextOnly ()
  109. {
  110. treeView.Style.HighlightModelTextOnly = !treeView.Style.HighlightModelTextOnly;
  111. highlightModelTextOnly.Checked = treeView.Style.HighlightModelTextOnly;
  112. treeView.SetNeedsDisplay ();
  113. }
  114. private void ShowPrivate ()
  115. {
  116. miShowPrivate.Checked = !miShowPrivate.Checked;
  117. treeView.RebuildTree ();
  118. treeView.SetFocus ();
  119. }
  120. private BindingFlags GetFlags ()
  121. {
  122. if (miShowPrivate.Checked) {
  123. return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
  124. }
  125. return BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
  126. }
  127. private void TreeView_SelectionChanged (object sender, SelectionChangedEventArgs<object> e)
  128. {
  129. var val = e.NewValue;
  130. var all = treeView.GetAllSelectedObjects ().ToArray ();
  131. if (val == null || val is ShowForType) {
  132. return;
  133. }
  134. try {
  135. if (all.Length > 1) {
  136. textView.Text = all.Length + " Objects";
  137. } else {
  138. StringBuilder sb = new StringBuilder ();
  139. // tell the user about the currently selected tree node
  140. sb.AppendLine (e.NewValue.GetType ().Name);
  141. if (val is Assembly ass) {
  142. sb.AppendLine ($"Location:{ass.Location}");
  143. sb.AppendLine ($"FullName:{ass.FullName}");
  144. }
  145. if (val is PropertyInfo p) {
  146. sb.AppendLine ($"Name:{p.Name}");
  147. sb.AppendLine ($"Type:{p.PropertyType}");
  148. sb.AppendLine ($"CanWrite:{p.CanWrite}");
  149. sb.AppendLine ($"CanRead:{p.CanRead}");
  150. }
  151. if (val is FieldInfo f) {
  152. sb.AppendLine ($"Name:{f.Name}");
  153. sb.AppendLine ($"Type:{f.FieldType}");
  154. }
  155. if (val is EventInfo ev) {
  156. sb.AppendLine ($"Name:{ev.Name}");
  157. sb.AppendLine ($"Parameters:");
  158. foreach (var parameter in ev.EventHandlerType.GetMethod ("Invoke").GetParameters ()) {
  159. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  160. }
  161. }
  162. if (val is MethodInfo method) {
  163. sb.AppendLine ($"Name:{method.Name}");
  164. sb.AppendLine ($"IsPublic:{method.IsPublic}");
  165. sb.AppendLine ($"IsStatic:{method.IsStatic}");
  166. sb.AppendLine ($"Parameters:{(method.GetParameters ().Any () ? "" : "None")}");
  167. foreach (var parameter in method.GetParameters ()) {
  168. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  169. }
  170. }
  171. if (val is ConstructorInfo ctor) {
  172. sb.AppendLine ($"Name:{ctor.Name}");
  173. sb.AppendLine ($"Parameters:{(ctor.GetParameters ().Any () ? "" : "None")}");
  174. foreach (var parameter in ctor.GetParameters ()) {
  175. sb.AppendLine ($" {parameter.ParameterType} {parameter.Name}");
  176. }
  177. }
  178. textView.Text = sb.ToString ().Replace ("\r\n", "\n");
  179. }
  180. } catch (Exception ex) {
  181. textView.Text = ex.Message;
  182. }
  183. textView.SetNeedsDisplay ();
  184. }
  185. private bool CanExpand (object arg)
  186. {
  187. return arg is Assembly || arg is Type || arg is ShowForType;
  188. }
  189. private IEnumerable<object> ChildGetter (object arg)
  190. {
  191. try {
  192. if (arg is Assembly a) {
  193. return a.GetTypes ();
  194. }
  195. if (arg is Type t) {
  196. // Note that here we cannot simply return the enum values as the same object cannot appear under multiple branches
  197. return Enum.GetValues (typeof (Showable))
  198. .Cast<Showable> ()
  199. // Although we new the Type every time the delegate is called state is preserved because the class has appropriate equality members
  200. .Select (v => new ShowForType (v, t));
  201. }
  202. if (arg is ShowForType show) {
  203. switch (show.ToShow) {
  204. case Showable.Properties:
  205. return show.Type.GetProperties (GetFlags ());
  206. case Showable.Constructors:
  207. return show.Type.GetConstructors (GetFlags ());
  208. case Showable.Events:
  209. return show.Type.GetEvents (GetFlags ());
  210. case Showable.Fields:
  211. return show.Type.GetFields (GetFlags ());
  212. case Showable.Methods:
  213. return show.Type.GetMethods (GetFlags ());
  214. }
  215. }
  216. } catch (Exception) {
  217. return Enumerable.Empty<object> ();
  218. }
  219. return Enumerable.Empty<object> ();
  220. }
  221. private string GetRepresentation (object model)
  222. {
  223. try {
  224. if (model is Assembly ass) {
  225. return ass.GetName ().Name;
  226. }
  227. if (model is PropertyInfo p) {
  228. return p.Name;
  229. }
  230. if (model is FieldInfo f) {
  231. return f.Name;
  232. }
  233. if (model is EventInfo ei) {
  234. return ei.Name;
  235. }
  236. } catch (Exception ex) {
  237. return ex.Message;
  238. }
  239. return model.ToString ();
  240. }
  241. private void Quit ()
  242. {
  243. Application.RequestStop ();
  244. }
  245. }
  246. }