From 8e148013eca896ed361177e04055880974e55629 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 8 May 2022 22:44:28 +0100 Subject: [PATCH 1/2] Added ColorGetter to TreeView --- Terminal.Gui/Core/Trees/Branch.cs | 22 ++++++++- Terminal.Gui/Views/TreeView.cs | 7 +++ UnitTests/TreeViewTests.cs | 80 +++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Core/Trees/Branch.cs b/Terminal.Gui/Core/Trees/Branch.cs index 10c189320..35a81965a 100644 --- a/Terminal.Gui/Core/Trees/Branch.cs +++ b/Terminal.Gui/Core/Trees/Branch.cs @@ -159,9 +159,27 @@ namespace Terminal.Gui.Trees { availableWidth -= lineBody.Length; } - //reset the line color if it was changed for rendering expansion symbol - driver.SetAttribute (lineColor); + + // default behaviour is for model to use the color scheme + // of the tree view + var modelColor = lineColor; + + // if custom color delegate invoke it + if(tree.ColorGetter != null) + { + var modelScheme = tree.ColorGetter(Model); + + // if custom color scheme is defined for this Model + if(modelScheme != null) + { + // use it + modelColor = isSelected ? modelScheme.Focus : modelScheme.Normal; + } + } + + driver.SetAttribute (modelColor); driver.AddStr (lineBody); + driver.SetAttribute (lineColor); if (availableWidth > 0) { driver.AddStr (new string (' ', availableWidth)); diff --git a/Terminal.Gui/Views/TreeView.cs b/Terminal.Gui/Views/TreeView.cs index b8908bd8c..fc9723dbe 100644 --- a/Terminal.Gui/Views/TreeView.cs +++ b/Terminal.Gui/Views/TreeView.cs @@ -140,6 +140,13 @@ namespace Terminal.Gui { /// public MouseFlags? ObjectActivationButton { get; set; } = MouseFlags.Button1DoubleClicked; + + /// + /// Delegate for multi colored tree views. Return the to use + /// for each passed object or null to use the default. + /// + public Func ColorGetter {get;set;} + /// /// Secondary selected regions of tree when is true /// diff --git a/UnitTests/TreeViewTests.cs b/UnitTests/TreeViewTests.cs index f5121e1f2..f1520b40c 100644 --- a/UnitTests/TreeViewTests.cs +++ b/UnitTests/TreeViewTests.cs @@ -6,10 +6,19 @@ using System.Threading.Tasks; using Terminal.Gui; using Terminal.Gui.Trees; using Xunit; +using Xunit.Abstractions; namespace Terminal.Gui.Views { public class TreeViewTests { + + readonly ITestOutputHelper output; + + public TreeViewTests (ITestOutputHelper output) + { + this.output = output; + } + #region Test Setup Methods class Factory { public Car [] Cars { get; set; } @@ -712,6 +721,77 @@ namespace Terminal.Gui.Views { } + [Fact, AutoInitShutdown] + public void TestTreeViewColor() + { + var tv = new TreeView{Width = 20,Height = 10}; + + var n1 = new TreeNode("normal"); + var n1_1 = new TreeNode("pink"); + var n1_2 = new TreeNode("normal"); + n1.Children.Add(n1_1); + n1.Children.Add(n1_2); + + var n2 = new TreeNode("pink"); + tv.AddObject(n1); + tv.AddObject(n2); + tv.Expand(n1); + + var pink = new Attribute(Color.Magenta,Color.Black); + var hotpink = new Attribute(Color.BrightMagenta,Color.Black); + + tv.ColorScheme = new ColorScheme(); + tv.Redraw(tv.Bounds); + + // Normal drawing of the tree view + GraphViewTests.AssertDriverContentsAre( +@"├-normal +│ ├─pink +│ └─normal +└─pink +",output); + // Should all be the same color + GraphViewTests.AssertDriverColorsAre( +@"00000000 +00000000 +0000000000 +000000 +", + new []{tv.ColorScheme.Normal,pink}); + + // create a new color scheme + var pinkScheme = new ColorScheme + { + Normal = pink, + Focus = hotpink + }; + + // and a delegate that uses the pink color scheme + // for nodes "pink" + tv.ColorGetter = (n)=> n.Text.Equals("pink") ? pinkScheme : null; + + // redraw now that the custom color + // delegate is registered + tv.Redraw(tv.Bounds); + + // Same text + GraphViewTests.AssertDriverContentsAre( +@"├-normal +│ ├─pink +│ └─normal +└─pink +",output); + // but now the item (only not lines) appear + // in pink when they are the word "pink" + GraphViewTests.AssertDriverColorsAre( +@"00000000 +00001111 +0000000000 +001111 +", + new []{tv.ColorScheme.Normal,pink}); + } + /// /// Test object which considers for equality only /// From f534c2e7eb87d4d9c5c455c778bc2fffe88a9890 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 8 May 2022 23:02:47 +0100 Subject: [PATCH 2/2] Added color tailoring into scenario --- UICatalog/Scenarios/TreeViewFileSystem.cs | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/UICatalog/Scenarios/TreeViewFileSystem.cs b/UICatalog/Scenarios/TreeViewFileSystem.cs index d3510209b..f823690bb 100644 --- a/UICatalog/Scenarios/TreeViewFileSystem.cs +++ b/UICatalog/Scenarios/TreeViewFileSystem.cs @@ -24,6 +24,7 @@ namespace UICatalog.Scenarios { private MenuItem miUnicodeSymbols; private MenuItem miFullPaths; private MenuItem miLeaveLastRow; + private MenuItem miCustomColors; private Terminal.Gui.Attribute green; private Terminal.Gui.Attribute red; @@ -52,6 +53,7 @@ namespace UICatalog.Scenarios { miInvertSymbols = new MenuItem ("_InvertSymbols", "", () => InvertExpandableSymbols()){Checked = false, CheckType = MenuItemCheckStyle.Checked}, miFullPaths = new MenuItem ("_FullPaths", "", () => SetFullName()){Checked = false, CheckType = MenuItemCheckStyle.Checked}, miLeaveLastRow = new MenuItem ("_LeaveLastRow", "", () => SetLeaveLastRow()){Checked = true, CheckType = MenuItemCheckStyle.Checked}, + miCustomColors = new MenuItem ("C_ustomColors", "", () => SetCustomColors()){Checked = false, CheckType = MenuItemCheckStyle.Checked}, }), }); Top.Add (menu); @@ -209,6 +211,28 @@ namespace UICatalog.Scenarios { miLeaveLastRow.Checked = !miLeaveLastRow.Checked; treeViewFiles.Style.LeaveLastRow = miLeaveLastRow.Checked; } + private void SetCustomColors() + { + var yellow = new ColorScheme + { + Focus = new Terminal.Gui.Attribute(Color.BrightYellow,treeViewFiles.ColorScheme.Focus.Background), + Normal = new Terminal.Gui.Attribute (Color.BrightYellow,treeViewFiles.ColorScheme.Normal.Background), + }; + + miCustomColors.Checked = !miCustomColors.Checked; + + if(miCustomColors.Checked) + { + treeViewFiles.ColorGetter = (m)=> + { + return m is DirectoryInfo ? yellow : null; + }; + } + else + { + treeViewFiles.ColorGetter = null; + } + } private IEnumerable GetChildren (FileSystemInfo model)