Ver código fonte

Added ExpandAll / CollapseAll (shortcut Ctrl + Right/Left)

tznind 4 anos atrás
pai
commit
5b763bd44f
2 arquivos alterados com 104 adições e 7 exclusões
  1. 101 6
      Terminal.Gui/Views/TreeView.cs
  2. 3 1
      UICatalog/Scenarios/ClassExplorer.cs

+ 101 - 6
Terminal.Gui/Views/TreeView.cs

@@ -620,8 +620,12 @@ namespace Terminal.Gui {
 				case Key.CursorRight:
 				case Key.CursorRight:
 					Expand(SelectedObject);
 					Expand(SelectedObject);
 				break;
 				break;
+				case Key.CursorRight | Key.CtrlMask:
+					ExpandAll(SelectedObject);
+				break;
 				case Key.CursorLeft:
 				case Key.CursorLeft:
-					CursorLeft();
+				case Key.CursorLeft | Key.CtrlMask:
+					CursorLeft(keyEvent.Key.HasFlag(Key.CtrlMask));
 				break;
 				break;
 			
 			
 				case Key.CursorUp:
 				case Key.CursorUp:
@@ -768,10 +772,16 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// Determines systems behaviour when the left arrow key is pressed.  Default behaviour is to collapse the current tree node if possible otherwise changes selection to current branches parent
 		/// Determines systems behaviour when the left arrow key is pressed.  Default behaviour is to collapse the current tree node if possible otherwise changes selection to current branches parent
 		/// </summary>
 		/// </summary>
-		protected virtual void CursorLeft()
+		protected virtual void CursorLeft(bool ctrl)
 		{
 		{
-			if(IsExpanded(SelectedObject))
-				Collapse(SelectedObject);
+			if(IsExpanded(SelectedObject)) {
+				
+				if(ctrl)
+					CollapseAll(SelectedObject);
+				else
+					Collapse(SelectedObject);
+
+			}
 			else
 			else
 			{
 			{
 				var parent = GetParent(SelectedObject);
 				var parent = GetParent(SelectedObject);
@@ -886,6 +896,29 @@ namespace Terminal.Gui {
 			SetNeedsDisplay();
 			SetNeedsDisplay();
 		}
 		}
 		
 		
+		/// <summary>
+		/// Expands the supplied object and all child objects
+		/// </summary>
+		/// <param name="toExpand">The object to expand</param>
+		public void ExpandAll(T toExpand)
+		{
+			if(toExpand == null)
+				return;
+			
+			ObjectToBranch(toExpand)?.ExpandAll();
+			SetNeedsDisplay();
+		}
+		/// <summary>
+		/// Fully expands all nodes in the tree, if the tree is very big and built dynamically this may take a while (e.g. for file system)
+		/// </summary>
+		public void ExpandAll()
+		{
+			foreach (var item in roots) {
+				item.Value.ExpandAll();
+			}
+
+			SetNeedsDisplay();
+		}
 		/// <summary>
 		/// <summary>
 		/// Returns true if the given object <paramref name="o"/> is exposed in the tree and can be expanded otherwise false
 		/// Returns true if the given object <paramref name="o"/> is exposed in the tree and can be expanded otherwise false
 		/// </summary>
 		/// </summary>
@@ -912,6 +945,38 @@ namespace Terminal.Gui {
 		/// <param name="toCollapse">The object to collapse</param>
 		/// <param name="toCollapse">The object to collapse</param>
 		public void Collapse(T toCollapse)
 		public void Collapse(T toCollapse)
 		{
 		{
+			CollapseImpl(toCollapse,false);
+		}
+		
+		/// <summary>
+		/// Collapses the supplied object if it is currently expanded.  Also collapses all children branches (this will only become apparent when/if the user expands it again)
+		/// </summary>
+		/// <param name="toCollapse">The object to collapse</param>
+		public void CollapseAll(T toCollapse)
+		{
+			CollapseImpl(toCollapse,true);
+		}
+
+		/// <summary>
+		/// Collapses all root nodes in the tree
+		/// </summary>
+		public void CollapseAll()
+		{
+			foreach (var item in roots) {
+				item.Value.Collapse();
+			}
+
+			SetNeedsDisplay();
+		}
+
+		/// <summary>
+		/// Implementation of <see cref="Collapse(T)"/> and <see cref="CollapseAll(T)"/>.  Performs operation and updates selection if disapeared
+		/// </summary>
+		/// <param name="toCollapse"></param>
+		/// <param name="all"></param>
+		protected void CollapseImpl(T toCollapse, bool all)
+		{
+			
 			if(toCollapse == null)
 			if(toCollapse == null)
 				return;
 				return;
 
 
@@ -920,8 +985,12 @@ namespace Terminal.Gui {
 			// Nothing to collapse
 			// Nothing to collapse
 			if(branch == null)
 			if(branch == null)
 				return;
 				return;
-			
-			branch.Collapse();
+
+			if (all) {
+				branch.CollapseAll();
+			} else {
+				branch.Collapse();
+			}
 
 
 			if(SelectedObject != null && ObjectToBranch(SelectedObject) == null)
 			if(SelectedObject != null && ObjectToBranch(SelectedObject) == null)
 			{
 			{
@@ -1393,6 +1462,32 @@ namespace Terminal.Gui {
 
 
 			return false;
 			return false;
 		}
 		}
+
+		/// <summary>
+		/// Expands the current branch and all children branches
+		/// </summary>
+		internal void ExpandAll ()
+		{
+			Expand();
+
+			if(ChildBranches != null)
+				foreach (var child in ChildBranches) {
+					child.Value.ExpandAll();
+				}
+		}
+
+		/// <summary>
+		/// Collapses the current branch and all children branches (even though those branches are no longer visible they retain collapse/expansion state)
+		/// </summary>
+		internal void CollapseAll ()
+		{
+			Collapse();
+
+			if(ChildBranches != null)
+				foreach (var child in ChildBranches) {
+					child.Value.CollapseAll();
+				}
+		}
 	}
 	}
 
 
 	/// <summary>
 	/// <summary>

+ 3 - 1
UICatalog/Scenarios/ClassExplorer.cs

@@ -68,7 +68,9 @@ namespace UICatalog.Scenarios {
 					miShowPrivate = new MenuItem ("_Include Private", "", () => ShowPrivate()){
 					miShowPrivate = new MenuItem ("_Include Private", "", () => ShowPrivate()){
 						Checked = false,
 						Checked = false,
 						CheckType = MenuItemCheckStyle.Checked
 						CheckType = MenuItemCheckStyle.Checked
-					} }),
+					},
+				new MenuItem ("_Expand All", "", () => treeView.ExpandAll()),
+				new MenuItem ("_Collapse All", "", () => treeView.CollapseAll()) }),
 			});
 			});
 			Top.Add (menu);
 			Top.Add (menu);