Class TreeState<T>
- Namespace
- MiniTui
- Assembly
- MiniTui.dll
The rows, expanded nodes, selection and scroll offset of a tree (1.x spec §11), drawn with
Tree<T>(TreeState<T>). T is the app's own node type: the state asks for each
expanded node's children instead of holding a copy of the tree.
public sealed class TreeState<T> where T : class
Type Parameters
T
- Inheritance
-
TreeState<T>
- Inherited Members
Constructors
TreeState(Func<T, IReadOnlyList<T>>, Func<T, string>, IEqualityComparer<T>?)
Create a state.
public TreeState(Func<T, IReadOnlyList<T>> children, Func<T, string> text, IEqualityComparer<T>? comparer = null)
Parameters
childrenFunc<T, IReadOnlyList<T>>A node's children. Asked for only while the node is expanded.
textFunc<T, string>A node's label: drawn by default, and searched by type-ahead.
comparerIEqualityComparer<T>How nodes are matched, for the expanded set and the selection.
Properties
Checkboxes
Draw a check box on each row, toggled by Space and a click on it (1.x spec §18.5). Off by default.
public bool Checkboxes { get; set; }
Property Value
CheckedNodes
The checked nodes in tree order: each checked node without children and each parent whose descendants are all checked, without what's under it.
public IReadOnlyList<T> CheckedNodes { get; }
Property Value
Filter
Show only the nodes this matches and their ancestors, drawn open so the matches show (1.x spec §18.4). The children of every node are read, expanded or not. ← and the expander can still close a node while filtering; that's forgotten when the filter changes, and the expanded nodes aren't touched. Null shows the whole tree.
public Func<T, bool>? Filter { get; set; }
Property Value
HasChildren
Whether a node can expand, without reading its children; drawn as an expander. By default, whether its children aren't empty. Set it when reading children is slow, or to show an expander before they're loaded.
public Func<T, bool>? HasChildren { get; set; }
Property Value
Offset
The first visible row. Adjusted when drawn so Selected is visible after the selection moves; the wheel and the scrollbar scroll without moving the selection (1.x spec §14.4).
public int Offset { get; set; }
Property Value
Roots
The top-level nodes. Setting them keeps the expanded nodes and the selection where they still match.
public IReadOnlyList<T> Roots { get; set; }
Property Value
Rows
The rows the tree shows: every root and, depth first, the children of each expanded node. Computed when drawn, on each key and mouse event, and when the roots or the expanded nodes change (1.x spec §11.4).
public IReadOnlyList<TreeRow<T>> Rows { get; }
Property Value
- IReadOnlyList<TreeRow<T>>
Selected
The selected node, or null for none (the first row is selected when the rows are next computed). Setting it doesn't raise SelectionChanged or expand anything; a node that isn't in Rows is replaced when the rows are next computed.
public T? Selected { get; set; }
Property Value
- T
ViewportHeight
How many rows were visible at the last draw. Used by PgUp and PgDn.
public int ViewportHeight { get; }
Property Value
Methods
Collapse(T)
Collapse a node. If the selection was inside it, the node is selected.
public void Collapse(T node)
Parameters
nodeT
CollapseAll()
Collapse every node.
public void CollapseAll()
Expand(T)
Expand a node. Does nothing for a node without children or one already expanded.
public void Expand(T node)
Parameters
nodeT
ExpandAll()
Expand every node that has children. This reads the whole tree.
public void ExpandAll()
HandleKey(ConsoleKeyInfo)
Handle ↑ ↓ PgUp PgDn Home End, → (expand, or the first child), ← (collapse, or the parent), Enter (toggle, or Activated on a leaf) and type-ahead.
public bool HandleKey(ConsoleKeyInfo key)
Parameters
keyConsoleKeyInfo
Returns
- bool
True if the key was one of these.
HandleMouse(MouseEvent)
Handle the mouse over the tree drawn last: the wheel scrolls by one row, a left press on a row selects it and, on its expander, also toggles it, and the scrollbar's thumb drags and its track pages (1.x spec §14.4). A double-click on a row activates it as Enter does: a leaf raises Activated, a parent expands or collapses (§17.4). Scrolling doesn't move the selection.
public bool HandleMouse(MouseEvent mouse)
Parameters
mouseMouseEvent
Returns
- bool
True if the event did any of these.
IsChecked(T)
Whether a node is checked: for a node with children, true when they're all checked, false when none is, and null when some are; for one without (or with none loaded), whether it was checked.
public bool? IsChecked(T node)
Parameters
nodeT
Returns
- bool?
IsExpanded(T)
Whether node is expanded.
public bool IsExpanded(T node)
Parameters
nodeT
Returns
Reload(T)
Forget a node's loaded children (and cancel a load that's running), so they load again when it's next expanded, or now if it's expanded. Does nothing for a state without a loader.
public void Reload(T node)
Parameters
nodeT
Reveal(IReadOnlyList<T>)
Expand each node of path but the last, select the last, and scroll it into view at the next
draw (1.x spec §18.3). The path starts at a root, and each node must be among the previous one's children.
public bool Reveal(IReadOnlyList<T> path)
Parameters
pathIReadOnlyList<T>
Returns
- bool
False, changing nothing, when the path doesn't match the tree or the filter hides its last node.
RevealAsync(IReadOnlyList<T>, CancellationToken)
As Reveal(IReadOnlyList<T>), after loading the children of each node on the path that aren't loaded yet, for a state from Async<T>(Func<T, CancellationToken, Task<IReadOnlyList<T>>>, Func<T, string>, IEqualityComparer<T>?) (1.x spec §18.3). Without a loader it's Reveal(IReadOnlyList<T>).
public Task<bool> RevealAsync(IReadOnlyList<T> path, CancellationToken cancellationToken = default)
Parameters
pathIReadOnlyList<T>cancellationTokenCancellationToken
Returns
- Task<bool>
False when the path doesn't match the tree (as far as it loaded) or the filter hides its node.
SetChecked(T, bool)
Check or uncheck a node and everything under it, and raise CheckedChanged.
public void SetChecked(T node, bool isChecked)
Parameters
nodeTisCheckedbool
Toggle(T)
Expand a collapsed node or collapse an expanded one.
public void Toggle(T node)
Parameters
nodeT
Events
Activated
Raised when Enter is pressed on a node without children.
public event EventHandler<TreeNodeEventArgs<T>>? Activated
Event Type
CheckedChanged
Raised when Space, a click or SetChecked(T, bool) checks or unchecks a node (the one toggled).
public event EventHandler<TreeNodeEventArgs<T>>? CheckedChanged
Event Type
Collapsed
Raised when a node collapses, from a key, a click or code.
public event EventHandler<TreeNodeEventArgs<T>>? Collapsed
Event Type
Expanded
Raised when a node expands, from a key, a click or code. Lazy loaders start loading here.
public event EventHandler<TreeNodeEventArgs<T>>? Expanded
Event Type
SelectionChanged
Raised on the UI thread when the user moves the selection (not when code sets it).
public event EventHandler? SelectionChanged