diff --git a/Terminal.Gui/Views/TreeBuilder.cs b/Terminal.Gui/Views/TreeBuilder.cs new file mode 100644 index 000000000..a76af982e --- /dev/null +++ b/Terminal.Gui/Views/TreeBuilder.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Terminal.Gui { + + /// + /// Interface for supplying data to a on demand as root level nodes + /// are expanded by the user + /// + public interface ITreeBuilder { + /// + /// Returns true if is implemented by this class + /// + /// + bool SupportsCanExpand { get; } + + /// + /// 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 toExpand); + + /// + /// Returns all children of a given which should be added to the + /// tree as new branches underneath it + /// + /// + /// + IEnumerable GetChildren (T forObject); + } + + /// + /// Abstract implementation of . + /// + public abstract class TreeBuilder : ITreeBuilder { + + /// + public bool SupportsCanExpand { get; protected set; } = false; + + /// + /// 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 + /// + /// + /// + public virtual bool CanExpand (T toExpand) + { + + return GetChildren (toExpand).Any (); + } + + /// + public abstract IEnumerable GetChildren (T forObject); + + /// + /// Constructs base and initializes + /// + /// Pass true if you intend to + /// implement otherwise false + public TreeBuilder (bool supportsCanExpand) + { + SupportsCanExpand = supportsCanExpand; + } + } + + + + /// + /// implementation for objects + /// + public class TreeNodeBuilder : TreeBuilder { + + /// + /// Initialises a new instance of builder for any model objects of + /// Type + /// + public TreeNodeBuilder () : base (false) + { + + } + + /// + /// Returns from + /// + /// + /// + public override IEnumerable GetChildren (ITreeNode model) + { + return model.Children; + } + } + + + /// + /// Implementation of that uses user defined functions + /// + public class DelegateTreeBuilder : TreeBuilder { + private Func> childGetter; + private Func canExpand; + + /// + /// Constructs an implementation of that calls the user + /// defined method to determine children + /// + /// + /// + public DelegateTreeBuilder (Func> childGetter) : base (false) + { + this.childGetter = childGetter; + } + + /// + /// Constructs an implementation of that calls the user + /// defined method to determine children + /// and to determine expandability + /// + /// + /// + /// + public DelegateTreeBuilder (Func> childGetter, Func canExpand) : base (true) + { + this.childGetter = childGetter; + this.canExpand = canExpand; + } + + /// + /// Returns whether a node can be expanded based on the delegate passed during construction + /// + /// + /// + public override bool CanExpand (T toExpand) + { + return canExpand?.Invoke (toExpand) ?? base.CanExpand (toExpand); + } + + /// + /// Returns children using the delegate method passed during construction + /// + /// + /// + public override IEnumerable GetChildren (T forObject) + { + return childGetter.Invoke (forObject); + } + } +} \ No newline at end of file diff --git a/Terminal.Gui/Views/TreeNode.cs b/Terminal.Gui/Views/TreeNode.cs new file mode 100644 index 000000000..5ae07ae19 --- /dev/null +++ b/Terminal.Gui/Views/TreeNode.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; + +namespace Terminal.Gui { + + /// + /// Interface to implement when you want the regular (non generic) + /// to automatically determine children for your class (without having to specify + /// an ) + /// + public interface ITreeNode { + /// + /// Text to display when rendering the node + /// + string Text { get; set; } + + /// + /// The children of your class which should be rendered underneath it when expanded + /// + /// + IList Children { get; } + + /// + /// Optionally allows you to store some custom data/class here. + /// + object Tag { get; set; } + } + + /// + /// Simple class for representing nodes, use with regular (non generic) . + /// + public class TreeNode : ITreeNode { + /// + /// Children of the current node + /// + /// + public virtual IList Children { get; set; } = new List (); + + /// + /// Text to display in tree node for current entry + /// + /// + public virtual string Text { get; set; } + + /// + /// Optionally allows you to store some custom data/class here. + /// + public object Tag { get; set; } + + /// + /// returns + /// + /// + public override string ToString () + { + return Text ?? "Unamed Node"; + } + + /// + /// Initialises a new instance with no + /// + public TreeNode () + { + + } + /// + /// Initialises a new instance and sets starting + /// + public TreeNode (string text) + { + Text = text; + } + } +} \ No newline at end of file diff --git a/Terminal.Gui/Views/TreeStyle.cs b/Terminal.Gui/Views/TreeStyle.cs new file mode 100644 index 000000000..a6269aa60 --- /dev/null +++ b/Terminal.Gui/Views/TreeStyle.cs @@ -0,0 +1,47 @@ +using System; + +namespace Terminal.Gui { + /// + /// Defines rendering options that affect how the tree is displayed + /// + public class TreeStyle { + + /// + /// True to render vertical lines under expanded nodes to show which node belongs to which + /// parent. False to use only whitespace + /// + /// + public bool ShowBranchLines { get; set; } = true; + + /// + /// 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; } = '+'; + + /// + /// Symbol to use for branch nodes that can be collapsed (are currently expanded). + /// Defaults to '-'. Set to null to hide + /// + public Rune? CollapseableSymbol { get; set; } = '-'; + + /// + /// Set to true to highlight expand/collapse symbols in hot key color + /// + public bool ColorExpandSymbol { get; set; } + + /// + /// Invert console colours used to render the expand symbol + /// + public bool InvertExpandSymbolColors { get; set; } + + /// + /// True to leave the last row of the control free for overwritting (e.g. by a scrollbar) + /// When True scrolling will be triggered on the second last row of the control rather than + /// the last. + /// + /// + public bool LeaveLastRow { get; set; } + + } +} \ No newline at end of file diff --git a/Terminal.Gui/Views/TreeView.cs b/Terminal.Gui/Views/TreeView.cs index 82db24c7b..770daca0c 100644 --- a/Terminal.Gui/Views/TreeView.cs +++ b/Terminal.Gui/Views/TreeView.cs @@ -9,221 +9,6 @@ using NStack; namespace Terminal.Gui { - /// - /// Interface to implement when you want the regular (non generic) - /// to automatically determine children for your class (without having to specify - /// an ) - /// - public interface ITreeNode { - /// - /// Text to display when rendering the node - /// - string Text { get; set; } - - /// - /// The children of your class which should be rendered underneath it when expanded - /// - /// - IList Children { get; } - - /// - /// Optionally allows you to store some custom data/class here. - /// - object Tag { get; set; } - } - - /// - /// Simple class for representing nodes, use with regular (non generic) . - /// - public class TreeNode : ITreeNode { - /// - /// Children of the current node - /// - /// - public virtual IList Children { get; set; } = new List (); - - /// - /// Text to display in tree node for current entry - /// - /// - public virtual string Text { get; set; } - - /// - /// Optionally allows you to store some custom data/class here. - /// - public object Tag { get; set; } - - /// - /// returns - /// - /// - public override string ToString () - { - return Text ?? "Unamed Node"; - } - - /// - /// Initialises a new instance with no - /// - public TreeNode () - { - - } - /// - /// Initialises a new instance and sets starting - /// - public TreeNode (string text) - { - Text = text; - } - } - - /// - /// Interface for supplying data to a on demand as root level nodes - /// are expanded by the user - /// - public interface ITreeBuilder { - /// - /// Returns true if is implemented by this class - /// - /// - bool SupportsCanExpand { get; } - - /// - /// 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 toExpand); - - /// - /// Returns all children of a given which should be added to the - /// tree as new branches underneath it - /// - /// - /// - IEnumerable GetChildren (T forObject); - } - - /// - /// Abstract implementation of . - /// - public abstract class TreeBuilder : ITreeBuilder { - - /// - public bool SupportsCanExpand { get; protected set; } = false; - - /// - /// 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 - /// - /// - /// - public virtual bool CanExpand (T toExpand) - { - - return GetChildren (toExpand).Any (); - } - - /// - public abstract IEnumerable GetChildren (T forObject); - - /// - /// Constructs base and initializes - /// - /// Pass true if you intend to - /// implement otherwise false - public TreeBuilder (bool supportsCanExpand) - { - SupportsCanExpand = supportsCanExpand; - } - } - - /// - /// implementation for objects - /// - public class TreeNodeBuilder : TreeBuilder { - - /// - /// Initialises a new instance of builder for any model objects of - /// Type - /// - public TreeNodeBuilder () : base (false) - { - - } - - /// - /// Returns from - /// - /// - /// - public override IEnumerable GetChildren (ITreeNode model) - { - return model.Children; - } - } - - /// - /// Implementation of that uses user defined functions - /// - public class DelegateTreeBuilder : TreeBuilder { - private Func> childGetter; - private Func canExpand; - - /// - /// Constructs an implementation of that calls the user - /// defined method to determine children - /// - /// - /// - public DelegateTreeBuilder (Func> childGetter) : base (false) - { - this.childGetter = childGetter; - } - - /// - /// Constructs an implementation of that calls the user - /// defined method to determine children - /// and to determine expandability - /// - /// - /// - /// - public DelegateTreeBuilder (Func> childGetter, Func canExpand) : base (true) - { - this.childGetter = childGetter; - this.canExpand = canExpand; - } - - /// - /// Returns whether a node can be expanded based on the delegate passed during construction - /// - /// - /// - public override bool CanExpand (T toExpand) - { - return canExpand?.Invoke (toExpand) ?? base.CanExpand (toExpand); - } - - /// - /// Returns children using the delegate method passed during construction - /// - /// - /// - public override IEnumerable GetChildren (T forObject) - { - return childGetter.Invoke (forObject); - } - } - /// /// Interface for all non generic members of /// @@ -263,50 +48,6 @@ namespace Terminal.Gui { } } - /// - /// Defines rendering options that affect how the tree is displayed - /// - public class TreeStyle { - - /// - /// True to render vertical lines under expanded nodes to show which node belongs to which - /// parent. False to use only whitespace - /// - /// - public bool ShowBranchLines { get; set; } = true; - - /// - /// 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; } = '+'; - - /// - /// Symbol to use for branch nodes that can be collapsed (are currently expanded). - /// Defaults to '-'. Set to null to hide - /// - public Rune? CollapseableSymbol { get; set; } = '-'; - - /// - /// Set to true to highlight expand/collapse symbols in hot key color - /// - public bool ColorExpandSymbol { get; set; } - - /// - /// Invert console colours used to render the expand symbol - /// - public bool InvertExpandSymbolColors { get; set; } - - /// - /// True to leave the last row of the control free for overwritting (e.g. by a scrollbar) - /// When True scrolling will be triggered on the second last row of the control rather than - /// the last. - /// - /// - public bool LeaveLastRow { get; set; } - - } - /// /// Hierarchical tree view with expandable branches. Branch objects are dynamically determined /// when expanded using a user defined