Bladeren bron

Added CanExpandGetter logic (optional)

tznind 4 jaren geleden
bovenliggende
commit
c147f79e28
2 gewijzigde bestanden met toevoegingen van 30 en 1 verwijderingen
  1. 27 1
      Terminal.Gui/Views/TreeView.cs
  2. 3 0
      UICatalog/Scenarios/TreeViewFileSystem.cs

+ 27 - 1
Terminal.Gui/Views/TreeView.cs

@@ -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);
 }

+ 3 - 0
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;