Skip to content

Commit

Permalink
[Peek] Implementation of a performant and reliable Neighboring Files …
Browse files Browse the repository at this point in the history
…Query (#24943)

* Use IShellItemArray as the backing array of item

* Finalize and cleanup NFQ implementation

* Cleanup remainder of the code

* Remove unused using
  • Loading branch information
SamChaps authored Mar 29, 2023
1 parent 160824b commit 3257965
Show file tree
Hide file tree
Showing 19 changed files with 315 additions and 261 deletions.
14 changes: 14 additions & 0 deletions src/modules/peek/Peek.Common/Helpers/MathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Peek.Common.Helpers
{
public static class MathHelper
{
public static int Modulo(int a, int b)
{
return a < 0 ? ((a % b) + b) % b : a % b;
}
}
}
15 changes: 3 additions & 12 deletions src/modules/peek/Peek.FilePreviewer/FilePreview.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
using Peek.Common.Models;
using Peek.FilePreviewer.Models;
using Peek.FilePreviewer.Previewers;
using Peek.FilePreviewer.Previewers.Interfaces;
using Windows.ApplicationModel.Resources;
using Windows.Foundation;

namespace Peek.FilePreviewer
{
Expand All @@ -33,7 +33,7 @@ public sealed partial class FilePreview : UserControl
nameof(Item),
typeof(IFileSystemItem),
typeof(FilePreview),
new PropertyMetadata(false, async (d, e) => await ((FilePreview)d).OnFilePropertyChanged()));
new PropertyMetadata(false, async (d, e) => await ((FilePreview)d).OnItemPropertyChanged()));

public static readonly DependencyProperty ScalingFactorProperty =
DependencyProperty.Register(
Expand Down Expand Up @@ -80,12 +80,8 @@ private async void Previewer_PropertyChanged(object? sender, System.ComponentMod

public IBrowserPreviewer? BrowserPreviewer => Previewer as IBrowserPreviewer;

public bool IsImageVisible => ImagePreviewer != null;

public IUnsupportedFilePreviewer? UnsupportedFilePreviewer => Previewer as IUnsupportedFilePreviewer;

public bool IsUnsupportedPreviewVisible => UnsupportedFilePreviewer != null;

public IFileSystemItem Item
{
get => (IFileSystemItem)GetValue(ItemProperty);
Expand Down Expand Up @@ -117,7 +113,7 @@ public Visibility IsPreviewVisible(IPreviewer? previewer, PreviewState? state)
return isValidPreview ? Visibility.Visible : Visibility.Collapsed;
}

private async Task OnFilePropertyChanged()
private async Task OnItemPropertyChanged()
{
// Cancel previous loading task
_cancellationTokenSource.Cancel();
Expand Down Expand Up @@ -258,11 +254,6 @@ private async Task UpdateImageTooltipAsync(CancellationToken cancellationToken)
string dateModifiedFormatted = string.IsNullOrEmpty(dateModified) ? string.Empty : "\n" + ReadableStringHelper.FormatResourceString("PreviewTooltip_DateModified", dateModified);
sb.Append(dateModifiedFormatted);

cancellationToken.ThrowIfCancellationRequested();
Size? dimensions = await Task.Run(Item.GetImageSize);
string dimensionsFormatted = dimensions == null ? string.Empty : "\n" + ReadableStringHelper.FormatResourceString("PreviewTooltip_Dimensions", dimensions.Value.Width, dimensions.Value.Height);
sb.Append(dimensionsFormatted);

cancellationToken.ThrowIfCancellationRequested();
ulong bytes = await Task.Run(Item.GetSizeInBytes);
string fileSize = ReadableStringHelper.BytesToReadableString(bytes);
Expand Down
4 changes: 4 additions & 0 deletions src/modules/peek/Peek.FilePreviewer/Peek.FilePreviewer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>

<ItemGroup>
<Folder Include="Previewers\DrivePreviewer\" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Peek.Common.Helpers;
using Peek.Common.Models;
using Peek.FilePreviewer.Previewers.Helpers;
using Peek.FilePreviewer.Previewers.Interfaces;
using Windows.Foundation;

namespace Peek.FilePreviewer.Previewers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
// See the LICENSE file in the project root for more information.

using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Foundation;

namespace Peek.FilePreviewer.Previewers
namespace Peek.FilePreviewer.Previewers.Interfaces
{
public interface IImagePreviewer : IPreviewer
{
Expand Down
51 changes: 46 additions & 5 deletions src/modules/peek/Peek.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

using System;
using ManagedCommon;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.UI.Xaml;
using Peek.FilePreviewer;
using Peek.UI.Views;
using WinUIEx;

namespace Peek.UI
Expand All @@ -16,14 +20,49 @@ public partial class App : Application
{
public static int PowerToysPID { get; set; }

public IHost Host
{
get;
}

private Window? Window { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="App"/> class.
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
InitializeComponent();

Host = Microsoft.Extensions.Hosting.Host.
CreateDefaultBuilder().
UseContentRoot(AppContext.BaseDirectory).
ConfigureServices((context, services) =>
{
// Core Services
services.AddTransient<NeighboringItemsQuery>();
// Views and ViewModels
services.AddTransient<TitleBar>();
services.AddTransient<FilePreview>();
services.AddTransient<MainWindowViewModel>();
}).
Build();

UnhandledException += App_UnhandledException;
}

public static T GetService<T>()
where T : class
{
if ((App.Current as App)!.Host.Services.GetService(typeof(T)) is not T service)
{
throw new ArgumentException($"{typeof(T)} needs to be registered in ConfigureServices within App.xaml.cs.");
}

return service;
}

/// <summary>
Expand All @@ -44,12 +83,14 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
}
}

window = new MainWindow();
Window = new MainWindow();

window.Activate();
window.Hide();
Window.Activate();
Window.Hide();
}

private Window? window;
private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)
{
}
}
}
28 changes: 28 additions & 0 deletions src/modules/peek/Peek.UI/Extensions/IShellItemExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using Peek.Common.Models;

namespace Peek.UI.Extensions
{
public static class IShellItemExtensions
{
public static IFileSystemItem ToIFileSystemItem(this IShellItem shellItem)
{
string path = string.Empty;
try
{
path = shellItem.GetDisplayName(Windows.Win32.UI.Shell.SIGDN.SIGDN_FILESYSPATH);
}
catch (Exception)
{
// TODO: Handle cases that do not have a file system path like Recycle Bin.
}

return File.Exists(path) ? new FileItem(path) : new FolderItem(path);
}
}
}
163 changes: 0 additions & 163 deletions src/modules/peek/Peek.UI/FolderItemsQuery.cs

This file was deleted.

Loading

0 comments on commit 3257965

Please sign in to comment.