[Peek] Fix crash when opening Peek with no files selected (#26470)

* catch exception

* Check count of items to avoid the exception being thrown

* Fix regression from #26364

---------

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
This commit is contained in:
Seraphima Zykova
2023-05-30 12:56:07 +02:00
committed by GitHub
parent 4905258c94
commit b72af5e247
2 changed files with 38 additions and 23 deletions

View File

@@ -87,10 +87,20 @@ namespace Peek.UI.Helpers
private static IShellItemArray? GetShellItemArray(IShellBrowser shellBrowser, bool onlySelectedFiles) private static IShellItemArray? GetShellItemArray(IShellBrowser shellBrowser, bool onlySelectedFiles)
{ {
var shellView = (IFolderView)shellBrowser.QueryActiveShellView(); var shellViewObject = shellBrowser.QueryActiveShellView();
var selectionFlag = onlySelectedFiles ? (uint)_SVGIO.SVGIO_SELECTION : (uint)_SVGIO.SVGIO_ALLVIEW; var shellView = shellViewObject as IFolderView;
shellView.Items(selectionFlag, typeof(IShellItemArray).GUID, out var items); if (shellView != null)
return items as IShellItemArray; {
var selectionFlag = onlySelectedFiles ? (uint)_SVGIO.SVGIO_SELECTION : (uint)_SVGIO.SVGIO_ALLVIEW;
shellView.ItemCount(selectionFlag, out var countItems);
if (countItems > 0)
{
shellView.Items(selectionFlag, typeof(IShellItemArray).GUID, out var items);
return items as IShellItemArray;
}
}
return null;
} }
} }
} }

View File

@@ -9,6 +9,7 @@ using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Input;
using Peek.Common.Constants; using Peek.Common.Constants;
using Peek.Common.Helpers;
using Peek.FilePreviewer.Models; using Peek.FilePreviewer.Models;
using Peek.UI.Extensions; using Peek.UI.Extensions;
using Peek.UI.Helpers; using Peek.UI.Helpers;
@@ -42,11 +43,6 @@ namespace Peek.UI
private void PeekWindow_Activated(object sender, Microsoft.UI.Xaml.WindowActivatedEventArgs args) private void PeekWindow_Activated(object sender, Microsoft.UI.Xaml.WindowActivatedEventArgs args)
{ {
if (args.WindowActivationState == Microsoft.UI.Xaml.WindowActivationState.CodeActivated)
{
this.BringToForeground();
}
if (args.WindowActivationState == Microsoft.UI.Xaml.WindowActivationState.Deactivated) if (args.WindowActivationState == Microsoft.UI.Xaml.WindowActivationState.Deactivated)
{ {
var userSettings = App.GetService<IUserSettings>(); var userSettings = App.GetService<IUserSettings>();
@@ -180,23 +176,32 @@ namespace Peek.UI
private bool IsNewSingleSelectedItem() private bool IsNewSingleSelectedItem()
{ {
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow(); try
var selectedItems = FileExplorerHelper.GetSelectedItems(foregroundWindowHandle);
var selectedItemsCount = selectedItems?.GetCount() ?? 0;
if (selectedItems == null || selectedItemsCount == 0 || selectedItemsCount > 1)
{ {
return false; var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
var selectedItems = FileExplorerHelper.GetSelectedItems(foregroundWindowHandle);
var selectedItemsCount = selectedItems?.GetCount() ?? 0;
if (selectedItems == null || selectedItemsCount == 0 || selectedItemsCount > 1)
{
return false;
}
var fileExplorerSelectedItemPath = selectedItems.GetItemAt(0).ToIFileSystemItem().Path;
var currentItemPath = ViewModel.CurrentItem?.Path;
if (fileExplorerSelectedItemPath == null || currentItemPath == null || fileExplorerSelectedItemPath == currentItemPath)
{
return false;
}
return true;
}
catch (Exception ex)
{
Logger.LogError(ex.Message);
} }
var fileExplorerSelectedItemPath = selectedItems.GetItemAt(0).ToIFileSystemItem().Path; return false;
var currentItemPath = ViewModel.CurrentItem?.Path;
if (fileExplorerSelectedItemPath == null || currentItemPath == null || fileExplorerSelectedItemPath == currentItemPath)
{
return false;
}
return true;
} }
} }
} }