-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DataTransferManager support on macOS
- Loading branch information
1 parent
8d2dabf
commit 09b4ad0
Showing
4 changed files
with
67 additions
and
4 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.macOS.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Uno.UWP/ApplicationModel/DataTransfer/DataTransferManager.other.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters