From 9b5711d3124622b1f1b18500d442c76492da877a Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期一, 17 八月 2026 16:52:33 +0800
Subject: [PATCH] 优化llm插件及bug修复
---
src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs | 153 ++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 134 insertions(+), 19 deletions(-)
diff --git a/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs b/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs
index bbda7f6..9756b3f 100644
--- a/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs
+++ b/src/Moli.Desktop/Interop/WorkerWDesktopLayerStrategy.cs
@@ -1,3 +1,4 @@
+using Moli.Desktop.Configuration;
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Layout;
@@ -17,18 +18,56 @@
public bool IsSupported() => NativeMethods.FindWindow("Progman", null) != IntPtr.Zero;
- public bool TryAttachDesktopWindow(IntPtr hwnd, RectD bounds, out string? error)
+ public bool TryAttachDesktopWindow(IntPtr hwnd, MonitorInfo monitor, RectD bounds, out string? error)
{
error = null;
- var desktopHost = FindDesktopHostWindow(out var hostClass);
- if (desktopHost == IntPtr.Zero)
+ var scale = monitor.Scale;
+ var physicalX = (int)(bounds.X * scale);
+ var physicalY = (int)(bounds.Y * scale);
+ var physicalW = (int)(bounds.Width * scale);
+ var physicalH = (int)(bounds.Height * scale);
+
+ var workers = EnumerateWorkerWindows();
+ if (workers.Count == 0)
{
- error = "Explorer desktop host window was not found.";
+ error = "No WorkerW or Progman desktop host window found.";
_diagnostics.Record(
DiagnosticSeverity.Warning,
DiagnosticCodes.DesktopLayerAttachFailed,
- "Explorer desktop host window was not found.",
+ "No WorkerW or Progman desktop host window found.",
new Dictionary<string, string> { ["strategy"] = Name });
+ return false;
+ }
+
+ var desktopHost = MatchWorkerWToMonitor(workers, monitor);
+ if (desktopHost == IntPtr.Zero)
+ {
+ error = "No matching WorkerW for target monitor.";
+ _diagnostics.Record(
+ DiagnosticSeverity.Warning,
+ DiagnosticCodes.DesktopLayerAttachFailed,
+ "No matching WorkerW for target monitor.",
+ new Dictionary<string, string>
+ {
+ ["strategy"] = Name,
+ ["monitorId"] = monitor.Id.Value
+ });
+ return false;
+ }
+
+ desktopHost = ValidateAndRefreshHandle(desktopHost, monitor, ref workers);
+ if (desktopHost == IntPtr.Zero)
+ {
+ error = "WorkerW handle became invalid and no replacement was found.";
+ _diagnostics.Record(
+ DiagnosticSeverity.Warning,
+ DiagnosticCodes.DesktopLayerAttachFailed,
+ "WorkerW handle invalidated and re-enumeration failed.",
+ new Dictionary<string, string>
+ {
+ ["strategy"] = Name,
+ ["monitorId"] = monitor.Id.Value
+ });
return false;
}
@@ -45,13 +84,17 @@
}
}
+ NativeMethods.GetWindowRect(desktopHost, out var hostRect);
+ var clientX = physicalX - hostRect.Left;
+ var clientY = physicalY - hostRect.Top;
+
if (!NativeMethods.SetWindowPos(
hwnd,
IntPtr.Zero,
- (int)bounds.X,
- (int)bounds.Y,
- (int)bounds.Width,
- (int)bounds.Height,
+ clientX,
+ clientY,
+ physicalW,
+ physicalH,
NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpFrameChanged | NativeMethods.SwpShowWindow))
{
error = $"SetWindowPos failed with {NativeMethods.GetLastError()}";
@@ -64,13 +107,39 @@
DiagnosticSeverity.Info,
DiagnosticCodes.DesktopLayerStrategySelected,
"Desktop layer strategy selected.",
- new Dictionary<string, string> { ["strategy"] = Name, ["host"] = hostClass });
+ new Dictionary<string, string>
+ {
+ ["strategy"] = Name,
+ ["host"] = desktopHost.ToString("X"),
+ ["monitorId"] = monitor.Id.Value,
+ ["scale"] = scale.ToString("F2")
+ });
return true;
}
- private static IntPtr FindDesktopHostWindow(out string hostClass)
+ private static IntPtr ValidateAndRefreshHandle(
+ IntPtr candidate,
+ MonitorInfo monitor,
+ ref List<(IntPtr hwnd, NativeMethods.Rect rect)> workers)
{
- hostClass = "WorkerW";
+ if (NativeMethods.IsWindow(candidate))
+ {
+ return candidate;
+ }
+
+ workers.RemoveAll(w => w.hwnd == candidate);
+ if (workers.Count == 0)
+ {
+ workers = EnumerateWorkerWindows();
+ }
+
+ return MatchWorkerWToMonitor(workers, monitor);
+ }
+
+ private static List<(IntPtr hwnd, NativeMethods.Rect rect)> EnumerateWorkerWindows()
+ {
+ var workers = new List<(IntPtr, NativeMethods.Rect)>();
+
var progman = NativeMethods.FindWindow("Progman", null);
if (progman != IntPtr.Zero)
{
@@ -92,7 +161,6 @@
out _);
}
- IntPtr workerW = IntPtr.Zero;
NativeMethods.EnumWindows((topHandle, _) =>
{
var shellView = NativeMethods.FindWindowEx(topHandle, IntPtr.Zero, "SHELLDLL_DefView", null);
@@ -101,17 +169,64 @@
return true;
}
- workerW = NativeMethods.FindWindowEx(IntPtr.Zero, topHandle, "WorkerW", null);
- return workerW == IntPtr.Zero;
+ var workerW = NativeMethods.FindWindowEx(IntPtr.Zero, topHandle, "WorkerW", null);
+ if (workerW != IntPtr.Zero && NativeMethods.IsWindow(workerW) && NativeMethods.GetWindowRect(workerW, out var rect))
+ {
+ workers.Add((workerW, rect));
+ }
+
+ return true;
}, IntPtr.Zero);
- if (workerW != IntPtr.Zero)
+ if (workers.Count == 0 && progman != IntPtr.Zero && NativeMethods.GetWindowRect(progman, out var progmanRect))
{
- return workerW;
+ workers.Add((progman, progmanRect));
}
- hostClass = "Progman";
- return progman;
+ return workers;
+ }
+
+ private static IntPtr MatchWorkerWToMonitor(
+ List<(IntPtr hwnd, NativeMethods.Rect rect)> workers,
+ MonitorInfo monitor)
+ {
+ if (workers.Count == 0)
+ {
+ return IntPtr.Zero;
+ }
+
+ if (workers.Count == 1)
+ {
+ return workers[0].hwnd;
+ }
+
+ var scale = monitor.Scale;
+ var monitorLeft = monitor.Bounds.X * scale;
+ var monitorTop = monitor.Bounds.Y * scale;
+ var monitorRight = (monitor.Bounds.X + monitor.Bounds.Width) * scale;
+ var monitorBottom = (monitor.Bounds.Y + monitor.Bounds.Height) * scale;
+
+ var bestMatch = workers[0].hwnd;
+ var bestOverlap = 0.0;
+
+ foreach (var (hwnd, rect) in workers)
+ {
+ var overlapLeft = Math.Max(monitorLeft, (double)rect.Left);
+ var overlapTop = Math.Max(monitorTop, (double)rect.Top);
+ var overlapRight = Math.Min(monitorRight, (double)rect.Right);
+ var overlapBottom = Math.Min(monitorBottom, (double)rect.Bottom);
+ var overlapWidth = Math.Max(0, overlapRight - overlapLeft);
+ var overlapHeight = Math.Max(0, overlapBottom - overlapTop);
+ var overlapArea = overlapWidth * overlapHeight;
+
+ if (overlapArea > bestOverlap)
+ {
+ bestOverlap = overlapArea;
+ bestMatch = hwnd;
+ }
+ }
+
+ return bestMatch;
}
private static void SetDesktopChildStyle(IntPtr hwnd)
--
Gitblit v1.9.1