| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | private RectD _startBounds; |
| | | private bool _resizing; |
| | | private DisplayLayer? _lastDisplayLayer; |
| | | private bool _desktopLayerAttached; |
| | | |
| | | public WidgetWindow( |
| | | PluginDescriptor plugin, |
| | |
| | | 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 |
| | | { |
| | |
| | | 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); |
| | |
| | | _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; |
| | | } |
| | |
| | | { |
| | | 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; |
| | | } |
| | | |