From 09b4ad0fb029d646210f0652739fff9b30fdb0ea Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Sun, 24 Jan 2021 23:31:09 +0100 Subject: [PATCH] feat: DataTransferManager support on macOS --- .../DataTransfer/DataTransferManager.cs | 2 +- .../DataTransfer/DataTransferManager.macOS.cs | 63 +++++++++++++++++++ .../DataTransfer/DataTransferManager.other.cs | 2 +- .../DataTransferManager.cs | 4 +- 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.macOS.cs diff --git a/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs b/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs index 0733a431794b..3d38d93b0d94 100644 --- a/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs +++ b/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs @@ -1,6 +1,6 @@ #nullable enable -#if __WASM__ || __IOS__ || __ANDROID__ +#if __WASM__ || __IOS__ || __ANDROID__ || __SKIA__ || __MACOS__ using System; using Windows.Foundation; using Uno.Logging; diff --git a/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.macOS.cs b/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.macOS.cs new file mode 100644 index 000000000000..5f11893a6493 --- /dev/null +++ b/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.macOS.cs @@ -0,0 +1,63 @@ +#nullable enable + +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AppKit; +using CoreGraphics; +using Foundation; +using Windows.Foundation; + +namespace Windows.ApplicationModel.DataTransfer +{ + public partial class DataTransferManager + { + public static bool IsSupported() => true; + + private static async Task ShowShareUIAsync(ShareUIOptions options, DataPackage dataPackage) + { + var window = NSApplication.SharedApplication.MainWindow; + + if (window == null) + { + throw new InvalidOperationException("Sharing is not possible when no window is active."); + } + + var view = window.ContentView; + + var dataPackageView = dataPackage.GetView(); + + var sharedData = new List(); + + var title = dataPackage.Properties.Title ?? string.Empty; + + if (dataPackageView.Contains(StandardDataFormats.Text)) + { + var text = await dataPackageView.GetTextAsync(); + sharedData.Add(new NSString(text)); + } + + var uri = await GetSharedUriAsync(dataPackageView); + if (uri != null) + { + sharedData.Add(NSUrl.FromString(uri.ToString())); + } + + CGRect rect = options.SelectionRect ?? Rect.Empty; + rect.Y = view.Bounds.Height - rect.Bottom; + + var picker = new NSSharingServicePicker(sharedData.ToArray()); + + var completionSource = new TaskCompletionSource(); + + picker.DidChooseSharingService += (s, e) => + { + completionSource.SetResult(e.Service != null); + }; + + picker.ShowRelativeToRect(rect, view, NSRectEdge.MinYEdge); + + return await completionSource.Task; + } + } +} diff --git a/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs b/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs index d69dd37bc346..275ca37d839c 100644 --- a/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs +++ b/src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs @@ -1,4 +1,4 @@ -#if !__WASM__ && !__IOS__ && !__ANDROID__ +#if !__WASM__ && !__IOS__ && !__ANDROID__ && !__SKIA__ && !__MACOS__ namespace Windows.ApplicationModel.DataTransfer { diff --git a/src/Uno.UWP/Generated/3.0.0.0/Windows.ApplicationModel.DataTransfer/DataTransferManager.cs b/src/Uno.UWP/Generated/3.0.0.0/Windows.ApplicationModel.DataTransfer/DataTransferManager.cs index bf0722d20efc..ac99297cd0fc 100644 --- a/src/Uno.UWP/Generated/3.0.0.0/Windows.ApplicationModel.DataTransfer/DataTransferManager.cs +++ b/src/Uno.UWP/Generated/3.0.0.0/Windows.ApplicationModel.DataTransfer/DataTransferManager.cs @@ -13,7 +13,7 @@ public partial class DataTransferManager // Forced skipping of method Windows.ApplicationModel.DataTransfer.DataTransferManager.TargetApplicationChosen.remove // Forced skipping of method Windows.ApplicationModel.DataTransfer.DataTransferManager.ShareProvidersRequested.add // Forced skipping of method Windows.ApplicationModel.DataTransfer.DataTransferManager.ShareProvidersRequested.remove - #if false || false || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__ + #if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false [global::Uno.NotImplemented("NET461", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")] public static void ShowShareUI( global::Windows.ApplicationModel.DataTransfer.ShareUIOptions options) { @@ -27,7 +27,7 @@ public static bool IsSupported() throw new global::System.NotImplementedException("The member bool DataTransferManager.IsSupported() is not implemented in Uno."); } #endif - #if false || false || NET461 || false || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__ + #if false || false || NET461 || false || false || __NETSTD_REFERENCE__ || false [global::Uno.NotImplemented("NET461", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")] public static void ShowShareUI() {