using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace LanMountainDesktop.AirAppSdk; /// /// Base class for AirApp implementations. /// Inherit from this class and apply the [AirAppEntrance] attribute. /// public abstract class AirAppBase : IAirApp { /// /// Gets the runtime context after the AirApp has started. /// Available after OnStartedAsync is called. /// protected IAirAppRuntimeContext? RuntimeContext { get; private set; } /// /// Initialize the AirApp and register services. /// Override this method to register your components, windows, and services. /// /// Host builder context /// Service collection public virtual void Initialize(HostBuilderContext context, IServiceCollection services) { // Default implementation: do nothing // Derived classes can override to register services } /// /// Called after the host application has started. /// Override this for runtime initialization. /// /// AirApp runtime context public virtual Task OnStartedAsync(IAirAppRuntimeContext context) { RuntimeContext = context; return Task.CompletedTask; } /// /// Called when the host application is stopping. /// Override this for cleanup logic. /// public virtual Task OnStoppingAsync() { return Task.CompletedTask; } /// /// Register a desktop component widget. /// /// Widget implementation type /// Unique component identifier /// Display name /// Optional configuration protected void RegisterComponent( string id, string name, Action? configure = null) where TWidget : class, IAirAppWidget { if (RuntimeContext == null) { throw new InvalidOperationException( "RegisterComponent can only be called after OnStartedAsync. " + "Use IServiceCollection extension methods in Initialize() instead."); } var options = new AirAppComponentOptions { Id = id, Name = name, WidgetType = typeof(TWidget) }; configure?.Invoke(options); // Delegate to runtime context RuntimeContext.RegisterComponent(options); } /// /// Register a window. /// /// Window implementation type /// Unique window identifier /// Display name protected void RegisterWindow(string id, string name) where TWindow : class, IAirAppWindow { if (RuntimeContext == null) { throw new InvalidOperationException( "RegisterWindow can only be called after OnStartedAsync."); } RuntimeContext.RegisterWindow(id, name, typeof(TWindow)); } /// /// Register a service in the DI container. /// /// Service interface /// Implementation type protected void RegisterService() where TService : class where TImplementation : class, TService { if (RuntimeContext == null) { throw new InvalidOperationException( "RegisterService can only be called after OnStartedAsync. " + "Use IServiceCollection in Initialize() instead."); } RuntimeContext.RegisterService(); } }