Yizzho/peek/videos (#25983)

* Add basics of VideoPreviewer to build on

* WIP

* Minimal working code, todo next:dimension + MTC

* Nits

* Change back to GetImageSize as it indeed doesn't work with videos

* Add win32 helper methods to get video size; Refactor get size operation;

* Remove unused code

* Set VideoTask; Add message error for HR result;

* Add open read only for filestream

* Remove unused code

* Update expect.txt

* Remove comment

* Cleanup code

* Force pause videopreview on previewer change

---------

Co-authored-by: Jojo Zhou <yizzho@microsoft.com>
Co-authored-by: Yawen Hou <yawenhou@microsoft.com>
Co-authored-by: Clint Rutkas <clint@rutkas.com>
Co-authored-by: Yawen Hou <Sytta@users.noreply.github.com>
Co-authored-by: Samuel Chapleau 🌈 <sachaple@microsoft.com>
This commit is contained in:
Jojo Zhou
2023-05-15 14:06:08 -07:00
committed by GitHub
parent 5aa58bf922
commit a0b9af039d
15 changed files with 222 additions and 32 deletions

View File

@@ -26,6 +26,22 @@
ToolTipService.ToolTip="{x:Bind ImageInfoTooltip, Mode=OneWay}"
Visibility="{x:Bind IsPreviewVisible(ImagePreviewer, Previewer.State), Mode=OneWay}" />
<MediaPlayerElement
x:Name="VideoPreview"
AreTransportControlsEnabled="True"
AutoPlay="True"
Source="{x:Bind VideoPreviewer.Preview, Mode=OneWay}"
ToolTipService.ToolTip="{x:Bind ImageInfoTooltip, Mode=OneWay}"
Visibility="{x:Bind IsPreviewVisible(VideoPreviewer, Previewer.State), Mode=OneWay}">
<MediaPlayerElement.TransportControls>
<MediaTransportControls
x:Name="mediaTransport"
Width="auto"
MaxWidth="420"
IsCompact="True" />
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
<controls:BrowserControl
x:Name="BrowserPreview"
x:Load="True"

View File

@@ -47,6 +47,7 @@ namespace Peek.FilePreviewer
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ImagePreviewer))]
[NotifyPropertyChangedFor(nameof(VideoPreviewer))]
[NotifyPropertyChangedFor(nameof(BrowserPreviewer))]
[NotifyPropertyChangedFor(nameof(UnsupportedFilePreviewer))]
@@ -89,6 +90,8 @@ namespace Peek.FilePreviewer
public IImagePreviewer? ImagePreviewer => Previewer as IImagePreviewer;
public IVideoPreviewer? VideoPreviewer => Previewer as IVideoPreviewer;
public IBrowserPreviewer? BrowserPreviewer => Previewer as IBrowserPreviewer;
public IUnsupportedFilePreviewer? UnsupportedFilePreviewer => Previewer as IUnsupportedFilePreviewer;
@@ -140,6 +143,7 @@ namespace Peek.FilePreviewer
{
Previewer = null;
ImagePreview.Visibility = Visibility.Collapsed;
VideoPreview.Visibility = Visibility.Collapsed;
BrowserPreview.Visibility = Visibility.Collapsed;
UnsupportedFilePreview.Visibility = Visibility.Collapsed;
return;
@@ -199,6 +203,8 @@ namespace Peek.FilePreviewer
partial void OnPreviewerChanging(IPreviewer? value)
{
VideoPreview.MediaPlayer.Pause();
if (Previewer != null)
{
Previewer.PropertyChanged -= Previewer_PropertyChanged;

View File

@@ -0,0 +1,13 @@
// 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 Windows.Media.Core;
namespace Peek.FilePreviewer.Previewers.Interfaces
{
public interface IVideoPreviewer : IPreviewer
{
public MediaSource? Preview { get; }
}
}

View File

@@ -0,0 +1,120 @@
// 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.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.UI.Dispatching;
using Peek.Common.Extensions;
using Peek.Common.Helpers;
using Peek.Common.Models;
using Peek.FilePreviewer.Previewers.Interfaces;
using Windows.Foundation;
using Windows.Media.Core;
using Windows.Storage;
namespace Peek.FilePreviewer.Previewers
{
public partial class VideoPreviewer : ObservableObject, IVideoPreviewer, IDisposable
{
[ObservableProperty]
private MediaSource? preview;
[ObservableProperty]
private PreviewState state;
[ObservableProperty]
private Size videoSize;
public VideoPreviewer(IFileSystemItem file)
{
Item = file;
Dispatcher = DispatcherQueue.GetForCurrentThread();
}
private IFileSystemItem Item { get; }
private DispatcherQueue Dispatcher { get; }
private Task<bool>? VideoTask { get; set; }
public static bool IsFileTypeSupported(string fileExt)
{
return _supportedFileTypes.Contains(fileExt);
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
{
State = PreviewState.Loading;
VideoTask = LoadVideoAsync(cancellationToken);
cancellationToken.ThrowIfCancellationRequested();
await VideoTask;
if (Preview == null && HasFailedLoadingPreview())
{
State = PreviewState.Error;
}
}
partial void OnPreviewChanged(MediaSource? value)
{
if (Preview != null)
{
State = PreviewState.Loaded;
}
}
public async Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
return await Task.Run(Item.GetVideoSize);
}
public async Task CopyAsync()
{
await Dispatcher.RunOnUiThread(async () =>
{
var storageItem = await Item.GetStorageItemAsync();
ClipboardHelper.SaveToClipboard(storageItem);
});
}
private Task<bool> LoadVideoAsync(CancellationToken cancellationToken)
{
return TaskExtension.RunSafe(async () =>
{
cancellationToken.ThrowIfCancellationRequested();
var storageFile = await Item.GetStorageItemAsync() as StorageFile;
await Dispatcher.RunOnUiThread(() =>
{
cancellationToken.ThrowIfCancellationRequested();
Preview = MediaSource.CreateFromStorageFile(storageFile);
});
});
}
private bool HasFailedLoadingPreview()
{
return !(VideoTask?.Result ?? true);
}
private static readonly HashSet<string> _supportedFileTypes = new()
{
".mp4", ".3g2", ".3gp", ".3gp2", ".3gpp", ".asf", ".avi", ".m2t", ".m2ts",
".m4v", ".mkv", ".mov", ".mp4", ".mp4v", ".mts", ".wm", ".wmv",
};
}
}

View File

@@ -16,6 +16,10 @@ namespace Peek.FilePreviewer.Previewers
{
return new ImagePreviewer(file);
}
else if (VideoPreviewer.IsFileTypeSupported(file.Extension))
{
return new VideoPreviewer(file);
}
else if (WebBrowserPreviewer.IsFileTypeSupported(file.Extension))
{
return new WebBrowserPreviewer(file);