mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-15 03:07:56 +01:00
[Peek] fixes and enhancements (#25963)
This commit is contained in:
@@ -4,12 +4,21 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Microsoft.PowerToys.FilePreviewCommon
|
||||
{
|
||||
public static class Helper
|
||||
{
|
||||
public static void CleanupTempDir(string folder)
|
||||
public static Task<bool> CleanupTempDirAsync(string folder)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
return CleanupTempDir(folder);
|
||||
});
|
||||
}
|
||||
|
||||
public static bool CleanupTempDir(string folder)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -18,10 +27,14 @@ namespace Microsoft.PowerToys.FilePreviewCommon
|
||||
{
|
||||
file.Delete();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,8 +72,6 @@ namespace Peek.FilePreviewer.Controls
|
||||
{
|
||||
PreviewBrowser.CoreWebView2.DOMContentLoaded -= CoreWebView2_DOMContentLoaded;
|
||||
}
|
||||
|
||||
Microsoft.PowerToys.FilePreviewCommon.Helper.CleanupTempDir(TempFolderPath.Path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -156,11 +156,16 @@ namespace Peek.FilePreviewer
|
||||
|
||||
private async Task OnScalingFactorPropertyChanged()
|
||||
{
|
||||
// Cancel previous loading task
|
||||
_cancellationTokenSource.Cancel();
|
||||
_cancellationTokenSource = new();
|
||||
await UpdatePreviewSizeAsync(_cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
await UpdatePreviewAsync(_cancellationTokenSource.Token);
|
||||
private async Task UpdatePreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (Previewer != null)
|
||||
{
|
||||
var size = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
||||
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(size));
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdatePreviewAsync(CancellationToken cancellationToken)
|
||||
@@ -170,8 +175,8 @@ namespace Peek.FilePreviewer
|
||||
try
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var size = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
||||
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(size));
|
||||
await UpdatePreviewSizeAsync(cancellationToken);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await Previewer.LoadPreviewAsync(cancellationToken);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// 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.
|
||||
|
||||
@@ -7,11 +7,14 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using Common.UI;
|
||||
using ManagedCommon;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
public class MonacoHelper
|
||||
{
|
||||
public static readonly HashSet<string> SupportedMonacoFileTypes = GetExtensions();
|
||||
|
||||
public static HashSet<string> GetExtensions()
|
||||
{
|
||||
HashSet<string> set = new HashSet<string>();
|
||||
@@ -20,15 +23,16 @@ namespace Peek.FilePreviewer.Previewers
|
||||
JsonDocument languageListDocument = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguages();
|
||||
JsonElement languageList = languageListDocument.RootElement.GetProperty("list");
|
||||
foreach (JsonElement e in languageList.EnumerateArray())
|
||||
{
|
||||
for (int j = 0; j < e.GetProperty("extensions").GetArrayLength(); j++)
|
||||
{
|
||||
for (int j = 0; j < e.GetProperty("extensions").GetArrayLength(); j++)
|
||||
{
|
||||
set.Add(e.GetProperty("extensions")[j].ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Failed to get monaco extensions: " + ex.Message);
|
||||
}
|
||||
|
||||
return set;
|
||||
|
||||
@@ -31,8 +31,6 @@ namespace Peek.FilePreviewer.Previewers
|
||||
".md",
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> _supportedMonacoFileTypes = MonacoHelper.GetExtensions();
|
||||
|
||||
[ObservableProperty]
|
||||
private Uri? preview;
|
||||
|
||||
@@ -42,17 +40,34 @@ namespace Peek.FilePreviewer.Previewers
|
||||
[ObservableProperty]
|
||||
private bool isDevFilePreview;
|
||||
|
||||
private bool disposed;
|
||||
|
||||
public WebBrowserPreviewer(IFileSystemItem file)
|
||||
{
|
||||
File = file;
|
||||
Dispatcher = DispatcherQueue.GetForCurrentThread();
|
||||
}
|
||||
|
||||
~WebBrowserPreviewer()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual async void Dispose(bool disposing)
|
||||
{
|
||||
if (!this.disposed)
|
||||
{
|
||||
await Microsoft.PowerToys.FilePreviewCommon.Helper.CleanupTempDirAsync(TempFolderPath.Path);
|
||||
disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
private IFileSystemItem File { get; }
|
||||
|
||||
public bool IsPreviewLoaded => preview != null;
|
||||
@@ -89,7 +104,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
bool isHtml = File.Extension == ".html";
|
||||
bool isMarkdown = File.Extension == ".md";
|
||||
IsDevFilePreview = _supportedMonacoFileTypes.Contains(File.Extension);
|
||||
IsDevFilePreview = MonacoHelper.SupportedMonacoFileTypes.Contains(File.Extension);
|
||||
|
||||
if (IsDevFilePreview && !isHtml && !isMarkdown)
|
||||
{
|
||||
@@ -120,7 +135,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
public static bool IsFileTypeSupported(string fileExt)
|
||||
{
|
||||
return _supportedFileTypes.Contains(fileExt) || _supportedMonacoFileTypes.Contains(fileExt);
|
||||
return _supportedFileTypes.Contains(fileExt) || MonacoHelper.SupportedMonacoFileTypes.Contains(fileExt);
|
||||
}
|
||||
|
||||
private bool HasFailedLoadingPreview()
|
||||
|
||||
@@ -37,6 +37,7 @@ namespace Peek.UI
|
||||
public App()
|
||||
{
|
||||
InitializeComponent();
|
||||
Logger.InitializeLogger("\\Peek\\Logs");
|
||||
|
||||
Host = Microsoft.Extensions.Hosting.Host.
|
||||
CreateDefaultBuilder().
|
||||
|
||||
@@ -42,7 +42,15 @@ namespace Peek.UI
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
Items = NeighboringItemsQuery.GetNeighboringItems();
|
||||
try
|
||||
{
|
||||
Items = NeighboringItemsQuery.GetNeighboringItems();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError("Failed to get File Explorer Items: " + ex.Message);
|
||||
}
|
||||
|
||||
CurrentIndex = 0;
|
||||
|
||||
if (Items != null && Items.Count > 0)
|
||||
|
||||
Reference in New Issue
Block a user