[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 shellView = shellViewObject as IFolderView;
if (shellView != null)
{
var selectionFlag = onlySelectedFiles ? (uint)_SVGIO.SVGIO_SELECTION : (uint)_SVGIO.SVGIO_ALLVIEW; 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); shellView.Items(selectionFlag, typeof(IShellItemArray).GUID, out var items);
return items as IShellItemArray; 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>();
@@ -179,6 +175,8 @@ namespace Peek.UI
} }
private bool IsNewSingleSelectedItem() private bool IsNewSingleSelectedItem()
{
try
{ {
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow(); var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
@@ -198,5 +196,12 @@ namespace Peek.UI
return true; return true;
} }
catch (Exception ex)
{
Logger.LogError(ex.Message);
}
return false;
}
} }
} }