PathsToAddHandler.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.Linq;
  2. using System;
  3. using UnityEngine;
  4. using UnityEditor.TestTools.CodeCoverage.Utils;
  5. using UnityEditor.TestTools.CodeCoverage.Analytics;
  6. namespace UnityEditor.TestTools.CodeCoverage
  7. {
  8. internal class PathsToAddHandler
  9. {
  10. string m_PathsToFilter;
  11. CodeCoverageWindow m_Parent;
  12. PathFilterType m_PathFilterType;
  13. private readonly string kSelectIncludedDirectoryMessage = L10n.Tr($"Select directory to include");
  14. private readonly string kSelectIncludedFileMessage = L10n.Tr("Select file to include");
  15. private readonly string kSelectExcludedDirectoryMessage = L10n.Tr($"Select directory to exclude");
  16. private readonly string kSelectExcludedFileMessage = L10n.Tr("Select file to exclude");
  17. class Styles
  18. {
  19. public static readonly GUIContent PathsToFilterAddFolderLabel = EditorGUIUtility.TrTextContent("Add Folder");
  20. public static readonly GUIContent PathsToFilterAddFileLabel = EditorGUIUtility.TrTextContent("Add File");
  21. }
  22. public PathsToAddHandler(CodeCoverageWindow parent, PathFilterType type)
  23. {
  24. m_Parent = parent;
  25. m_PathFilterType = type;
  26. }
  27. public void BrowseForDir(string pathsToFilter)
  28. {
  29. m_PathsToFilter = pathsToFilter;
  30. string candidate = CoverageUtils.BrowseForDir(Application.dataPath, m_PathFilterType == PathFilterType.Include ? kSelectIncludedDirectoryMessage : kSelectExcludedDirectoryMessage);
  31. if (CoverageUtils.IsValidFolder(candidate))
  32. {
  33. candidate = string.Concat(candidate, "/*");
  34. UpdatePathToFilter(candidate);
  35. }
  36. }
  37. public void BrowseForFile(string pathsToFilter)
  38. {
  39. m_PathsToFilter = pathsToFilter;
  40. string candidate = CoverageUtils.BrowseForFile(Application.dataPath, m_PathFilterType == PathFilterType.Include ? kSelectIncludedFileMessage : kSelectExcludedFileMessage);
  41. if (CoverageUtils.IsValidFile(candidate))
  42. {
  43. UpdatePathToFilter(candidate);
  44. }
  45. }
  46. private void UpdatePathToFilter(string candidate)
  47. {
  48. string[] pathFilters = m_PathsToFilter.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  49. candidate = CoverageUtils.NormaliseFolderSeparators(candidate);
  50. if (!pathFilters.Contains(candidate))
  51. {
  52. if (m_PathsToFilter.Length > 0)
  53. m_PathsToFilter += ",";
  54. m_PathsToFilter += candidate;
  55. if (m_PathFilterType == PathFilterType.Include)
  56. {
  57. m_Parent.PathsToInclude = m_PathsToFilter;
  58. }
  59. else
  60. {
  61. m_Parent.PathsToExclude = m_PathsToFilter;
  62. }
  63. m_Parent.LoseFocus();
  64. }
  65. }
  66. }
  67. }