From 478f174e14470168bd6c322f39959279d9b360f1 Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Thu, 11 Apr 2024 12:48:38 +0100 Subject: [PATCH 1/2] [Catalyst,Windows] Allow drag item from outside the app (#21684) * [Catalyst] Allow drag item from outside the app * [Windows]Allow DropEvent to work from outside drag * [samples] Add sample for drop to OS * [sample] Small cleanup * Update DropFileToMauiApp.xaml.cs --------- Co-authored-by: Rui Marinho --- .../Controls.Sample.Sandbox/MauiProgram.cs | 2 + .../Pages/Core/DropFileToMauiApp.xaml | 15 +++ .../Pages/Core/DropFileToMauiApp.xaml.cs | 109 ++++++++++++++++++ .../ViewModels/GesturesViewModel.cs | 2 + .../GesturePlatformManager.Windows.cs | 5 +- .../Core/Platform/iOS/DragAndDropDelegate.cs | 13 +-- 6 files changed, 135 insertions(+), 11 deletions(-) create mode 100644 src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml create mode 100644 src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml.cs diff --git a/src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs b/src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs index c4082431bc46..3472acaffa8f 100644 --- a/src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs +++ b/src/Controls/samples/Controls.Sample.Sandbox/MauiProgram.cs @@ -10,7 +10,9 @@ public static class MauiProgram public static MauiApp CreateMauiApp() => MauiApp .CreateBuilder() +#if __ANDROID__ || __IOS__ .UseMauiMaps() +#endif .UseMauiApp() .Build(); } diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml b/src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml new file mode 100644 index 000000000000..24fb33b2f00f --- /dev/null +++ b/src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml.cs b/src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml.cs new file mode 100644 index 000000000000..f012ae60c479 --- /dev/null +++ b/src/Controls/samples/Controls.Sample/Pages/Core/DropFileToMauiApp.xaml.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Maui; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Graphics; + +#if WINDOWS +using Windows.ApplicationModel.DataTransfer; +using Windows.Storage; +#endif + +#if IOS || MACCATALYST +using UIKit; +using Foundation; +#endif + +namespace Maui.Controls.Sample.Pages +{ + public partial class DropFileToMauiApp + { + + public DropFileToMauiApp() + { + InitializeComponent(); + } + + void DropGestureDragLeave(object? sender, DragEventArgs e) + { + + } + + async void DropGestureDrop(object? sender, DropEventArgs e) + { + var filePaths = new List(); + +#if WINDOWS + if (e.PlatformArgs is not null && e.PlatformArgs.DragEventArgs.DataView.Contains(StandardDataFormats.StorageItems)) + { + var items = await e.PlatformArgs.DragEventArgs.DataView.GetStorageItemsAsync(); + if (items.Any()) + { + foreach (var item in items) + { + if (item is StorageFile file) + { + filePaths.Add(item.Path); + } + } + + } + } +#elif MACCATALYST + + var session = e.PlatformArgs?.DropSession; + if (session == null) + { + return; + } + foreach (UIDragItem item in session.Items) + { + var result = await LoadItemAsync(item.ItemProvider, item.ItemProvider.RegisteredTypeIdentifiers.ToList()); + if (result is not null) + { + filePaths.Add(result.FileUrl?.Path!); + } + } + foreach (var item in filePaths) + { + Debug.WriteLine($"Path: {item}"); + } + + static async Task LoadItemAsync(NSItemProvider itemProvider, List typeIdentifiers) + { + if (typeIdentifiers is null || typeIdentifiers.Count == 0) + { + return null; + } + + var typeIdent = typeIdentifiers.First(); + + if (itemProvider.HasItemConformingTo(typeIdent)) + { + return await itemProvider.LoadInPlaceFileRepresentationAsync(typeIdent); + } + + typeIdentifiers.Remove(typeIdent); + + return await LoadItemAsync(itemProvider, typeIdentifiers); + } +#else + await Task.CompletedTask; +#endif + + lblPath.Text = filePaths.FirstOrDefault(); + } + + void DropGestureDragOver(object? sender, DragEventArgs e) + { + Debug.WriteLine($"Dragging {e.Data?.Text}, {e.Data?.Image}"); + } + + } +} \ No newline at end of file diff --git a/src/Controls/samples/Controls.Sample/ViewModels/GesturesViewModel.cs b/src/Controls/samples/Controls.Sample/ViewModels/GesturesViewModel.cs index 0f9706ff5b26..b11e9270b554 100644 --- a/src/Controls/samples/Controls.Sample/ViewModels/GesturesViewModel.cs +++ b/src/Controls/samples/Controls.Sample/ViewModels/GesturesViewModel.cs @@ -11,6 +11,8 @@ protected override IEnumerable CreateItems() => new[] { new SectionModel(typeof(DragAndDropBetweenLayouts), "Drag And Drop", "Drag and Drop Views."), + new SectionModel(typeof(DropFileToMauiApp), "Drag and Drop file from OS", + "Drop File to App"), new SectionModel(typeof(PanGestureGallery), "Pan Gesture", "Pan Gesture."), new SectionModel(typeof(PinchGestureTestPage), "Pinch Gesture", diff --git a/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs b/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs index a59ff797e33a..b19e27b47792 100644 --- a/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs +++ b/src/Controls/src/Core/Platform/GestureManager/GesturePlatformManager.Windows.cs @@ -202,10 +202,7 @@ void HandleDrop(object sender, Microsoft.UI.Xaml.DragEventArgs e) element = ve; } - if (datapackage is null) - return; - - var args = new DropEventArgs(datapackage.View, (relativeTo) => GetPosition(relativeTo, e), new PlatformDropEventArgs(sender as UIElement, e)); + var args = new DropEventArgs(datapackage?.View, (relativeTo) => GetPosition(relativeTo, e), new PlatformDropEventArgs(sender as UIElement, e)); SendEventArgs(async rec => { if (!rec.AllowDrop) diff --git a/src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs b/src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs index 39ddc650ac34..4cf13cde1812 100644 --- a/src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs +++ b/src/Controls/src/Core/Platform/iOS/DragAndDropDelegate.cs @@ -90,15 +90,14 @@ public UIDropProposal SessionDidUpdate(UIDropInteraction interaction, IUIDropSes { UIDropOperation operation = UIDropOperation.Cancel; - if (session.LocalDragSession == null) - return new UIDropProposal(operation); - DataPackage package = null; - - if (session.LocalDragSession.Items.Length > 0 && - session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi) + if (session.LocalDragSession != null) { - package = cdi.DataPackage; + if (session.LocalDragSession.Items.Length > 0 && + session.LocalDragSession.Items[0].LocalObject is CustomLocalStateData cdi) + { + package = cdi.DataPackage; + } } var platformArgs = new PlatformDragEventArgs(_viewHandler.PlatformView, interaction, session); From 01e72e2ae85b2304b70c354f87d910c509f24593 Mon Sep 17 00:00:00 2001 From: Gerald Versluis Date: Thu, 11 Apr 2024 18:18:52 +0200 Subject: [PATCH 2/2] Add s/triaged label for issues opened by core team (#21775) --- .github/policies/resourceManagement.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/policies/resourceManagement.yml b/.github/policies/resourceManagement.yml index 8a1dbe199e2b..39ed64b00b3b 100644 --- a/.github/policies/resourceManagement.yml +++ b/.github/policies/resourceManagement.yml @@ -579,5 +579,15 @@ configuration: - removeLabel: label: s/try-latest-version description: Remove 's/try-latest-version' when new reply from author comes in + - if: + - payloadType: Issues + - activitySenderHasPermission: + permission: Write + - isAction: + action: Opened + then: + - addLabel: + label: s/triaged + description: Add 's/triaged' label to issues opened by the core team, we assume these issues do not need triaging onFailure: onSuccess: