TreeViewFileSystem.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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: "A Terminal.Gui tree view file system explorer")]
  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.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  30. });
  31. Top.Add (statusBar);
  32. _treeView = new TreeView () {
  33. X = 0,
  34. Y = 0,
  35. Width = Dim.Fill (),
  36. Height = Dim.Fill (),
  37. };
  38. string root = System.IO.Path.GetPathRoot(Environment.CurrentDirectory);
  39. if(root == null)
  40. {
  41. MessageBox.ErrorQuery(10,5,"Error","Unable to determine file system root","ok");
  42. return;
  43. }
  44. _treeView.ChildrenGetter = GetChildren;
  45. _treeView.AddObject(new DirectoryInfo(root));
  46. Win.Add (_treeView);
  47. }
  48. private IEnumerable<object> GetChildren(object model)
  49. {
  50. // If it is a directory it's children are all contained files and dirs
  51. if(model is DirectoryInfo d) {
  52. try {
  53. return d.GetDirectories().Cast<object>().Union(d.GetFileSystemInfos());
  54. }
  55. catch(SystemException ex) {
  56. return new []{ex};
  57. }
  58. }
  59. return new object[0];
  60. }
  61. private void Quit ()
  62. {
  63. Application.RequestStop ();
  64. }
  65. }
  66. }