Added CanExpandGetter logic (optional)

This commit is contained in:
tznind
2020-11-23 11:57:12 +00:00
parent 4047154eec
commit c147f79e28
2 changed files with 30 additions and 1 deletions

View File

@@ -25,6 +25,16 @@ namespace Terminal.Gui {
}
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
@@ -409,9 +419,18 @@ namespace Terminal.Gui {
if(IsExpanded)
return tree.ExpandedSymbol;
if(ChildBranches == null)
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;
}
@@ -448,4 +467,11 @@ namespace Terminal.Gui {
/// <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);
}

View File

@@ -54,6 +54,9 @@ namespace UICatalog.Scenarios {
return;
}
// As a shortcut to enumerating half the file system, tell tree that all directories are expandable (even if they turn out to be empty later on)
_treeView.CanExpandGetter = (o)=>o is DirectoryInfo;
// Determines how to compute children of any given branch
_treeView.ChildrenGetter = GetChildren;