1313using Windows . Storage ;
1414using Windows . Storage . Pickers ;
1515using Windows . Storage . Streams ;
16-
16+ using WinRT ;
1717namespace PhotoToys ;
1818interface IMatDisplayer
1919{
@@ -219,9 +219,18 @@ public MatImage(bool DisableView = false, string AddToInventoryLabel = "Add To I
219219 await SaveMat ( Mat_ ) ;
220220 } ) ,
221221 new MenuFlyoutItem
222+ {
223+ Text = "Share" ,
224+ Icon = new SymbolIcon ( Symbol . Share ) ,
225+ } . Edit ( x => x . Click += async delegate
226+ {
227+ await Share ( ) ;
228+ } ) ,
229+ new MenuFlyoutSeparator ( ) ,
230+ new MenuFlyoutItem
222231 {
223232 Text = "View" ,
224- Icon = new SymbolIcon ( Symbol . Zoom ) ,
233+ Icon = new SymbolIcon ( Symbol . View ) ,
225234 Visibility = DisableView ? Visibility . Collapsed : Visibility . Visible ,
226235 } . Edit ( x => x . Click += async delegate
227236 {
@@ -235,14 +244,54 @@ public MatImage(bool DisableView = false, string AddToInventoryLabel = "Add To I
235244 } . Edit ( x => x . Click += async ( _ , _ ) =>
236245 {
237246 await AddToInventory ( ) ;
238- } )
247+ } ) ,
239248 }
240249 } ;
241250 }
242251 public async Task CopyToClipboard ( )
243252 {
244253 if ( Mat_ != null ) Clipboard . SetContent ( await GetDataPackage ( ) ) ;
245254 }
255+ static readonly Guid _dtm_iid =
256+ new Guid ( 0xa5caee9b , 0x8708 , 0x49d1 , 0x8d , 0x36 , 0x67 , 0xd2 , 0x5a , 0x8d , 0xa0 , 0x0c ) ;
257+ public async Task Share ( )
258+ {
259+ var t = new TaskCompletionSource ( ) ;
260+ var Handle = App . CurrentWindowHandle ;
261+ IDataTransferManagerInterop interop =
262+ Windows . ApplicationModel . DataTransfer . DataTransferManager . As
263+ < IDataTransferManagerInterop > ( ) ;
264+
265+ IntPtr result = interop . GetForWindow ( Handle , _dtm_iid ) ;
266+ var dtm = WinRT . MarshalInterface
267+ < Windows . ApplicationModel . DataTransfer . DataTransferManager > . FromAbi ( result ) ;
268+
269+ async void A ( DataTransferManager o , DataRequestedEventArgs e )
270+ {
271+ var d = e . Request . GetDeferral ( ) ;
272+ try
273+ {
274+ if ( Mat_ == null ) return ;
275+ var datacontent = await GetDataPackageContent ( ) ;
276+ Debug . Assert ( datacontent != null ) ;
277+ var ( bytes , stream , BitmapMemRef , StorageFile ) = datacontent . Value ;
278+ e . Request . Data . Properties . Title = "Share Image" ;
279+ e . Request . Data . SetData ( "PhotoToys Image" , stream ) ;
280+ e . Request . Data . SetData ( "PNG" , stream ) ;
281+ e . Request . Data . SetBitmap ( BitmapMemRef ) ;
282+ e . Request . Data . SetStorageItems ( new IStorageItem [ ] { StorageFile } , readOnly : false ) ;
283+ }
284+ finally
285+ {
286+ d . Complete ( ) ;
287+ t . SetResult ( ) ;
288+ }
289+ }
290+ dtm . DataRequested += A ;
291+ interop . ShowShareUIForWindow ( Handle ) ;
292+ await t . Task ;
293+ dtm . DataRequested -= A ;
294+ }
246295 public async Task Save ( )
247296 {
248297 if ( Mat_ != null ) await SaveMat ( Mat_ ) ;
@@ -476,4 +525,11 @@ public void Dispose()
476525 {
477526 MatImage . Dispose ( ) ;
478527 }
528+ }
529+ [ System . Runtime . InteropServices . ComImport , System . Runtime . InteropServices . Guid ( "3A3DCD6C-3EAB-43DC-BCDE-45671CE800C8" ) ]
530+ [ System . Runtime . InteropServices . InterfaceType ( System . Runtime . InteropServices . ComInterfaceType . InterfaceIsIUnknown ) ]
531+ interface IDataTransferManagerInterop
532+ {
533+ IntPtr GetForWindow ( [ System . Runtime . InteropServices . In ] IntPtr appWindow , [ System . Runtime . InteropServices . In ] ref Guid riid ) ;
534+ void ShowShareUIForWindow ( IntPtr appWindow ) ;
479535}
0 commit comments