use Share UI on Windows 10 16299 and later

This commit is contained in:
Paddy Xu
2018-06-02 15:27:14 +03:00
parent b0b07e443c
commit d36da88f31
4 changed files with 143 additions and 21 deletions

View File

@@ -0,0 +1,126 @@
// Copyright © 2018 Paddy Xu
//
// This file is part of QuickLook program.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows;
using System.Windows.Interop;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
namespace QuickLook.Helpers
{
internal class ShareHelper
{
private static string _sharingPath = string.Empty;
internal static void Share(string path, Window parent)
{
if (string.IsNullOrEmpty(path))
return;
_sharingPath = path;
if (!Directory.Exists(path) && App.IsWin10 && Environment.OSVersion.Version >= new Version("10.0.16299.0"))
ShowShareUI(parent);
else
RunWith();
}
private static void RunWith()
{
try
{
Process.Start(new ProcessStartInfo("rundll32.exe")
{
Arguments = $"shell32.dll,OpenAs_RunDLL {_sharingPath}",
WorkingDirectory = Path.GetDirectoryName(_sharingPath)
});
}
catch (Exception e)
{
ProcessHelper.WriteLog(e.ToString());
}
}
private static void ShowShareUI(Window parent)
{
var hwnd = new WindowInteropHelper(parent).Handle;
var dtm = DataTransferManagerHelper.GetForWindow(hwnd);
dtm.DataRequested += OnShareDataRequested;
DataTransferManagerHelper.ShowShareUIForWindow(hwnd);
}
private static async void OnShareDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
var deferral = args.Request.GetDeferral();
try
{
var dp = args.Request.Data;
dp.Properties.Title =
$"“{Path.GetFileName(_sharingPath)}” ({new FileInfo(_sharingPath).Length.ToPrettySize()})";
var filesToShare = new List<IStorageItem>();
var imageFile = await StorageFile.GetFileFromPathAsync(_sharingPath);
filesToShare.Add(imageFile);
dp.SetStorageItems(filesToShare);
}
finally
{
_sharingPath = string.Empty;
//sender.DataRequested -= OnShareDataRequested;
deferral.Complete();
}
}
}
internal static class DataTransferManagerHelper
{
private static readonly Guid DTM_IID =
new Guid(0xa5caee9b, 0x8708, 0x49d1, 0x8d, 0x36, 0x67, 0xd2, 0x5a, 0x8d, 0xa0, 0x0c);
private static IDataTransferManagerInterop DataTransferManagerInterop =>
(IDataTransferManagerInterop) WindowsRuntimeMarshal.GetActivationFactory(typeof(DataTransferManager));
public static DataTransferManager GetForWindow(IntPtr hwnd)
{
return DataTransferManagerInterop.GetForWindow(hwnd, DTM_IID);
}
public static void ShowShareUIForWindow(IntPtr hwnd)
{
DataTransferManagerInterop.ShowShareUIForWindow(hwnd);
}
[ComImport]
[Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IDataTransferManagerInterop
{
DataTransferManager GetForWindow([In] IntPtr appWindow, [In] ref Guid riid);
void ShowShareUIForWindow(IntPtr appWindow);
}
}
}

View File

@@ -92,12 +92,25 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="windows">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.17134.0\Facade\windows.winmd</HintPath>
</Reference>
<Reference Include="Windows.Foundation.FoundationContract">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Windows Kits\10\References\10.0.17134.0\Windows.Foundation.FoundationContract\3.0.0.0\Windows.Foundation.FoundationContract.winmd</HintPath>
</Reference>
<Reference Include="Windows.Foundation.UniversalApiContract">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Windows Kits\10\References\10.0.17134.0\Windows.Foundation.UniversalApiContract\6.0.0.0\Windows.Foundation.UniversalApiContract.winmd</HintPath>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
@@ -129,6 +142,7 @@
<Compile Include="Controls\BusyDecorator\BackgroundVisualHost.cs" />
<Compile Include="Controls\BusyDecorator\BusyDecorator.cs" />
<Compile Include="Controls\BusyDecorator\VisualTargetPresentationSource.cs" />
<Compile Include="Helpers\ShareHelper.cs" />
<Compile Include="PipeServerManager.cs" />
<Compile Include="PluginManager.cs" />
<Compile Include="Plugin\InfoPanel\FileHelper.cs" />

View File

@@ -26,30 +26,12 @@ using System.Windows.Input;
using System.Windows.Threading;
using QuickLook.Common.Helpers;
using QuickLook.Common.Plugin;
using QuickLook.Helpers;
namespace QuickLook
{
public partial class ViewerWindow
{
internal void RunWith(string with, string arg)
{
if (string.IsNullOrEmpty(_path))
return;
try
{
Process.Start(new ProcessStartInfo(with)
{
Arguments = arg,
WorkingDirectory = Path.GetDirectoryName(_path)
});
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
internal void Run()
{
if (string.IsNullOrEmpty(_path))
@@ -288,7 +270,7 @@ namespace QuickLook
internal void Share(object sender, RoutedEventArgs e)
{
RunWith("rundll32.exe", $"shell32.dll,OpenAs_RunDLL {_path}");
ShareHelper.Share(_path, this);
}
}
}

View File

@@ -142,7 +142,7 @@
</Button.Style>
</Button>
<Button DockPanel.Dock="Left" x:Name="buttonShare" Style="{StaticResource CaptionButtonStyle}"
Content="&#xE7AC;" ToolTip="Open With..." />
Content="&#xE72D;" ToolTip="Open With..." />
<Grid x:Name="titleArea" Background="Transparent">
<TextBlock x:Name="titleAreaText" Text="{Binding ContextObject.Title, ElementName=mainWindow}" FontSize="12"
HorizontalAlignment="Left" TextTrimming="CharacterEllipsis"