Skip to content

Commit 99b8408

Browse files
authored
Fix: Fixed issue with navigating photos on Windows 10 (#16232)
1 parent 56df483 commit 99b8408

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

src/Files.App/Helpers/Navigation/NavigationHelpers.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
using Microsoft.UI.Xaml.Controls;
66
using Microsoft.UI.Xaml.Media;
77
using Microsoft.UI.Xaml.Media.Imaging;
8+
using Windows.Storage.Search;
9+
using Microsoft.UI.Xaml.Media;
810
using Windows.Storage;
911
using Windows.System;
12+
using System.IO;
1013

1114
namespace Files.App.Helpers
1215
{
@@ -586,7 +589,79 @@ private static async Task<FilesystemResult> OpenFile(string path, IShellPage ass
586589
}
587590
else
588591
{
589-
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance, args);
592+
var fileExtension = Path.GetExtension(path);
593+
594+
// Use NeighboringFilesQuery to launch photos
595+
// The query options no longer work with the Windows 11 Photo App but they still work for Windows 10
596+
if (FileExtensionHelpers.IsImageFile(fileExtension))
597+
{
598+
//try using launcher first
599+
bool launchSuccess = false;
600+
BaseStorageFileQueryResult? fileQueryResult = null;
601+
//Get folder to create a file query (to pass to apps like Photos, Movies & TV..., needed to scroll through the folder like what Windows Explorer does)
602+
BaseStorageFolder currentFolder = await associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(path));
603+
if (currentFolder is not null)
604+
{
605+
QueryOptions queryOptions = new(CommonFileQuery.DefaultQuery, null);
606+
//We can have many sort entries
607+
SortEntry sortEntry = new()
608+
{
609+
AscendingOrder = associatedInstance.InstanceViewModel.FolderSettings.DirectorySortDirection == SortDirection.Ascending
610+
};
611+
//Basically we tell to the launched app to follow how we sorted the files in the directory.
612+
var sortOption = associatedInstance.InstanceViewModel.FolderSettings.DirectorySortOption;
613+
switch (sortOption)
614+
{
615+
case SortOption.Name:
616+
sortEntry.PropertyName = "System.ItemNameDisplay";
617+
queryOptions.SortOrder.Clear();
618+
queryOptions.SortOrder.Add(sortEntry);
619+
break;
620+
case SortOption.DateModified:
621+
sortEntry.PropertyName = "System.DateModified";
622+
queryOptions.SortOrder.Clear();
623+
queryOptions.SortOrder.Add(sortEntry);
624+
break;
625+
case SortOption.DateCreated:
626+
sortEntry.PropertyName = "System.DateCreated";
627+
queryOptions.SortOrder.Clear();
628+
queryOptions.SortOrder.Add(sortEntry);
629+
break;
630+
//Unfortunately this is unsupported | Remarks: https://learn.microsoft.com/uwp/api/windows.storage.search.queryoptions.sortorder?view=winrt-19041
631+
//case Enums.SortOption.Size:
632+
//sortEntry.PropertyName = "System.TotalFileSize";
633+
//queryOptions.SortOrder.Clear();
634+
//queryOptions.SortOrder.Add(sortEntry);
635+
//break;
636+
//Unfortunately this is unsupported | Remarks: https://learn.microsoft.com/uwp/api/windows.storage.search.queryoptions.sortorder?view=winrt-19041
637+
//case Enums.SortOption.FileType:
638+
//sortEntry.PropertyName = "System.FileExtension";
639+
//queryOptions.SortOrder.Clear();
640+
//queryOptions.SortOrder.Add(sortEntry);
641+
//break;
642+
//Handle unsupported
643+
default:
644+
//keep the default one in SortOrder IList
645+
break;
646+
}
647+
var options = InitializeWithWindow(new LauncherOptions());
648+
if (currentFolder.AreQueryOptionsSupported(queryOptions))
649+
{
650+
fileQueryResult = currentFolder.CreateFileQueryWithOptions(queryOptions);
651+
options.NeighboringFilesQuery = fileQueryResult.ToStorageFileQueryResult();
652+
}
653+
// Now launch file with options.
654+
var storageItem = (StorageFile)await FilesystemTasks.Wrap(() => childFile.Item.ToStorageFileAsync().AsTask());
655+
if (storageItem is not null)
656+
launchSuccess = await Launcher.LaunchFileAsync(storageItem, options);
657+
}
658+
if (!launchSuccess)
659+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance, args);
660+
}
661+
else
662+
{
663+
await Win32Helper.InvokeWin32ComponentAsync(path, associatedInstance, args);
664+
}
590665
}
591666
});
592667
}

0 commit comments

Comments
 (0)