Files
LanMountainDesktop/airapp/LanMountainDesktop.AirAppSdk/AirAppServiceCollectionExtensions.cs

204 lines
7.6 KiB
C#
Raw Normal View History

2026-03-12 09:22:03 +08:00
using Avalonia.Controls;
using dotnetCampus.Ipc.CompilerServices.Attributes;
2026-03-12 09:22:03 +08:00
using Microsoft.Extensions.DependencyInjection;
2026-08-11 23:03:55 +08:00
namespace LanMountainDesktop.AirAppSdk;
2026-03-12 09:22:03 +08:00
2026-08-11 23:03:55 +08:00
public static class AirAppServiceCollectionExtensions
2026-03-12 09:22:03 +08:00
{
2026-08-11 23:03:55 +08:00
public static IServiceCollection AddAirAppSettingsSection(
2026-03-12 09:22:03 +08:00
this IServiceCollection services,
string id,
2026-03-13 00:33:00 +08:00
string titleLocalizationKey,
2026-08-11 23:03:55 +08:00
Action<AirAppSettingsSectionBuilder> configure,
2026-03-13 00:33:00 +08:00
string? descriptionLocalizationKey = null,
string iconKey = "PuzzlePiece",
2026-03-12 09:22:03 +08:00
int sortOrder = 0)
{
ArgumentNullException.ThrowIfNull(services);
2026-03-13 00:33:00 +08:00
ArgumentNullException.ThrowIfNull(configure);
2026-03-12 09:22:03 +08:00
2026-08-11 23:03:55 +08:00
var builder = new AirAppSettingsSectionBuilder(
2026-03-12 09:22:03 +08:00
id,
2026-03-13 00:33:00 +08:00
titleLocalizationKey,
descriptionLocalizationKey,
iconKey,
sortOrder);
configure(builder);
services.AddSingleton(builder.Build());
2026-03-12 09:22:03 +08:00
return services;
}
2026-04-13 01:23:11 +08:00
/// <summary>
/// Registers a plugin settings section with a custom AXAML view.
/// The host application will display <typeparamref name="TView"/> directly
/// in the settings window, allowing the plugin to use any Fluent Avalonia controls
/// and custom layouts — just like built-in settings pages.
/// </summary>
2026-08-11 23:03:55 +08:00
/// <typeparam name="TView">A <see cref="AirAppSettingsPageBase"/> subclass that defines the settings UI using AXAML.</typeparam>
public static IServiceCollection AddAirAppSettingsSection<TView>(
2026-04-13 01:23:11 +08:00
this IServiceCollection services,
string id,
string titleLocalizationKey,
string? descriptionLocalizationKey = null,
string iconKey = "PuzzlePiece",
int sortOrder = 0)
2026-08-11 23:03:55 +08:00
where TView : AirAppSettingsPageBase
2026-04-13 01:23:11 +08:00
{
ArgumentNullException.ThrowIfNull(services);
2026-08-11 23:03:55 +08:00
var builder = new AirAppSettingsSectionBuilder(
2026-04-13 01:23:11 +08:00
id,
titleLocalizationKey,
descriptionLocalizationKey,
iconKey,
sortOrder);
builder.SetCustomView<TView>();
services.AddSingleton(builder.Build());
return services;
}
2026-08-11 23:03:55 +08:00
public static IServiceCollection AddAirAppComponent<TControl>(
2026-03-12 09:22:03 +08:00
this IServiceCollection services,
2026-08-11 23:03:55 +08:00
AirAppComponentOptions options)
2026-03-12 09:22:03 +08:00
where TControl : Control
{
ArgumentNullException.ThrowIfNull(services);
2026-03-20 22:37:37 +08:00
ArgumentNullException.ThrowIfNull(options);
2026-03-12 09:22:03 +08:00
2026-08-11 23:03:55 +08:00
services.AddSingleton(new AirAppComponentRegistration(
2026-03-12 09:22:03 +08:00
(provider, context) => ActivatorUtilities.CreateInstance<TControl>(provider, context),
2026-03-20 22:37:37 +08:00
options));
2026-03-12 09:22:03 +08:00
return services;
}
2026-08-11 23:03:55 +08:00
public static IServiceCollection AddAirAppComponentEditor<TControl>(
2026-03-14 22:45:09 +08:00
this IServiceCollection services,
string componentId,
double preferredWidth = 720d,
double preferredHeight = 540d,
double minScale = 0.85d,
double maxScale = 1.45d)
where TControl : Control
{
ArgumentNullException.ThrowIfNull(services);
2026-08-11 23:03:55 +08:00
services.AddSingleton(new AirAppComponentEditorRegistration(
2026-03-14 22:45:09 +08:00
componentId,
(provider, context) => ActivatorUtilities.CreateInstance<TControl>(provider, context),
preferredWidth,
preferredHeight,
minScale,
maxScale));
return services;
}
2026-08-11 23:03:55 +08:00
/// <summary>
/// Registers a window AirApp entry.
/// The host resolves the window by <paramref name="id"/> and hosts its
/// <see cref="IAirAppWindow"/> content inside the AirAppHost window shell.
/// </summary>
/// <typeparam name="TWindow">A class implementing <see cref="IAirAppWindow"/>.</typeparam>
/// <param name="id">Unique window identifier.</param>
/// <param name="name">Display name.</param>
public static IServiceCollection AddAirAppWindow<TWindow>(
this IServiceCollection services,
string id,
string name)
where TWindow : class, IAirAppWindow
{
ArgumentNullException.ThrowIfNull(services);
services.AddTransient<TWindow>();
services.AddSingleton(new AirAppWindowRegistration(id, name, typeof(TWindow)));
return services;
}
public static IServiceCollection AddAirAppExport<TContract, TImplementation>(this IServiceCollection services)
2026-03-12 09:22:03 +08:00
where TContract : class
where TImplementation : class, TContract
{
ArgumentNullException.ThrowIfNull(services);
EnsureSingletonRegistration<TContract, TImplementation>(services);
if (!services.Any(descriptor =>
2026-08-11 23:03:55 +08:00
descriptor.ServiceType == typeof(AirAppServiceExportRegistration) &&
descriptor.ImplementationInstance is AirAppServiceExportRegistration existing &&
2026-03-12 09:22:03 +08:00
existing.ContractType == typeof(TContract) &&
existing.ImplementationType == typeof(TImplementation)))
{
2026-08-11 23:03:55 +08:00
services.AddSingleton(new AirAppServiceExportRegistration(typeof(TContract), typeof(TImplementation)));
2026-03-12 09:22:03 +08:00
}
return services;
}
2026-08-11 23:03:55 +08:00
public static IServiceCollection AddAirAppPublicIpc<TContract, TImplementation>(
this IServiceCollection services,
string? objectId = null,
params string[] notifyIds)
where TContract : class
where TImplementation : class, TContract
{
ArgumentNullException.ThrowIfNull(services);
EnsurePublicIpcContract(typeof(TContract));
EnsureSingletonRegistration<TContract, TImplementation>(services);
if (!services.Any(descriptor =>
2026-08-11 23:03:55 +08:00
descriptor.ServiceType == typeof(AirAppPublicIpcServiceRegistration) &&
descriptor.ImplementationInstance is AirAppPublicIpcServiceRegistration existing &&
existing.ContractType == typeof(TContract) &&
string.Equals(existing.ObjectId, objectId, StringComparison.Ordinal)))
{
2026-08-11 23:03:55 +08:00
services.AddSingleton(new AirAppPublicIpcServiceRegistration(
typeof(TContract),
objectId,
notifyIds ?? []));
}
return services;
}
2026-08-11 23:03:55 +08:00
public static IServiceCollection AddAirAppPublicIpcContributor<TContributor>(this IServiceCollection services)
where TContributor : class, IAirAppPublicIpcContributor
{
ArgumentNullException.ThrowIfNull(services);
2026-08-11 23:03:55 +08:00
services.AddSingleton<IAirAppPublicIpcContributor, TContributor>();
return services;
}
private static void EnsurePublicIpcContract(Type contractType)
{
if (!contractType.IsInterface)
{
throw new InvalidOperationException(
$"Public IPC contract '{contractType.FullName}' must be an interface.");
}
if (!Attribute.IsDefined(contractType, typeof(IpcPublicAttribute), inherit: false))
{
throw new InvalidOperationException(
$"Public IPC contract '{contractType.FullName}' must be marked with '{nameof(IpcPublicAttribute)}'.");
}
}
2026-03-12 09:22:03 +08:00
private static void EnsureSingletonRegistration<TContract, TImplementation>(IServiceCollection services)
where TContract : class
where TImplementation : class, TContract
{
var contractDescriptor = services.LastOrDefault(descriptor => descriptor.ServiceType == typeof(TContract));
if (contractDescriptor is null)
{
services.AddSingleton<TContract, TImplementation>();
return;
}
if (contractDescriptor.Lifetime != ServiceLifetime.Singleton)
{
throw new InvalidOperationException(
$"Exported contract '{typeof(TContract).FullName}' must be registered as Singleton.");
}
}
}