From 74448355f90c2f5a939db747721bca2de1840752 Mon Sep 17 00:00:00 2001 From: leileizhang Date: Tue, 13 Jan 2026 18:04:28 +0800 Subject: [PATCH] Peek: Show error message when activated in unsupported virtual folders (#44703) ## Summary of the Pull Request When Peek is activated in virtual folders like Home or Recent, it now displays a clear error message instead of showing a stuck loading state or "Search in Microsoft Store" link. ### Problem When users activate Peek (press the hotkey) in Windows virtual folders such as Home or Recent, the Shell API (SVGIO_SELECTION) returns 0 items because these folders don't support the standard file selection retrieval mechanism. Previously, this caused: - The Peek window to appear stuck in a loading state - The "Search in Microsoft Store" fallback UI to display incorrectly - Subsequent Peek activations to fail until the window was manually closed image ### Solution - Added a check in `Initialize()` to detect when no files are found after querying the Shell - Display an error InfoBar with a user-friendly message: "No files selected or this folder is not supported for preview." - Hide the `FilePreview` control when the error is shown to prevent displaying irrelevant fallback UI - Close the window automatically when the user dismisses the InfoBar (clicks X) image ## PR Checklist - [x] Closes: #44576 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [ ] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed --- .../peek/Peek.UI/MainWindowViewModel.cs | 7 ++++ .../peek/Peek.UI/PeekXAML/MainWindow.xaml | 4 ++- .../peek/Peek.UI/PeekXAML/MainWindow.xaml.cs | 34 +++++++++++++++++++ .../peek/Peek.UI/Strings/en-us/Resources.resw | 4 +++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/modules/peek/Peek.UI/MainWindowViewModel.cs b/src/modules/peek/Peek.UI/MainWindowViewModel.cs index 775664c042..fb2b326c17 100644 --- a/src/modules/peek/Peek.UI/MainWindowViewModel.cs +++ b/src/modules/peek/Peek.UI/MainWindowViewModel.cs @@ -388,6 +388,13 @@ namespace Peek.UI IsErrorVisible = true; } + public void ShowError(string message) + { + IsErrorVisible = false; + ErrorMessage = message; + IsErrorVisible = true; + } + private void NavigationThrottleTimer_Tick(object? sender, object e) { if (sender == null) diff --git a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml index f8e3166b0a..26dbb19b24 100644 --- a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml +++ b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml @@ -50,7 +50,8 @@ Item="{x:Bind ViewModel.CurrentItem, Mode=OneWay}" NumberOfFiles="{x:Bind ViewModel.DisplayItemCount, Mode=OneWay}" PreviewSizeChanged="FilePreviewer_PreviewSizeChanged" - ScalingFactor="{x:Bind ViewModel.ScalingFactor, Mode=OneWay}" /> + ScalingFactor="{x:Bind ViewModel.ScalingFactor, Mode=OneWay}" + Visibility="{x:Bind ContentVisibility(ViewModel.IsErrorVisible), Mode=OneWay}" /> diff --git a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs index 2c8983c634..6e257cd73b 100644 --- a/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs +++ b/src/modules/peek/Peek.UI/PeekXAML/MainWindow.xaml.cs @@ -14,6 +14,7 @@ using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Input; using Peek.Common.Constants; using Peek.Common.Extensions; +using Peek.Common.Helpers; using Peek.FilePreviewer.Models; using Peek.FilePreviewer.Previewers; using Peek.UI.Extensions; @@ -195,6 +196,20 @@ namespace Peek.UI bootTime.Start(); ViewModel.Initialize(selectedItem); + + // If no files were found (e.g., in virtual folders like Home/Recent), show an error + if (ViewModel.CurrentItem == null) + { + Logger.LogInfo("Peek: No files found to preview, showing error."); + var errorMessage = ResourceLoaderInstance.ResourceLoader.GetString("NoFilesSelected"); + ViewModel.ShowError(errorMessage); + + // Still show the window so user can see the warning + this.Show(); + WindowHelpers.BringToForeground(this.GetWindowHandle()); + return; + } + ViewModel.ScalingFactor = this.GetMonitorScale(); this.Content.KeyUp += Content_KeyUp; @@ -302,5 +317,24 @@ namespace Peek.UI { themeListener?.Dispose(); } + + /// + /// Returns Visibility.Collapsed when error is showing, Visibility.Visible when not. + /// + public Visibility ContentVisibility(bool isErrorVisible) + { + return isErrorVisible ? Visibility.Collapsed : Visibility.Visible; + } + + /// + /// Handle InfoBar closed - if there's no current item, close the window. + /// + private void ErrorInfoBar_Closed(InfoBar sender, InfoBarClosedEventArgs args) + { + if (ViewModel.CurrentItem == null) + { + Uninitialize(); + } + } } } diff --git a/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw b/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw index 26482bb75a..f3dbc0f54d 100644 --- a/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw +++ b/src/modules/peek/Peek.UI/Strings/en-us/Resources.resw @@ -341,6 +341,10 @@ No more files to preview. The message to show when there are no files remaining to preview. + + No files selected or this folder is not supported for preview. + Displayed when Peek is activated in a virtual folder (like Home or Recent) where file selection cannot be retrieved. + The file cannot be found. Please check if the file has been moved, renamed, or deleted. Displayed if the file or path was not found