Skip to content

Feature: Added support for previewing Office files #13096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Files.App/Files.App.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!-- Copyright (c) 2023 Files Community. Licensed under the MIT License. See the LICENSE. -->
<!-- Copyright (c) 2023 Files Community. Licensed under the MIT License. See the LICENSE. -->
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
Expand Down Expand Up @@ -77,6 +77,7 @@
<ItemGroup>
<PackageReference Include="ByteSize" Version="2.1.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="DirectNCore" Version="1.13.2.1" />
<PackageReference Include="DiscUtils.Udf" Version="0.16.13" />
<PackageReference Include="FluentFTP" Version="43.0.1" />
<PackageReference Include="ini-parser-netstandard" Version="2.5.2" />
Expand All @@ -97,6 +98,7 @@
<PackageReference Include="CommunityToolkit.WinUI.UI.Behaviors" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
<PackageReference Include="Tulpep.ActiveDirectoryObjectPicker" Version="3.0.11" />
<PackageReference Include="Vanara.PInvoke.DwmApi" Version="3.4.15" />
<PackageReference Include="Vanara.Windows.Extensions" Version="3.4.16" />
<PackageReference Include="WinUIEx" Version="2.1.0" />
<PackageReference Include="Vanara.PInvoke.Mpr" Version="3.4.16" />
Expand Down
16 changes: 16 additions & 0 deletions src/Files.App/Helpers/Interop/InteropHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.UI.Xaml;
using System.Reflection;
using System.Runtime.InteropServices;
using Vanara.PInvoke;
using static Vanara.PInvoke.User32;

namespace Files.App.Helpers
{
Expand Down Expand Up @@ -51,6 +53,20 @@ public static void ChangeCursor(this UIElement uiElement, InputCursor cursor)
new object[] { cursor }
);
}

[DllImport(Lib.User32, SetLastError = true, EntryPoint = "SetWindowLong")]
private static extern int SetWindowLongPtr32(HWND hWnd, WindowLongFlags nIndex, IntPtr dwNewLong);

[DllImport(Lib.User32, SetLastError = true, EntryPoint = "SetWindowLongPtr")]
private static extern IntPtr SetWindowLongPtr64(HWND hWnd, WindowLongFlags nIndex, IntPtr dwNewLong);

public static IntPtr SetWindowLong(HWND hWnd, WindowLongFlags nIndex, IntPtr dwNewLong)
{
if (IntPtr.Size == 4)
return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
else
return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
}
}

[ComImport]
Expand Down
18 changes: 18 additions & 0 deletions src/Files.App/UserControls/FilePreviews/ShellPreview.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<UserControl
x:Class="Files.App.UserControls.FilePreviews.ShellPreview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Files.App.UserControls.FilePreviews"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Unloaded="PreviewHost_Unloaded"
mc:Ignorable="d">

<ContentPresenter
Name="contentPresenter"
Background="Transparent"
Loaded="PreviewHost_Loaded"
PointerEntered="PreviewHost_PointerEntered"
SizeChanged="PreviewHost_SizeChanged" />
</UserControl>
64 changes: 64 additions & 0 deletions src/Files.App/UserControls/FilePreviews/ShellPreview.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Files.App.ViewModels.Previews;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Vanara.PInvoke;
using Windows.Foundation;

namespace Files.App.UserControls.FilePreviews
{
public sealed partial class ShellPreview : UserControl
{
private ShellPreviewViewModel ViewModel { get; set; }

public ShellPreview(ShellPreviewViewModel model)
{
ViewModel = model;
this.InitializeComponent();
}

private void PreviewHost_Loaded(object sender, RoutedEventArgs e)
{
ViewModel.LoadPreview(contentPresenter);
ViewModel.SizeChanged(GetPreviewSize());
if (XamlRoot.Content is FrameworkElement element)
{
element.SizeChanged += PreviewHost_SizeChanged;
element.PointerEntered += PreviewHost_PointerEntered;
}
}

private void PreviewHost_SizeChanged(object sender, SizeChangedEventArgs e)
{
ViewModel.SizeChanged(GetPreviewSize());
}

private RECT GetPreviewSize()
{
var source = contentPresenter.TransformToVisual(XamlRoot.Content);
var physicalSize = contentPresenter.RenderSize;
var physicalPos = source.TransformPoint(new Point(0, 0));
var scale = XamlRoot.RasterizationScale;
var result = new RECT();
result.Left = (int)(physicalPos.X * scale + 0.5);
result.Top = (int)(physicalPos.Y * scale + 0.5);
result.Width = (int)(physicalSize.Width * scale + 0.5);
result.Height = (int)(physicalSize.Height * scale + 0.5);
return result;
}

private void PreviewHost_Unloaded(object sender, RoutedEventArgs e)
{
if (XamlRoot.Content is FrameworkElement element)
{
element.SizeChanged -= PreviewHost_SizeChanged;
element.PointerEntered -= PreviewHost_PointerEntered;
}
ViewModel.UnloadPreview();
}

private void PreviewHost_PointerEntered(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
{
ViewModel.PointerEntered(sender == contentPresenter);
}
}
}
43 changes: 43 additions & 0 deletions src/Files.App/Utils/Shell/ItemStreamHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Runtime.InteropServices;
using Vanara.PInvoke;

namespace Files.App.Utils.Shell
{
public static class ItemStreamHelper
{
[DllImport("shlwapi.dll", CallingConvention = CallingConvention.StdCall, PreserveSig = true, CharSet = CharSet.Unicode)]
static extern HRESULT SHCreateStreamOnFileEx(string pszFile, STGM grfMode, uint dwAttributes, uint fCreate, IntPtr pstmTemplate, out IntPtr ppstm);

[DllImport("shell32.dll", CallingConvention = CallingConvention.StdCall, PreserveSig = true, CharSet = CharSet.Unicode)]
static extern HRESULT SHCreateItemFromParsingName(string pszPath, IntPtr pbc, ref Guid riid, out IntPtr ppv);

static readonly Guid IShellItemIid = Guid.ParseExact("43826d1e-e718-42ee-bc55-a1e261c37bfe", "d");

public static IntPtr IShellItemFromPath(string path)
{
IntPtr psi;
Guid iid = IShellItemIid;
var hr = SHCreateItemFromParsingName(path, IntPtr.Zero, ref iid, out psi);
if ((int)hr < 0)
return IntPtr.Zero;
return psi;
}

public static IntPtr IStreamFromPath(string path)
{
IntPtr pstm;
var hr = SHCreateStreamOnFileEx(path,
STGM.STGM_READ | STGM.STGM_FAILIFTHERE | STGM.STGM_SHARE_DENY_NONE,
0, 0, IntPtr.Zero, out pstm);
if ((int)hr < 0)
return IntPtr.Zero;
return pstm;
}

public static void ReleaseObject(IntPtr obj)
{
Marshal.Release(obj);
}
}
}
Loading