From da97410f48a1e94d7e1fe019c69a7c847fe67ddf Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期三, 12 八月 2026 17:42:02 +0800
Subject: [PATCH] 修复LLM工具无法点击的问题

---
 src/Moli.Desktop/Windows/WidgetWindow.cs |   69 ++++++++++++++++++++++++++++++----
 1 files changed, 61 insertions(+), 8 deletions(-)

diff --git a/src/Moli.Desktop/Windows/WidgetWindow.cs b/src/Moli.Desktop/Windows/WidgetWindow.cs
index 94ecba0..d9041f3 100644
--- a/src/Moli.Desktop/Windows/WidgetWindow.cs
+++ b/src/Moli.Desktop/Windows/WidgetWindow.cs
@@ -13,6 +13,8 @@
 using Moli.Desktop.Runtime;
 using Moli.Desktop.WebView;
 using Brushes = System.Windows.Media.Brushes;
+using Color = System.Windows.Media.Color;
+using SolidColorBrush = System.Windows.Media.SolidColorBrush;
 using Cursors = System.Windows.Input.Cursors;
 using HorizontalAlignment = System.Windows.HorizontalAlignment;
 using MouseEventArgs = System.Windows.Input.MouseEventArgs;
@@ -24,6 +26,13 @@
 public sealed class WidgetWindow : Window
 {
     private const double ResizeGripSize = 18;
+    /// <summary>
+    /// WPF AllowsTransparency uses UpdateLayeredWindow hit-testing against the WPF alpha
+    /// bitmap. Fully transparent pixels are skipped even when WS_EX_TRANSPARENT is off, so a
+    /// Transparent background over WebView2 (HwndHost) makes the widget look like a sticker.
+    /// Alpha=1 keeps hits while remaining visually invisible.
+    /// </summary>
+    private static readonly SolidColorBrush HitTestBackground = CreateHitTestBackground();
     private readonly PluginDescriptor _plugin;
     private readonly IWebViewHost _webView;
     private readonly IWin32InteropService _interop;
@@ -40,6 +49,8 @@
     private Point? _dragStart;
     private RectD _startBounds;
     private bool _resizing;
+    private DisplayLayer? _lastDisplayLayer;
+    private bool _desktopLayerAttached;
 
     public WidgetWindow(
         PluginDescriptor plugin,
@@ -62,12 +73,12 @@
         WindowStyle = WindowStyle.None;
         ResizeMode = ResizeMode.NoResize;
         AllowsTransparency = true;
-        Background = Brushes.Transparent;
+        Background = HitTestBackground;
         ShowInTaskbar = false;
         Topmost = false;
         SizeToContent = SizeToContent.Manual;
 
-        var root = new Grid { Background = Brushes.Transparent };
+        var root = new Grid { Background = HitTestBackground };
         root.Children.Add(webView.View);
         _editBorder = new Border
         {
@@ -149,7 +160,6 @@
             ? height.Value
             : null;
         Height = Math.Max(_layout.Bounds.Height, _requestedHeight ?? _layout.Bounds.Height);
-        ApplyWin32Styles();
     }
 
     public Task SetEditModeAsync(bool enabled, CancellationToken cancellationToken = default)
@@ -298,6 +308,13 @@
         return overlapX * overlapY;
     }
 
+    private static SolidColorBrush CreateHitTestBackground()
+    {
+        var brush = new SolidColorBrush(Color.FromArgb(1, 0, 0, 0));
+        brush.Freeze();
+        return brush;
+    }
+
     private void ApplyWin32Styles()
     {
         var hwnd = _interop.GetWindowHandle(this);
@@ -311,27 +328,63 @@
             _interop.SetToolWindow(hwnd, true);
             _interop.SetLayered(hwnd, true);
             _interop.SetClickThrough(hwnd, !_editMode && !_safeInputMode && _layout.ClickThrough);
-            switch (_layout.DisplayLayer)
+
+            var currentLayer = _layout.DisplayLayer;
+            var layerChanged = currentLayer != _lastDisplayLayer;
+            var needsDesktopAttach = currentLayer == DisplayLayer.Desktop
+                && _monitorInfo is not null
+                && (!_desktopLayerAttached || layerChanged);
+            var needsTopLevelRestore = currentLayer != DisplayLayer.Desktop
+                && (layerChanged || _desktopLayerAttached);
+
+            if (!layerChanged && !needsDesktopAttach && !needsTopLevelRestore)
+            {
+                return;
+            }
+
+            var parentChanged = false;
+            switch (currentLayer)
             {
                 case DisplayLayer.Desktop:
                     Topmost = false;
-                    if (_monitorInfo is not null)
+                    if (needsDesktopAttach)
                     {
-                        _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitorInfo, _layout.Bounds);
+                        _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitorInfo!, _layout.Bounds);
+                        _desktopLayerAttached = true;
+                        parentChanged = true;
                     }
 
                     break;
                 case DisplayLayer.GlobalTop:
-                    _interop.RestoreTopLevelWindow(hwnd);
+                    if (needsTopLevelRestore)
+                    {
+                        _interop.RestoreTopLevelWindow(hwnd);
+                        _desktopLayerAttached = false;
+                        parentChanged = true;
+                    }
+
                     Topmost = true;
                     break;
                 case DisplayLayer.AppTop:
                 case DisplayLayer.Normal:
                 default:
-                    _interop.RestoreTopLevelWindow(hwnd);
+                    if (needsTopLevelRestore)
+                    {
+                        _interop.RestoreTopLevelWindow(hwnd);
+                        _desktopLayerAttached = false;
+                        parentChanged = true;
+                    }
+
                     Topmost = false;
                     break;
             }
+
+            _lastDisplayLayer = currentLayer;
+
+            if (parentChanged)
+            {
+                _ = _webView.NotifyParentWindowPositionChangedAsync();
+            }
         }
         catch (Exception ex)
         {

--
Gitblit v1.9.1