-
-
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
Open
AkazaRenn
wants to merge
21
commits into
dotMorten:main
Choose a base branch
from
AkazaRenn:share
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
25eba75
Add share sheet support
AkazaRenn ab23884
Move all changes into extensions
AkazaRenn e187a0c
Remove extra line in imports
AkazaRenn 65ebd32
Remove extra line in imports
AkazaRenn 0d670fe
Remove redundant null check
AkazaRenn 806dac4
Use window.GetWindowHandle
AkazaRenn f328fe2
Move ShowShareUIForWindow to HwndExtensions
AkazaRenn a163654
Unsubscribe handler first just to be safe
AkazaRenn fb2d9a2
Fix xml doc
AkazaRenn b83d03d
Fix xml doc
AkazaRenn 7b40965
Also extend window with ShowShareUI
AkazaRenn 5558831
Merge branch 'main' into share
dotMorten 7935a17
Merge branch 'main' into share
dotMorten 72fc53e
Merge branch 'main' into share
dotMorten 554a224
Remove helper function
AkazaRenn f1e7175
Improve dtm object ownership
AkazaRenn 1b76eb0
Use proper type check
AkazaRenn 91f28b3
Keep a stable reference of DataTransferManager_DataRequested
AkazaRenn 424f146
Use a cleaner solution, should still be safe
AkazaRenn 32a62fc
Revert some changes that should no tbe there
AkazaRenn 11ddece
Revert some changes that should no tbe there
AkazaRenn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,73 @@ | ||
| using System; | ||
|
|
||
| using Windows.ApplicationModel.DataTransfer; | ||
|
|
||
| namespace WinUIEx | ||
| { | ||
| // https://learn.microsoft.com/en-us/windows/apps/develop/windows-integration/integrate-sharesheet-send#implement-share-for-desktop-apps-winui-3-wpf-winforms | ||
|
|
||
| [System.Runtime.InteropServices.ComImport] | ||
| [System.Runtime.InteropServices.Guid("3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8")] | ||
| [System.Runtime.InteropServices.InterfaceType( | ||
| System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown)] | ||
| interface IDataTransferManagerInterop | ||
| { | ||
| IntPtr GetForWindow([System.Runtime.InteropServices.In] IntPtr appWindow, | ||
| [System.Runtime.InteropServices.In] ref Guid riid); | ||
| void ShowShareUIForWindow(IntPtr appWindow); | ||
| } | ||
|
|
||
| public partial class WindowEx | ||
| { | ||
| private DataTransferManager? _dtm; | ||
|
|
||
| // Call this from your window or form constructor (or load handler): | ||
| private void InitializeShare() | ||
| { | ||
| // Retrieve the window handle (HWND) for the current window: | ||
| // WinUI 3: IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); | ||
| // WPF: IntPtr hWnd = new System.Windows.Interop.WindowInteropHelper(this).Handle; | ||
| // WinForms: IntPtr hWnd = this.Handle; | ||
| IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); | ||
|
|
||
| IDataTransferManagerInterop interop = | ||
| DataTransferManager.As<IDataTransferManagerInterop>(); | ||
|
|
||
| // IID of DataTransferManager, passed as the riid to GetForWindow: | ||
| Guid dtmIid = new(0xa5caee9b, 0x8708, 0x49d1, 0x8d, 0x36, 0x67, 0xd2, 0x5a, 0x8d, 0xa0, 0x0c); | ||
| _dtm = WinRT.MarshalInterface<DataTransferManager>.FromAbi(interop.GetForWindow(hWnd, dtmIid)); | ||
|
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 this be done on-the-fly in the extension method and cleaned up after instead?
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. After ab23884, it is done on-the-fly, please see #275 (comment). |
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Displays the Windows Share UI for this window and supplies the specified | ||
| /// <see cref="DataPackage"/> when the user completes the share operation. | ||
| /// </summary> | ||
| /// <param name="data"> | ||
| /// The data package to share. This is provided to the Share UI when the | ||
| /// <c>DataRequested</c> event is raised. | ||
| /// </param> | ||
| /// <exception cref="InvalidOperationException"> | ||
| /// Thrown if <see cref="InitializeShare"/> has not been called before invoking this method. | ||
| /// </exception> | ||
| public void Share(DataPackage data) | ||
| { | ||
| if (_dtm is null) | ||
| { | ||
| throw new InvalidOperationException("Share has not been initialized. Call InitializeShare() first."); | ||
| } | ||
|
|
||
| void handler(DataTransferManager sender, DataRequestedEventArgs args) | ||
| { | ||
| args.Request.Data = data; | ||
| if (_dtm is not null) { | ||
| _dtm.DataRequested -= handler; | ||
| } | ||
| } | ||
|
|
||
| _dtm.DataRequested += handler; | ||
| var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this); | ||
| var interop = DataTransferManager.As<IDataTransferManagerInterop>(); | ||
| interop.ShowShareUIForWindow(hWnd); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.