TreeViewFileSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Determines how to compute children of any given branch
  48. _treeView.ChildrenGetter = GetChildren;
  49. // Determines how to represent objects as strings on the screen
  50. _treeView.AspectGetter = AspectGetter;
  51. Win.Add (_treeView);
  52. }
  53. private void ClearObjects()
  54. {
  55. _treeView.ClearObjects();
  56. }
  57. private void AddRootDrives()
  58. {
  59. _treeView.AddObjects(DriveInfo.GetDrives().Select(d=>d.RootDirectory));
  60. }
  61. private void RemoveRoot()
  62. {
  63. if(_treeView.SelectedObject == null)
  64. MessageBox.ErrorQuery(10,5,"Error","No object selected","ok");
  65. else {
  66. _treeView.Remove(_treeView.SelectedObject);
  67. }
  68. }
  69. private IEnumerable<object> GetChildren(object model)
  70. {
  71. // If it is a directory it's children are all contained files and dirs
  72. if(model is DirectoryInfo d) {
  73. try {
  74. return d.GetDirectories().Cast<object>().Union(d.GetFileSystemInfos());
  75. }
  76. catch(SystemException ex) {
  77. return new []{ex};
  78. }
  79. }
  80. return new object[0];
  81. }
  82. private string AspectGetter(object model)
  83. {
  84. if(model is DirectoryInfo d)
  85. return d.Name;
  86. if(model is FileInfo f)
  87. return f.Name;
  88. return model.ToString();
  89. }
  90. private void Quit ()
  91. {
  92. Application.RequestStop ();
  93. }
  94. }
  95. }