using Moli.Desktop.Diagnostics; using Moli.Desktop.Layout; using Moli.Desktop.Plugins; namespace Moli.Desktop.Runtime; public sealed record WallpaperRuntimeState { public bool IsEnabled { get; init; } public bool IsPaused { get; init; } public bool IsMuted { get; init; } public double Opacity { get; init; } = 1; public bool ClickThrough { get; init; } = true; public PluginId? PluginId { get; init; } } public sealed record AppRuntimeState { public bool IsEditMode { get; init; } public bool IsGlobalLocked { get; init; } public bool IsSafeInputMode { get; init; } public WallpaperRuntimeState Wallpaper { get; init; } = new(); public IReadOnlyList Plugins { get; init; } = []; public IReadOnlyList Widgets { get; init; } = []; public IReadOnlyList Diagnostics { get; init; } = []; } public sealed record RuntimeEvent { public required string Name { get; init; } public object? Payload { get; init; } public DateTimeOffset Timestamp { get; init; } = DateTimeOffset.UtcNow; } public interface IRuntimeEventBus { IDisposable Subscribe(Func handler); Task PublishAsync(RuntimeEvent runtimeEvent, CancellationToken cancellationToken = default); } public sealed class RuntimeEventBus : IRuntimeEventBus { private readonly object _gate = new(); private readonly List> _handlers = []; public IDisposable Subscribe(Func handler) { lock (_gate) { _handlers.Add(handler); } return new Subscription(() => { lock (_gate) { _handlers.Remove(handler); } }); } public async Task PublishAsync(RuntimeEvent runtimeEvent, CancellationToken cancellationToken = default) { Func[] handlers; lock (_gate) { handlers = [.. _handlers]; } foreach (var handler in handlers) { cancellationToken.ThrowIfCancellationRequested(); await handler(runtimeEvent).ConfigureAwait(false); } } private sealed class Subscription(Action dispose) : IDisposable { public void Dispose() => dispose(); } }