Skip to content

Commit

Permalink
feat: DataTransferManager support on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 29, 2021
1 parent 8d2dabf commit 09b4ad0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#nullable enable

#if __WASM__ || __IOS__ || __ANDROID__
#if __WASM__ || __IOS__ || __ANDROID__ || __SKIA__ || __MACOS__
using System;
using Windows.Foundation;
using Uno.Logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<bool> 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<NSObject>();

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<bool>();

picker.DidChooseSharingService += (s, e) =>
{
completionSource.SetResult(e.Service != null);
};

picker.ShowRelativeToRect(rect, view, NSRectEdge.MinYEdge);

return await completionSource.Task;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if !__WASM__ && !__IOS__ && !__ANDROID__
#if !__WASM__ && !__IOS__ && !__ANDROID__ && !__SKIA__ && !__MACOS__

namespace Windows.ApplicationModel.DataTransfer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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()
{
Expand Down

0 comments on commit 09b4ad0

Please sign in to comment.