namespace Lattice.Core.DragDrop.Utilities;
///
/// Утилиты для работы с системой перетаскивания.
///
public static class DragDropUtilities
{
#region Effect Utilities
///
/// Проверяет, совместимы ли эффекты источника и цели.
///
public static bool AreEffectsCompatible(Enums.DragDropEffects sourceEffects, Enums.DragDropEffects targetEffects)
{
if (sourceEffects == Enums.DragDropEffects.None || targetEffects == Enums.DragDropEffects.None)
return false;
return (sourceEffects & targetEffects) != Enums.DragDropEffects.None;
}
///
/// Получает наиболее подходящий эффект на основе доступных.
///
public static Enums.DragDropEffects GetBestEffect(Enums.DragDropEffects available, Enums.DragDropEffects preferred)
{
if ((available & preferred) != Enums.DragDropEffects.None)
return available & preferred;
if ((available & Enums.DragDropEffects.Move) != Enums.DragDropEffects.None)
return Enums.DragDropEffects.Move;
if ((available & Enums.DragDropEffects.Copy) != Enums.DragDropEffects.None)
return Enums.DragDropEffects.Copy;
if ((available & Enums.DragDropEffects.Link) != Enums.DragDropEffects.None)
return Enums.DragDropEffects.Link;
return Enums.DragDropEffects.None;
}
#endregion
#region Geometry Utilities
///
/// Вычисляет расстояние между двумя точками.
///
public static double CalculateDistance(Geometry.Point p1, Geometry.Point p2)
{
var dx = p2.X - p1.X;
var dy = p2.Y - p1.Y;
return Math.Sqrt(dx * dx + dy * dy);
}
///
/// Проверяет, превысило ли перемещение пороговое значение.
///
public static bool HasExceededDragThreshold(Geometry.Point startPoint, Geometry.Point currentPoint, double threshold)
{
var distance = CalculateDistance(startPoint, currentPoint);
return distance >= threshold;
}
///
/// Определяет позицию сброса относительно прямоугольника.
///
public static Enums.DropPosition GetDropPosition(Geometry.Point point, Geometry.Rect bounds, double edgeThreshold = 20.0)
{
if (!bounds.Contains(new Geometry.Point(point.X, point.Y)))
return Enums.DropPosition.Inside;
var relativeX = (point.X - bounds.X) / bounds.Width;
var relativeY = (point.Y - bounds.Y) / bounds.Height;
if (relativeX < edgeThreshold / bounds.Width)
return Enums.DropPosition.Left;
if (relativeX > 1 - edgeThreshold / bounds.Width)
return Enums.DropPosition.Right;
if (relativeY < edgeThreshold / bounds.Height)
return Enums.DropPosition.Top;
if (relativeY > 1 - edgeThreshold / bounds.Height)
return Enums.DropPosition.Bottom;
return Enums.DropPosition.Center;
}
#endregion
#region Factory Methods
///
/// Создает информацию о перетаскивании.
///
public static Models.DragInfo CreateDragInfo(
object data,
Geometry.Point startPosition,
Enums.DragDropEffects allowedEffects = Enums.DragDropEffects.Copy | Enums.DragDropEffects.Move,
object? source = null,
Dictionary? parameters = null)
{
var dragInfo = new Models.DragInfo(data, allowedEffects, startPosition, source);
if (parameters != null)
{
foreach (var param in parameters)
{
dragInfo.SetParameter(param.Key, param.Value);
}
}
return dragInfo;
}
///
/// Создает простую реализацию источника перетаскивания.
///
public static Abstractions.IDragSource CreateSimpleDragSource(
Func