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/Win32InteropService.cs | 80 +++++++++++++++++++++++++++------------
1 files changed, 55 insertions(+), 25 deletions(-)
diff --git a/src/Moli.Desktop/Interop/Win32InteropService.cs b/src/Moli.Desktop/Interop/Win32InteropService.cs
index 6bed968..512cf03 100644
--- a/src/Moli.Desktop/Interop/Win32InteropService.cs
+++ b/src/Moli.Desktop/Interop/Win32InteropService.cs
@@ -22,7 +22,9 @@
public void SetClickThrough(IntPtr hwnd, bool enabled)
{
- SetStyleFlag(hwnd, NativeMethods.WsExLayered | NativeMethods.WsExTransparent, enabled, "SetClickThrough");
+ // Only toggle WS_EX_TRANSPARENT. WS_EX_LAYERED is owned by SetLayered / WPF
+ // AllowsTransparency; clearing it here would break per-pixel transparency.
+ SetStyleFlag(hwnd, NativeMethods.WsExTransparent, enabled, "SetClickThrough");
}
public void SetToolWindow(IntPtr hwnd, bool enabled)
@@ -35,19 +37,16 @@
SetStyleFlag(hwnd, NativeMethods.WsExNoActivate, enabled, "SetNoActivate");
}
- public void SetLayered(IntPtr hwnd, bool enabled, byte opacity)
+ public void SetLayered(IntPtr hwnd, bool enabled)
{
+ // Only manage the WS_EX_LAYERED style bit. Never call SetLayeredWindowAttributes here:
+ // it switches the window from WPF AllowsTransparency per-pixel alpha (UpdateLayeredWindow)
+ // to constant-alpha mode, in which transparent pixels composite as opaque black.
+ // Global opacity is handled by Window.Opacity instead.
SetStyleFlag(hwnd, NativeMethods.WsExLayered, enabled, "SetLayered");
- if (enabled && !NativeMethods.SetLayeredWindowAttributes(hwnd, 0, opacity, NativeMethods.LwaAlpha))
- {
- RecordWin32Failure("SetLayeredWindowAttributes", hwnd);
- }
}
- public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, MonitorInfo monitor)
- => MoveToDesktopLayer(hwnd, mode, monitor.Bounds);
-
- public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, RectD bounds)
+ public void MoveToDesktopLayer(IntPtr hwnd, DesktopLayerMode mode, MonitorInfo monitor, RectD bounds)
{
var candidates = mode == DesktopLayerMode.WorkerW
? _strategies.OrderBy(strategy => strategy is WorkerWDesktopLayerStrategy ? 0 : 1)
@@ -60,7 +59,7 @@
continue;
}
- if (strategy.TryAttachDesktopWindow(hwnd, bounds, out var error))
+ if (strategy.TryAttachDesktopWindow(hwnd, monitor, bounds, out var error))
{
return;
}
@@ -139,16 +138,36 @@
return true;
}
- var id = string.IsNullOrWhiteSpace(native.DeviceName)
+ var scale = 1.0;
+ var hr = NativeMethods.GetDpiForMonitor(hMonitor, NativeMethods.MdtEffectiveDpi, out var dpiX, out _);
+ if (hr == 0 && dpiX > 0)
+ {
+ scale = dpiX / 96.0;
+ }
+
+ var physicalBounds = ToRectD(native.Monitor);
+ var physicalWorkArea = ToRectD(native.WorkArea);
+ var dipBounds = new RectD(
+ physicalBounds.X / scale,
+ physicalBounds.Y / scale,
+ physicalBounds.Width / scale,
+ physicalBounds.Height / scale);
+ var dipWorkArea = new RectD(
+ physicalWorkArea.X / scale,
+ physicalWorkArea.Y / scale,
+ physicalWorkArea.Width / scale,
+ physicalWorkArea.Height / scale);
+
+ var deviceName = string.IsNullOrWhiteSpace(native.DeviceName)
? $"monitor-{monitors.Count + 1}"
: native.DeviceName;
monitors.Add(new MonitorInfo
{
- Id = new MonitorId(native.Flags ? "primary" : id),
- Bounds = ToRectD(native.Monitor),
- WorkArea = ToRectD(native.WorkArea),
+ Id = new MonitorId(deviceName),
+ Bounds = dipBounds,
+ WorkArea = dipWorkArea,
IsPrimary = native.Flags,
- Scale = 1
+ Scale = scale
});
return true;
}, IntPtr.Zero);
@@ -160,7 +179,8 @@
Id = new MonitorId("primary"),
Bounds = new RectD(0, 0, SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight),
WorkArea = new RectD(0, 0, SystemParameters.WorkArea.Width, SystemParameters.WorkArea.Height),
- IsPrimary = true
+ IsPrimary = true,
+ Scale = 1
}];
}
@@ -170,15 +190,25 @@
var styleFlag = unchecked((uint)flag);
var currentStyle = unchecked((uint)current);
var updated = enabled ? currentStyle | styleFlag : currentStyle & ~styleFlag;
- NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlExStyle, new IntPtr(updated));
- var error = NativeMethods.GetLastError();
- if (error != 0)
+ if (updated == currentStyle)
{
- _diagnostics.Record(
- DiagnosticSeverity.Warning,
- DiagnosticCodes.Win32InteropFailed,
- $"{operation} failed.",
- new Dictionary<string, string> { ["hwnd"] = hwnd.ToString("X"), ["error"] = error.ToString() });
+ return;
+ }
+
+ NativeMethods.SetWindowLongPtr(hwnd, NativeMethods.GwlExStyle, new IntPtr(unchecked((int)updated)));
+ // Extended-style changes (especially WS_EX_TRANSPARENT) do not reliably update hit-testing
+ // until the frame is refreshed. Without this, clearing click-through in edit/safe-input
+ // mode can leave the window visually present but permanently unclickable.
+ if (!NativeMethods.SetWindowPos(
+ hwnd,
+ IntPtr.Zero,
+ 0,
+ 0,
+ 0,
+ 0,
+ NativeMethods.SwpNoMove | NativeMethods.SwpNoSize | NativeMethods.SwpNoZOrder | NativeMethods.SwpNoActivate | NativeMethods.SwpFrameChanged))
+ {
+ RecordWin32Failure($"{operation}.SetWindowPos", hwnd);
}
}
--
Gitblit v1.9.1