Browse Source

Added TODO in FileSearchResult and added current file browsing to the search window

CPKreuz 1 year ago
parent
commit
6ec7b73ba0

+ 1 - 1
src/PixiEditor.AvaloniaUI/Models/Commands/Search/FileSearchResult.cs

@@ -26,7 +26,7 @@ internal class FileSearchResult : SearchResult
     {
         FilePath = path;
         var drawing = new GeometryDrawing() { Brush = FileExtensionToColorConverter.GetBrush(FilePath) };
-        var geometry = new RectangleGeometry(new Rect(0, 0, 10, 10)) { };
+        var geometry = new RectangleGeometry(new Rect(0, 0, 10, 10)) { }; // TODO: Avalonia 11.1 introduces rounded rectangle geometry, let's make this round again then 
         drawing.Geometry = geometry;
         icon = new DrawingImage(drawing);
         this.asReferenceLayer = asReferenceLayer;

+ 36 - 12
src/PixiEditor.AvaloniaUI/Views/Main/CommandSearch/CommandSearchControlHelper.cs

@@ -96,22 +96,24 @@ internal static class CommandSearchControlHelper
         try
         {
             // add matching files
-            newResults.AddRange(MaybeParseFilePaths(query));
+            newResults.AddRange(MaybeParseFilePaths(query, warnings));
         }
         catch
         {
             // ignored
         }
 
-        // add matching recent files
-        newResults.AddRange(
-            ViewModelMain.Current.FileSubViewModel.RecentlyOpened
-                .Where(x => x.FilePath.Contains(query))
-                .Select(file => new FileSearchResult(file.FilePath)
-                {
-                    SearchTerm = query,
-                    Match = Match(file.FilePath, query)
-                }));
+        if (!query.StartsWith('.'))
+        {
+            // add matching recent files
+            newResults.AddRange(
+                ViewModelMain.Current.FileSubViewModel.RecentlyOpened
+                    .Where(x => x.FilePath.Contains(query))
+                    .Select(file => new FileSearchResult(file.FilePath)
+                    {
+                        SearchTerm = query, Match = Match(file.FilePath, query)
+                    }));
+        }
 
         return (newResults, warnings);
     }
@@ -119,13 +121,35 @@ internal static class CommandSearchControlHelper
     private static Match Match(string text, string searchTerm) =>
             Regex.Match(text, $"(.*)({Regex.Escape(searchTerm ?? string.Empty)})(.*)", RegexOptions.IgnoreCase);
 
-    private static IEnumerable<SearchResult> MaybeParseFilePaths(string query)
+    private static IEnumerable<SearchResult> MaybeParseFilePaths(string query, List<string> warnings)
     {
         var filePath = query.Trim(' ', '"', '\'');
 
-        if (filePath.StartsWith("~"))
+        if (filePath.StartsWith('~'))
             filePath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), filePath[1..]);
 
+        if (filePath.StartsWith('.') && (filePath.Length == 1 || filePath[1] != '.'))
+        {
+            if (ViewModelMain.Current.DocumentManagerSubViewModel.ActiveDocument is { FullFilePath: { } path })
+            {
+                int skip = 1;
+                
+                path = Path.GetDirectoryName(path);
+                
+                if (filePath.Length > 1 && filePath[1] == '.')
+                {
+                    skip++;
+                    path = Path.GetDirectoryName(path);
+                }
+                
+                filePath = Path.Join(path, filePath[skip..]);
+            }
+            else
+            {
+                warnings.Add("Save current document to browse files");
+            }
+        }
+        
         if (!Path.IsPathFullyQualified(filePath))
             return Enumerable.Empty<SearchResult>();