|
1 | 1 | using Microsoft.Extensions.DependencyInjection; |
2 | 2 | using Microsoft.Extensions.Hosting; |
3 | 3 | using Microsoft.UI.Xaml; |
| 4 | +using Microsoft.Windows.AppLifecycle; |
4 | 5 | using Simple_Icon_File_Maker.Activation; |
5 | 6 | using Simple_Icon_File_Maker.Contracts.Services; |
6 | 7 | using Simple_Icon_File_Maker.Models; |
7 | 8 | using Simple_Icon_File_Maker.Services; |
8 | 9 | using Simple_Icon_File_Maker.ViewModels; |
9 | 10 | using Simple_Icon_File_Maker.Views; |
| 11 | +using Windows.ApplicationModel.DataTransfer; |
| 12 | +using Windows.ApplicationModel.DataTransfer.ShareTarget; |
| 13 | +using Windows.Storage; |
| 14 | +using Windows.Storage.Streams; |
10 | 15 |
|
11 | 16 | namespace Simple_Icon_File_Maker; |
12 | 17 |
|
@@ -37,6 +42,8 @@ public static T GetService<T>() |
37 | 42 |
|
38 | 43 | public static UIElement? AppTitlebar { get; set; } |
39 | 44 |
|
| 45 | + public static string? SharedImagePath { get; set; } |
| 46 | + |
40 | 47 | public App() |
41 | 48 | { |
42 | 49 | InitializeComponent(); |
@@ -93,8 +100,63 @@ private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledEx |
93 | 100 | protected override async void OnLaunched(LaunchActivatedEventArgs args) |
94 | 101 | { |
95 | 102 | base.OnLaunched(args); |
| 103 | + |
| 104 | + var activatedEventArgs = AppInstance.GetCurrent().GetActivatedEventArgs(); |
| 105 | + if (activatedEventArgs.Kind == ExtendedActivationKind.ShareTarget) |
| 106 | + { |
| 107 | + await HandleShareTargetActivationAsync(activatedEventArgs); |
| 108 | + } |
| 109 | + |
96 | 110 | await App.GetService<IActivationService>().ActivateAsync(args); |
97 | 111 | } |
98 | 112 |
|
| 113 | + private static async Task HandleShareTargetActivationAsync(AppActivationArguments activatedEventArgs) |
| 114 | + { |
| 115 | + if (activatedEventArgs.Data is Windows.ApplicationModel.Activation.IShareTargetActivatedEventArgs shareArgs) |
| 116 | + { |
| 117 | + ShareOperation shareOperation = shareArgs.ShareOperation; |
| 118 | + shareOperation.ReportStarted(); |
| 119 | + |
| 120 | + try |
| 121 | + { |
| 122 | + if (shareOperation.Data.Contains(StandardDataFormats.StorageItems)) |
| 123 | + { |
| 124 | + IReadOnlyList<IStorageItem> items = await shareOperation.Data.GetStorageItemsAsync(); |
| 125 | + foreach (IStorageItem item in items) |
| 126 | + { |
| 127 | + if (item is StorageFile file && Constants.FileTypes.SupportedImageFormats.Contains(file.FileType, StringComparer.OrdinalIgnoreCase)) |
| 128 | + { |
| 129 | + StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder; |
| 130 | + StorageFile copiedFile = await file.CopyAsync(tempFolder, file.Name, NameCollisionOption.GenerateUniqueName); |
| 131 | + SharedImagePath = copiedFile.Path; |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + else if (shareOperation.Data.Contains(StandardDataFormats.Bitmap)) |
| 137 | + { |
| 138 | + var bitmapRef = await shareOperation.Data.GetBitmapAsync(); |
| 139 | + var stream = await bitmapRef.OpenReadAsync(); |
| 140 | + |
| 141 | + StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder; |
| 142 | + StorageFile tempFile = await tempFolder.CreateFileAsync("shared_image.png", CreationCollisionOption.GenerateUniqueName); |
| 143 | + |
| 144 | + using (var outputStream = await tempFile.OpenAsync(FileAccessMode.ReadWrite)) |
| 145 | + { |
| 146 | + await RandomAccessStream.CopyAsync(stream, outputStream); |
| 147 | + } |
| 148 | + |
| 149 | + SharedImagePath = tempFile.Path; |
| 150 | + } |
| 151 | + } |
| 152 | + catch (Exception) |
| 153 | + { |
| 154 | + // If share handling fails, continue with normal launch |
| 155 | + } |
| 156 | + |
| 157 | + shareOperation.ReportCompleted(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
99 | 161 | public static string[]? cliArgs { get; } = Environment.GetCommandLineArgs(); |
100 | 162 | } |
0 commit comments