Bladeren bron

Added horizontal scrollbar, fixed tests

tznind 4 jaren geleden
bovenliggende
commit
2884b8e94d
3 gewijzigde bestanden met toevoegingen van 52 en 14 verwijderingen
  1. 39 1
      Terminal.Gui/Views/TreeView.cs
  2. 8 8
      UICatalog/Scenarios/TreeViewFileSystem.cs
  3. 5 5
      UnitTests/TreeViewTests.cs

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

@@ -346,7 +346,6 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public int ContentHeight => BuildLineMap().Count();
 		public int ContentHeight => BuildLineMap().Count();
 
 
-
 		/// <summary>
 		/// <summary>
 		/// Returns the string representation of model objects hosted in the tree.  Default implementation is to call <see cref="object.ToString"/>
 		/// Returns the string representation of model objects hosted in the tree.  Default implementation is to call <see cref="object.ToString"/>
 		/// </summary>
 		/// </summary>
@@ -523,6 +522,32 @@ namespace Terminal.Gui {
 			return -1;
 			return -1;
 		}
 		}
 
 
+		/// <summary>
+		/// Returns the maximum width line in the tree including prefix and expansion symbols
+		/// </summary>
+		/// <param name="visible">True to consider only rows currently visible (based on window bounds and <see cref="ScrollOffsetVertical"/>.  False to calculate the width of every exposed branch in the tree</param>
+		/// <returns></returns>
+		public int GetContentWidth(bool visible){
+			
+			var map = BuildLineMap();
+
+			if(map.Length == 0)
+				return 0;
+
+			if(visible){
+
+				//Somehow we managed to scroll off the end of the control
+				if(ScrollOffsetVertical > map.Length)
+					return 0;
+
+				return map.Skip(ScrollOffsetVertical).Take(Bounds.Height).Max(b=>b.GetWidth(Driver));
+			}
+			else{
+
+				return map.Max(b=>b.GetWidth(Driver));
+			}
+		}
+
 		/// <summary>
 		/// <summary>
 		/// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen
 		/// Calculates all currently visible/expanded branches (including leafs) and outputs them by index from the top of the screen
 		/// </summary>
 		/// </summary>
@@ -898,6 +923,18 @@ namespace Terminal.Gui {
 			this.ChildBranches = children.ToDictionary(k=>k,val=>new Branch<T>(tree,this,val));
 			this.ChildBranches = children.ToDictionary(k=>k,val=>new Branch<T>(tree,this,val));
 		}
 		}
 
 
+		/// <summary>
+		/// Returns the width of the line including prefix and the results of <see cref="TreeView{T}.AspectGetter"/> (the line body).
+		/// </summary>
+		/// <returns></returns>
+		public virtual int GetWidth (ConsoleDriver driver)
+		{
+			return 
+				GetLinePrefix(driver).Sum(Rune.ColumnWidth) + 
+				Rune.ColumnWidth(GetExpandableSymbol(driver)) + 
+				(tree.AspectGetter(Model) ?? "").Length;
+		}
+
 		/// <summary>
 		/// <summary>
 		/// Renders the current <see cref="Model"/> on the specified line <paramref name="y"/>
 		/// Renders the current <see cref="Model"/> on the specified line <paramref name="y"/>
 		/// </summary>
 		/// </summary>
@@ -1210,6 +1247,7 @@ namespace Terminal.Gui {
 
 
 			return false;
 			return false;
 		}
 		}
+
 	}
 	}
 
 
 	/// <summary>
 	/// <summary>

+ 8 - 8
UICatalog/Scenarios/TreeViewFileSystem.cs

@@ -92,20 +92,20 @@ namespace UICatalog.Scenarios {
 				}
 				}
 				treeViewFiles.SetNeedsDisplay ();
 				treeViewFiles.SetNeedsDisplay ();
 			};
 			};
-			/*
+			
 			_scrollBar.OtherScrollBarView.ChangedPosition += () => {
 			_scrollBar.OtherScrollBarView.ChangedPosition += () => {
-				_listView.LeftItem = _scrollBar.OtherScrollBarView.Position;
-				if (_listView.LeftItem != _scrollBar.OtherScrollBarView.Position) {
-					_scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
+				treeViewFiles.ScrollOffsetHorizontal = _scrollBar.OtherScrollBarView.Position;
+				if (treeViewFiles.ScrollOffsetHorizontal != _scrollBar.OtherScrollBarView.Position) {
+					_scrollBar.OtherScrollBarView.Position = treeViewFiles.ScrollOffsetHorizontal;
 				}
 				}
-				_listView.SetNeedsDisplay ();
-			};*/
+				treeViewFiles.SetNeedsDisplay ();
+			};
 			
 			
 			treeViewFiles.DrawContent += (e) => {
 			treeViewFiles.DrawContent += (e) => {
 				_scrollBar.Size = treeViewFiles.ContentHeight;
 				_scrollBar.Size = treeViewFiles.ContentHeight;
 				_scrollBar.Position = treeViewFiles.ScrollOffsetVertical;
 				_scrollBar.Position = treeViewFiles.ScrollOffsetVertical;
-			//	_scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1;
-			//	_scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
+				_scrollBar.OtherScrollBarView.Size = treeViewFiles.GetContentWidth(true);
+				_scrollBar.OtherScrollBarView.Position = treeViewFiles.ScrollOffsetHorizontal;
 				_scrollBar.Refresh ();
 				_scrollBar.Refresh ();
 			};
 			};
 		}
 		}

+ 5 - 5
UnitTests/TreeViewTests.cs

@@ -89,13 +89,13 @@ namespace UnitTests {
 		{
 		{
 			var tree = CreateTree();
 			var tree = CreateTree();
 
 
-			Assert.Equal(0,tree.ScrollOffset);
+			Assert.Equal(0,tree.ScrollOffsetVertical);
 
 
-			tree.ScrollOffset = -100;
-			Assert.Equal(0,tree.ScrollOffset);
+			tree.ScrollOffsetVertical = -100;
+			Assert.Equal(0,tree.ScrollOffsetVertical);
 			
 			
-			tree.ScrollOffset = 10;
-			Assert.Equal(10,tree.ScrollOffset);
+			tree.ScrollOffsetVertical = 10;
+			Assert.Equal(10,tree.ScrollOffsetVertical);
 		}
 		}