From 25050482441d9f871218d6cb22d1dd6d6744474c Mon Sep 17 00:00:00 2001 From: Ariel De Los Santos Date: Tue, 5 Sep 2023 16:49:59 -0400 Subject: [PATCH] fix(share): fix share button unresponsive on Overview for iOS and Android --- .../Controls/SamplePageLayout.cs | 22 ++++++-- .../Helpers/DataTransferManagerHelper.cs | 53 +++++++++++++++++++ .../SamplePages/SharingSamplePage.xaml.cs | 44 +-------------- .../Views/Styles/SamplePageLayout.xaml | 2 +- 4 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 Uno.Gallery/Uno.Gallery.Shared/Helpers/DataTransferManagerHelper.cs diff --git a/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs b/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs index 4ea485da7..5c6fc2636 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/Controls/SamplePageLayout.cs @@ -12,6 +12,8 @@ using Microsoft.UI.Xaml.Data; using Microsoft.UI.Xaml.Documents; using Microsoft.UI.Xaml.Media; +using Windows.ApplicationModel.DataTransfer; + namespace Uno.Gallery { @@ -166,10 +168,22 @@ void OnScrolled(object sender, ScrollViewerViewChangedEventArgs e) private void OnShareClicked(Hyperlink sender, HyperlinkClickEventArgs args) { -#if (__IOS__ || __ANDROID__) && !NET6_0_OR_GREATER - var sample = DataContext as Sample; - _ = Deeplinking.BranchService.Instance.ShareSample(sample, _design); -#endif + if (DataTransferManager.IsSupported()) + { + var dataTransferManager = DataTransferManagerHelper.GetForCurrentView(); + dataTransferManager.DataRequested += DataRequested_URI; + DataTransferManagerHelper.ShowShareUI(); + } + } + + private void DataRequested_URI(DataTransferManager sender, DataRequestedEventArgs args) + { + args.Request.Data.Properties.Title = "Uno Gallery - Share-Sample Title"; + args.Request.Data.Properties.Description = "See this awesome project:"; + + args.Request.Data.SetWebLink(new Uri("https://gallery.platform.uno/")); + + sender.DataRequested -= DataRequested_URI; } /// diff --git a/Uno.Gallery/Uno.Gallery.Shared/Helpers/DataTransferManagerHelper.cs b/Uno.Gallery/Uno.Gallery.Shared/Helpers/DataTransferManagerHelper.cs new file mode 100644 index 000000000..28146bb03 --- /dev/null +++ b/Uno.Gallery/Uno.Gallery.Shared/Helpers/DataTransferManagerHelper.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +using System.Text; +using Windows.ApplicationModel.DataTransfer; +using WinRT.Interop; +using WinRT; + +namespace Uno.Gallery.Helpers +{ + public static class DataTransferManagerHelper + { + private static readonly Guid _dtm_iid = new Guid("a5caee9b-8708-49d1-8d36-67d25a8da00c"); + +#if WINDOWS + static IDataTransferManagerInterop DataTransferManagerInterop => DataTransferManager.As(); +#endif + + public static DataTransferManager GetForCurrentView() + { +#if WINDOWS + IntPtr result; + var hwnd = WindowNative.GetWindowHandle(App.Instance.MainWindow); + result = DataTransferManagerInterop.GetForWindow(hwnd, _dtm_iid); + DataTransferManager dataTransferManager = MarshalInterface.FromAbi(result); + return (dataTransferManager); +#else + return DataTransferManager.GetForCurrentView(); +#endif + } + + public static void ShowShareUI(ShareUIOptions options = null) + { +#if WINDOWS + var hwnd = WindowNative.GetWindowHandle(App.Instance.MainWindow); + DataTransferManagerInterop.ShowShareUIForWindow(hwnd, options); +#else + DataTransferManager.ShowShareUI(options); +#endif + } + +#if WINDOWS + [ComImport] + [Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + public interface IDataTransferManagerInterop + { + IntPtr GetForWindow([In] IntPtr appWindow, [In] ref Guid riid); + void ShowShareUIForWindow(IntPtr appWindow, ShareUIOptions options); + } +#endif + } +} diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/SharingSamplePage.xaml.cs b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/SharingSamplePage.xaml.cs index d17955dc6..f758aceec 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/SharingSamplePage.xaml.cs +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/SamplePages/SharingSamplePage.xaml.cs @@ -6,6 +6,7 @@ using System.Runtime.InteropServices; using WinRT; using WinRT.Interop; +using Uno.Gallery.Helpers; namespace Uno.Gallery.Views.Samples { @@ -133,47 +134,4 @@ private void DataRequested_TextLight(DataTransferManager sender, DataRequestedEv sender.DataRequested -= DataRequested_TextLight; } } - - public static class DataTransferManagerHelper - { - private static readonly Guid _dtm_iid = new Guid("a5caee9b-8708-49d1-8d36-67d25a8da00c"); - -#if WINDOWS - static IDataTransferManagerInterop DataTransferManagerInterop => DataTransferManager.As(); -#endif - - public static DataTransferManager GetForCurrentView() - { -#if WINDOWS - IntPtr result; - var hwnd = WindowNative.GetWindowHandle(App.Instance.MainWindow); - result = DataTransferManagerInterop.GetForWindow(hwnd, _dtm_iid); - DataTransferManager dataTransferManager = MarshalInterface.FromAbi(result); - return (dataTransferManager); -#else - return DataTransferManager.GetForCurrentView(); -#endif - } - - public static void ShowShareUI(ShareUIOptions options = null) - { -#if WINDOWS - var hwnd = WindowNative.GetWindowHandle(App.Instance.MainWindow); - DataTransferManagerInterop.ShowShareUIForWindow(hwnd, options); -#else - DataTransferManager.ShowShareUI(options); -#endif - } - -#if WINDOWS - [ComImport] - [Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")] - [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] - public interface IDataTransferManagerInterop - { - IntPtr GetForWindow([In] IntPtr appWindow, [In] ref Guid riid); - void ShowShareUIForWindow(IntPtr appWindow, ShareUIOptions options); - } -#endif - } } diff --git a/Uno.Gallery/Uno.Gallery.Shared/Views/Styles/SamplePageLayout.xaml b/Uno.Gallery/Uno.Gallery.Shared/Views/Styles/SamplePageLayout.xaml index 58a52787d..240221b96 100644 --- a/Uno.Gallery/Uno.Gallery.Shared/Views/Styles/SamplePageLayout.xaml +++ b/Uno.Gallery/Uno.Gallery.Shared/Views/Styles/SamplePageLayout.xaml @@ -293,7 +293,7 @@ Style="{StaticResource BodyMedium}"> - +