// This code is based on http://objectlistview.sourceforge.net (GPLv3 tree/list controls by phillip.piper@gmail.com)
using System;
using System.Collections.Generic;
using System.Linq;
namespace Terminal.Gui {
///
/// Hierarchical tree view with expandable branches. Branch objects are dynamically determined when expanded using a user defined
///
public class TreeView : View
{
///
/// Default implementation of a , returns an empty collection (i.e. no children)
///
static ChildrenGetterDelegate DefaultChildrenGetter = (s)=>{return new object[0];};
///
/// This is the delegate that will be used to fetch the children of a model object
///
public ChildrenGetterDelegate ChildrenGetter {
get { return childrenGetter ?? DefaultChildrenGetter; }
set { childrenGetter = value; }
}
private ChildrenGetterDelegate childrenGetter;
private CanExpandGetterDelegate canExpandGetter;
///
/// Optional delegate where 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)
///
/// When this is null is used directly to determine if a node should be expandable
public CanExpandGetterDelegate CanExpandGetter {
get { return canExpandGetter; }
set { canExpandGetter = value; }
}
///
/// The currently selected object in the tree
///
public object SelectedObject {get;set;}
///
/// The root objects in the tree, note that this collection is of root objects only
///
public IEnumerable Objects {get=>roots.Keys;}
///
/// Map of root objects to the branches under them. All objects have a even if that branch has no children
///
Dictionary roots {get; set;} = new Dictionary();
///
/// The amount of tree view that has been scrolled off the top of the screen (by the user scrolling down)
///
public int ScrollOffset {get; private set;}
///
/// Creates a new tree view with absolute positioning. Use to set set root objects for the tree
///
public TreeView ():base()
{
CanFocus = true;
}
///
/// Adds a new root level object unless it is already a root of the tree
///
///
public void AddObject(object o)
{
if(!roots.ContainsKey(o)) {
roots.Add(o,new Branch(this,null,o));
SetNeedsDisplay();
}
}
///
/// Removes all objects from the tree and clears
///
public void ClearObjects()
{
SelectedObject = null;
roots = new Dictionary();
SetNeedsDisplay();
}
///
/// Removes the given root object from the tree
///
/// If is the currently then the selection is cleared
///
public void Remove(object o)
{
if(roots.ContainsKey(o)) {
roots.Remove(o);
SetNeedsDisplay();
if(Equals(SelectedObject,o))
SelectedObject = null;
}
}
///
/// Adds many new root level objects. Objects that are already root objects are ignored
///
/// Objects to add as new root level objects
public void AddObjects(IEnumerable 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();
}
///
/// Returns the string representation of model objects hosted in the tree. Default implementation is to call
///
///
public AspectGetterDelegate AspectGetter {get;set;} = (o)=>o.ToString();
///
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));
}
}
}
///
/// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen
///
/// 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.
///
private Branch[] BuildLineMap()
{
List toReturn = new List();
foreach(var root in roots.Values) {
toReturn.AddRange(AddToLineMap(root));
}
return toReturn.ToArray();
}
private IEnumerable 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;
}
}
}
}
///
/// Symbol to use for expanded branch nodes to indicate to the user that they can be collapsed. Defaults to '-'
///
public char ExpandedSymbol {get;set;} = '-';
///
/// Symbol to use for branch nodes that can be expanded to indicate this to the user. Defaults to '+'
///
public char ExpandableSymbol {get;set;} = '+';
///
/// Symbol to use for branch nodes that cannot be expanded (as they have no children). Defaults to space ' '
///
public char LeafSymbol {get;set;} = ' ';
///
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;
}
///
/// Changes the to the first root object and resets the to 0
///
public void GoToFirst()
{
ScrollOffset = 0;
SelectedObject = roots.Keys.FirstOrDefault();
SetNeedsDisplay();
}
///
/// Changes the to the last object in the tree and scrolls so that it is visible
///
public void GoToEnd ()
{
var map = BuildLineMap();
ScrollOffset = Math.Max(0,map.Length - Bounds.Height +1);
SelectedObject = map.Last().Model;
SetNeedsDisplay();
}
///
/// Changes the selected object by a number of screen lines
///
/// 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
///
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();
}
///
/// Expands the supplied object if it is contained in the tree (either as a root object or as an exposed branch object)
///
/// The object to expand
public void Expand(object toExpand)
{
if(toExpand == null)
return;
ObjectToBranch(toExpand)?.Expand();
SetNeedsDisplay();
}
///
/// Collapses the supplied object if it is currently expanded
///
/// The object to collapse
public void Collapse(object toCollapse)
{
if(toCollapse == null)
return;
ObjectToBranch(toCollapse)?.Collapse();
SetNeedsDisplay();
}
///
/// Returns the corresponding in the tree for . This will not work for objects hidden by their parent being collapsed
///
///
/// The branch for or null if it is not currently exposed in the tree
private Branch ObjectToBranch(object toFind)
{
return BuildLineMap().FirstOrDefault(o=>o.Model.Equals(toFind));
}
}
class Branch
{
///
/// True if the branch is expanded to reveal child branches
///
public bool IsExpanded {get;set;}
///
/// The users object that is being displayed by this branch of the tree
///
public object Model {get;set;}
///
/// The depth of the current branch. Depth of 0 indicates root level branches
///
public int Depth {get;set;} = 0;
///
/// The children of the current branch. This is null until the first call to to avoid enumerating the entire underlying hierarchy
///
public Dictionary ChildBranches {get;set;}
private TreeView tree;
///
/// Declares a new branch of in which the users object is presented
///
/// The UI control in which the branch resides
/// Pass null for root level branches, otherwise pass the parent
/// The user's object that should be displayed
public Branch(TreeView tree,Branch parentBranchIfAny,object model)
{
this.tree = tree;
this.Model = model;
if(parentBranchIfAny != null) {
Depth = parentBranchIfAny.Depth +1;
}
}
///
/// Fetch the children of this branch. This method populates
///
public virtual void FetchChildren()
{
if (tree.ChildrenGetter == null)
return;
this.ChildBranches = tree.ChildrenGetter(this.Model).ToDictionary(k=>k,val=>new Branch(tree,this,val));
}
///
/// Renders the current on the specified line
///
///
///
///
///
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));
}
///
/// Returns an appropriate symbol for displaying next to the string representation of the object to indicate whether it or not (or it is a leaf)
///
///
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;
}
///
/// Expands the current branch if possible
///
public void Expand()
{
if(ChildBranches == null) {
FetchChildren();
}
if (ChildBranches.Any ()) {
IsExpanded = true;
}
}
internal void Collapse ()
{
IsExpanded = false;
}
}
///
/// Delegates of this type are used to fetch the children of the given model object
///
/// The parent whose children should be fetched
/// An enumerable over the children
public delegate IEnumerable ChildrenGetterDelegate(object model);
///
/// Delegates of this type are used to fetch string representations of user's model objects
///
///
///
public delegate string AspectGetterDelegate(object model);
///
/// 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)
///
///
///
public delegate bool CanExpandGetterDelegate(object model);
}