using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terminal.Gui;
namespace UICatalog.Scenarios {
[ScenarioMetadata (Name: "TreeViewFileSystem", Description: "Hierarchical file system explorer based on TreeView")]
[ScenarioCategory ("Controls")]
[ScenarioCategory ("Dialogs")]
[ScenarioCategory ("Text")]
[ScenarioCategory ("Dialogs")]
[ScenarioCategory ("TopLevel")]
class TreeViewFileSystem : Scenario {
///
/// A tree view where nodes are files and folders
///
TreeView treeViewFiles;
///
/// A tree view where nodes are
///
TreeView treeViewNodes;
public override void Setup ()
{
Win.Title = this.GetName();
Win.Y = 1; // menu
Win.Height = Dim.Fill (1); // status bar
Top.LayoutSubviews ();
var menu = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem ("_Quit", "", () => Quit()),
}),
new MenuBarItem ("_View", new MenuItem [] {
new MenuItem ("_ShowLines", "", () => ShowLines()),
new MenuItem ("_PlusMinusSymbols", "", () => SetExpandableSymbols('+','-')),
new MenuItem ("_ArrowSymbols", "", () => SetExpandableSymbols('>','v')),
new MenuItem ("_NoSymbols", "", () => SetExpandableSymbols(null,null)),
new MenuItem ("_ColoredSymbols", "", () => ShowColoredExpandableSymbols()),
}),
});
Top.Add (menu);
var statusBar = new StatusBar (new StatusItem [] {
new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
});
Top.Add (statusBar);
var lblFiles = new Label("File Tree:"){
X=0,
Y=1
};
Win.Add(lblFiles);
treeViewFiles = new TreeView () {
X = 0,
Y = Pos.Bottom(lblFiles),
Width = 40,
Height = 9,
};
SetupFileTree();
Win.Add(treeViewFiles);
var lblNodeTree = new Label("Node Tree:"){
X=0,
Y=Pos.Bottom(treeViewFiles)+1
};
Win.Add(lblNodeTree);
treeViewNodes = new TreeView() {
X = 0,
Y = Pos.Bottom(lblNodeTree),
Width = Dim.Fill (),
Height = Dim.Fill (),
};
SetupNodeTree();
Win.Add(treeViewNodes);
}
private void SetupNodeTree ()
{
// Add 2 root nodes with simple set of subfolders
treeViewNodes.AddObject(CreateSimpleRoot());
treeViewNodes.AddObject(CreateSimpleRoot());
}
private void SetupFileTree ()
{
// setup delegates
treeViewFiles.TreeBuilder = new DelegateTreeBuilder(
// Determines how to compute children of any given branch
GetChildren,
// As a shortcut to enumerating half the file system, tell tree that all directories are expandable (even if they turn out to be empty later on)
(o)=>o is DirectoryInfo
);
// Determines how to represent objects as strings on the screen
treeViewFiles.AspectGetter = FileSystemAspectGetter;
treeViewFiles.AddObjects(DriveInfo.GetDrives().Select(d=>d.RootDirectory));
}
private void ShowLines ()
{
treeViewNodes.Style.ShowBranchLines = !treeViewNodes.Style.ShowBranchLines;
treeViewNodes.SetNeedsDisplay();
treeViewFiles.Style.ShowBranchLines = !treeViewFiles.Style.ShowBranchLines;
treeViewFiles.SetNeedsDisplay();
}
private void SetExpandableSymbols(Rune? expand, Rune? collapse)
{
treeViewNodes.Style.ExpandableSymbol = expand;
treeViewNodes.Style.CollapseableSymbol = collapse;
treeViewNodes.SetNeedsDisplay();
treeViewFiles.Style.ExpandableSymbol = expand;
treeViewFiles.Style.CollapseableSymbol = collapse;
treeViewFiles.SetNeedsDisplay();
}
private void ShowColoredExpandableSymbols()
{
ShowColoredExpandableSymbols(treeViewNodes);
ShowColoredExpandableSymbols(treeViewFiles);
}
private void ShowColoredExpandableSymbols (ITreeView treeView)
{
// TODO: how to know what the normal window background is this member is private
//Win.ColorScheme.Normal.Background
// Toggle Green expand symbols
if(treeView.Style.ExpandableSymbolColor.HasValue)
treeView.Style.ExpandableSymbolColor = null; //clear it
else
treeView.Style.ExpandableSymbolColor = new Terminal.Gui.Attribute(Color.Green,Color.Blue);
// Toggle Red collapse symbols
if(treeView.Style.CollapseableSymbolColor.HasValue)
treeView.Style.CollapseableSymbolColor = null; //clear it
else
treeView.Style.CollapseableSymbolColor = new Terminal.Gui.Attribute(Color.Red,Color.Blue);
treeView.SetNeedsDisplay();
}
private ITreeNode CreateSimpleRoot ()
{
return new TreeNode("Root"){
Children = new List()
{
new TreeNode("Folder_1"){
Children = new List()
{
new TreeNode("Folder_1.1"){
Children = new List()
{
new TreeNode("File_1.1.1"),
new TreeNode("File_1.1.2")
}},
new TreeNode("Folder_1.2"){
Children = new List()
{
new TreeNode("File_1.2.1"),
new TreeNode("File_1.2.2")
}},
new TreeNode("File_1.1")
}},
new TreeNode("Folder_2"){
Children = new List()
{
new TreeNode("Folder_2.1"){
Children = new List()
{
new TreeNode("File_2.1.1"),
new TreeNode("File_2.1.2")
}},
new TreeNode("Folder_2.2"){
Children = new List()
{
new TreeNode("File_2.2.1"),
new TreeNode("File_2.2.2")
}},
new TreeNode("File_2.1")
}},
new TreeNode("Folder_3"){
Children = new List()
{
new TreeNode("Folder_3.1"){
Children = new List()
{
new TreeNode("File_3.1.1"),
new TreeNode("File_3.1.2")
}},
new TreeNode("Folder_3.2"){
Children = new List()
{
new TreeNode("File_3.2.1"),
new TreeNode("File_3.2.2")
}},
new TreeNode("File_3.1")
}}
}
};
}
private IEnumerable GetChildren(FileSystemInfo model)
{
// If it is a directory it's children are all contained files and dirs
if(model is DirectoryInfo d) {
try {
return d.GetFileSystemInfos()
//show directories first
.OrderBy(a=>a is DirectoryInfo ? 0:1)
.ThenBy(b=>b.Name);
}
catch(SystemException) {
// Access violation or other error getting the file list for directory
return Enumerable.Empty();
}
}
return Enumerable.Empty();;
}
private string FileSystemAspectGetter(FileSystemInfo model)
{
if(model is DirectoryInfo d)
return d.Name;
if(model is FileInfo f)
return f.Name;
return model.ToString();
}
private void Quit ()
{
Application.RequestStop ();
}
}
}