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/Management/ManagementWindow.xaml.cs |   35 +++++++++++
 src/Moli.Desktop/Host/DefaultContentInstaller.cs     |   32 ++++++++++
 src/Moli.Desktop/Management/ManagementWindow.xaml    |    1 
 src/Moli.Desktop/Interop/Win32InteropService.cs      |   26 ++++++--
 src/Moli.Desktop/Windows/WidgetWindow.cs             |   53 ++++++++++++++---
 5 files changed, 128 insertions(+), 19 deletions(-)

diff --git a/src/Moli.Desktop/Host/DefaultContentInstaller.cs b/src/Moli.Desktop/Host/DefaultContentInstaller.cs
index b0690d1..1248653 100644
--- a/src/Moli.Desktop/Host/DefaultContentInstaller.cs
+++ b/src/Moli.Desktop/Host/DefaultContentInstaller.cs
@@ -155,6 +155,11 @@
                     changed = true;
                 }
 
+                if (RepairManifestClickThrough(manifest, layout.Instances))
+                {
+                    changed = true;
+                }
+
                 continue;
             }
 
@@ -225,6 +230,33 @@
         return changed;
     }
 
+    /// <summary>
+    /// Heal instances whose persisted clickThrough drifted from the plugin default.
+    /// Interactive plugins (e.g. LLM palette) ship with clickThrough=false; "鎭㈠榛樿"
+    /// used to force true for every widget and left them permanently unclickable.
+    /// </summary>
+    private static bool RepairManifestClickThrough(PluginManifest manifest, IList<WidgetLayout> layouts)
+    {
+        if (manifest.DefaultLayout.ClickThrough)
+        {
+            return false;
+        }
+
+        var changed = false;
+        var pluginId = new PluginId(manifest.Id);
+        for (var index = 0; index < layouts.Count; index++)
+        {
+            var layout = layouts[index];
+            if (layout.PluginId == pluginId && layout.ClickThrough)
+            {
+                layouts[index] = layout with { ClickThrough = false };
+                changed = true;
+            }
+        }
+
+        return changed;
+    }
+
     private static bool IsAudioVisualizer(PluginManifest manifest)
         => string.Equals(manifest.Id, "sample.audio-visualizer", StringComparison.OrdinalIgnoreCase);
 
diff --git a/src/Moli.Desktop/Interop/Win32InteropService.cs b/src/Moli.Desktop/Interop/Win32InteropService.cs
index 1b56207..512cf03 100644
--- a/src/Moli.Desktop/Interop/Win32InteropService.cs
+++ b/src/Moli.Desktop/Interop/Win32InteropService.cs
@@ -190,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);
         }
     }
 
diff --git a/src/Moli.Desktop/Management/ManagementWindow.xaml b/src/Moli.Desktop/Management/ManagementWindow.xaml
index de834cb..564c81f 100644
--- a/src/Moli.Desktop/Management/ManagementWindow.xaml
+++ b/src/Moli.Desktop/Management/ManagementWindow.xaml
@@ -63,6 +63,7 @@
                         <ComboBox x:Name="WidgetSelector" Width="260" DisplayMemberPath="InstanceId" Margin="0,0,8,0" />
                         <Button Content="鏄剧ず/闅愯棌" Width="96" Margin="0,0,8,0" Click="ToggleWidgetVisible_Click" />
                         <Button Content="閿佸畾/瑙i攣" Width="96" Margin="0,0,8,0" Click="ToggleWidgetLocked_Click" />
+                        <Button x:Name="ClickThroughButton" Content="鐐瑰嚮绌块��" Width="96" Margin="0,0,8,0" Click="ToggleWidgetClickThrough_Click" />
                         <ComboBox x:Name="DisplayLayerSelector" Width="132" Margin="0,0,8,0" />
                         <Button Content="搴旂敤灞傜骇" Width="104" Margin="0,0,8,0" Click="ApplyDisplayLayer_Click" />
                     </WrapPanel>
diff --git a/src/Moli.Desktop/Management/ManagementWindow.xaml.cs b/src/Moli.Desktop/Management/ManagementWindow.xaml.cs
index b2aef4b..f21754e 100644
--- a/src/Moli.Desktop/Management/ManagementWindow.xaml.cs
+++ b/src/Moli.Desktop/Management/ManagementWindow.xaml.cs
@@ -27,6 +27,7 @@
     private readonly IDesktopRuntime _runtime;
     private readonly IDiagnosticsService _diagnostics;
     private readonly ILocalApiStatus _apiStatus;
+    private readonly IPluginManager _plugins;
     private readonly DesktopPaths _paths;
     private readonly DispatcherTimer _timer;
     private AppRuntimeState? _state;
@@ -37,11 +38,13 @@
         IDesktopRuntime runtime,
         IDiagnosticsService diagnostics,
         ILocalApiStatus apiStatus,
+        IPluginManager plugins,
         DesktopPaths paths)
     {
         _runtime = runtime;
         _diagnostics = diagnostics;
         _apiStatus = apiStatus;
+        _plugins = plugins;
         _paths = paths;
         InitializeComponent();
         DisplayLayerSelector.ItemsSource = Enum.GetValues<DisplayLayer>();
@@ -148,6 +151,16 @@
         await ExecuteWidgetAsync(widget.InstanceId, new WidgetLayoutPatch { IsLocked = !widget.IsLocked });
     }
 
+    private async void ToggleWidgetClickThrough_Click(object sender, RoutedEventArgs e)
+    {
+        if (SelectedWidget() is not { } widget)
+        {
+            return;
+        }
+
+        await ExecuteWidgetAsync(widget.InstanceId, new WidgetLayoutPatch { ClickThrough = !widget.ClickThrough });
+    }
+
     private async void ResetLayout_Click(object sender, RoutedEventArgs e)
         => await ResetRuntimeLayoutAsync(confirm: false);
 
@@ -177,6 +190,21 @@
 
         foreach (var widget in _state.Widgets)
         {
+            var clickThrough = true;
+            var displayLayer = DisplayLayer.Desktop;
+            try
+            {
+                var plugin = await _plugins.GetAsync(widget.PluginId);
+                if (plugin is not null)
+                {
+                    clickThrough = plugin.Manifest.DefaultLayout.ClickThrough;
+                    displayLayer = plugin.Manifest.DefaultLayout.DisplayLayer;
+                }
+            }
+            catch
+            {
+            }
+
             await _runtime.UpdateWidgetLayoutAsync(
                 widget.InstanceId,
                 new WidgetLayoutPatch
@@ -184,8 +212,8 @@
                     IsVisible = true,
                     IsLocked = false,
                     Opacity = 1,
-                    ClickThrough = true,
-                    DisplayLayer = DisplayLayer.Desktop
+                    ClickThrough = clickThrough,
+                    DisplayLayer = displayLayer
                 });
         }
 
@@ -261,9 +289,12 @@
         {
             DisplayLayerSelector.SelectedItem = DisplayLayer.Desktop;
             _selectedWidgetForDisplayLayer = null;
+            ClickThroughButton.Content = "鐐瑰嚮绌块��";
             return;
         }
 
+        ClickThroughButton.Content = widget.ClickThrough ? "鍏抽棴绌块��" : "寮�鍚┛閫�";
+
         if (!force)
         {
             return;
diff --git a/src/Moli.Desktop/Windows/WidgetWindow.cs b/src/Moli.Desktop/Windows/WidgetWindow.cs
index 23a87cf..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;
@@ -41,6 +50,7 @@
     private RectD _startBounds;
     private bool _resizing;
     private DisplayLayer? _lastDisplayLayer;
+    private bool _desktopLayerAttached;
 
     public WidgetWindow(
         PluginDescriptor plugin,
@@ -63,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
         {
@@ -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);
@@ -313,7 +330,14 @@
             _interop.SetClickThrough(hwnd, !_editMode && !_safeInputMode && _layout.ClickThrough);
 
             var currentLayer = _layout.DisplayLayer;
-            if (currentLayer == _lastDisplayLayer)
+            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;
             }
@@ -323,24 +347,35 @@
             {
                 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;
-                    parentChanged = true;
                     break;
                 case DisplayLayer.AppTop:
                 case DisplayLayer.Normal:
                 default:
-                    _interop.RestoreTopLevelWindow(hwnd);
+                    if (needsTopLevelRestore)
+                    {
+                        _interop.RestoreTopLevelWindow(hwnd);
+                        _desktopLayerAttached = false;
+                        parentChanged = true;
+                    }
+
                     Topmost = false;
-                    parentChanged = true;
                     break;
             }
 

--
Gitblit v1.9.1