FileDialogTreeBuilder.cs 858 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace Terminal.Gui {
  6. class FileDialogTreeBuilder : ITreeBuilder<object> {
  7. public bool SupportsCanExpand => true;
  8. public bool CanExpand (object toExpand)
  9. {
  10. return this.TryGetDirectories (NodeToDirectory (toExpand)).Any ();
  11. }
  12. public IEnumerable<object> GetChildren (object forObject)
  13. {
  14. return this.TryGetDirectories (NodeToDirectory (forObject));
  15. }
  16. internal static DirectoryInfo NodeToDirectory (object toExpand)
  17. {
  18. return toExpand is FileDialogRootTreeNode f ? f.Path : (DirectoryInfo)toExpand;
  19. }
  20. private IEnumerable<DirectoryInfo> TryGetDirectories (DirectoryInfo directoryInfo)
  21. {
  22. try {
  23. return directoryInfo.EnumerateDirectories ();
  24. } catch (Exception) {
  25. return Enumerable.Empty<DirectoryInfo> ();
  26. }
  27. }
  28. }
  29. }