| | |
| | | using Moli.Desktop.Plugins; |
| | | using Moli.Desktop.Runtime; |
| | | using Moli.Desktop.WebView; |
| | | using Brushes = System.Windows.Media.Brushes; |
| | | using Cursors = System.Windows.Input.Cursors; |
| | | using HorizontalAlignment = System.Windows.HorizontalAlignment; |
| | | using MouseEventArgs = System.Windows.Input.MouseEventArgs; |
| | | using Point = System.Windows.Point; |
| | | |
| | | namespace Moli.Desktop.Windows; |
| | | |
| | |
| | | private readonly Border _editBorder; |
| | | private readonly Border _resizeGrip; |
| | | private WidgetLayout _layout; |
| | | private MonitorInfo? _monitorInfo; |
| | | private double? _requestedHeight; |
| | | private bool _editMode; |
| | | private bool _safeInputMode; |
| | | private Point? _dragStart; |
| | | private RectD _startBounds; |
| | | private bool _resizing; |
| | | private DisplayLayer? _lastDisplayLayer; |
| | | |
| | | public WidgetWindow( |
| | | PluginDescriptor plugin, |
| | |
| | | AllowsTransparency = true; |
| | | Background = Brushes.Transparent; |
| | | ShowInTaskbar = false; |
| | | Topmost = true; |
| | | Topmost = false; |
| | | SizeToContent = SizeToContent.Manual; |
| | | |
| | | var root = new Grid { Background = Brushes.Transparent }; |
| | |
| | | |
| | | public WidgetInstanceId InstanceId => _layout.InstanceId; |
| | | |
| | | public WidgetLayout CurrentLayout => _layout; |
| | | |
| | | public string HostId => _webView.HostId; |
| | | |
| | | public async Task LoadAsync(CancellationToken cancellationToken = default) |
| | | { |
| | | ApplyLayout(_layout, _editMode, _safeInputMode); |
| | |
| | | Left = layout.Bounds.X; |
| | | Top = layout.Bounds.Y; |
| | | Width = layout.Bounds.Width; |
| | | Height = layout.Bounds.Height; |
| | | Height = Math.Max(layout.Bounds.Height, _requestedHeight ?? layout.Bounds.Height); |
| | | Opacity = Math.Clamp(layout.Opacity, 0.1, 1.0); |
| | | Visibility = layout.IsVisible ? Visibility.Visible : Visibility.Hidden; |
| | | Topmost = layout.DisplayLayer == DisplayLayer.GlobalTop; |
| | | _editBorder.Visibility = editMode ? Visibility.Visible : Visibility.Collapsed; |
| | | _resizeGrip.Visibility = editMode && !layout.IsLocked ? Visibility.Visible : Visibility.Collapsed; |
| | | ApplyWin32Styles(); |
| | | } |
| | | |
| | | public void ApplyRequestedSize(double? width, double? height) |
| | | { |
| | | if (width is > 0) |
| | | { |
| | | Width = Math.Max(LayoutManager.MinWidth, width.Value); |
| | | } |
| | | |
| | | _requestedHeight = height is > 0 && height.Value > _layout.Bounds.Height |
| | | ? height.Value |
| | | : null; |
| | | Height = Math.Max(_layout.Bounds.Height, _requestedHeight ?? _layout.Bounds.Height); |
| | | } |
| | | |
| | | public Task SetEditModeAsync(bool enabled, CancellationToken cancellationToken = default) |
| | |
| | | public void SetSafeInputMode(bool enabled) |
| | | { |
| | | _safeInputMode = enabled; |
| | | ApplyWin32Styles(); |
| | | } |
| | | |
| | | public void SetMonitorInfo(MonitorInfo? monitorInfo) |
| | | { |
| | | _monitorInfo = monitorInfo; |
| | | ApplyWin32Styles(); |
| | | } |
| | | |
| | |
| | | var bounds = new RectD(Left, Top, Width, Height); |
| | | _dragStart = null; |
| | | _resizing = false; |
| | | await _commitLayoutAsync(_layout.InstanceId, new WidgetLayoutPatch { Bounds = bounds }, CancellationToken.None); |
| | | |
| | | var patch = new WidgetLayoutPatch { Bounds = bounds }; |
| | | try |
| | | { |
| | | var monitors = _interop.GetMonitors(); |
| | | if (monitors.Count > 0) |
| | | { |
| | | var bestMonitor = monitors |
| | | .Select(m => new { Monitor = m, Overlap = ComputeOverlapArea(bounds, m.Bounds) }) |
| | | .MaxBy(x => x.Overlap); |
| | | if (bestMonitor is not null && bestMonitor.Monitor.Id != _layout.MonitorId) |
| | | { |
| | | patch = patch with { MonitorId = bestMonitor.Monitor.Id }; |
| | | } |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | } |
| | | |
| | | await _commitLayoutAsync(_layout.InstanceId, patch, CancellationToken.None); |
| | | } |
| | | |
| | | private void BeginPointerOperation(MouseButtonEventArgs e) |
| | |
| | | IsVisible = _layout.IsVisible, |
| | | IsLocked = _layout.IsLocked, |
| | | Opacity = _layout.Opacity, |
| | | ClickThrough = _layout.ClickThrough |
| | | ClickThrough = _layout.ClickThrough, |
| | | DisplayLayer = _layout.DisplayLayer |
| | | }; |
| | | |
| | | private static double ComputeOverlapArea(RectD a, RectD b) |
| | | { |
| | | var overlapX = Math.Max(0, Math.Min(a.Right, b.Right) - Math.Max(a.X, b.X)); |
| | | var overlapY = Math.Max(0, Math.Min(a.Bottom, b.Bottom) - Math.Max(a.Y, b.Y)); |
| | | return overlapX * overlapY; |
| | | } |
| | | |
| | | private void ApplyWin32Styles() |
| | | { |
| | |
| | | try |
| | | { |
| | | _interop.SetToolWindow(hwnd, true); |
| | | _interop.SetLayered(hwnd, true, ToAlpha(_layout.Opacity)); |
| | | _interop.SetLayered(hwnd, true); |
| | | _interop.SetClickThrough(hwnd, !_editMode && !_safeInputMode && _layout.ClickThrough); |
| | | |
| | | var currentLayer = _layout.DisplayLayer; |
| | | if (currentLayer == _lastDisplayLayer) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var parentChanged = false; |
| | | switch (currentLayer) |
| | | { |
| | | case DisplayLayer.Desktop: |
| | | Topmost = false; |
| | | if (_monitorInfo is not null) |
| | | { |
| | | _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitorInfo, _layout.Bounds); |
| | | parentChanged = true; |
| | | } |
| | | |
| | | break; |
| | | case DisplayLayer.GlobalTop: |
| | | _interop.RestoreTopLevelWindow(hwnd); |
| | | Topmost = true; |
| | | parentChanged = true; |
| | | break; |
| | | case DisplayLayer.AppTop: |
| | | case DisplayLayer.Normal: |
| | | default: |
| | | _interop.RestoreTopLevelWindow(hwnd); |
| | | Topmost = false; |
| | | parentChanged = true; |
| | | break; |
| | | } |
| | | |
| | | _lastDisplayLayer = currentLayer; |
| | | |
| | | if (parentChanged) |
| | | { |
| | | _ = _webView.NotifyParentWindowPositionChangedAsync(); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | new Dictionary<string, string> { ["instanceId"] = _layout.InstanceId.Value, ["exception"] = ex.GetType().Name }); |
| | | } |
| | | } |
| | | |
| | | private static byte ToAlpha(double opacity) => (byte)Math.Round(Math.Clamp(opacity, 0.1, 1.0) * 255); |
| | | } |