Skip to content

Commit

Permalink
Yizzho/peek/videos (#25983)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Yawen Hou <[email protected]>
Co-authored-by: Clint Rutkas <[email protected]>
Co-authored-by: Yawen Hou <[email protected]>
Co-authored-by: Samuel Chapleau 🌈 <[email protected]>
  • Loading branch information
6 people committed May 15, 2023
1 parent 5aa58bf commit a0b9af0
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 32 deletions.
11 changes: 7 additions & 4 deletions .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ arsinh
artanh
arw
asdf
asf
AShortcut
ASingle
ASSOCCHANGED
Expand Down Expand Up @@ -225,8 +226,8 @@ bytearray
CABD
CALG
callbackptr
cameligo
calpwstr
cameligo
Cangjie
CANRENAME
CAPTUREBLT
Expand Down Expand Up @@ -771,6 +772,7 @@ google
gpedit
gpo
GPOCA
gpp
GPT
gpu
graphql
Expand Down Expand Up @@ -1121,13 +1123,13 @@ LPCTSTR
LPCWSTR
lpdw
lpfn
lpmi
LPINPUT
lpmi
LPMINMAXINFO
LPMONITORINFO
LPOSVERSIONINFOEXW
lprc
LPPOINT
lprc
LPRECT
LPSAFEARRAY
LPSTR
Expand Down Expand Up @@ -1230,6 +1232,7 @@ Miracast
mjpg
mkd
mkdn
mkv
mlcfg
mmc
mmcexe
Expand Down Expand Up @@ -1511,7 +1514,6 @@ pft
pgp
pgsql
pguid
pkey
PHANDLE
phbm
phbmp
Expand All @@ -1525,6 +1527,7 @@ pinfo
pinvoke
pipename
PKBDLLHOOKSTRUCT
pkey
PKEY
plib
PLK
Expand Down
24 changes: 24 additions & 0 deletions src/modules/peek/Peek.Common/Extensions/DispatcherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,29 @@ public static Task RunOnUiThread(this DispatcherQueue dispatcher, Func<Task> wor

return tcs.Task;
}

/// <summary>
/// Run work on UI thread safely.
/// </summary>
/// <returns>True if the work was run successfully, False otherwise.</returns>
public static Task RunOnUiThread(this DispatcherQueue dispatcher, Action work)
{
var tcs = new TaskCompletionSource();
dispatcher.TryEnqueue(() =>
{
try
{
work();
tcs.SetResult();
}
catch (Exception e)
{
tcs.SetException(e);
}
});

return tcs.Task;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using Peek.Common.Helpers;
using Peek.Common.Models;
using Scripting;
using Windows.Foundation;
Expand All @@ -19,17 +20,12 @@ public static class IFileSystemItemExtensions
{
public static Size? GetImageSize(this IFileSystemItem item)
{
Size? size = null;

var width = item.Width;
var height = item.Height;

if (width != null && height != null)
{
size = new Size((int)width, (int)height);
}
return PropertyStoreHelper.TryGetUintSizeProperty(item.Path, PropertyKey.ImageHorizontalSize, PropertyKey.ImageVerticalSize);
}

return size;
public static Size? GetVideoSize(this IFileSystemItem item)
{
return PropertyStoreHelper.TryGetUintSizeProperty(item.Path, PropertyKey.FrameWidth, PropertyKey.FrameHeight);
}

public static Size? GetSvgSize(this IFileSystemItem item)
Expand Down
37 changes: 24 additions & 13 deletions src/modules/peek/Peek.Common/Helpers/PropertyStoreHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@
using System.Runtime.InteropServices;
using Peek.Common.Extensions;
using Peek.Common.Models;
using Windows.Foundation;
using Windows.Win32.UI.Shell.PropertiesSystem;

namespace Peek.Common.Helpers
{
public static partial class PropertyStoreHelper
{
/// <summary>
/// Gets a uint type value from PropertyStore from the given item.
/// </summary>
/// <param name="path">The file/folder path</param>
/// <param name="key">The property key</param>
/// <returns>a nullable uint</returns>
public static uint? TryGetUintProperty(string path, PropertyKey key)
{
using DisposablePropertyStore propertyStore = GetPropertyStoreFromPath(path);
return propertyStore.TryGetUInt(key);
}

/// <summary>
/// Gets a ulong type value from PropertyStore from the given item.
/// </summary>
Expand All @@ -49,6 +38,28 @@ public static partial class PropertyStoreHelper
return propertyStore.TryGetString(key);
}

/// <summary>
/// Gets Size composed of weight (uint) and height (uint) from PropertyStore from the given item.
/// </summary>
/// <param name="path">The file/folder path</param>
/// <param name="widthKey">The property key for width</param>
/// <param name="heightKey">The property key for height</param>
/// <returns>a nullable string</returns>
public static Size? TryGetUintSizeProperty(string path, PropertyKey widthKey, PropertyKey heightKey)
{
Size? size = null;
using DisposablePropertyStore propertyStore = GetPropertyStoreFromPath(path);
uint? width = propertyStore.TryGetUInt(widthKey);
uint? height = propertyStore.TryGetUInt(heightKey);

if (width != null && height != null)
{
size = new Size((float)width, (float)height);
}

return size;
}

/// <summary>
/// Gets a IPropertyStore interface (wrapped in DisposablePropertyStore) from the given path.
/// </summary>
Expand All @@ -73,7 +84,7 @@ private static DisposablePropertyStore GetPropertyStoreFromPath(string path, GET

if (hr != 0)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "GetPropertyStore returned hresult={0}", hr));
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "GetPropertyStore returned hresult={0}, errorMessage={1}", hr, Marshal.GetExceptionForHR(hr)!.Message));
}

return new DisposablePropertyStore((IPropertyStore)Marshal.GetObjectForIUnknown(ppPropertyStore));
Expand Down
5 changes: 0 additions & 5 deletions src/modules/peek/Peek.Common/Models/IFileSystemItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Globalization;
using System.Threading.Tasks;
using Peek.Common.Extensions;
using Peek.Common.Helpers;
using Windows.Storage;

Expand Down Expand Up @@ -39,10 +38,6 @@ public DateTime? DateModified

public string Path { get; init; }

public uint? Width => PropertyStoreHelper.TryGetUintProperty(Path, PropertyKey.ImageHorizontalSize);

public uint? Height => PropertyStoreHelper.TryGetUintProperty(Path, PropertyKey.ImageVerticalSize);

public ulong FileSizeBytes => PropertyStoreHelper.TryGetUlongProperty(Path, PropertyKey.FileSizeBytes) ?? 0;

public string FileType => PropertyStoreHelper.TryGetStringProperty(Path, PropertyKey.FileType) ?? string.Empty;
Expand Down
2 changes: 2 additions & 0 deletions src/modules/peek/Peek.Common/Models/Win32/PropertyKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ public override int GetHashCode()
public static readonly PropertyKey ImageVerticalSize = new PropertyKey(new Guid(0x6444048F, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03), 4);
public static readonly PropertyKey FileSizeBytes = new PropertyKey(new Guid(0xb725f130, 0x47ef, 0x101a, 0xa5, 0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac), 12);
public static readonly PropertyKey FileType = new PropertyKey(new Guid(0xd5cdd502, 0x2e9c, 0x101b, 0x93, 0x97, 0x08, 0x00, 0x2b, 0x2c, 0xf9, 0xae), 26);
public static readonly PropertyKey FrameWidth = new PropertyKey(new Guid(0x64440491, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03), 3);
public static readonly PropertyKey FrameHeight = new PropertyKey(new Guid(0x64440491, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03), 4);
}
}
16 changes: 16 additions & 0 deletions src/modules/peek/Peek.FilePreviewer/FilePreview.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/modules/peek/Peek.FilePreviewer/FilePreview.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public sealed partial class FilePreview : UserControl, IDisposable

[ObservableProperty]
[NotifyPropertyChangedFor(nameof(ImagePreviewer))]
[NotifyPropertyChangedFor(nameof(VideoPreviewer))]
[NotifyPropertyChangedFor(nameof(BrowserPreviewer))]
[NotifyPropertyChangedFor(nameof(UnsupportedFilePreviewer))]

Expand Down Expand Up @@ -89,6 +90,8 @@ private async void Previewer_PropertyChanged(object? sender, System.ComponentMod

public IImagePreviewer? ImagePreviewer => Previewer as IImagePreviewer;

public IVideoPreviewer? VideoPreviewer => Previewer as IVideoPreviewer;

public IBrowserPreviewer? BrowserPreviewer => Previewer as IBrowserPreviewer;

public IUnsupportedFilePreviewer? UnsupportedFilePreviewer => Previewer as IUnsupportedFilePreviewer;
Expand Down Expand Up @@ -140,6 +143,7 @@ private async Task OnItemPropertyChanged()
{
Previewer = null;
ImagePreview.Visibility = Visibility.Collapsed;
VideoPreview.Visibility = Visibility.Collapsed;
BrowserPreview.Visibility = Visibility.Collapsed;
UnsupportedFilePreview.Visibility = Visibility.Collapsed;
return;
Expand Down Expand Up @@ -199,6 +203,8 @@ private async Task UpdatePreviewAsync(CancellationToken cancellationToken)

partial void OnPreviewerChanging(IPreviewer? value)
{
VideoPreview.MediaPlayer.Pause();

if (Previewer != null)
{
Previewer.PropertyChanged -= Previewer_PropertyChanged;
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Loading

0 comments on commit a0b9af0

Please sign in to comment.