Skip to content

Commit d0e99d5

Browse files
committed
Add Windows Share Target support for image files
Enable the app to receive images via the Windows Share UI by declaring share target support in the manifest and handling share activation. Shared images are copied to a temp folder and loaded automatically on launch. CLI argument loading is now secondary to share activation.
1 parent 58ecf0b commit d0e99d5

3 files changed

Lines changed: 82 additions & 2 deletions

File tree

Simple Icon File Maker/Simple Icon File Maker (Package)/Package.appxmanifest

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22

33
<Package
44
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
@@ -57,6 +57,18 @@
5757
<uap:Logo>Images\Image128.png</uap:Logo>
5858
</uap:FileTypeAssociation>
5959
</uap:Extension>
60+
<uap:Extension Category="windows.shareTarget">
61+
<uap:ShareTarget>
62+
<uap:SupportedFileTypes>
63+
<uap:FileType>.png</uap:FileType>
64+
<uap:FileType>.jpg</uap:FileType>
65+
<uap:FileType>.jpeg</uap:FileType>
66+
<uap:FileType>.bmp</uap:FileType>
67+
<uap:FileType>.ico</uap:FileType>
68+
</uap:SupportedFileTypes>
69+
<uap:DataFormat>Bitmap</uap:DataFormat>
70+
</uap:ShareTarget>
71+
</uap:Extension>
6072
</Extensions>
6173
</Application>
6274
</Applications>

Simple Icon File Maker/Simple Icon File Maker/App.xaml.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.UI.Xaml;
4+
using Microsoft.Windows.AppLifecycle;
45
using Simple_Icon_File_Maker.Activation;
56
using Simple_Icon_File_Maker.Contracts.Services;
67
using Simple_Icon_File_Maker.Models;
78
using Simple_Icon_File_Maker.Services;
89
using Simple_Icon_File_Maker.ViewModels;
910
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;
1015

1116
namespace Simple_Icon_File_Maker;
1217

@@ -37,6 +42,8 @@ public static T GetService<T>()
3742

3843
public static UIElement? AppTitlebar { get; set; }
3944

45+
public static string? SharedImagePath { get; set; }
46+
4047
public App()
4148
{
4249
InitializeComponent();
@@ -93,8 +100,63 @@ private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledEx
93100
protected override async void OnLaunched(LaunchActivatedEventArgs args)
94101
{
95102
base.OnLaunched(args);
103+
104+
var activatedEventArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
105+
if (activatedEventArgs.Kind == ExtendedActivationKind.ShareTarget)
106+
{
107+
await HandleShareTargetActivationAsync(activatedEventArgs);
108+
}
109+
96110
await App.GetService<IActivationService>().ActivateAsync(args);
97111
}
98112

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+
99161
public static string[]? cliArgs { get; } = Environment.GetCommandLineArgs();
100162
}

Simple Icon File Maker/Simple Icon File Maker/ViewModels/MainViewModel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,14 @@ public async void OnNavigatedTo(object parameter)
152152

153153
ShowUpgradeToProButton = !_storeService.OwnsPro;
154154

155+
// Load shared image path from share target activation
156+
if (!string.IsNullOrEmpty(App.SharedImagePath))
157+
{
158+
ImagePath = App.SharedImagePath;
159+
App.SharedImagePath = null;
160+
}
155161
// Load CLI args if present
156-
if (App.cliArgs?.Length > 1)
162+
else if (App.cliArgs?.Length > 1)
157163
{
158164
ImagePath = App.cliArgs[1];
159165
}

0 commit comments

Comments
 (0)