98 lines
3.5 KiB
C#
98 lines
3.5 KiB
C#
using Prism.Ioc;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using XP.Common.Logging.Interfaces;
|
|
using XplorePlane.ViewModels;
|
|
|
|
namespace XplorePlane.Views
|
|
{
|
|
public partial class OperatorToolboxView : UserControl
|
|
{
|
|
public const string DragFormat = "PipelineOperatorKey";
|
|
|
|
private readonly ILoggerService _logger;
|
|
private Point _dragStartPoint;
|
|
private bool _isDragging;
|
|
|
|
public OperatorToolboxView()
|
|
{
|
|
InitializeComponent();
|
|
|
|
var baseLogger = ContainerLocator.Current?.Resolve<ILoggerService>();
|
|
_logger = baseLogger?.ForModule<OperatorToolboxView>() ?? baseLogger;
|
|
|
|
Loaded += OnLoaded;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 当宿主为无边框窗口时,启用标题栏拖拽和关闭按钮
|
|
/// </summary>
|
|
private void SetupBorderlessWindowSupport()
|
|
{
|
|
var hostWindow = Window.GetWindow(this);
|
|
if (hostWindow is OperatorToolboxWindow toolboxWindow)
|
|
{
|
|
TitleBar.MouseLeftButtonDown += toolboxWindow.TitleBar_MouseLeftButtonDown;
|
|
CloseBtn.Visibility = Visibility.Visible;
|
|
CloseBtn.Click += toolboxWindow.CloseButton_Click;
|
|
}
|
|
}
|
|
|
|
private void OnLoaded(object sender, RoutedEventArgs e)
|
|
{
|
|
SetupBorderlessWindowSupport();
|
|
|
|
ToolboxListBox.PreviewMouseLeftButtonDown += OnPreviewMouseDown;
|
|
ToolboxListBox.PreviewMouseMove += OnPreviewMouseMove;
|
|
_logger?.Debug("OperatorToolboxView 原生拖拽源已注册, DataContext={Type}",
|
|
DataContext?.GetType().Name);
|
|
}
|
|
|
|
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
_dragStartPoint = e.GetPosition(null);
|
|
_isDragging = false;
|
|
}
|
|
|
|
private void OnPreviewMouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
if (e.LeftButton != MouseButtonState.Pressed) return;
|
|
|
|
var pos = e.GetPosition(null);
|
|
var diff = pos - _dragStartPoint;
|
|
|
|
if (!_isDragging
|
|
&& (System.Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
|
|
|| System.Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
|
|
{
|
|
_isDragging = true;
|
|
|
|
var hitResult = System.Windows.Media.VisualTreeHelper.HitTest(
|
|
ToolboxListBox, e.GetPosition(ToolboxListBox));
|
|
|
|
OperatorDescriptor descriptor = null;
|
|
var node = hitResult?.VisualHit as DependencyObject;
|
|
while (node != null)
|
|
{
|
|
if (node is FrameworkElement fe && fe.DataContext is OperatorDescriptor d)
|
|
{
|
|
descriptor = d;
|
|
break;
|
|
}
|
|
node = System.Windows.Media.VisualTreeHelper.GetParent(node);
|
|
}
|
|
|
|
if (descriptor == null)
|
|
{
|
|
_logger?.Warn("拖拽初始化失败:HitTest 未命中算子项");
|
|
return;
|
|
}
|
|
|
|
_logger?.Info("开始拖拽算子:{OperatorKey} ({DisplayName})", descriptor.Key, descriptor.DisplayName);
|
|
var data = new DataObject(DragFormat, descriptor.Key);
|
|
DragDrop.DoDragDrop(ToolboxListBox, data, DragDropEffects.Copy);
|
|
}
|
|
}
|
|
}
|
|
} |