Kaynağa Gözat

Use the new DrawLine event in file system tree to color folder icons

Thomas 2 yıl önce
ebeveyn
işleme
95e0d72bda

+ 16 - 2
Terminal.Gui/Views/TreeView/Branch.cs

@@ -96,6 +96,9 @@ namespace Terminal.Gui {
 		public virtual void Draw (ConsoleDriver driver, ColorScheme colorScheme, int y, int availableWidth)
 		{
 			var cells = new List<RuneCell>();
+			int? indexOfExpandCollapseSymbol = null;
+			int indexOfModelText;
+			
 
 			// true if the current line of the tree is the selected one and control has focus
 			bool isSelected = tree.IsSelected (Model);
@@ -149,19 +152,28 @@ namespace Terminal.Gui {
 			if (toSkip > 0) {
 				toSkip--;
 			} else {
-
+				indexOfExpandCollapseSymbol = cells.Count;
 				cells.Add(NewRuneCell(attr,expansion));
 				availableWidth -= expansion.GetColumns ();
 			}
 
 			// horizontal scrolling has already skipped the prefix but now must also skip some of the line body
 			if (toSkip > 0) {
+				
+				// For the event record a negative location for where model text starts since it
+				// is pushed off to the left because of scrolling
+				indexOfModelText = -toSkip;
+
 				if (toSkip > lineBody.Length) {
 					lineBody = "";
 				} else {
 					lineBody = lineBody.Substring (toSkip);
 				}
 			}
+			else
+			{
+				indexOfModelText = cells.Count;
+			}
 
 			// If body of line is too long
 			if (lineBody.EnumerateRunes ().Sum (l => l.GetColumns ()) > availableWidth) {
@@ -205,7 +217,9 @@ namespace Terminal.Gui {
 				Model = Model,
 				Y = y,
 				RuneCells = cells,
-				Tree = tree
+				Tree = tree,
+				IndexOfExpandCollapseSymbol = indexOfExpandCollapseSymbol,
+				IndexOfModelText = indexOfModelText,
 			};
 			tree.OnDrawLine(e);
 

+ 20 - 0
Terminal.Gui/Views/TreeView/TreeView.cs

@@ -1476,6 +1476,26 @@ namespace Terminal.Gui {
 		/// Changing the length of this collection may result in corrupt rendering
 		/// </remarks>
 		public List<RuneCell> RuneCells {get; init;}
+
+		/// <summary>
+		/// The notional index in <see cref="RuneCells"/> which contains the first
+		/// character of the <see cref="TreeView{T}.AspectGetter"/> text (i.e.
+		/// after all branch lines and expansion/collapse sybmols).
+		/// </summary>
+		/// <remarks>
+		/// May be negative or outside of bounds of <see cref="RuneCells"/> if the view
+		/// has been scrolled horizontally.
+		/// </remarks>
+
+		public int IndexOfModelText {get;init;}
+
+		/// <summary>
+		/// If line contains a branch that can be expanded/collapsed then this is
+		/// the index in <see cref="RuneCells"/> at which the symbol is (or null for
+		/// leaf elements).
+		/// </summary>
+		public int? IndexOfExpandCollapseSymbol {get;init;}
+
 	}
 
 

+ 21 - 0
UICatalog/Scenarios/TreeViewFileSystem.cs

@@ -84,6 +84,7 @@ namespace UICatalog.Scenarios {
 				Width = Dim.Percent (50),
 				Height = Dim.Fill (),
 			};
+			treeViewFiles.DrawLine += TreeViewFiles_DrawLine;
 
 			_detailsFrame = new DetailsFrame (_iconProvider) {
 				X = Pos.Right (treeViewFiles),
@@ -140,6 +141,26 @@ namespace UICatalog.Scenarios {
 			ShowPropertiesOf (e.NewValue);
 		}
 
+		private void TreeViewFiles_DrawLine (object sender, DrawTreeViewLineEventArgs<IFileSystemInfo> e)
+		{
+			// Render directory icons in yellow
+			if(e.Model is IDirectoryInfo d)
+			{
+				if(_iconProvider.UseNerdIcons || _iconProvider.UseUnicodeCharacters)
+				{
+					if(e.IndexOfModelText > 0 && e.IndexOfModelText < e.RuneCells.Count)
+					{
+						var cell = e.RuneCells[e.IndexOfModelText];
+						cell.ColorScheme = new ColorScheme(
+							new Terminal.Gui.Attribute(
+								Color.BrightYellow,
+								cell.ColorScheme.Normal.Background)
+						);
+					}
+				}
+			}
+		}
+
 		private void TreeViewFiles_KeyPress (object sender, KeyEventEventArgs obj)
 		{
 			if (obj.KeyEvent.Key == (Key.R | Key.CtrlMask)) {