TreeViewFileSystem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios {
  8. [ScenarioMetadata (Name: "TreeViewFileSystem", Description: "Hierarchical file system explorer based on TreeView")]
  9. [ScenarioCategory ("Controls")]
  10. [ScenarioCategory ("Dialogs")]
  11. [ScenarioCategory ("Text")]
  12. [ScenarioCategory ("Dialogs")]
  13. [ScenarioCategory ("TopLevel")]
  14. class TreeViewFileSystem : Scenario {
  15. TreeView _treeView;
  16. public override void Setup ()
  17. {
  18. Win.Title = this.GetName();
  19. Win.Y = 1; // menu
  20. Win.Height = Dim.Fill (1); // status bar
  21. Top.LayoutSubviews ();
  22. var menu = new MenuBar (new MenuBarItem [] {
  23. new MenuBarItem ("_File", new MenuItem [] {
  24. new MenuItem ("_Quit", "", () => Quit()),
  25. }),
  26. });
  27. Top.Add (menu);
  28. var statusBar = new StatusBar (new StatusItem [] {
  29. new StatusItem(Key.F2, "~F2~ Add Root Drives", () => AddRootDrives()),
  30. new StatusItem(Key.F3, "~F3~ Remove Root Object", () => RemoveRoot()),
  31. new StatusItem(Key.F4, "~F4~ Clear Objects", () => ClearObjects()),
  32. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  33. });
  34. Top.Add (statusBar);
  35. _treeView = new TreeView () {
  36. X = 0,
  37. Y = 0,
  38. Width = Dim.Fill (),
  39. Height = Dim.Fill (),
  40. };
  41. string root = System.IO.Path.GetPathRoot(Environment.CurrentDirectory);
  42. if(root == null)
  43. {
  44. MessageBox.ErrorQuery(10,5,"Error","Unable to determine file system root","ok");
  45. return;
  46. }
  47. // 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)
  48. _treeView.CanExpandGetter = (o)=>o is DirectoryInfo;
  49. // Determines how to compute children of any given branch
  50. _treeView.ChildrenGetter = GetChildren;
  51. // Determines how to represent objects as strings on the screen
  52. _treeView.AspectGetter = AspectGetter;
  53. Win.Add (_treeView);
  54. }
  55. private void ClearObjects()
  56. {
  57. _treeView.ClearObjects();
  58. }
  59. private void AddRootDrives()
  60. {
  61. _treeView.AddObjects(DriveInfo.GetDrives().Select(d=>d.RootDirectory));
  62. }
  63. private void RemoveRoot()
  64. {
  65. if(_treeView.SelectedObject == null)
  66. MessageBox.ErrorQuery(10,5,"Error","No object selected","ok");
  67. else {
  68. _treeView.Remove(_treeView.SelectedObject);
  69. }
  70. }
  71. private IEnumerable<object> GetChildren(object model)
  72. {
  73. // If it is a directory it's children are all contained files and dirs
  74. if(model is DirectoryInfo d) {
  75. try {
  76. return d.GetFileSystemInfos();
  77. }
  78. catch(SystemException ex) {
  79. return new []{ex};
  80. }
  81. }
  82. return new object[0];
  83. }
  84. private string AspectGetter(object model)
  85. {
  86. if(model is DirectoryInfo d)
  87. return d.Name;
  88. if(model is FileInfo f)
  89. return f.Name;
  90. return model.ToString();
  91. }
  92. private void Quit ()
  93. {
  94. Application.RequestStop ();
  95. }
  96. }
  97. }