ManagedFileChooserFilterViewModel.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia.Controls;
  5. namespace Avalonia.Dialogs
  6. {
  7. internal class ManagedFileChooserFilterViewModel : InternalViewModelBase
  8. {
  9. private readonly string[] _extensions;
  10. public string Name { get; }
  11. public ManagedFileChooserFilterViewModel(FileDialogFilter filter)
  12. {
  13. Name = filter.Name;
  14. if (filter.Extensions.Contains("*"))
  15. {
  16. return;
  17. }
  18. _extensions = filter.Extensions?.Select(e => "." + e.ToLowerInvariant()).ToArray();
  19. }
  20. public ManagedFileChooserFilterViewModel()
  21. {
  22. Name = "All files";
  23. }
  24. public bool Match(string filename)
  25. {
  26. if (_extensions == null)
  27. {
  28. return true;
  29. }
  30. foreach (var ext in _extensions)
  31. {
  32. if (filename.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase))
  33. {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. public override string ToString() => Name;
  40. }
  41. }