using System.Windows; using Microsoft.Extensions.DependencyInjection; 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.WebView; namespace Moli.Desktop.Windows; /// Creates and updates real WPF desktop surfaces for runtime commands. public sealed class RuntimeSurfaceController : IRuntimeSurfaceController { private readonly IPluginManager _plugins; private readonly IWin32InteropService _interop; private readonly IWebViewHostFactory _webViews; private readonly DesktopPaths _paths; private readonly IDiagnosticsService _diagnostics; private readonly IServiceProvider _services; private readonly Dictionary _widgets = []; private WallpaperWindow? _wallpaper; private WallpaperRuntimeState _wallpaperState = new(); private bool _editMode; private bool _safeInputMode; public RuntimeSurfaceController( IPluginManager plugins, IWin32InteropService interop, IWebViewHostFactory webViews, DesktopPaths paths, IDiagnosticsService diagnostics, IServiceProvider services) { _plugins = plugins; _interop = interop; _webViews = webViews; _paths = paths; _diagnostics = diagnostics; _services = services; } public Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default) => OnUiThreadAsync(async () => { _editMode = enabled; foreach (var widget in _widgets.Values) { cancellationToken.ThrowIfCancellationRequested(); await widget.SetEditModeAsync(enabled, cancellationToken).ConfigureAwait(true); } }); public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default) => OnUiThreadAsync(() => { _safeInputMode = enabled; _wallpaper?.ApplyState(_wallpaperState, _safeInputMode); foreach (var widget in _widgets.Values) { widget.SetSafeInputMode(enabled); } return Task.CompletedTask; }); public Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default) => OnUiThreadAsync(async () => { if (!_widgets.TryGetValue(layout.InstanceId, out var window)) { var plugin = await _plugins.GetAsync(layout.PluginId, cancellationToken).ConfigureAwait(true); if (plugin is null || plugin.State == PluginState.Invalid) { _diagnostics.Record( DiagnosticSeverity.Warning, "WidgetPluginNotFound", "Widget plugin was not available for layout surface.", new Dictionary { ["instanceId"] = layout.InstanceId.Value, ["pluginId"] = layout.PluginId.Value }); return; } window = new WidgetWindow( plugin, layout, _webViews.Create(plugin), _interop, _paths, _diagnostics, CommitWidgetLayoutAsync); _widgets[layout.InstanceId] = window; window.Closed += (_, _) => _widgets.Remove(layout.InstanceId); window.Show(); await window.LoadAsync(cancellationToken).ConfigureAwait(true); } window.ApplyLayout(layout, _editMode, _safeInputMode); }); public Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default) => OnUiThreadAsync(async () => { if (wallpaper.PluginId is null) { return; } var plugin = await _plugins.GetAsync(wallpaper.PluginId.Value, cancellationToken).ConfigureAwait(true); if (plugin is null) { _diagnostics.Record( DiagnosticSeverity.Warning, "WallpaperPluginNotFound", "Wallpaper plugin was not available for surface creation.", new Dictionary { ["pluginId"] = wallpaper.PluginId.Value.Value }); return; } _wallpaper?.Close(); _wallpaperState = wallpaper; var monitor = _interop.GetMonitors().FirstOrDefault(monitor => monitor.IsPrimary) ?? _interop.GetMonitors().First(); _wallpaper = new WallpaperWindow(plugin, monitor, _webViews.Create(plugin), _interop, _paths, _diagnostics); _wallpaper.Closed += (_, _) => _wallpaper = null; _wallpaper.Show(); await _wallpaper.LoadAsync(wallpaper, cancellationToken).ConfigureAwait(true); _wallpaper.ApplyState(wallpaper, _safeInputMode); }); public Task DisableWallpaperAsync(CancellationToken cancellationToken = default) => OnUiThreadAsync(() => { _wallpaperState = _wallpaperState with { IsEnabled = false }; _wallpaper?.Close(); _wallpaper = null; return Task.CompletedTask; }); public Task ShutdownAsync(CancellationToken cancellationToken = default) => OnUiThreadAsync(() => { foreach (var widget in _widgets.Values.ToArray()) { widget.Close(); } _widgets.Clear(); _wallpaper?.Close(); _wallpaper = null; return Task.CompletedTask; }); private async Task CommitWidgetLayoutAsync( WidgetInstanceId instanceId, WidgetLayoutPatch patch, CancellationToken cancellationToken) { var runtime = _services.GetRequiredService(); await runtime.UpdateWidgetLayoutAsync(instanceId, patch, cancellationToken).ConfigureAwait(true); } private static async Task OnUiThreadAsync(Func action) { var dispatcher = Application.Current?.Dispatcher; if (dispatcher is null || dispatcher.CheckAccess()) { await action().ConfigureAwait(true); return; } await await dispatcher.InvokeAsync(action).Task.ConfigureAwait(false); } }