From 51aefce32ba77a894994f2a15deefee0fa50723c Mon Sep 17 00:00:00 2001 From: tznind Date: Fri, 18 Dec 2020 18:52:09 +0000 Subject: [PATCH] changed expand color settings to use ColorScheme --- Terminal.Gui/Views/TreeView.cs | 60 +++++++++++------------ UICatalog/Scenarios/TreeViewFileSystem.cs | 34 ++++++------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/Terminal.Gui/Views/TreeView.cs b/Terminal.Gui/Views/TreeView.cs index 70ad88e63..6064479ca 100644 --- a/Terminal.Gui/Views/TreeView.cs +++ b/Terminal.Gui/Views/TreeView.cs @@ -7,7 +7,7 @@ using System.Linq; namespace Terminal.Gui { /// - /// Interface to implement when you want to automatically determine children for your class + /// Interface to implement when you want the regular (non generic) to automatically determine children for your class (without having to specify a ) /// public interface ITreeNode { @@ -25,7 +25,7 @@ namespace Terminal.Gui { } /// - /// Simple class for representing nodes of a . + /// Simple class for representing nodes, use with regular (non generic) . /// public class TreeNode : ITreeNode { @@ -80,6 +80,7 @@ namespace Terminal.Gui { /// /// Returns true/false for whether a model has children. This method should be implemented when is an expensive operation otherwise should return false (in which case this method will not be called) /// + /// Only implement this method if you have a very fast way of determining whether an object can have children e.g. checking a Type (directories can always be expanded) /// /// bool CanExpand(T model); @@ -93,7 +94,7 @@ namespace Terminal.Gui { } /// - /// Abstract implementation of + /// Abstract implementation of . /// public abstract class TreeBuilder : ITreeBuilder { @@ -101,7 +102,7 @@ namespace Terminal.Gui { public bool SupportsCanExpand { get; protected set;} = false; /// - /// Override this method to return a rapid answer as to whether returns results. + /// Override this method to return a rapid answer as to whether returns results. If you are implementing this method ensure you passed true in base constructor or set /// /// /// @@ -250,12 +251,6 @@ namespace Terminal.Gui { /// Symbol to use for branch nodes that can be expanded to indicate this to the user. Defaults to '+'. Set to null to hide /// public Rune? ExpandableSymbol {get;set;} = '+'; - - /// - /// Optional color scheme to use when rendering (defaults to null) - /// - public Attribute? ExpandableSymbolColor {get;set;} - /// /// Symbol to use for branch nodes that can be collapsed (are currently expanded). Defaults to '-'. Set to null to hide @@ -263,9 +258,14 @@ namespace Terminal.Gui { public Rune? CollapseableSymbol {get;set;} = '-'; /// - /// Optional color scheme to use when rendering (defaults to null) + /// Set to true to highlight expand/collapse symbols in hot key color /// - public Attribute? CollapseableSymbolColor {get;set;} + public bool ColorExpandSymbol {get;set;} + + /// + /// Invert console colours used to render the expand symbol + /// + public bool InvertExpandSymbolColors {get;set;} } @@ -790,14 +790,13 @@ namespace Terminal.Gui { { // true if the current line of the tree is the selected one and control has focus bool isSelected = tree.SelectedObject == Model && tree.HasFocus; - Attribute lineColor = isSelected? colorScheme.HotFocus : colorScheme.Normal; + Attribute lineColor = isSelected? colorScheme.Focus : colorScheme.Normal; driver.SetAttribute(lineColor); // Everything on line before the expansion run and branch text Rune[] prefix = GetLinePrefix(driver).ToArray(); Rune expansion = GetExpandableSymbol(driver); - Attribute? expansionColor = GetExpandableSymbolColor(); string lineBody = tree.AspectGetter(Model); var remainingWidth = availableWidth - (prefix.Length + 1 + lineBody.Length); @@ -807,9 +806,21 @@ namespace Terminal.Gui { foreach(Rune r in prefix) driver.AddRune(r); - // if it is not the curerntly selected line render the expansion symbol in the appropriate color scheme - if(!isSelected && expansionColor.HasValue) - driver.SetAttribute(expansionColor.Value); + // pick color for expanded symbol + if(tree.Style.ColorExpandSymbol || tree.Style.InvertExpandSymbolColors) + { + Attribute color; + + if(tree.Style.ColorExpandSymbol) + color = isSelected ? tree.ColorScheme.HotFocus : tree.ColorScheme.HotNormal; + else + color = lineColor; + + if(tree.Style.InvertExpandSymbolColors) + color = new Attribute(color.Background,color.Foreground); + + driver.SetAttribute(color); + } driver.AddRune(expansion); @@ -890,21 +901,6 @@ namespace Terminal.Gui { return leafSymbol; } - /// - /// Returns an appropriate color according to the for displaying the - /// - /// - public Attribute? GetExpandableSymbolColor() - { - if(IsExpanded) - return tree.Style.CollapseableSymbolColor; - - if(CanExpand()) - return tree.Style.ExpandableSymbolColor; - - return null; - } - /// /// Returns true if the current branch can be expanded according to the or cached children already fetched /// diff --git a/UICatalog/Scenarios/TreeViewFileSystem.cs b/UICatalog/Scenarios/TreeViewFileSystem.cs index a9ba12fac..24c48fe37 100644 --- a/UICatalog/Scenarios/TreeViewFileSystem.cs +++ b/UICatalog/Scenarios/TreeViewFileSystem.cs @@ -28,6 +28,7 @@ namespace UICatalog.Scenarios { private MenuItem miArrowSymbols; private MenuItem miNoSymbols; private MenuItem miColoredSymbols; + private MenuItem miInvertSymbols; private Terminal.Gui.Attribute green; private Terminal.Gui.Attribute red; @@ -50,6 +51,7 @@ namespace UICatalog.Scenarios { miArrowSymbols = new MenuItem ("_ArrowSymbols", "", () => SetExpandableSymbols('>','v')){Checked = false, CheckType = MenuItemCheckStyle.Radio}, miNoSymbols = new MenuItem ("_NoSymbols", "", () => SetExpandableSymbols(null,null)){Checked = false, CheckType = MenuItemCheckStyle.Radio}, miColoredSymbols = new MenuItem ("_ColoredSymbols", "", () => ShowColoredExpandableSymbols()){Checked = false, CheckType = MenuItemCheckStyle.Checked}, + miInvertSymbols = new MenuItem ("_InvertSymbols", "", () => InvertExpandableSymbols()){Checked = false, CheckType = MenuItemCheckStyle.Checked}, }), }); Top.Add (menu); @@ -152,26 +154,22 @@ namespace UICatalog.Scenarios { { miColoredSymbols.Checked = !miColoredSymbols.Checked; - ShowColoredExpandableSymbols(treeViewFiles); - ShowColoredExpandableSymbols(treeViewNodes); + treeViewFiles.Style.ColorExpandSymbol = miColoredSymbols.Checked; + treeViewFiles.SetNeedsDisplay(); + + treeViewNodes.Style.ColorExpandSymbol = miColoredSymbols.Checked; + treeViewNodes.SetNeedsDisplay(); + } + private void InvertExpandableSymbols(){ + miInvertSymbols.Checked = !miInvertSymbols.Checked; + + treeViewFiles.Style.InvertExpandSymbolColors = miInvertSymbols.Checked; + treeViewFiles.SetNeedsDisplay(); + + treeViewNodes.Style.InvertExpandSymbolColors = miInvertSymbols.Checked; + treeViewNodes.SetNeedsDisplay(); } - private void ShowColoredExpandableSymbols (ITreeView treeView) - { - // Toggle Green expand symbols - if(miColoredSymbols.Checked) - treeView.Style.ExpandableSymbolColor = green; - else - treeView.Style.ExpandableSymbolColor = null; //clear it - - // Toggle Red collapse symbols - if(miColoredSymbols.Checked) - treeView.Style.CollapseableSymbolColor = red; - else - treeView.Style.CollapseableSymbolColor = null; //clear it - - treeView.SetNeedsDisplay(); - } private ITreeNode CreateSimpleRoot () {