TreeView.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // This code is based on http://objectlistview.sourceforge.net (GPLv3 tree/list controls by [email protected])
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Terminal.Gui {
  6. public class TreeView : View
  7. {
  8. /// <summary>
  9. /// Default implementation of a <see cref="ChildrenGetterDelegate"/>, returns an empty collection (i.e. no children)
  10. /// </summary>
  11. static ChildrenGetterDelegate DefaultChildrenGetter = (s)=>{return new object[0];};
  12. /// <summary>
  13. /// This is the delegate that will be used to fetch the children of a model object
  14. /// </summary>
  15. public ChildrenGetterDelegate ChildrenGetter {
  16. get { return childrenGetter ?? DefaultChildrenGetter; }
  17. set { childrenGetter = value; }
  18. }
  19. private ChildrenGetterDelegate childrenGetter;
  20. /// <summary>
  21. /// The currently selected object in the tree
  22. /// </summary>
  23. public object SelectedObject {get;set;}
  24. /// <summary>
  25. /// The root objects in the tree, note that this collection is of root objects only
  26. /// </summary>
  27. public IEnumerable<object> Objects {get=>roots.Keys;}
  28. /// <summary>
  29. /// Map of root objects to the branches under them. All objects have a <see cref="Branch"/> even if that branch has no children
  30. /// </summary>
  31. Dictionary<object,Branch> roots {get; set;} = new Dictionary<object, Branch>();
  32. /// <summary>
  33. /// The amount of tree view that has been scrolled off the top of the screen (by the user scrolling down)
  34. /// </summary>
  35. public int ScrollOffset {get; private set;}
  36. public TreeView ()
  37. {
  38. CanFocus = true;
  39. }
  40. /// <summary>
  41. /// Adds a new root level object unless it is already a root of the tree
  42. /// </summary>
  43. /// <param name="o"></param>
  44. public void AddObject(object o)
  45. {
  46. if(!roots.ContainsKey(o)) {
  47. roots.Add(o,new Branch(this,null,o));
  48. SetNeedsDisplay();
  49. }
  50. }
  51. /// <summary>
  52. /// Adds many new root level objects. Objects that are already root objects are ignored
  53. /// </summary>
  54. /// <param name="o"></param>
  55. public void AddObjects(IEnumerable<object> collection)
  56. {
  57. bool objectsAdded = false;
  58. foreach(var o in collection) {
  59. if (!roots.ContainsKey (o)) {
  60. roots.Add(o,new Branch(this,null,o));
  61. objectsAdded = true;
  62. }
  63. }
  64. if(objectsAdded)
  65. SetNeedsDisplay();
  66. }
  67. /// <summary>
  68. /// Returns the string representation of model objects hosted in the tree. Default implementation is to call <see cref="object.ToString"/>
  69. /// </summary>
  70. /// <value></value>
  71. public Func<object,string> AspectGetter {get;set;} = (o)=>o.ToString();
  72. ///<inheritdoc/>
  73. public override void Redraw (Rect bounds)
  74. {
  75. if(roots == null)
  76. return;
  77. var map = BuildLineMap();
  78. for(int line = 0 ; line < bounds.Height; line++){
  79. var idxToRender = ScrollOffset + line;
  80. // Is there part of the tree view to render?
  81. if(idxToRender < map.Length) {
  82. // Render the line
  83. map[idxToRender].Draw(Driver,ColorScheme,line,bounds.Width);
  84. } else {
  85. // Else clear the line to prevent stale symbols due to scrolling etc
  86. Move(0,line);
  87. Driver.SetAttribute(ColorScheme.Normal);
  88. Driver.AddStr(new string(' ',bounds.Width));
  89. }
  90. }
  91. }
  92. /// <summary>
  93. /// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen
  94. /// </summary>
  95. /// <remarks>Index 0 of the returned array is the first item that should be visible in the top of the control, index 1 is the next etc.</remarks>
  96. /// <returns></returns>
  97. private Branch[] BuildLineMap()
  98. {
  99. List<Branch> toReturn = new List<Branch>();
  100. foreach(var root in roots.Values) {
  101. toReturn.AddRange(AddToLineMap(root));
  102. }
  103. return toReturn.ToArray();
  104. }
  105. private IEnumerable<Branch> AddToLineMap (Branch currentBranch)
  106. {
  107. yield return currentBranch;
  108. if(currentBranch.IsExpanded){
  109. foreach(var subBranch in currentBranch.ChildBranches.Values){
  110. foreach(var sub in AddToLineMap(subBranch)) {
  111. yield return sub;
  112. }
  113. }
  114. }
  115. }
  116. public char ExpandedSymbol {get;set;} = '-';
  117. public char ExpandableSymbol {get;set;} = '+';
  118. public char LeafSymbol {get;set;} = ' ';
  119. /// <inheritdoc/>
  120. public override bool ProcessKey (KeyEvent keyEvent)
  121. {
  122. switch (keyEvent.Key) {
  123. case Key.CursorRight:
  124. Expand(SelectedObject);
  125. break;
  126. case Key.CursorLeft:
  127. Collapse(SelectedObject);
  128. break;
  129. case Key.CursorUp:
  130. AdjustSelection(-1);
  131. break;
  132. case Key.CursorDown:
  133. AdjustSelection(1);
  134. break;
  135. }
  136. PositionCursor ();
  137. return true;
  138. }
  139. /// <summary>
  140. /// Changes the selected object by a number of screen lines
  141. /// </summary>
  142. /// <remarks>If nothing is currently selected the first root is selected. If the selected object is no longer in the tree the first object is selected</remarks>
  143. /// <param name="offset"></param>
  144. private void AdjustSelection (int offset)
  145. {
  146. if(SelectedObject == null){
  147. SelectedObject = roots.Keys.FirstOrDefault();
  148. }
  149. else {
  150. var map = BuildLineMap();
  151. var idx = Array.FindIndex(map,b=>b.Model.Equals(SelectedObject));
  152. if(idx == -1) {
  153. // The current selection has disapeared!
  154. SelectedObject = roots.Keys.FirstOrDefault();
  155. }
  156. else {
  157. var newIdx = Math.Min(Math.Max(0,idx+offset),map.Length-1);
  158. SelectedObject = map[newIdx].Model;
  159. if(newIdx < ScrollOffset) {
  160. //if user has scrolled up too far to see their selection
  161. ScrollOffset = newIdx;
  162. }
  163. else if(newIdx >= ScrollOffset + Bounds.Height){
  164. //if user has scrolled off bottom of visible tree
  165. ScrollOffset = Math.Max(0,newIdx - Bounds.Height);
  166. }
  167. }
  168. }
  169. SetNeedsDisplay();
  170. }
  171. private void Expand(object selectedObject)
  172. {
  173. if(selectedObject == null)
  174. return;
  175. ObjectToBranch(selectedObject).IsExpanded = true;
  176. SetNeedsDisplay();
  177. }
  178. private void Collapse(object selectedObject)
  179. {
  180. if(selectedObject == null)
  181. return;
  182. ObjectToBranch(selectedObject).IsExpanded = false;
  183. SetNeedsDisplay();
  184. }
  185. /// <summary>
  186. /// Returns the corresponding <see cref="Branch"/> in the tree for <paramref name="toFind"/>. This will not work for objects hidden by their parent being collapsed
  187. /// </summary>
  188. /// <param name="toFind"></param>
  189. /// <returns></returns>
  190. private Branch ObjectToBranch(object toFind)
  191. {
  192. return BuildLineMap().FirstOrDefault(o=>o.Model.Equals(toFind));
  193. }
  194. }
  195. class Branch
  196. {
  197. public bool IsExpanded {get;set;}
  198. public Object Model{get;set;}
  199. public int Depth {get;set;} = 0;
  200. public Dictionary<object,Branch> ChildBranches {get;set;}
  201. private TreeView tree;
  202. public Branch(TreeView tree,Branch parentBranchIfAny,Object model)
  203. {
  204. this.tree = tree;
  205. this.Model = model;
  206. if(parentBranchIfAny != null) {
  207. Depth = parentBranchIfAny.Depth +1;
  208. }
  209. }
  210. /// <summary>
  211. /// Fetch the children of this branch. This method populates <see cref="ChildBranches"/>
  212. /// </summary>
  213. public virtual void FetchChildren()
  214. {
  215. if (tree.ChildrenGetter == null)
  216. return;
  217. this.ChildBranches = tree.ChildrenGetter(this.Model).ToDictionary(k=>k,val=>new Branch(tree,this,val));
  218. }
  219. /// <summary>
  220. /// Renders the current <see cref="Model"/> on the specified line <paramref name="y"/>
  221. /// </summary>
  222. /// <param name="driver"></param>
  223. /// <param name="colorScheme"></param>
  224. /// <param name="y"></param>
  225. /// <param name="availableWidth"></param>
  226. public virtual void Draw(ConsoleDriver driver,ColorScheme colorScheme, int y, int availableWidth)
  227. {
  228. string representation = new string(' ',Depth) + GetExpandableIcon() + tree.AspectGetter(Model);
  229. tree.Move(0,y);
  230. driver.SetAttribute(tree.SelectedObject == Model ?
  231. colorScheme.HotFocus :
  232. colorScheme.Normal);
  233. driver.AddStr(representation.PadRight(availableWidth));
  234. }
  235. char GetExpandableIcon()
  236. {
  237. if(IsExpanded)
  238. return tree.ExpandedSymbol;
  239. if(ChildBranches == null)
  240. FetchChildren();
  241. return ChildBranches.Any() ? tree.ExpandableSymbol : tree.LeafSymbol;
  242. }
  243. }
  244. /// <summary>
  245. /// Delegates of this type are used to fetch the children of the given model object
  246. /// </summary>
  247. /// <param name="model">The parent whose children should be fetched</param>
  248. /// <returns>An enumerable over the children</returns>
  249. public delegate IEnumerable<object> ChildrenGetterDelegate(object model);
  250. }