From 09d4e2f1a1bc206c80d8d3287d070c70ec7fd63d Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期五, 14 八月 2026 20:00:52 +0800
Subject: [PATCH] Merge branch 'master' of http://git.linmoli.cn/r/Moli.Desktop

---
 src/Moli.Desktop/Windows/RuntimeSurfaceController.cs |  174 +++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 159 insertions(+), 15 deletions(-)

diff --git a/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs b/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs
index 00fd78c..0e70893 100644
--- a/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs
+++ b/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs
@@ -24,11 +24,12 @@
     private readonly IServiceProvider _services;
     private readonly IRuntimeEventBus _events;
     private readonly Dictionary<WidgetInstanceId, WidgetWindow> _widgets = [];
-    private WallpaperWindow? _wallpaper;
+    private readonly Dictionary<MonitorId, WallpaperWindow> _wallpapers = [];
     private WallpaperRuntimeState _wallpaperState = new();
     private bool _editMode;
     private bool _safeInputMode;
     private IDisposable? _eventSubscription;
+    private readonly Dictionary<WidgetInstanceId, WidgetSessionOverlay> _sessions = [];
 
     public RuntimeSurfaceController(
         IPluginManager plugins,
@@ -64,7 +65,11 @@
         => OnUiThreadAsync(() =>
         {
             _safeInputMode = enabled;
-            _wallpaper?.ApplyState(_wallpaperState, _safeInputMode);
+            foreach (var wallpaper in _wallpapers.Values)
+            {
+                wallpaper.ApplyState(_wallpaperState, _safeInputMode);
+            }
+
             foreach (var widget in _widgets.Values)
             {
                 widget.SetSafeInputMode(enabled);
@@ -103,7 +108,26 @@
                 await window.LoadAsync(cancellationToken).ConfigureAwait(true);
             }
 
-            window.ApplyLayout(layout, _editMode, _safeInputMode);
+            var monitors = _interop.GetMonitors();
+            var monitor = monitors.FirstOrDefault(m => m.Id == layout.MonitorId) ?? monitors.FirstOrDefault();
+            if (monitor is not null)
+            {
+                window.SetMonitorInfo(monitor);
+            }
+
+            window.ApplyLayout(layout, _editMode, _safeInputMode, GetSession(layout.InstanceId));
+        });
+
+    public Task ApplyWidgetSessionOverlayAsync(WidgetLayout layout, WidgetSessionOverlay overlay, CancellationToken cancellationToken = default)
+        => OnUiThreadAsync(async () =>
+        {
+            _sessions[layout.InstanceId] = overlay;
+            if (!_widgets.TryGetValue(layout.InstanceId, out var window))
+            {
+                return;
+            }
+
+            await window.ApplySessionOverlayAsync(overlay, _editMode, _safeInputMode, cancellationToken).ConfigureAwait(true);
         });
 
     public Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default)
@@ -125,14 +149,26 @@
                 return;
             }
 
-            _wallpaper?.Close();
+            foreach (var (_, existing) in _wallpapers)
+            {
+                existing.Close();
+            }
+
+            _wallpapers.Clear();
             _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);
+
+            var monitors = _interop.GetMonitors();
+            foreach (var monitor in monitors)
+            {
+                cancellationToken.ThrowIfCancellationRequested();
+                var wallpaperWindow = new WallpaperWindow(plugin, monitor, _webViews.Create(plugin), _interop, _paths, _diagnostics);
+                _wallpapers[monitor.Id] = wallpaperWindow;
+                wallpaperWindow.Closed += (_, _) => _wallpapers.Remove(monitor.Id);
+                wallpaperWindow.Show();
+                await wallpaperWindow.LoadAsync(wallpaper, cancellationToken).ConfigureAwait(true);
+                wallpaperWindow.ApplyState(wallpaper, _safeInputMode);
+            }
+
             ReapplyDesktopWidgetLayers();
         });
 
@@ -140,8 +176,12 @@
         => OnUiThreadAsync(() =>
         {
             _wallpaperState = _wallpaperState with { IsEnabled = false };
-            _wallpaper?.Close();
-            _wallpaper = null;
+            foreach (var (_, wallpaper) in _wallpapers)
+            {
+                wallpaper.Close();
+            }
+
+            _wallpapers.Clear();
             return Task.CompletedTask;
         });
 
@@ -154,12 +194,116 @@
             }
 
             _widgets.Clear();
-            _wallpaper?.Close();
-            _wallpaper = null;
+            _sessions.Clear();
+            foreach (var (_, wallpaper) in _wallpapers)
+            {
+                wallpaper.Close();
+            }
+
+            _wallpapers.Clear();
             _eventSubscription?.Dispose();
             _eventSubscription = null;
             return Task.CompletedTask;
         });
+
+    public Task SyncAllLayoutsAsync(CancellationToken cancellationToken = default)
+        => OnUiThreadAsync(() =>
+        {
+            var monitors = _interop.GetMonitors();
+            foreach (var (id, window) in _widgets)
+            {
+                cancellationToken.ThrowIfCancellationRequested();
+                var layout = window.CurrentLayout;
+                var monitor = monitors.FirstOrDefault(m => m.Id == layout.MonitorId) ?? monitors.FirstOrDefault();
+                if (monitor is not null)
+                {
+                    window.SetMonitorInfo(monitor);
+                }
+
+                window.ApplyLayout(layout, _editMode, _safeInputMode, GetSession(id));
+            }
+
+            return Task.CompletedTask;
+        });
+
+    public Task SyncWallpaperMonitorsAsync(CancellationToken cancellationToken = default)
+        => OnUiThreadAsync(async () =>
+        {
+            if (_wallpaperState.PluginId is null || _wallpapers.Count == 0)
+            {
+                return;
+            }
+
+            var plugin = await _plugins.GetAsync(_wallpaperState.PluginId.Value, cancellationToken).ConfigureAwait(true);
+            if (plugin is null)
+            {
+                return;
+            }
+
+            var monitors = _interop.GetMonitors();
+            var currentIds = new HashSet<MonitorId>(_wallpapers.Keys);
+            var newIds = new HashSet<MonitorId>(monitors.Select(m => m.Id));
+
+            foreach (var removed in currentIds.Where(id => !newIds.Contains(id)))
+            {
+                if (_wallpapers.TryGetValue(removed, out var removedWindow))
+                {
+                    removedWindow.Close();
+                    _wallpapers.Remove(removed);
+                }
+            }
+
+            foreach (var monitor in monitors)
+            {
+                cancellationToken.ThrowIfCancellationRequested();
+                if (_wallpapers.TryGetValue(monitor.Id, out var existing))
+                {
+                    existing.ApplyState(_wallpaperState, _safeInputMode);
+                }
+                else
+                {
+                    var wallpaperWindow = new WallpaperWindow(plugin, monitor, _webViews.Create(plugin), _interop, _paths, _diagnostics);
+                    _wallpapers[monitor.Id] = wallpaperWindow;
+                    wallpaperWindow.Closed += (_, _) => _wallpapers.Remove(monitor.Id);
+                    wallpaperWindow.Show();
+                    await wallpaperWindow.LoadAsync(_wallpaperState, cancellationToken).ConfigureAwait(true);
+                    wallpaperWindow.ApplyState(_wallpaperState, _safeInputMode);
+                }
+            }
+        });
+
+    public Task ReattachDesktopLayersAsync(CancellationToken cancellationToken = default)
+        => OnUiThreadAsync(() =>
+        {
+            var monitors = _interop.GetMonitors();
+            foreach (var (_, window) in _widgets.Where(kv => kv.Value.CurrentLayout.DisplayLayer == DisplayLayer.Desktop))
+            {
+                cancellationToken.ThrowIfCancellationRequested();
+                var layout = window.CurrentLayout;
+                var monitor = monitors.FirstOrDefault(m => m.Id == layout.MonitorId) ?? monitors.FirstOrDefault();
+                if (monitor is not null)
+                {
+                    window.SetMonitorInfo(monitor);
+                }
+
+                window.ApplyLayout(layout, _editMode, _safeInputMode, GetSession(window.InstanceId));
+            }
+
+            foreach (var (monitorId, wallpaper) in _wallpapers)
+            {
+                cancellationToken.ThrowIfCancellationRequested();
+                var monitor = monitors.FirstOrDefault(m => m.Id == monitorId);
+                if (monitor is not null)
+                {
+                    wallpaper.ApplyState(_wallpaperState, _safeInputMode);
+                }
+            }
+
+            return Task.CompletedTask;
+        });
+
+    private WidgetSessionOverlay GetSession(WidgetInstanceId instanceId)
+        => _sessions.TryGetValue(instanceId, out var overlay) ? overlay : WidgetSessionOverlay.Empty;
 
     private async Task CommitWidgetLayoutAsync(
         WidgetInstanceId instanceId,
@@ -174,7 +318,7 @@
     {
         foreach (var widget in _widgets.Values.Where(widget => widget.CurrentLayout.DisplayLayer == DisplayLayer.Desktop))
         {
-            widget.ApplyLayout(widget.CurrentLayout, _editMode, _safeInputMode);
+            widget.ApplyLayout(widget.CurrentLayout, _editMode, _safeInputMode, GetSession(widget.InstanceId));
         }
     }
 

--
Gitblit v1.9.1