| src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/Moli.Desktop/Host/DesktopHostBuilder.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/Moli.Desktop/Runtime/IDesktopRuntime.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs
New file @@ -0,0 +1,207 @@ using Microsoft.Extensions.Logging.Abstractions; using Moli.Desktop.Configuration; using Moli.Desktop.Diagnostics; using Moli.Desktop.Host; using Moli.Desktop.Interop; using Moli.Desktop.Layout; using Moli.Desktop.Plugins; using Moli.Desktop.Runtime; using Moli.Desktop.Tests.Interop; namespace Moli.Desktop.Tests.Runtime; public class DesktopRuntimeCoordinatorTests { [Fact] public async Task SetEditModeAsync_PersistsSettingAndBroadcastsStatus() { var fixture = await RuntimeFixture.CreateAsync(); var events = new List<RuntimeEvent>(); using var subscription = fixture.Events.Subscribe(runtimeEvent => { events.Add(runtimeEvent); return Task.CompletedTask; }); await fixture.Runtime.StartAsync(); var result = await fixture.Runtime.SetEditModeAsync(true); Assert.True(result.Succeeded); Assert.True(result.Value!.IsEditMode); Assert.True(fixture.Surface.EditModeEnabled); var settings = await fixture.Configuration.LoadAppSettingsAsync(); Assert.True(settings.Runtime.EditMode); Assert.Contains(events, runtimeEvent => runtimeEvent.Name == "app.editModeChanged"); Assert.Contains(events, runtimeEvent => runtimeEvent.Name == "app.statusChanged"); } [Fact] public async Task UpdateWidgetLayoutAsync_ConstrainsAppliesSavesAndBroadcasts() { var fixture = await RuntimeFixture.CreateAsync(); var events = new List<RuntimeEvent>(); using var subscription = fixture.Events.Subscribe(runtimeEvent => { events.Add(runtimeEvent); return Task.CompletedTask; }); await fixture.Runtime.StartAsync(); var result = await fixture.Runtime.UpdateWidgetLayoutAsync( fixture.WidgetId, new WidgetLayoutPatch { Bounds = new RectD(-5000, -5000, 10, 10), Opacity = 0 }); Assert.True(result.Succeeded); Assert.NotNull(fixture.Surface.LastAppliedLayout); Assert.Equal(LayoutManager.MinWidth, fixture.Surface.LastAppliedLayout.Bounds.Width); Assert.Equal(LayoutManager.MinOpacity, fixture.Surface.LastAppliedLayout.Opacity); Assert.Contains(events, runtimeEvent => runtimeEvent.Name == "widget.layoutChanged"); var saved = await fixture.Configuration.LoadLayoutAsync(); var savedLayout = Assert.Single(saved.Instances); Assert.Equal(LayoutManager.MinWidth, savedLayout.Bounds.Width); Assert.Equal(LayoutManager.MinOpacity, savedLayout.Opacity); } [Fact] public async Task UpdateWidgetLayoutAsync_WhenGlobalLockedRejectsGeometryPatch() { var fixture = await RuntimeFixture.CreateAsync(); await fixture.Runtime.StartAsync(); await fixture.Runtime.SetGlobalLockedAsync(true); var result = await fixture.Runtime.UpdateWidgetLayoutAsync( fixture.WidgetId, new WidgetLayoutPatch { Bounds = new RectD(20, 20, 320, 160) }); Assert.False(result.Succeeded); Assert.Equal("WidgetLayoutLocked", result.ErrorCode); Assert.Null(fixture.Surface.LastAppliedLayout); } private sealed class RuntimeFixture { private RuntimeFixture( IDesktopRuntime runtime, JsonConfigurationService configuration, RuntimeEventBus events, FakeRuntimeSurfaceController surface, WidgetInstanceId widgetId) { Runtime = runtime; Configuration = configuration; Events = events; Surface = surface; WidgetId = widgetId; } public IDesktopRuntime Runtime { get; } public JsonConfigurationService Configuration { get; } public RuntimeEventBus Events { get; } public FakeRuntimeSurfaceController Surface { get; } public WidgetInstanceId WidgetId { get; } public static async Task<RuntimeFixture> CreateAsync() { var root = Path.Combine(Path.GetTempPath(), "Moli.Desktop.Tests", Guid.NewGuid().ToString("N")); var paths = new DesktopPaths(Path.Combine(root, "app"), Path.Combine(root, "local")); var diagnostics = new DiagnosticsService(paths, NullLogger<DiagnosticsService>.Instance); var configuration = new JsonConfigurationService(paths, diagnostics); var events = new RuntimeEventBus(); var surface = new FakeRuntimeSurfaceController(); var pluginManager = new PluginManager(paths, configuration, new PluginManifestValidator(), diagnostics, new PluginStateMachine()); var layoutManager = new LayoutManager(configuration, diagnostics); var widgetId = new WidgetInstanceId("widget-01"); await CreatePluginAsync(paths); await configuration.SaveLayoutAsync(new LayoutDocument { Monitors = [ new MonitorSnapshot { Monitors = [PrimaryMonitor()] } ], Instances = [ new WidgetLayout { InstanceId = widgetId, PluginId = new PluginId("sample.clock"), MonitorId = new MonitorId("primary"), Bounds = new RectD(100, 100, 320, 160) } ] }); var runtime = new DesktopRuntimeCoordinator( configuration, pluginManager, layoutManager, new FakeWin32InteropService(), diagnostics, events, surface); return new RuntimeFixture(runtime, configuration, events, surface, widgetId); } private static async Task CreatePluginAsync(DesktopPaths paths) { var directory = Path.Combine(paths.PluginDirectory, "sample.clock"); Directory.CreateDirectory(directory); await File.WriteAllTextAsync(Path.Combine(directory, "index.html"), "<!doctype html><title>Clock</title>"); await File.WriteAllTextAsync( Path.Combine(directory, "plugin.json"), """ { "id": "sample.clock", "name": "Clock", "version": "1.0.0", "type": "widget", "entry": "index.html", "defaultLayout": { "width": 320, "height": 160, "opacity": 1.0 } } """); } private static MonitorInfo PrimaryMonitor() => new() { Id = new MonitorId("primary"), Bounds = new RectD(0, 0, 1920, 1080), WorkArea = new RectD(0, 0, 1920, 1040), IsPrimary = true }; } private sealed class FakeRuntimeSurfaceController : IRuntimeSurfaceController { public bool EditModeEnabled { get; private set; } public WidgetLayout? LastAppliedLayout { get; private set; } public Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default) { EditModeEnabled = enabled; return Task.CompletedTask; } public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default) { LastAppliedLayout = layout; return Task.CompletedTask; } public Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task DisableWallpaperAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ShutdownAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; } } src/Moli.Desktop/Host/DesktopHostBuilder.cs
@@ -27,6 +27,8 @@ services.AddSingleton<DesktopPaths>(); services.AddSingleton<IDiagnosticsService, DiagnosticsService>(); services.AddSingleton<IRuntimeEventBus, RuntimeEventBus>(); services.AddSingleton<IRuntimeSurfaceController, NullRuntimeSurfaceController>(); services.AddSingleton<IDesktopRuntime, DesktopRuntimeCoordinator>(); services.AddSingleton<IConfigurationService, JsonConfigurationService>(); services.AddSingleton<IPluginManifestValidator, PluginManifestValidator>(); services.AddSingleton<PluginStateMachine>(); src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
New file @@ -0,0 +1,317 @@ using Moli.Desktop.Configuration; using Moli.Desktop.Diagnostics; using Moli.Desktop.Interop; using Moli.Desktop.Layout; using Moli.Desktop.Plugins; namespace Moli.Desktop.Runtime; public sealed class DesktopRuntimeCoordinator : IDesktopRuntime { private readonly IConfigurationService _configuration; private readonly IPluginManager _plugins; private readonly ILayoutManager _layout; private readonly IWin32InteropService _interop; private readonly IDiagnosticsService _diagnostics; private readonly IRuntimeEventBus _events; private readonly IRuntimeSurfaceController _surfaces; private readonly SemaphoreSlim _lock = new(1, 1); private AppSettingsDocument _settings = new(); private IReadOnlyList<PluginDescriptor> _pluginSnapshot = []; private WallpaperRuntimeState _wallpaper = new(); private bool _isEditMode; private bool _isGlobalLocked; private bool _isSafeInputMode; public DesktopRuntimeCoordinator( IConfigurationService configuration, IPluginManager plugins, ILayoutManager layout, IWin32InteropService interop, IDiagnosticsService diagnostics, IRuntimeEventBus events, IRuntimeSurfaceController surfaces) { _configuration = configuration; _plugins = plugins; _layout = layout; _interop = interop; _diagnostics = diagnostics; _events = events; _surfaces = surfaces; } public async Task<OperationResult<AppRuntimeState>> StartAsync(CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _settings = await _configuration.LoadAppSettingsAsync(cancellationToken).ConfigureAwait(false); _isEditMode = _settings.Runtime.EditMode; _isSafeInputMode = _settings.Runtime.SafeInputMode; await _layout.LoadAsync(cancellationToken).ConfigureAwait(false); ReconcileCurrentMonitors(); await _layout.SaveAsync(cancellationToken).ConfigureAwait(false); _pluginSnapshot = await _plugins.ScanAsync(cancellationToken).ConfigureAwait(false); _diagnostics.Record(DiagnosticSeverity.Info, DiagnosticCodes.ApplicationStarted, "Desktop runtime started."); var state = BuildState(); await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false); return OperationResult<AppRuntimeState>.Success(state); } finally { _lock.Release(); } } public async Task<OperationResult> ShutdownAsync(CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { await SaveRuntimeSettingsAsync(cancellationToken).ConfigureAwait(false); await _layout.SaveAsync(cancellationToken).ConfigureAwait(false); await _surfaces.ShutdownAsync(cancellationToken).ConfigureAwait(false); await _events.PublishAsync(new RuntimeEvent { Name = "app.shutdown", Payload = BuildState() }, cancellationToken).ConfigureAwait(false); return OperationResult.Success("Desktop runtime stopped."); } finally { _lock.Release(); } } public async Task<AppRuntimeState> GetStateAsync(CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { return BuildState(); } finally { _lock.Release(); } } public Task<OperationResult<AppRuntimeState>> SetEditModeAsync(bool enabled, CancellationToken cancellationToken = default) => UpdateRuntimeFlagAsync( "app.editModeChanged", () => _isEditMode = enabled, () => _surfaces.ApplyEditModeAsync(enabled, cancellationToken), cancellationToken); public async Task<OperationResult<AppRuntimeState>> SetGlobalLockedAsync(bool enabled, CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _isGlobalLocked = enabled; var state = BuildState(); await _events.PublishAsync(new RuntimeEvent { Name = "app.globalLockedChanged", Payload = state }, cancellationToken).ConfigureAwait(false); await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false); return OperationResult<AppRuntimeState>.Success(state); } finally { _lock.Release(); } } public Task<OperationResult<AppRuntimeState>> SetSafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default) => UpdateRuntimeFlagAsync( "app.safeInputModeChanged", () => _isSafeInputMode = enabled, () => _surfaces.ApplySafeInputModeAsync(enabled, cancellationToken), cancellationToken); public async Task<OperationResult<AppRuntimeState>> ReloadPluginsAsync(CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _pluginSnapshot = await _plugins.ScanAsync(cancellationToken).ConfigureAwait(false); var state = BuildState(); await _events.PublishAsync(new RuntimeEvent { Name = "plugin.reloaded", Payload = state.Plugins }, cancellationToken).ConfigureAwait(false); await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false); return OperationResult<AppRuntimeState>.Success(state); } finally { _lock.Release(); } } public async Task<OperationResult<AppRuntimeState>> EnableWallpaperAsync(CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { var wallpaperPlugin = _pluginSnapshot.FirstOrDefault(plugin => plugin.Type == PluginType.Wallpaper && plugin.State is PluginState.Enabled or PluginState.Loaded or PluginState.Running or PluginState.Paused); if (wallpaperPlugin is null) { return OperationResult<AppRuntimeState>.Failure("WallpaperPluginNotFound", "No enabled wallpaper plugin is available."); } _wallpaper = _wallpaper with { IsEnabled = true, IsPaused = false, PluginId = wallpaperPlugin.Id }; await _surfaces.EnableWallpaperAsync(_wallpaper, cancellationToken).ConfigureAwait(false); var state = BuildState(); await _events.PublishAsync(new RuntimeEvent { Name = "wallpaper.enabled", Payload = _wallpaper }, cancellationToken).ConfigureAwait(false); await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false); return OperationResult<AppRuntimeState>.Success(state); } finally { _lock.Release(); } } public async Task<OperationResult<AppRuntimeState>> DisableWallpaperAsync(CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { _wallpaper = _wallpaper with { IsEnabled = false, IsPaused = false }; await _surfaces.DisableWallpaperAsync(cancellationToken).ConfigureAwait(false); var state = BuildState(); await _events.PublishAsync(new RuntimeEvent { Name = "wallpaper.disabled", Payload = _wallpaper }, cancellationToken).ConfigureAwait(false); await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false); return OperationResult<AppRuntimeState>.Success(state); } finally { _lock.Release(); } } public async Task<OperationResult<WidgetRuntimeState>> UpdateWidgetLayoutAsync( WidgetInstanceId instanceId, WidgetLayoutPatch patch, CancellationToken cancellationToken = default) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (_isGlobalLocked && IsGeometryPatch(patch)) { return OperationResult<WidgetRuntimeState>.Failure("WidgetLayoutLocked", "Global lock is enabled."); } var result = _layout.Update(instanceId, patch); if (!result.Succeeded || result.Value is null) { return OperationResult<WidgetRuntimeState>.Failure(result.ErrorCode ?? "WidgetLayoutUpdateFailed", result.Message ?? "Widget layout update failed."); } await _surfaces.ApplyWidgetLayoutAsync(result.Value, cancellationToken).ConfigureAwait(false); await _layout.SaveAsync(cancellationToken).ConfigureAwait(false); var runtimeState = ToWidgetRuntimeState(result.Value); await _events.PublishAsync(new RuntimeEvent { Name = "widget.layoutChanged", Payload = runtimeState }, cancellationToken).ConfigureAwait(false); await PublishStatusAsync(BuildState(), cancellationToken).ConfigureAwait(false); return OperationResult<WidgetRuntimeState>.Success(runtimeState); } finally { _lock.Release(); } } private async Task<OperationResult<AppRuntimeState>> UpdateRuntimeFlagAsync( string eventName, Action update, Func<Task> applySurfaces, CancellationToken cancellationToken) { await _lock.WaitAsync(cancellationToken).ConfigureAwait(false); try { update(); await SaveRuntimeSettingsAsync(cancellationToken).ConfigureAwait(false); await applySurfaces().ConfigureAwait(false); var state = BuildState(); await _events.PublishAsync(new RuntimeEvent { Name = eventName, Payload = state }, cancellationToken).ConfigureAwait(false); await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false); return OperationResult<AppRuntimeState>.Success(state); } finally { _lock.Release(); } } private void ReconcileCurrentMonitors() { var monitors = _interop.GetMonitors(); _layout.ReconcileMonitors(monitors); } private async Task SaveRuntimeSettingsAsync(CancellationToken cancellationToken) { _settings = _settings with { Runtime = _settings.Runtime with { EditMode = _isEditMode, SafeInputMode = _isSafeInputMode } }; await _configuration.SaveAppSettingsAsync(_settings, cancellationToken).ConfigureAwait(false); } private AppRuntimeState BuildState() { return new AppRuntimeState { IsEditMode = _isEditMode, IsGlobalLocked = _isGlobalLocked, IsSafeInputMode = _isSafeInputMode, Wallpaper = _wallpaper, Plugins = [.. _pluginSnapshot.Select(ToPluginRuntimeState)], Widgets = [.. _layout.GetAll().Select(ToWidgetRuntimeState)], Diagnostics = _diagnostics.Snapshot() }; } private async Task PublishStatusAsync(AppRuntimeState state, CancellationToken cancellationToken) { await _events.PublishAsync(new RuntimeEvent { Name = "app.statusChanged", Payload = state }, cancellationToken).ConfigureAwait(false); } private PluginRuntimeState ToPluginRuntimeState(PluginDescriptor plugin) => new() { Id = plugin.Id, Name = plugin.Manifest.Name, Type = plugin.Type, State = plugin.State, ErrorCode = plugin.ErrorCode, Entry = plugin.EntryUri.ToString() }; private WidgetRuntimeState ToWidgetRuntimeState(WidgetLayout layout) => new() { InstanceId = layout.InstanceId, PluginId = layout.PluginId, Bounds = layout.Bounds, MonitorId = layout.MonitorId, IsVisible = layout.IsVisible, IsLocked = _isGlobalLocked || layout.IsLocked, Opacity = layout.Opacity, ClickThrough = _isEditMode || _isSafeInputMode ? false : layout.ClickThrough }; private static bool IsGeometryPatch(WidgetLayoutPatch patch) => patch.Bounds is not null || patch.MonitorId is not null || patch.Scale is not null; } src/Moli.Desktop/Runtime/IDesktopRuntime.cs
New file @@ -0,0 +1,21 @@ using Moli.Desktop.Layout; namespace Moli.Desktop.Runtime; /// <summary>Single command surface consumed by UI, tray, local API and automation.</summary> public interface IDesktopRuntime { Task<OperationResult<AppRuntimeState>> StartAsync(CancellationToken cancellationToken = default); Task<OperationResult> ShutdownAsync(CancellationToken cancellationToken = default); Task<AppRuntimeState> GetStateAsync(CancellationToken cancellationToken = default); Task<OperationResult<AppRuntimeState>> SetEditModeAsync(bool enabled, CancellationToken cancellationToken = default); Task<OperationResult<AppRuntimeState>> SetGlobalLockedAsync(bool enabled, CancellationToken cancellationToken = default); Task<OperationResult<AppRuntimeState>> SetSafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default); Task<OperationResult<AppRuntimeState>> ReloadPluginsAsync(CancellationToken cancellationToken = default); Task<OperationResult<AppRuntimeState>> EnableWallpaperAsync(CancellationToken cancellationToken = default); Task<OperationResult<AppRuntimeState>> DisableWallpaperAsync(CancellationToken cancellationToken = default); Task<OperationResult<WidgetRuntimeState>> UpdateWidgetLayoutAsync( WidgetInstanceId instanceId, WidgetLayoutPatch patch, CancellationToken cancellationToken = default); } src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs
New file @@ -0,0 +1,29 @@ using Moli.Desktop.Configuration; namespace Moli.Desktop.Runtime; /// <summary>Applies runtime commands to real desktop surfaces when window tasks are available.</summary> public interface IRuntimeSurfaceController { Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default); Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default); Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default); Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default); Task DisableWallpaperAsync(CancellationToken cancellationToken = default); Task ShutdownAsync(CancellationToken cancellationToken = default); } public sealed class NullRuntimeSurfaceController : IRuntimeSurfaceController { public Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default) => Task.CompletedTask; public Task DisableWallpaperAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; public Task ShutdownAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; }