浏览代码

Added DrawLine event to TreeView

Thomas 2 年之前
父节点
当前提交
2f1db90fcd
共有 2 个文件被更改,包括 71 次插入4 次删除
  1. 15 4
      Terminal.Gui/Views/TreeView/Branch.cs
  2. 56 0
      Terminal.Gui/Views/TreeView/TreeView.cs

+ 15 - 4
Terminal.Gui/Views/TreeView/Branch.cs

@@ -201,10 +201,21 @@ namespace Terminal.Gui {
 						));
 			}
 
-			foreach(var cell in cells)
-			{
-				driver.SetAttribute(cell.ColorScheme.Normal);
-				driver.AddRune(cell.Rune);
+			var e = new DrawTreeViewLineEventArgs<T>{
+				Model = Model,
+				Y = y,
+				RuneCells = cells,
+				Tree = tree
+			};
+			tree.OnDrawLine(e);
+
+			if(!e.Handled)
+			{	
+				foreach(var cell in cells)
+				{
+					driver.SetAttribute(cell.ColorScheme.Normal);
+					driver.AddRune(cell.Rune);
+				}
 			}
 
 			driver.SetAttribute (colorScheme.Normal);

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

@@ -169,6 +169,12 @@ namespace Terminal.Gui {
 		/// </summary>
 		public event EventHandler<SelectionChangedEventArgs<T>> SelectionChanged;
 
+		/// <summary>
+		/// Called once for each visible row during rendering.  Can be used
+		/// to make last minute changes to color or text rendered
+		/// </summary>
+		public event EventHandler<DrawTreeViewLineEventArgs<T>> DrawLine;
+
 		/// <summary>
 		/// The root objects in the tree, note that this collection is of root objects only.
 		/// </summary>
@@ -1421,8 +1427,58 @@ namespace Terminal.Gui {
 		{
 			SelectionChanged?.Invoke (this, e);
 		}
+
+		/// <summary>
+		/// Raises the DrawLine event
+		/// </summary>
+		/// <param name="e"></param>
+		internal void OnDrawLine (DrawTreeViewLineEventArgs<T> e)
+		{
+			DrawLine?.Invoke(this,e);
+		}
+
+	}
+
+	/// <summary>
+	/// Event args for the <see cref="TreeView{T}.DrawLine"/> event
+	/// </summary>
+	/// <typeparam name="T"></typeparam>
+	public class DrawTreeViewLineEventArgs<T> where T : class{
+
+		/// <summary>
+		/// The object at this line in the tree
+		/// </summary>
+		public T Model {get;init;}
+
+		/// <summary>
+		/// The <see cref="TreeView{T}"/> that is performing the
+		/// rendering.
+		/// </summary>
+		public TreeView<T> Tree {get; init;}
+
+		/// <summary>
+		/// The line within tree view bounds that is being rendered
+		/// </summary>
+		public int Y {get;init;}
+
+		/// <summary>
+		/// Set to true to cancel drawing (e.g. if you have already manually
+		/// drawn content).
+		/// </summary>
+		public bool Handled {get;set;}
+
+		/// <summary>
+		/// The rune and color of each symbol that will be rendered.  Note
+		/// that only <see cref="ColorScheme.Normal"/> is respected.  You
+		/// can modify these to change what is rendered.
+		/// </summary>
+		/// <remarks>
+		/// Changing the length of this collection may result in corrupt rendering
+		/// </remarks>
+		public List<RuneCell> RuneCells {get; init;}
 	}
 
+
 	class TreeSelection<T> where T : class {
 
 		public Branch<T> Origin { get; }