diff --git a/Terminal.Gui/Views/TreeView.cs b/Terminal.Gui/Views/TreeView.cs index 58467fc57..1c72764ab 100644 --- a/Terminal.Gui/Views/TreeView.cs +++ b/Terminal.Gui/Views/TreeView.cs @@ -25,6 +25,16 @@ namespace Terminal.Gui { } 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 @@ -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 { /// /// 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); } \ No newline at end of file diff --git a/UICatalog/Scenarios/TreeViewFileSystem.cs b/UICatalog/Scenarios/TreeViewFileSystem.cs index cc1c260ed..bc02b3bb6 100644 --- a/UICatalog/Scenarios/TreeViewFileSystem.cs +++ b/UICatalog/Scenarios/TreeViewFileSystem.cs @@ -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;