-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Peek] add extra logic to properly render PNG files with transparency (…
…#22613) * [Peek] added extra logic to render PNG files with proper transparency * Moved logic to ThumbnailHelper Cleanup * Created a separated previewer for PNG to only load the preview image with thumbnail logic * removed unused code * Updated state loading change
- Loading branch information
1 parent
5590eb9
commit 5bd9dd5
Showing
4 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/modules/peek/Peek.FilePreviewer/Previewers/PngPreviewer/PngPreviewer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// 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.FilePreviewer.Previewers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing.Imaging; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using Microsoft.UI.Dispatching; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
using Peek.Common; | ||
using Windows.Foundation; | ||
using Windows.Graphics.Imaging; | ||
using Windows.Storage.Streams; | ||
using File = Peek.Common.Models.File; | ||
|
||
public partial class PngPreviewer : ObservableObject, IBitmapPreviewer | ||
{ | ||
private readonly uint _png_image_size = 1280; | ||
|
||
[ObservableProperty] | ||
[NotifyPropertyChangedFor(nameof(IsPreviewLoaded))] | ||
private BitmapSource? preview; | ||
|
||
[ObservableProperty] | ||
private PreviewState state; | ||
|
||
public PngPreviewer(File file) | ||
{ | ||
File = file; | ||
Dispatcher = DispatcherQueue.GetForCurrentThread(); | ||
|
||
PropertyChanged += OnPropertyChanged; | ||
} | ||
|
||
public bool IsPreviewLoaded => preview != null; | ||
|
||
private File File { get; } | ||
|
||
private DispatcherQueue Dispatcher { get; } | ||
|
||
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
private CancellationToken CancellationToken => _cancellationTokenSource.Token; | ||
|
||
public void Dispose() | ||
{ | ||
_cancellationTokenSource.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
public async Task<Size> GetPreviewSizeAsync() | ||
{ | ||
var propertyImageSize = await PropertyHelper.GetImageSize(File.Path); | ||
if (propertyImageSize != Size.Empty) | ||
{ | ||
return propertyImageSize; | ||
} | ||
|
||
return await WICHelper.GetImageSize(File.Path); | ||
} | ||
|
||
public async Task LoadPreviewAsync() | ||
{ | ||
State = PreviewState.Loading; | ||
|
||
var previewTask = LoadPreviewImageAsync(); | ||
|
||
await Task.WhenAll(previewTask); | ||
|
||
if (Preview == null) | ||
{ | ||
State = PreviewState.Error; | ||
} | ||
} | ||
|
||
public static bool IsFileTypeSupported(string fileExt) | ||
{ | ||
return fileExt == ".png" ? true : false; | ||
} | ||
|
||
private void OnPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) | ||
{ | ||
if (e.PropertyName == nameof(Preview)) | ||
{ | ||
if (Preview != null) | ||
{ | ||
State = PreviewState.Loaded; | ||
} | ||
} | ||
} | ||
|
||
private Task LoadPreviewImageAsync() | ||
{ | ||
var thumbnailTCS = new TaskCompletionSource(); | ||
Dispatcher.TryEnqueue(async () => | ||
{ | ||
if (CancellationToken.IsCancellationRequested) | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
return; | ||
} | ||
Preview = await ThumbnailHelper.GetThumbnailAsync(File.Path, _png_image_size); | ||
thumbnailTCS.SetResult(); | ||
}); | ||
|
||
return thumbnailTCS.Task; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5bd9dd5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@check-spelling-bot Report
🔴 Please review
See the 📜action log for details.
Unrecognized words (140)
Previously acknowledged words that are now absent
brucelindbloom chromaticities companding DCR Eqn ffaa FILETIME HICON ITHUMBNAIL Pbgra PKEY Windowsapp :arrow_right:To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands
... in a clone of the git@github.com:microsoft/PowerToys.git repository
on the
peek
branch (ℹ️ how do I use this?):Available 📚 dictionaries could cover words not in the 📘 dictionary
This includes both expected items (2140) from .github/actions/spell-check/expect.txt and unrecognized words (140)
Consider adding them using (in
.github/workflows/spelling2.yml
):To stop checking additional dictionaries, add:
Errors (1)
See the 📜action log for details.
See ❌ Event descriptions for more information.
If the flagged items are 🤯 false positives
If items relate to a ...
binary file (or some other file you wouldn't want to check at all).
Please add a file path to the
excludes.txt
file matching the containing file.File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.
^
refers to the file's path from the root of the repository, so^README\.md$
would exclude README.md (on whichever branch you're using).well-formed pattern.
If you can write a pattern that would match it,
try adding it to the
patterns.txt
file.Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.
Note that patterns can't match multiline strings.