TreeView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /// <summary>
  7. /// Hierarchical tree view with expandable branches. Branch objects are dynamically determined when expanded using a user defined <see cref="ChildrenGetterDelegate"/>
  8. /// </summary>
  9. public class TreeView : View
  10. {
  11. /// <summary>
  12. /// Default implementation of a <see cref="ChildrenGetterDelegate"/>, returns an empty collection (i.e. no children)
  13. /// </summary>
  14. static ChildrenGetterDelegate DefaultChildrenGetter = (s)=>{return new object[0];};
  15. /// <summary>
  16. /// This is the delegate that will be used to fetch the children of a model object
  17. /// </summary>
  18. public ChildrenGetterDelegate ChildrenGetter {
  19. get { return childrenGetter ?? DefaultChildrenGetter; }
  20. set { childrenGetter = value; }
  21. }
  22. private ChildrenGetterDelegate childrenGetter;
  23. private CanExpandGetterDelegate canExpandGetter;
  24. /// <summary>
  25. /// Optional delegate where <see cref="ChildrenGetter"/> is expensive. This should quickly return true/false for whether an object is expandable. (e.g. indicating to a user that all folders can be expanded because they are folders without having to calculate contents)
  26. /// </summary>
  27. /// <remarks>When this is null <see cref="ChildrenGetter"/> is used directly to determine if a node should be expandable</remarks>
  28. public CanExpandGetterDelegate CanExpandGetter {
  29. get { return canExpandGetter; }
  30. set { canExpandGetter = value; }
  31. }
  32. /// <summary>
  33. /// The currently selected object in the tree
  34. /// </summary>
  35. public object SelectedObject {get;set;}
  36. /// <summary>
  37. /// The root objects in the tree, note that this collection is of root objects only
  38. /// </summary>
  39. public IEnumerable<object> Objects {get=>roots.Keys;}
  40. /// <summary>
  41. /// Map of root objects to the branches under them. All objects have a <see cref="Branch"/> even if that branch has no children
  42. /// </summary>
  43. Dictionary<object,Branch> roots {get; set;} = new Dictionary<object, Branch>();
  44. /// <summary>
  45. /// The amount of tree view that has been scrolled off the top of the screen (by the user scrolling down)
  46. /// </summary>
  47. public int ScrollOffset {get; private set;}
  48. /// <summary>
  49. /// Creates a new tree view with absolute positioning. Use <see cref="AddObjects(IEnumerable{object})"/> to set set root objects for the tree
  50. /// </summary>
  51. public TreeView ():base()
  52. {
  53. CanFocus = true;
  54. }
  55. /// <summary>
  56. /// Adds a new root level object unless it is already a root of the tree
  57. /// </summary>
  58. /// <param name="o"></param>
  59. public void AddObject(object o)
  60. {
  61. if(!roots.ContainsKey(o)) {
  62. roots.Add(o,new Branch(this,null,o));
  63. SetNeedsDisplay();
  64. }
  65. }
  66. /// <summary>
  67. /// Removes all objects from the tree and clears <see cref="SelectedObject"/>
  68. /// </summary>
  69. public void ClearObjects()
  70. {
  71. SelectedObject = null;
  72. roots = new Dictionary<object, Branch>();
  73. SetNeedsDisplay();
  74. }
  75. /// <summary>
  76. /// Removes the given root object from the tree
  77. /// </summary>
  78. /// <remarks>If <paramref name="o"/> is the currently <see cref="SelectedObject"/> then the selection is cleared</remarks>
  79. /// <param name="o"></param>
  80. public void Remove(object o)
  81. {
  82. if(roots.ContainsKey(o)) {
  83. roots.Remove(o);
  84. SetNeedsDisplay();
  85. if(Equals(SelectedObject,o))
  86. SelectedObject = null;
  87. }
  88. }
  89. /// <summary>
  90. /// Adds many new root level objects. Objects that are already root objects are ignored
  91. /// </summary>
  92. /// <param name="collection">Objects to add as new root level objects</param>
  93. public void AddObjects(IEnumerable<object> collection)
  94. {
  95. bool objectsAdded = false;
  96. foreach(var o in collection) {
  97. if (!roots.ContainsKey (o)) {
  98. roots.Add(o,new Branch(this,null,o));
  99. objectsAdded = true;
  100. }
  101. }
  102. if(objectsAdded)
  103. SetNeedsDisplay();
  104. }
  105. /// <summary>
  106. /// Returns the string representation of model objects hosted in the tree. Default implementation is to call <see cref="object.ToString"/>
  107. /// </summary>
  108. /// <value></value>
  109. public AspectGetterDelegate AspectGetter {get;set;} = (o)=>o.ToString();
  110. ///<inheritdoc/>
  111. public override void Redraw (Rect bounds)
  112. {
  113. if(roots == null)
  114. return;
  115. var map = BuildLineMap();
  116. for(int line = 0 ; line < bounds.Height; line++){
  117. var idxToRender = ScrollOffset + line;
  118. // Is there part of the tree view to render?
  119. if(idxToRender < map.Length) {
  120. // Render the line
  121. map[idxToRender].Draw(Driver,ColorScheme,line,bounds.Width);
  122. } else {
  123. // Else clear the line to prevent stale symbols due to scrolling etc
  124. Move(0,line);
  125. Driver.SetAttribute(ColorScheme.Normal);
  126. Driver.AddStr(new string(' ',bounds.Width));
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen
  132. /// </summary>
  133. /// <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>
  134. /// <returns></returns>
  135. private Branch[] BuildLineMap()
  136. {
  137. List<Branch> toReturn = new List<Branch>();
  138. foreach(var root in roots.Values) {
  139. toReturn.AddRange(AddToLineMap(root));
  140. }
  141. return toReturn.ToArray();
  142. }
  143. private IEnumerable<Branch> AddToLineMap (Branch currentBranch)
  144. {
  145. yield return currentBranch;
  146. if(currentBranch.IsExpanded){
  147. foreach(var subBranch in currentBranch.ChildBranches.Values){
  148. foreach(var sub in AddToLineMap(subBranch)) {
  149. yield return sub;
  150. }
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// Symbol to use for expanded branch nodes to indicate to the user that they can be collapsed. Defaults to '-'
  156. /// </summary>
  157. public char ExpandedSymbol {get;set;} = '-';
  158. /// <summary>
  159. /// Symbol to use for branch nodes that can be expanded to indicate this to the user. Defaults to '+'
  160. /// </summary>
  161. public char ExpandableSymbol {get;set;} = '+';
  162. /// <summary>
  163. /// Symbol to use for branch nodes that cannot be expanded (as they have no children). Defaults to space ' '
  164. /// </summary>
  165. public char LeafSymbol {get;set;} = ' ';
  166. /// <inheritdoc/>
  167. public override bool ProcessKey (KeyEvent keyEvent)
  168. {
  169. switch (keyEvent.Key) {
  170. case Key.CursorRight:
  171. Expand(SelectedObject);
  172. break;
  173. case Key.CursorLeft:
  174. Collapse(SelectedObject);
  175. break;
  176. case Key.CursorUp:
  177. AdjustSelection(-1);
  178. break;
  179. case Key.CursorDown:
  180. AdjustSelection(1);
  181. break;
  182. case Key.PageUp:
  183. AdjustSelection(-Bounds.Height);
  184. break;
  185. case Key.PageDown:
  186. AdjustSelection(Bounds.Height);
  187. break;
  188. case Key.Home:
  189. GoToFirst();
  190. break;
  191. case Key.End:
  192. GoToEnd();
  193. break;
  194. default:
  195. // we don't care about this keystroke
  196. return false;
  197. }
  198. PositionCursor ();
  199. return true;
  200. }
  201. /// <summary>
  202. /// Changes the <see cref="SelectedObject"/> to the first root object and resets the <see cref="ScrollOffset"/> to 0
  203. /// </summary>
  204. public void GoToFirst()
  205. {
  206. ScrollOffset = 0;
  207. SelectedObject = roots.Keys.FirstOrDefault();
  208. SetNeedsDisplay();
  209. }
  210. /// <summary>
  211. /// Changes the <see cref="SelectedObject"/> to the last object in the tree and scrolls so that it is visible
  212. /// </summary>
  213. public void GoToEnd ()
  214. {
  215. var map = BuildLineMap();
  216. ScrollOffset = Math.Max(0,map.Length - Bounds.Height +1);
  217. SelectedObject = map.Last().Model;
  218. SetNeedsDisplay();
  219. }
  220. /// <summary>
  221. /// Changes the selected object by a number of screen lines
  222. /// </summary>
  223. /// <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>
  224. /// <param name="offset"></param>
  225. private void AdjustSelection (int offset)
  226. {
  227. if(SelectedObject == null){
  228. SelectedObject = roots.Keys.FirstOrDefault();
  229. }
  230. else {
  231. var map = BuildLineMap();
  232. var idx = Array.FindIndex(map,b=>b.Model.Equals(SelectedObject));
  233. if(idx == -1) {
  234. // The current selection has disapeared!
  235. SelectedObject = roots.Keys.FirstOrDefault();
  236. }
  237. else {
  238. var newIdx = Math.Min(Math.Max(0,idx+offset),map.Length-1);
  239. SelectedObject = map[newIdx].Model;
  240. if(newIdx < ScrollOffset) {
  241. //if user has scrolled up too far to see their selection
  242. ScrollOffset = newIdx;
  243. }
  244. else if(newIdx >= ScrollOffset + Bounds.Height){
  245. //if user has scrolled off bottom of visible tree
  246. ScrollOffset = Math.Max(0,(newIdx+1) - Bounds.Height);
  247. }
  248. }
  249. }
  250. SetNeedsDisplay();
  251. }
  252. /// <summary>
  253. /// Expands the supplied object if it is contained in the tree (either as a root object or as an exposed branch object)
  254. /// </summary>
  255. /// <param name="toExpand">The object to expand</param>
  256. public void Expand(object toExpand)
  257. {
  258. if(toExpand == null)
  259. return;
  260. ObjectToBranch(toExpand)?.Expand();
  261. SetNeedsDisplay();
  262. }
  263. /// <summary>
  264. /// Collapses the supplied object if it is currently expanded
  265. /// </summary>
  266. /// <param name="toCollapse">The object to collapse</param>
  267. public void Collapse(object toCollapse)
  268. {
  269. if(toCollapse == null)
  270. return;
  271. ObjectToBranch(toCollapse)?.Collapse();
  272. SetNeedsDisplay();
  273. }
  274. /// <summary>
  275. /// 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
  276. /// </summary>
  277. /// <param name="toFind"></param>
  278. /// <returns>The branch for <paramref name="toFind"/> or null if it is not currently exposed in the tree</returns>
  279. private Branch ObjectToBranch(object toFind)
  280. {
  281. return BuildLineMap().FirstOrDefault(o=>o.Model.Equals(toFind));
  282. }
  283. }
  284. class Branch
  285. {
  286. /// <summary>
  287. /// True if the branch is expanded to reveal child branches
  288. /// </summary>
  289. public bool IsExpanded {get;set;}
  290. /// <summary>
  291. /// The users object that is being displayed by this branch of the tree
  292. /// </summary>
  293. public object Model {get;set;}
  294. /// <summary>
  295. /// The depth of the current branch. Depth of 0 indicates root level branches
  296. /// </summary>
  297. public int Depth {get;set;} = 0;
  298. /// <summary>
  299. /// The children of the current branch. This is null until the first call to <see cref="FetchChildren"/> to avoid enumerating the entire underlying hierarchy
  300. /// </summary>
  301. public Dictionary<object,Branch> ChildBranches {get;set;}
  302. private TreeView tree;
  303. /// <summary>
  304. /// Declares a new branch of <paramref name="tree"/> in which the users object <paramref name="model"/> is presented
  305. /// </summary>
  306. /// <param name="tree">The UI control in which the branch resides</param>
  307. /// <param name="parentBranchIfAny">Pass null for root level branches, otherwise pass the parent</param>
  308. /// <param name="model">The user's object that should be displayed</param>
  309. public Branch(TreeView tree,Branch parentBranchIfAny,object model)
  310. {
  311. this.tree = tree;
  312. this.Model = model;
  313. if(parentBranchIfAny != null) {
  314. Depth = parentBranchIfAny.Depth +1;
  315. }
  316. }
  317. /// <summary>
  318. /// Fetch the children of this branch. This method populates <see cref="ChildBranches"/>
  319. /// </summary>
  320. public virtual void FetchChildren()
  321. {
  322. if (tree.ChildrenGetter == null)
  323. return;
  324. this.ChildBranches = tree.ChildrenGetter(this.Model).ToDictionary(k=>k,val=>new Branch(tree,this,val));
  325. }
  326. /// <summary>
  327. /// Renders the current <see cref="Model"/> on the specified line <paramref name="y"/>
  328. /// </summary>
  329. /// <param name="driver"></param>
  330. /// <param name="colorScheme"></param>
  331. /// <param name="y"></param>
  332. /// <param name="availableWidth"></param>
  333. public virtual void Draw(ConsoleDriver driver,ColorScheme colorScheme, int y, int availableWidth)
  334. {
  335. string representation = new string(' ',Depth) + GetExpandableIcon() + tree.AspectGetter(Model);
  336. tree.Move(0,y);
  337. driver.SetAttribute(tree.SelectedObject == Model ?
  338. colorScheme.HotFocus :
  339. colorScheme.Normal);
  340. driver.AddStr(representation.PadRight(availableWidth));
  341. }
  342. /// <summary>
  343. /// Returns an appropriate symbol for displaying next to the string representation of the <see cref="Model"/> object to indicate whether it <see cref="IsExpanded"/> or not (or it is a leaf)
  344. /// </summary>
  345. /// <returns></returns>
  346. public char GetExpandableIcon()
  347. {
  348. if(IsExpanded)
  349. return tree.ExpandedSymbol;
  350. if(ChildBranches == null) {
  351. //if there is a rapid method for determining whether there are children
  352. if(tree.CanExpandGetter != null) {
  353. return tree.CanExpandGetter(Model) ? tree.ExpandableSymbol : tree.LeafSymbol;
  354. }
  355. //there is no way of knowing whether we can expand without fetching the children
  356. FetchChildren();
  357. }
  358. //we fetched or already know the children, so return whether we are a leaf or a expandable branch
  359. return ChildBranches.Any() ? tree.ExpandableSymbol : tree.LeafSymbol;
  360. }
  361. /// <summary>
  362. /// Expands the current branch if possible
  363. /// </summary>
  364. public void Expand()
  365. {
  366. if(ChildBranches == null) {
  367. FetchChildren();
  368. }
  369. if (ChildBranches.Any ()) {
  370. IsExpanded = true;
  371. }
  372. }
  373. internal void Collapse ()
  374. {
  375. IsExpanded = false;
  376. }
  377. }
  378. /// <summary>
  379. /// Delegates of this type are used to fetch the children of the given model object
  380. /// </summary>
  381. /// <param name="model">The parent whose children should be fetched</param>
  382. /// <returns>An enumerable over the children</returns>
  383. public delegate IEnumerable<object> ChildrenGetterDelegate(object model);
  384. /// <summary>
  385. /// Delegates of this type are used to fetch string representations of user's model objects
  386. /// </summary>
  387. /// <param name="model"></param>
  388. /// <returns></returns>
  389. public delegate string AspectGetterDelegate(object model);
  390. /// <summary>
  391. /// Delegates of this type are used to quickly display to the user whether a given user object can be expanded when fetching it's children is expensive (e.g. indicating to a user that all 1000 folders can be expanded because they are folders without having to calculate contents)
  392. /// </summary>
  393. /// <param name="model"></param>
  394. /// <returns></returns>
  395. public delegate bool CanExpandGetterDelegate(object model);
  396. }