FileDialogTreeBuilder.cs 857 B

12345678910111213141516171819202122232425262728293031323334353637
  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. }