Made the Win10 share work again, work in progress

This commit is contained in:
Robin Krom 2020-02-06 22:28:33 +01:00
parent ecad3856cf
commit 19b6576311
10 changed files with 619 additions and 178 deletions

View file

@ -11,8 +11,10 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.Contracts" version="10.0.18362.2005" />
<PackageReference Include="System.Reactive" Version="4.3.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\GreenshotPlugin\GreenshotPlugin.csproj" />

View file

@ -1,19 +1,51 @@
using System.IO;
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* 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 1 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.IO;
using Windows.Storage.Streams;
namespace GreenshotWin10Plugin
{
/// <summary>
/// This is an IRandomAccessStream implementation which uses a MemoryStream
/// </summary>
public sealed class MemoryRandomAccessStream : MemoryStream, IRandomAccessStream
{
/// <summary>
/// Default constructor
/// </summary>
public MemoryRandomAccessStream()
{
}
/// <summary>
/// Constructor where also bytes are already passed
/// </summary>
/// <param name="bytes">byte array</param>
public MemoryRandomAccessStream(byte[] bytes)
{
Write(bytes, 0, bytes.Length);
}
/// <inheritdoc />
public IInputStream GetInputStreamAt(ulong position)
{
Seek((long)position, SeekOrigin.Begin);
@ -21,6 +53,7 @@ namespace GreenshotWin10Plugin
return this.AsInputStream();
}
/// <inheritdoc />
public IOutputStream GetOutputStreamAt(ulong position)
{
Seek((long)position, SeekOrigin.Begin);
@ -28,14 +61,17 @@ namespace GreenshotWin10Plugin
return this.AsOutputStream();
}
/// <inheritdoc />
ulong IRandomAccessStream.Position => (ulong)Position;
/// <inheritdoc />
public ulong Size
{
get { return (ulong)Length; }
set { SetLength((long)value); }
}
/// <inheritdoc />
public IRandomAccessStream CloneStream()
{
var cloned = new MemoryRandomAccessStream();
@ -43,23 +79,27 @@ namespace GreenshotWin10Plugin
return cloned;
}
/// <inheritdoc />
public void Seek(ulong position)
{
Seek((long)position, SeekOrigin.Begin);
}
/// <inheritdoc />
public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
{
var inputStream = GetInputStreamAt(0);
return inputStream.ReadAsync(buffer, count, options);
}
/// <inheritdoc />
Windows.Foundation.IAsyncOperation<bool> IOutputStream.FlushAsync()
{
var outputStream = GetOutputStreamAt(0);
return outputStream.FlushAsync();
}
/// <inheritdoc />
public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
{
var outputStream = GetOutputStreamAt(0);

View file

@ -36,12 +36,18 @@ namespace GreenshotWin10Plugin.Native
private readonly IDataTransferManagerInterOp _dataTransferManagerInterOp;
private readonly IntPtr _windowHandle;
/// <summary>
/// The DataTransferManager
/// </summary>
public DataTransferManager DataTransferManager
{
get;
private set;
}
/// <summary>
/// Constructor which takes a handle to initialize
/// </summary>
/// <param name="handle"></param>
public DataTransferManagerHelper(IntPtr handle)
{
//TODO: Add a check for failure here. This will fail for versions of Windows below Windows 10
@ -53,7 +59,7 @@ namespace GreenshotWin10Plugin.Native
_windowHandle = handle;
var riid = new Guid(DataTransferManagerId);
var hresult = _dataTransferManagerInterOp.GetForWindow(_windowHandle, riid, out var dataTransferManager);
if (hresult != 0)
if (hresult.Failed())
{
Log.WarnFormat("HResult for GetForWindow: {0}", hresult);
}
@ -66,7 +72,7 @@ namespace GreenshotWin10Plugin.Native
public void ShowShareUi()
{
var hresult = _dataTransferManagerInterOp.ShowShareUIForWindow(_windowHandle);
if (hresult != 0)
if (hresult.Failed())
{
Log.WarnFormat("HResult for ShowShareUIForWindow: {0}", hresult);
}

View file

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GreenshotWin10Plugin.Native
{
/// <summary>
/// The HRESULT represents Windows error codes
/// See <a href="https://en.wikipedia.org/wiki/HRESULT">wikipedia</a>
/// </summary>
[SuppressMessage("ReSharper", "InconsistentNaming")]
public enum HResult
{
#pragma warning disable 1591
S_OK = 0,
S_FALSE = 1,
E_FAIL = unchecked((int)0x80004005),
E_INVALIDARG = unchecked((int)0x80070057),
E_NOTIMPL = unchecked((int)0x80004001),
E_POINTER = unchecked((int)0x80004003),
E_PENDING = unchecked((int)0x8000000A),
E_NOINTERFACE = unchecked((int)0x80004002),
E_ABORT = unchecked((int)0x80004004),
E_ACCESSDENIED = unchecked((int)0x80070006),
E_HANDLE = unchecked((int)0x80070006),
E_UNEXPECTED = unchecked((int)0x8000FFFF),
E_FILENOTFOUND = unchecked((int)0x80070002),
E_PATHNOTFOUND = unchecked((int)0x80070003),
E_INVALID_DATA = unchecked((int)0x8007000D),
E_OUTOFMEMORY = unchecked((int)0x8007000E),
E_INSUFFICIENT_BUFFER = unchecked((int)0x8007007A),
WSAECONNABORTED = unchecked((int)0x80072745),
WSAECONNRESET = unchecked((int)0x80072746),
ERROR_TOO_MANY_CMDS = unchecked((int)0x80070038),
ERROR_NOT_SUPPORTED = unchecked((int)0x80070032),
TYPE_E_ELEMENTNOTFOUND = unchecked((int)0x8002802B)
#pragma warning restore 1591
}
}

View file

@ -0,0 +1,64 @@
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// 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 1 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.Diagnostics.Contracts;
using System.Runtime.InteropServices;
namespace GreenshotWin10Plugin.Native
{
/// <summary>
/// Extensions to handle the HResult
/// </summary>
public static class HResultExtensions
{
/// <summary>
/// Test if the HResult respresents a fail
/// </summary>
/// <param name="hResult">HResult</param>
/// <returns>bool</returns>
[Pure]
public static bool Failed(this HResult hResult)
{
return hResult < 0;
}
/// <summary>
/// Test if the HResult respresents a success
/// </summary>
/// <param name="hResult">HResult</param>
/// <returns>bool</returns>
[Pure]
public static bool Succeeded(this HResult hResult)
{
return hResult >= HResult.S_OK;
}
/// <summary>
/// Throw an exception on Failure
/// </summary>
/// <param name="hResult">HResult</param>
public static void ThrowOnFailure(this HResult hResult)
{
if (Failed(hResult))
{
throw Marshal.GetExceptionForHR((int)hResult);
}
}
}
}

View file

@ -1,23 +1,21 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* 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 1 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/>.
*/
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// 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 1 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.Runtime.InteropServices;
@ -43,7 +41,7 @@ namespace GreenshotWin10Plugin.Native
/// <param name="pDataTransferManager">The DataTransferManager instance for this window handle</param>
/// <returns>HRESULT</returns>
[PreserveSig]
uint GetForWindow([In] IntPtr appWindow, [In] ref Guid riid, [Out] out DataTransferManager pDataTransferManager);
HResult GetForWindow([In] IntPtr appWindow, [In] ref Guid riid, [Out] out DataTransferManager pDataTransferManager);
/// <summary>
/// Show the share flyout for the window identified by a window handle
@ -51,7 +49,7 @@ namespace GreenshotWin10Plugin.Native
/// <param name="appWindow">The window handle</param>
/// <returns>HRESULT</returns>
[PreserveSig]
uint ShowShareUIForWindow(IntPtr appWindow);
HResult ShowShareUIForWindow(IntPtr appWindow);
}
}

View file

@ -0,0 +1,70 @@
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// 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 1 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 GreenshotPlugin.UnmanagedHelpers;
using System;
namespace Greenshot.Addon.Win10.Native
{
/// <summary>
/// Container for the windows messages
/// </summary>
public class WindowMessageInfo
{
/// <summary>
/// IntPtr with the Handle of the window
/// </summary>
public IntPtr Handle { get; private set; }
/// <summary>
/// WindowsMessages which is the actual message
/// </summary>
public WindowsMessages Message { get; private set; }
/// <summary>
/// IntPtr with the word-param
/// </summary>
public IntPtr WordParam { get; private set; }
/// <summary>
/// IntPtr with the long-param
/// </summary>
public IntPtr LongParam { get; private set; }
/// <summary>
/// Factory method for the Window Message Info
/// </summary>
/// <param name="hwnd">IntPtr with the Handle of the window</param>
/// <param name="msg">WindowsMessages which is the actual message</param>
/// <param name="wParam">IntPtr with the word-param</param>
/// <param name="lParam">IntPtr with the long-param</param>
/// <param name="handled"></param>
/// <returns>WindowMessageInfo</returns>
public static WindowMessageInfo Create(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
return new WindowMessageInfo
{
Handle = hwnd,
Message = (WindowsMessages)msg,
WordParam = wParam,
LongParam = lParam
};
}
}
}

View file

@ -0,0 +1,137 @@
// Greenshot - a free and open source screenshot tool
// Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom
//
// For more information see: http://getgreenshot.org/
// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot
//
// 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 1 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.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Interop;
namespace Greenshot.Addon.Win10.Native
{
/// <summary>
/// A monitor for window messages
/// </summary>
public static class WindowMessageMonitor
{
/// <summary>
/// Create a HwndSource for the specified Window
/// </summary>
/// <param name="window">Window</param>
/// <returns>HwndSource</returns>
private static HwndSource ToHwndSource(this Window window)
{
IntPtr windowHandle = new WindowInteropHelper(window).Handle;
if (windowHandle == IntPtr.Zero)
{
return null;
}
return HwndSource.FromHwnd(windowHandle);
}
/// <summary>
/// Create an observable for the specified window
/// </summary>
public static IObservable<WindowMessageInfo> WindowMessages(this Window window)
{
return WindowMessages(window, null);
}
/// <summary>
/// Create an observable for the specified HwndSource
/// </summary>
public static IObservable<WindowMessageInfo> WindowMessages(this HwndSource hwndSource)
{
return WindowMessages(null, hwndSource);
}
/// <summary>
/// Create an observable for the specified window or HwndSource
/// </summary>
/// <param name="window">Window</param>
/// <param name="hwndSource">HwndSource</param>
/// <returns>IObservable</returns>
private static IObservable<WindowMessageInfo> WindowMessages(Window window, HwndSource hwndSource)
{
if (window == null && hwndSource == null)
{
throw new NotSupportedException("One of Window or HwndSource must be supplied");
}
if (window != null && hwndSource != null)
{
throw new NotSupportedException("Either Window or HwndSource must be supplied");
}
return Observable.Create<WindowMessageInfo>(observer =>
{
// This handles the message, and generates the observable OnNext
IntPtr WindowMessageHandler(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
observer.OnNext(WindowMessageInfo.Create(hwnd, msg, wParam, lParam, ref handled));
// ReSharper disable once AccessToDisposedClosure
if (hwndSource.IsDisposed)
{
observer.OnCompleted();
}
return IntPtr.Zero;
}
void HwndSourceDisposedHandle(object sender, EventArgs e)
{
observer.OnCompleted();
}
void RegisterHwndSource()
{
hwndSource.Disposed += HwndSourceDisposedHandle;
hwndSource.AddHook(WindowMessageHandler);
}
if (window != null)
{
hwndSource = window.ToHwndSource();
}
if (hwndSource != null)
{
RegisterHwndSource();
}
else
{
// No, try to get it later
window.SourceInitialized += (sender, args) =>
{
hwndSource = window.ToHwndSource();
RegisterHwndSource();
};
}
return Disposable.Create(() =>
{
hwndSource.Disposed -= HwndSourceDisposedHandle;
hwndSource.RemoveHook(WindowMessageHandler);
hwndSource.Dispose();
});
})
// Make sure there is always a value produced when connecting
.Publish()
.RefCount();
}
}
}

View file

@ -31,7 +31,6 @@ namespace GreenshotWin10Plugin
/// </summary>
public class Win10Plugin : IGreenshotPlugin
{
public void Dispose()
{
Dispose(true);

View file

@ -21,15 +21,21 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
using Windows.Storage.Streams;
using Greenshot.Addon.Win10.Native;
using Color = Windows.UI.Color;
using System.Reactive.Linq;
using GreenshotPlugin.UnmanagedHelpers;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using GreenshotWin10Plugin.Native;
using System.Threading.Tasks;
using Windows.Storage;
using Color = Windows.UI.Color;
using System.Collections.Generic;
using System.Drawing;
using GreenshotWin10Plugin.Native;
namespace GreenshotWin10Plugin
{
@ -48,6 +54,20 @@ namespace GreenshotWin10Plugin
/// </summary>
public override Image DisplayIcon => PluginUtils.GetCachedExeIcon(FilenameHelper.FillCmdVariables(@"%windir%\system32\shell32.dll"), 238);
private class ShareInfo
{
public string ApplicationName { get; set; }
public bool AreShareProvidersRequested { get; set; }
public bool IsDeferredFileCreated { get; set; }
public DataPackageOperation CompletedWithOperation { get; set; }
public string AcceptedFormat { get; set; }
public bool IsDestroyed { get; set; }
public bool IsShareCompleted { get; set; }
public TaskCompletionSource<bool> ShareTask { get; } = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
public bool IsDataRequested { get; set; }
}
/// <summary>
/// Share the screenshot with a windows app
/// </summary>
@ -60,108 +80,43 @@ namespace GreenshotWin10Plugin
var exportInformation = new ExportInformation(Designation, Description);
try
{
var handle = PluginUtils.Host.GreenshotForm.Handle;
var exportTarget = Task.Run(async () =>
var triggerWindow = new Window
{
var taskCompletionSource = new TaskCompletionSource<string>();
using var imageStream = new MemoryRandomAccessStream();
using var logoStream = new MemoryRandomAccessStream();
using var thumbnailStream = new MemoryRandomAccessStream();
var outputSettings = new SurfaceOutputSettings();
outputSettings.PreventGreenshotFormat();
// Create capture for export
ImageOutput.SaveToStream(surface, imageStream, outputSettings);
imageStream.Position = 0;
Log.Info("Created RandomAccessStreamReference for the image");
var imageRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(imageStream);
RandomAccessStreamReference thumbnailRandomAccessStreamReference;
RandomAccessStreamReference logoRandomAccessStreamReference;
// Create thumbnail
using (var tmpImageForThumbnail = surface.GetImageForExport())
{
using var thumbnail = ImageHelper.CreateThumbnail(tmpImageForThumbnail, 240, 160);
ImageOutput.SaveToStream(thumbnail, null, thumbnailStream, outputSettings);
thumbnailStream.Position = 0;
thumbnailRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(thumbnailStream);
Log.Info("Created RandomAccessStreamReference for the thumbnail");
}
// Create logo
using (var logo = GreenshotResources.getGreenshotIcon().ToBitmap())
{
using var logoThumbnail = ImageHelper.CreateThumbnail(logo, 30, 30);
ImageOutput.SaveToStream(logoThumbnail, null, logoStream, outputSettings);
logoStream.Position = 0;
logoRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(logoStream);
Log.Info("Created RandomAccessStreamReference for the logo");
}
string applicationName = null;
var dataTransferManagerHelper = new DataTransferManagerHelper(handle);
dataTransferManagerHelper.DataTransferManager.TargetApplicationChosen += (dtm, args) =>
{
Log.InfoFormat("Trying to share with {0}", args.ApplicationName);
applicationName = args.ApplicationName;
WindowState = WindowState.Normal,
WindowStartupLocation = WindowStartupLocation.CenterScreen,
Width = 400,
Height = 400
};
var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails);
var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest =>
{
// Information on how was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/
Log.DebugFormat("Creating deferred file {0}", filename);
try
{
using (var deferredStream = streamedFileDataRequest.AsStreamForWrite())
{
await imageStream.CopyToAsync(deferredStream).ConfigureAwait(false);
await imageStream.FlushAsync().ConfigureAwait(false);
}
// Signal that the stream is ready
streamedFileDataRequest.Dispose();
}
catch (Exception)
{
streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete);
}
// Signal transfer ready to the await down below
taskCompletionSource.TrySetResult(applicationName);
}, imageRandomAccessStreamReference).AsTask().ConfigureAwait(false);
dataTransferManagerHelper.DataTransferManager.DataRequested += (sender, args) =>
triggerWindow.Show();
var shareInfo = new ShareInfo();
// This is a bad trick, but don't know how else to do it.
// Wait for the focus to return, and depending on the state close the window!
triggerWindow.WindowMessages()
.Where(m => m.Message == WindowsMessages.WM_SETFOCUS).Delay(TimeSpan.FromSeconds(1))
.Subscribe(info =>
{
var deferral = args.Request.GetDeferral();
args.Request.Data.OperationCompleted += (dp, eventArgs) =>
if (shareInfo.ApplicationName != null)
{
Log.DebugFormat("OperationCompleted: {0}, shared with", eventArgs.Operation);
taskCompletionSource.TrySetResult(applicationName);
};
var dataPackage = args.Request.Data;
dataPackage.Properties.Title = captureDetails.Title;
dataPackage.Properties.ApplicationName = "Greenshot";
dataPackage.Properties.Description = "Share a screenshot";
dataPackage.Properties.Thumbnail = thumbnailRandomAccessStreamReference;
dataPackage.Properties.Square30x30Logo = logoRandomAccessStreamReference;
dataPackage.Properties.LogoBackgroundColor = Color.FromArgb(0xff, 0x3d, 0x3d, 0x3d);
dataPackage.SetStorageItems(new List<IStorageItem> { storageFile });
dataPackage.SetBitmap(imageRandomAccessStreamReference);
dataPackage.Destroyed += (dp, o) =>
{
Log.Debug("Destroyed.");
};
deferral.Complete();
};
dataTransferManagerHelper.ShowShareUi();
return await taskCompletionSource.Task.ConfigureAwait(false);
}).Result;
if (string.IsNullOrWhiteSpace(exportTarget))
return;
}
shareInfo.ShareTask.TrySetResult(false);
});
var windowHandle = new WindowInteropHelper(triggerWindow).Handle;
Share(shareInfo, windowHandle, surface, captureDetails).GetAwaiter().GetResult();
Log.Debug("Sharing finished, closing window.");
triggerWindow.Close();
if (string.IsNullOrWhiteSpace(shareInfo.ApplicationName))
{
exportInformation.ExportMade = false;
}
else
{
exportInformation.ExportMade = true;
exportInformation.DestinationDescription = exportTarget;
exportInformation.DestinationDescription = shareInfo.ApplicationName;
}
}
catch (Exception ex)
@ -174,5 +129,133 @@ namespace GreenshotWin10Plugin
return exportInformation;
}
/// <summary>
/// Share the surface by using the Share-UI of Windows 10
/// </summary>
/// <param name="shareInfo">ShareInfo</param>
/// <param name="handle">IntPtr with the handle for the hosting window</param>
/// <param name="surface">ISurface with the bitmap to share</param>
/// <param name="captureDetails">ICaptureDetails</param>
/// <returns>Task with string, which describes the application which was used to share with</returns>
private async Task Share(ShareInfo shareInfo, IntPtr handle, ISurface surface, ICaptureDetails captureDetails)
{
using (var imageStream = new MemoryRandomAccessStream())
using (var logoStream = new MemoryRandomAccessStream())
using (var thumbnailStream = new MemoryRandomAccessStream())
{
var outputSettings = new SurfaceOutputSettings();
outputSettings.PreventGreenshotFormat();
// Create capture for export
ImageOutput.SaveToStream(surface, imageStream, outputSettings);
imageStream.Position = 0;
Log.Debug("Created RandomAccessStreamReference for the image");
var imageRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(imageStream);
// Create thumbnail
RandomAccessStreamReference thumbnailRandomAccessStreamReference;
using (var tmpImageForThumbnail = surface.GetImageForExport())
using (var thumbnail = ImageHelper.CreateThumbnail(tmpImageForThumbnail, 240, 160))
{
ImageOutput.SaveToStream(thumbnail, null, thumbnailStream, outputSettings);
thumbnailStream.Position = 0;
thumbnailRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(thumbnailStream);
Log.Debug("Created RandomAccessStreamReference for the thumbnail");
}
// Create logo
RandomAccessStreamReference logoRandomAccessStreamReference;
using (var logo = GreenshotResources.getGreenshotIcon().ToBitmap())
using (var logoThumbnail = ImageHelper.CreateThumbnail(logo, 30, 30))
{
ImageOutput.SaveToStream(logoThumbnail, null, logoStream, outputSettings);
logoStream.Position = 0;
logoRandomAccessStreamReference = RandomAccessStreamReference.CreateFromStream(logoStream);
Log.Info("Created RandomAccessStreamReference for the logo");
}
var dataTransferManagerHelper = new DataTransferManagerHelper(handle);
dataTransferManagerHelper.DataTransferManager.ShareProvidersRequested += (sender, args) =>
{
shareInfo.AreShareProvidersRequested = true;
Log.DebugFormat("Share providers requested: {0}", string.Join(",", args.Providers.Select(p => p.Title)));
};
dataTransferManagerHelper.DataTransferManager.TargetApplicationChosen += (dtm, args) =>
{
shareInfo.ApplicationName = args.ApplicationName;
Log.DebugFormat("TargetApplicationChosen: {0}", args.ApplicationName);
};
var filename = FilenameHelper.GetFilename(OutputFormat.png, captureDetails);
var storageFile = await StorageFile.CreateStreamedFileAsync(filename, async streamedFileDataRequest =>
{
shareInfo.IsDeferredFileCreated = true;
// Information on the "how" was found here: https://socialeboladev.wordpress.com/2013/03/15/how-to-use-createstreamedfileasync/
Log.DebugFormat("Creating deferred file {0}", filename);
try
{
using (var deferredStream = streamedFileDataRequest.AsStreamForWrite())
{
await imageStream.CopyToAsync(deferredStream).ConfigureAwait(false);
await imageStream.FlushAsync().ConfigureAwait(false);
}
// Signal that the stream is ready
streamedFileDataRequest.Dispose();
// Signal that the action is ready, bitmap was exported
shareInfo.ShareTask.TrySetResult(true);
}
catch (Exception)
{
streamedFileDataRequest.FailAndClose(StreamedFileFailureMode.Incomplete);
}
}, imageRandomAccessStreamReference).AsTask().ConfigureAwait(false);
dataTransferManagerHelper.DataTransferManager.DataRequested += (dataTransferManager, dataRequestedEventArgs) =>
{
var deferral = dataRequestedEventArgs.Request.GetDeferral();
try
{
shareInfo.IsDataRequested = true;
Log.DebugFormat("DataRequested with operation {0}", dataRequestedEventArgs.Request.Data.RequestedOperation);
var dataPackage = dataRequestedEventArgs.Request.Data;
dataPackage.OperationCompleted += (dp, eventArgs) =>
{
Log.DebugFormat("OperationCompleted: {0}, shared with", eventArgs.Operation);
shareInfo.CompletedWithOperation = eventArgs.Operation;
shareInfo.AcceptedFormat = eventArgs.AcceptedFormatId;
shareInfo.ShareTask.TrySetResult(true);
};
dataPackage.Destroyed += (dp, o) =>
{
shareInfo.IsDestroyed = true;
Log.Debug("Destroyed");
shareInfo.ShareTask.TrySetResult(true);
};
dataPackage.ShareCompleted += (dp, shareCompletedEventArgs) =>
{
shareInfo.IsShareCompleted = true;
Log.Debug("ShareCompleted");
shareInfo.ShareTask.TrySetResult(true);
};
dataPackage.Properties.Title = captureDetails.Title;
dataPackage.Properties.ApplicationName = "Greenshot";
dataPackage.Properties.Description = "Share a screenshot";
dataPackage.Properties.Thumbnail = thumbnailRandomAccessStreamReference;
dataPackage.Properties.Square30x30Logo = logoRandomAccessStreamReference;
dataPackage.Properties.LogoBackgroundColor = Color.FromArgb(0xff, 0x3d, 0x3d, 0x3d);
dataPackage.SetStorageItems(new[] {storageFile});
dataPackage.SetBitmap(imageRandomAccessStreamReference);
}
finally
{
deferral.Complete();
Log.Debug("Called deferral.Complete()");
}
};
dataTransferManagerHelper.ShowShareUi();
Log.Debug("ShowShareUi finished.");
await shareInfo.ShareTask.Task.ConfigureAwait(false);
}
}
}
}