-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add share sheet support #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 11 commits
25eba75
ab23884
e187a0c
65ebd32
0d670fe
806dac4
f328fe2
a163654
fb2d9a2
b83d03d
7b40965
5558831
7935a17
72fc53e
554a224
f1e7175
1b76eb0
91f28b3
424f146
32a62fc
11ddece
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| using Microsoft.UI; | ||
| using Microsoft.UI.Windowing; | ||
| using System; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using Windows.ApplicationModel.DataTransfer; | ||
| using Windows.Win32; | ||
|
|
||
| namespace WinUIEx | ||
|
|
@@ -121,7 +123,7 @@ private static void UpdateOverlappedPresenter(this Microsoft.UI.Xaml.Window wind | |
| var appwindow = window.AppWindow; | ||
| if (appwindow.Presenter is OverlappedPresenter overlapped) | ||
| action(overlapped); | ||
| else | ||
| else | ||
| throw new NotSupportedException($"Not supported with a {appwindow.Presenter.Kind} presenter"); | ||
| } | ||
| private static T GetOverlappedPresenterValue<T>(this Microsoft.UI.Xaml.Window window, Func<OverlappedPresenter?,T> action) | ||
|
|
@@ -361,7 +363,7 @@ public static void SetWindowChromaKey(this Microsoft.UI.Xaml.Window window, Wind | |
| /// <param name="window">window</param> | ||
| /// <param name="chromaKey">The color that specifies the transparency color key to be used when composing the layered window. All pixels painted by the window in this color will be transparent.</param> | ||
| /// <param name="alpha">Alpha value used to describe the opacity of the layered window. When <paramref name="alpha"/> is 0, the window is completely transparent. When <paramref name="alpha"/> is 255, the window is opaque.</param> | ||
| public static void SetLayeredWindowAttributes(this Microsoft.UI.Xaml.Window window, Windows.UI.Color chromaKey, byte alpha) | ||
| public static void SetLayeredWindowAttributes(this Microsoft.UI.Xaml.Window window, Windows.UI.Color chromaKey, byte alpha) | ||
| => HwndExtensions.SetLayeredWindowAttributes(GetWindowHandle(window), chromaKey.R, chromaKey.G, chromaKey.B, alpha);*/ | ||
|
|
||
| /// <summary> | ||
|
|
@@ -385,5 +387,37 @@ public static void SetRegion(this Microsoft.UI.Xaml.Window window, Region? regio | |
| PInvoke.DeleteObject(rgn); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the <see cref="DataTransferManager"/> for the specified window. | ||
| /// </summary> | ||
| /// <param name="window">The window for which to get the <see cref="DataTransferManager"/>.</param> | ||
| /// <returns>The <see cref="DataTransferManager"/> associated with the specified window.</returns> | ||
| public static DataTransferManager GetDataTransferManager(this Microsoft.UI.Xaml.Window window) => HwndExtensions.GetDataTransferManagerForWindow(window.GetWindowHandle()); | ||
|
|
||
| /// <summary> | ||
| /// Displays the Windows Share UI for the specified window. | ||
| /// </summary> | ||
| /// <param name="window">The window for which to display the Share UI.</param> | ||
| public static void ShowShareUI(this Microsoft.UI.Xaml.Window window) => HwndExtensions.ShowShareUIForWindow(window.GetWindowHandle()); | ||
|
|
||
| /// <summary> | ||
| /// A convenience method that displays the Windows Share UI with the specified <see cref="DataPackage"/>. | ||
| /// </summary> | ||
| /// <param name="window">The window for which to display the Share UI.</param> | ||
| /// <param name="data">The <see cref="DataPackage"/> to share.</param> | ||
| public static void Share(this Microsoft.UI.Xaml.Window window, DataPackage data) | ||
| { | ||
| var dtm = window.GetDataTransferManager(); | ||
|
|
||
| void handler(DataTransferManager sender, DataRequestedEventArgs args) | ||
| { | ||
| dtm.DataRequested -= handler; | ||
| args.Request.Data = data; | ||
| } | ||
|
|
||
| dtm.DataRequested += handler; | ||
| window.ShowShareUI(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could there be a case here where the dtm/handler goes out of scope and gets garbage collected before the event fires?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that won't happen because we are marshalling a COM object, and it is stable per window. Also, please correct me if I'm wrong, since we are keeping a reference of
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Is there a case where the handler won't run, and thus the handler is now leaking? Or could multiple calls to this method in parallel cause the handler to trigger multiple times?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For leaking, I think it has a risk of happening, though I'm not certain whether or how it can be triggered. The COM wrapper As for multiple calls, I think it's safe, since each handler is designed to run only once and unregister itself.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually we could probably just remove this helper function if you feel really worried. Don't feel like it is something that must be in this project.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the datarequested call just not happen if the user cancels the dialog? There's also the chance that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it happens fast enough maybe? As for windows closed while there are uninvoked handlers, yeah if that happens it would be a leak without any way to recover... Might just should remove it in this case? |
||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.