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