基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 da97410f48a1e94d7e1fe019c69a7c847fe67ddf
修复LLM工具无法点击的问题
5个文件已修改
143 ■■■■■ 已修改文件
src/Moli.Desktop/Host/DefaultContentInstaller.cs 32 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Moli.Desktop/Interop/Win32InteropService.cs 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Moli.Desktop/Management/ManagementWindow.xaml 1 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Moli.Desktop/Management/ManagementWindow.xaml.cs 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/Moli.Desktop/Windows/WidgetWindow.cs 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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);
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);
        }
    }
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="锁定/解锁" 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>
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;
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:
                    if (needsTopLevelRestore)
                    {
                    _interop.RestoreTopLevelWindow(hwnd);
                    Topmost = true;
                        _desktopLayerAttached = false;
                    parentChanged = true;
                    }
                    Topmost = true;
                    break;
                case DisplayLayer.AppTop:
                case DisplayLayer.Normal:
                default:
                    if (needsTopLevelRestore)
                    {
                    _interop.RestoreTopLevelWindow(hwnd);
                    Topmost = false;
                        _desktopLayerAttached = false;
                    parentChanged = true;
                    }
                    Topmost = false;
                    break;
            }