123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- // This code is based on http://objectlistview.sourceforge.net (GPLv3 tree/list controls by [email protected])
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Terminal.Gui {
- /// <summary>
- /// Hierarchical tree view with expandable branches. Branch objects are dynamically determined when expanded using a user defined <see cref="ChildrenGetterDelegate"/>
- /// </summary>
- public class TreeView : View
- {
- /// <summary>
- /// Default implementation of a <see cref="ChildrenGetterDelegate"/>, returns an empty collection (i.e. no children)
- /// </summary>
- static ChildrenGetterDelegate DefaultChildrenGetter = (s)=>{return new object[0];};
- /// <summary>
- /// This is the delegate that will be used to fetch the children of a model object
- /// </summary>
- public ChildrenGetterDelegate ChildrenGetter {
- get { return childrenGetter ?? DefaultChildrenGetter; }
- set { childrenGetter = value; }
- }
-
- private ChildrenGetterDelegate childrenGetter;
- private CanExpandGetterDelegate canExpandGetter;
- /// <summary>
- /// 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)
- /// </summary>
- /// <remarks>When this is null <see cref="ChildrenGetter"/> is used directly to determine if a node should be expandable</remarks>
- public CanExpandGetterDelegate CanExpandGetter {
- get { return canExpandGetter; }
- set { canExpandGetter = value; }
- }
- /// <summary>
- /// The currently selected object in the tree
- /// </summary>
- public object SelectedObject {get;set;}
- /// <summary>
- /// The root objects in the tree, note that this collection is of root objects only
- /// </summary>
- public IEnumerable<object> Objects {get=>roots.Keys;}
- /// <summary>
- /// Map of root objects to the branches under them. All objects have a <see cref="Branch"/> even if that branch has no children
- /// </summary>
- Dictionary<object,Branch> roots {get; set;} = new Dictionary<object, Branch>();
- /// <summary>
- /// The amount of tree view that has been scrolled off the top of the screen (by the user scrolling down)
- /// </summary>
- public int ScrollOffset {get; private set;}
- /// <summary>
- /// Creates a new tree view with absolute positioning. Use <see cref="AddObjects(IEnumerable{object})"/> to set set root objects for the tree
- /// </summary>
- public TreeView ():base()
- {
- CanFocus = true;
- }
- /// <summary>
- /// Adds a new root level object unless it is already a root of the tree
- /// </summary>
- /// <param name="o"></param>
- public void AddObject(object o)
- {
- if(!roots.ContainsKey(o)) {
- roots.Add(o,new Branch(this,null,o));
- SetNeedsDisplay();
- }
- }
- /// <summary>
- /// Removes all objects from the tree and clears <see cref="SelectedObject"/>
- /// </summary>
- public void ClearObjects()
- {
- SelectedObject = null;
- roots = new Dictionary<object, Branch>();
- SetNeedsDisplay();
- }
- /// <summary>
- /// Removes the given root object from the tree
- /// </summary>
- /// <remarks>If <paramref name="o"/> is the currently <see cref="SelectedObject"/> then the selection is cleared</remarks>
- /// <param name="o"></param>
- public void Remove(object o)
- {
- if(roots.ContainsKey(o)) {
- roots.Remove(o);
- SetNeedsDisplay();
- if(Equals(SelectedObject,o))
- SelectedObject = null;
- }
- }
-
- /// <summary>
- /// Adds many new root level objects. Objects that are already root objects are ignored
- /// </summary>
- /// <param name="collection">Objects to add as new root level objects</param>
- public void AddObjects(IEnumerable<object> collection)
- {
- bool objectsAdded = false;
- foreach(var o in collection) {
- if (!roots.ContainsKey (o)) {
- roots.Add(o,new Branch(this,null,o));
- objectsAdded = true;
- }
- }
-
- if(objectsAdded)
- SetNeedsDisplay();
- }
- /// <summary>
- /// Returns the string representation of model objects hosted in the tree. Default implementation is to call <see cref="object.ToString"/>
- /// </summary>
- /// <value></value>
- public AspectGetterDelegate AspectGetter {get;set;} = (o)=>o.ToString();
- ///<inheritdoc/>
- public override void Redraw (Rect bounds)
- {
- if(roots == null)
- return;
- var map = BuildLineMap();
- for(int line = 0 ; line < bounds.Height; line++){
- var idxToRender = ScrollOffset + line;
- // Is there part of the tree view to render?
- if(idxToRender < map.Length) {
- // Render the line
- map[idxToRender].Draw(Driver,ColorScheme,line,bounds.Width);
- } else {
- // Else clear the line to prevent stale symbols due to scrolling etc
- Move(0,line);
- Driver.SetAttribute(ColorScheme.Normal);
- Driver.AddStr(new string(' ',bounds.Width));
- }
-
- }
- }
- /// <summary>
- /// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen
- /// </summary>
- /// <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>
- /// <returns></returns>
- private Branch[] BuildLineMap()
- {
- List<Branch> toReturn = new List<Branch>();
- foreach(var root in roots.Values) {
- toReturn.AddRange(AddToLineMap(root));
- }
- return toReturn.ToArray();
- }
- private IEnumerable<Branch> AddToLineMap (Branch currentBranch)
- {
- yield return currentBranch;
- if(currentBranch.IsExpanded){
- foreach(var subBranch in currentBranch.ChildBranches.Values){
- foreach(var sub in AddToLineMap(subBranch)) {
- yield return sub;
- }
- }
- }
- }
- /// <summary>
- /// Symbol to use for expanded branch nodes to indicate to the user that they can be collapsed. Defaults to '-'
- /// </summary>
- public char ExpandedSymbol {get;set;} = '-';
- /// <summary>
- /// Symbol to use for branch nodes that can be expanded to indicate this to the user. Defaults to '+'
- /// </summary>
- public char ExpandableSymbol {get;set;} = '+';
- /// <summary>
- /// Symbol to use for branch nodes that cannot be expanded (as they have no children). Defaults to space ' '
- /// </summary>
- public char LeafSymbol {get;set;} = ' ';
- /// <inheritdoc/>
- public override bool ProcessKey (KeyEvent keyEvent)
- {
- switch (keyEvent.Key) {
- case Key.CursorRight:
- Expand(SelectedObject);
- break;
- case Key.CursorLeft:
- Collapse(SelectedObject);
- break;
-
- case Key.CursorUp:
- AdjustSelection(-1);
- break;
- case Key.CursorDown:
- AdjustSelection(1);
- break;
- case Key.PageUp:
- AdjustSelection(-Bounds.Height);
- break;
-
- case Key.PageDown:
- AdjustSelection(Bounds.Height);
- break;
- case Key.Home:
- GoToFirst();
- break;
- case Key.End:
- GoToEnd();
- break;
- default:
- // we don't care about this keystroke
- return false;
- }
- PositionCursor ();
- return true;
- }
- /// <summary>
- /// Changes the <see cref="SelectedObject"/> to the first root object and resets the <see cref="ScrollOffset"/> to 0
- /// </summary>
- public void GoToFirst()
- {
- ScrollOffset = 0;
- SelectedObject = roots.Keys.FirstOrDefault();
- SetNeedsDisplay();
- }
- /// <summary>
- /// Changes the <see cref="SelectedObject"/> to the last object in the tree and scrolls so that it is visible
- /// </summary>
- public void GoToEnd ()
- {
- var map = BuildLineMap();
- ScrollOffset = Math.Max(0,map.Length - Bounds.Height +1);
- SelectedObject = map.Last().Model;
-
- SetNeedsDisplay();
- }
- /// <summary>
- /// Changes the selected object by a number of screen lines
- /// </summary>
- /// <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>
- /// <param name="offset"></param>
- private void AdjustSelection (int offset)
- {
- if(SelectedObject == null){
- SelectedObject = roots.Keys.FirstOrDefault();
- }
- else {
- var map = BuildLineMap();
- var idx = Array.FindIndex(map,b=>b.Model.Equals(SelectedObject));
- if(idx == -1) {
- // The current selection has disapeared!
- SelectedObject = roots.Keys.FirstOrDefault();
- }
- else {
- var newIdx = Math.Min(Math.Max(0,idx+offset),map.Length-1);
- SelectedObject = map[newIdx].Model;
-
- if(newIdx < ScrollOffset) {
- //if user has scrolled up too far to see their selection
- ScrollOffset = newIdx;
- }
- else if(newIdx >= ScrollOffset + Bounds.Height){
-
- //if user has scrolled off bottom of visible tree
- ScrollOffset = Math.Max(0,(newIdx+1) - Bounds.Height);
- }
- }
- }
-
- SetNeedsDisplay();
- }
- /// <summary>
- /// Expands the supplied object if it is contained in the tree (either as a root object or as an exposed branch object)
- /// </summary>
- /// <param name="toExpand">The object to expand</param>
- public void Expand(object toExpand)
- {
- if(toExpand == null)
- return;
-
- ObjectToBranch(toExpand)?.Expand();
- SetNeedsDisplay();
- }
- /// <summary>
- /// Collapses the supplied object if it is currently expanded
- /// </summary>
- /// <param name="toCollapse">The object to collapse</param>
- public void Collapse(object toCollapse)
- {
- if(toCollapse == null)
- return;
- ObjectToBranch(toCollapse)?.Collapse();
- SetNeedsDisplay();
- }
- /// <summary>
- /// 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
- /// </summary>
- /// <param name="toFind"></param>
- /// <returns>The branch for <paramref name="toFind"/> or null if it is not currently exposed in the tree</returns>
- private Branch ObjectToBranch(object toFind)
- {
- return BuildLineMap().FirstOrDefault(o=>o.Model.Equals(toFind));
- }
- }
- class Branch
- {
- /// <summary>
- /// True if the branch is expanded to reveal child branches
- /// </summary>
- public bool IsExpanded {get;set;}
- /// <summary>
- /// The users object that is being displayed by this branch of the tree
- /// </summary>
- public object Model {get;set;}
-
- /// <summary>
- /// The depth of the current branch. Depth of 0 indicates root level branches
- /// </summary>
- public int Depth {get;set;} = 0;
- /// <summary>
- /// The children of the current branch. This is null until the first call to <see cref="FetchChildren"/> to avoid enumerating the entire underlying hierarchy
- /// </summary>
- public Dictionary<object,Branch> ChildBranches {get;set;}
- private TreeView tree;
- /// <summary>
- /// Declares a new branch of <paramref name="tree"/> in which the users object <paramref name="model"/> is presented
- /// </summary>
- /// <param name="tree">The UI control in which the branch resides</param>
- /// <param name="parentBranchIfAny">Pass null for root level branches, otherwise pass the parent</param>
- /// <param name="model">The user's object that should be displayed</param>
- public Branch(TreeView tree,Branch parentBranchIfAny,object model)
- {
- this.tree = tree;
- this.Model = model;
-
- if(parentBranchIfAny != null) {
- Depth = parentBranchIfAny.Depth +1;
- }
- }
- /// <summary>
- /// Fetch the children of this branch. This method populates <see cref="ChildBranches"/>
- /// </summary>
- public virtual void FetchChildren()
- {
- if (tree.ChildrenGetter == null)
- return;
- this.ChildBranches = tree.ChildrenGetter(this.Model).ToDictionary(k=>k,val=>new Branch(tree,this,val));
- }
- /// <summary>
- /// Renders the current <see cref="Model"/> on the specified line <paramref name="y"/>
- /// </summary>
- /// <param name="driver"></param>
- /// <param name="colorScheme"></param>
- /// <param name="y"></param>
- /// <param name="availableWidth"></param>
- public virtual void Draw(ConsoleDriver driver,ColorScheme colorScheme, int y, int availableWidth)
- {
- string representation = new string(' ',Depth) + GetExpandableIcon() + tree.AspectGetter(Model);
-
- tree.Move(0,y);
- driver.SetAttribute(tree.SelectedObject == Model ?
- colorScheme.HotFocus :
- colorScheme.Normal);
- driver.AddStr(representation.PadRight(availableWidth));
- }
- /// <summary>
- /// 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)
- /// </summary>
- /// <returns></returns>
- public char GetExpandableIcon()
- {
- if(IsExpanded)
- return tree.ExpandedSymbol;
- if(ChildBranches == null) {
-
- //if there is a rapid method for determining whether there are children
- if(tree.CanExpandGetter != null) {
- return tree.CanExpandGetter(Model) ? tree.ExpandableSymbol : tree.LeafSymbol;
- }
-
- //there is no way of knowing whether we can expand without fetching the children
- FetchChildren();
- }
- //we fetched or already know the children, so return whether we are a leaf or a expandable branch
- return ChildBranches.Any() ? tree.ExpandableSymbol : tree.LeafSymbol;
- }
- /// <summary>
- /// Expands the current branch if possible
- /// </summary>
- public void Expand()
- {
- if(ChildBranches == null) {
- FetchChildren();
- }
- if (ChildBranches.Any ()) {
- IsExpanded = true;
- }
- }
- internal void Collapse ()
- {
- IsExpanded = false;
- }
- }
-
- /// <summary>
- /// Delegates of this type are used to fetch the children of the given model object
- /// </summary>
- /// <param name="model">The parent whose children should be fetched</param>
- /// <returns>An enumerable over the children</returns>
- public delegate IEnumerable<object> ChildrenGetterDelegate(object model);
- /// <summary>
- /// Delegates of this type are used to fetch string representations of user's model objects
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public delegate string AspectGetterDelegate(object model);
- /// <summary>
- /// 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)
- /// </summary>
- /// <param name="model"></param>
- /// <returns></returns>
- public delegate bool CanExpandGetterDelegate(object model);
- }
|