mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-11-27 23:19:13 +08:00
Update to pass cancellation token individually to each async methods
This commit is contained in:
parent
e8ebf73033
commit
f2a7a5ce1a
@ -52,8 +52,8 @@ namespace Peek.FilePreviewer
|
||||
_cancellationTokenSource.Cancel();
|
||||
_cancellationTokenSource = new ();
|
||||
|
||||
Previewer = previewerFactory.CreateDefaultPreviewer(File, _cancellationTokenSource.Token);
|
||||
await UpdatePreviewAsync();
|
||||
Previewer = previewerFactory.CreateDefaultPreviewer(File);
|
||||
await UpdatePreviewAsync(_cancellationTokenSource.Token);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -102,22 +102,22 @@ namespace Peek.FilePreviewer
|
||||
return;
|
||||
}
|
||||
|
||||
Previewer = previewerFactory.Create(File, _cancellationTokenSource.Token);
|
||||
Previewer = previewerFactory.Create(File);
|
||||
|
||||
await UpdatePreviewAsync();
|
||||
await UpdatePreviewAsync(_cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
private async Task UpdatePreviewAsync()
|
||||
private async Task UpdatePreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (Previewer != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
_cancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
var size = await Previewer.GetPreviewSizeAsync();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var size = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
||||
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(size));
|
||||
_cancellationTokenSource.Token.ThrowIfCancellationRequested();
|
||||
await Previewer.LoadPreviewAsync();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await Previewer.LoadPreviewAsync(cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
@ -16,9 +16,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
public static bool IsFileTypeSupported(string fileExt) => throw new NotImplementedException();
|
||||
|
||||
public Task<Size> GetPreviewSizeAsync();
|
||||
public Task<Size> GetPreviewSizeAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task LoadPreviewAsync();
|
||||
Task LoadPreviewAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public enum PreviewState
|
||||
|
@ -27,10 +27,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
[ObservableProperty]
|
||||
private PreviewState state;
|
||||
|
||||
public ImagePreviewer(File file, CancellationToken cancellationToken)
|
||||
public ImagePreviewer(File file)
|
||||
{
|
||||
File = file;
|
||||
CancellationToken = cancellationToken;
|
||||
Dispatcher = DispatcherQueue.GetForCurrentThread();
|
||||
|
||||
PropertyChanged += OnPropertyChanged;
|
||||
@ -38,8 +37,6 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
private File File { get; }
|
||||
|
||||
private CancellationToken CancellationToken { get; }
|
||||
|
||||
private DispatcherQueue Dispatcher { get; }
|
||||
|
||||
private Task<bool>? LowQualityThumbnailTask { get; set; }
|
||||
@ -57,9 +54,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public async Task<Size> GetPreviewSizeAsync()
|
||||
public async Task<Size> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var propertyImageSize = await PropertyHelper.GetImageSize(File.Path);
|
||||
if (propertyImageSize != Size.Empty)
|
||||
{
|
||||
@ -69,14 +66,14 @@ namespace Peek.FilePreviewer.Previewers
|
||||
return await WICHelper.GetImageSize(File.Path);
|
||||
}
|
||||
|
||||
public async Task LoadPreviewAsync()
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
State = PreviewState.Loading;
|
||||
|
||||
LowQualityThumbnailTask = LoadLowQualityThumbnailAsync();
|
||||
HighQualityThumbnailTask = LoadHighQualityThumbnailAsync();
|
||||
FullQualityImageTask = LoadFullQualityImageAsync();
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
LowQualityThumbnailTask = LoadLowQualityThumbnailAsync(cancellationToken);
|
||||
HighQualityThumbnailTask = LoadHighQualityThumbnailAsync(cancellationToken);
|
||||
FullQualityImageTask = LoadFullQualityImageAsync(cancellationToken);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await Task.WhenAll(LowQualityThumbnailTask, HighQualityThumbnailTask, FullQualityImageTask);
|
||||
|
||||
@ -97,11 +94,11 @@ namespace Peek.FilePreviewer.Previewers
|
||||
}
|
||||
}
|
||||
|
||||
private Task<bool> LoadLowQualityThumbnailAsync()
|
||||
private Task<bool> LoadLowQualityThumbnailAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return TaskExtension.RunSafe(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (!IsFullImageLoaded && !IsHighQualityThumbnailLoaded)
|
||||
{
|
||||
@ -113,23 +110,23 @@ namespace Peek.FilePreviewer.Previewers
|
||||
throw new ArgumentNullException(nameof(hbitmap));
|
||||
}
|
||||
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
var thumbnailBitmap = await GetBitmapFromHBitmapAsync(hbitmap, CancellationToken);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var thumbnailBitmap = await GetBitmapFromHBitmapAsync(hbitmap, cancellationToken);
|
||||
Preview = thumbnailBitmap;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Task<bool> LoadHighQualityThumbnailAsync()
|
||||
private Task<bool> LoadHighQualityThumbnailAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return TaskExtension.RunSafe(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (!IsFullImageLoaded)
|
||||
{
|
||||
@ -141,28 +138,29 @@ namespace Peek.FilePreviewer.Previewers
|
||||
throw new ArgumentNullException(nameof(hbitmap));
|
||||
}
|
||||
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
var thumbnailBitmap = await GetBitmapFromHBitmapAsync(hbitmap, CancellationToken);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var thumbnailBitmap = await GetBitmapFromHBitmapAsync(hbitmap, cancellationToken);
|
||||
Preview = thumbnailBitmap;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Task<bool> LoadFullQualityImageAsync()
|
||||
private Task<bool> LoadFullQualityImageAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return TaskExtension.RunSafe(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
// TODO: Check if this is performant
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
var bitmap = await GetFullBitmapFromPathAsync(File.Path, CancellationToken);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var bitmap = await GetFullBitmapFromPathAsync(File.Path, cancellationToken);
|
||||
Preview = bitmap;
|
||||
});
|
||||
});
|
||||
@ -197,13 +195,14 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
var bitmap = System.Drawing.Image.FromHbitmap(hbitmap);
|
||||
var bitmapImage = new BitmapImage();
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(stream, ImageFormat.Bmp);
|
||||
stream.Position = 0;
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
await bitmapImage.SetSourceAsync(stream.AsRandomAccessStream());
|
||||
}
|
||||
|
||||
|
@ -9,24 +9,24 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
public class PreviewerFactory
|
||||
{
|
||||
public IPreviewer Create(File file, CancellationToken cancellationToken)
|
||||
public IPreviewer Create(File file)
|
||||
{
|
||||
if (ImagePreviewer.IsFileTypeSupported(file.Extension))
|
||||
{
|
||||
return new ImagePreviewer(file, cancellationToken);
|
||||
return new ImagePreviewer(file);
|
||||
}
|
||||
else if (WebBrowserPreviewer.IsFileTypeSupported(file.Extension))
|
||||
{
|
||||
return new WebBrowserPreviewer(file, cancellationToken);
|
||||
return new WebBrowserPreviewer(file);
|
||||
}
|
||||
|
||||
// Other previewer types check their supported file types here
|
||||
return CreateDefaultPreviewer(file, cancellationToken);
|
||||
return CreateDefaultPreviewer(file);
|
||||
}
|
||||
|
||||
public IPreviewer CreateDefaultPreviewer(File file, CancellationToken cancellationToken)
|
||||
public IPreviewer CreateDefaultPreviewer(File file)
|
||||
{
|
||||
return new UnsupportedFilePreviewer(file, cancellationToken);
|
||||
return new UnsupportedFilePreviewer(file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -40,10 +40,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
[ObservableProperty]
|
||||
private PreviewState state;
|
||||
|
||||
public UnsupportedFilePreviewer(File file, CancellationToken cancellationToken)
|
||||
public UnsupportedFilePreviewer(File file)
|
||||
{
|
||||
File = file;
|
||||
CancellationToken = cancellationToken;
|
||||
FileName = file.FileName;
|
||||
DateModified = file.DateModified.ToString();
|
||||
Dispatcher = DispatcherQueue.GetForCurrentThread();
|
||||
@ -53,8 +52,6 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
private File File { get; }
|
||||
|
||||
private CancellationToken CancellationToken { get; }
|
||||
|
||||
private DispatcherQueue Dispatcher { get; }
|
||||
|
||||
private Task<bool>? IconPreviewTask { get; set; }
|
||||
@ -66,7 +63,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public Task<Size> GetPreviewSizeAsync()
|
||||
public Task<Size> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return Task.Run(() =>
|
||||
{
|
||||
@ -75,14 +72,14 @@ namespace Peek.FilePreviewer.Previewers
|
||||
});
|
||||
}
|
||||
|
||||
public async Task LoadPreviewAsync()
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
State = PreviewState.Loading;
|
||||
|
||||
IconPreviewTask = LoadIconPreviewAsync();
|
||||
DisplayInfoTask = LoadDisplayInfoAsync();
|
||||
IconPreviewTask = LoadIconPreviewAsync(cancellationToken);
|
||||
DisplayInfoTask = LoadDisplayInfoAsync(cancellationToken);
|
||||
|
||||
await Task.WhenAll(IconPreviewTask, DisplayInfoTask);
|
||||
|
||||
@ -92,35 +89,34 @@ namespace Peek.FilePreviewer.Previewers
|
||||
}
|
||||
}
|
||||
|
||||
public Task<bool> LoadIconPreviewAsync()
|
||||
public Task<bool> LoadIconPreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return TaskExtension.RunSafe(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
// TODO: Get icon with transparency
|
||||
IconHelper.GetIcon(Path.GetFullPath(File.Path), out IntPtr hbitmap);
|
||||
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
await Dispatcher.RunOnUiThread(async () =>
|
||||
{
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
var iconBitmap = await GetBitmapFromHBitmapAsync(hbitmap, CancellationToken);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var iconBitmap = await GetBitmapFromHBitmapAsync(hbitmap, cancellationToken);
|
||||
IconPreview = iconBitmap;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public Task<bool> LoadDisplayInfoAsync()
|
||||
public Task<bool> LoadDisplayInfoAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return TaskExtension.RunSafe(async () =>
|
||||
{
|
||||
// File Properties
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var bytes = await PropertyHelper.GetFileSizeInBytes(File.Path);
|
||||
|
||||
CancellationToken.ThrowIfCancellationRequested();
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
var type = await PropertyHelper.GetFileType(File.Path);
|
||||
|
||||
await Dispatcher.RunOnUiThread(() =>
|
||||
|
@ -30,24 +30,21 @@ namespace Peek.FilePreviewer.Previewers
|
||||
[ObservableProperty]
|
||||
private PreviewState state;
|
||||
|
||||
public WebBrowserPreviewer(File file, CancellationToken cancellationToken)
|
||||
public WebBrowserPreviewer(File file)
|
||||
{
|
||||
File = file;
|
||||
CancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
private File File { get; }
|
||||
|
||||
private CancellationToken CancellationToken { get; }
|
||||
|
||||
public Task<Size> GetPreviewSizeAsync()
|
||||
public Task<Size> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// TODO: define how to proper window size on HTML content.
|
||||
var size = new Size(1280, 720);
|
||||
return Task.FromResult(size);
|
||||
}
|
||||
|
||||
public Task LoadPreviewAsync()
|
||||
public Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
State = PreviewState.Loading;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user