Убраны синхронные методы
This commit is contained in:
@@ -130,7 +130,8 @@ public sealed class DragDropService : IDragDropService
|
||||
{
|
||||
// Инициализация таймера очистки (каждые 5 минут)
|
||||
_cleanupTimer = new Timer(CleanupExpiredTargets, null,
|
||||
TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5));
|
||||
TimeSpan.FromMinutes(Constants.DragDropConstants.TargetLifetimeMinutes / 2),
|
||||
TimeSpan.FromMinutes(Constants.DragDropConstants.TargetLifetimeMinutes / 2));
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -266,7 +267,7 @@ public sealed class DragDropService : IDragDropService
|
||||
Models.DragInfo? dragInfo = null;
|
||||
|
||||
// Проверка возможности начала перетаскивания
|
||||
if (EnableAsyncOperations && source is Abstractions.IAsyncDragSource asyncSource)
|
||||
if (EnableAsyncOperations && source is Abstractions.IDragSource asyncSource)
|
||||
{
|
||||
var result = await ExecuteWithTimeoutAsync(
|
||||
asyncSource.CanStartDragAsync(),
|
||||
@@ -280,8 +281,11 @@ public sealed class DragDropService : IDragDropService
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!source.CanStartDrag(out dragInfo) || dragInfo == null)
|
||||
var startDragResult = await source.CanStartDragAsync();
|
||||
if (!startDragResult.CanStart || startDragResult.DragInfo == null)
|
||||
return false;
|
||||
|
||||
dragInfo = startDragResult.DragInfo;
|
||||
}
|
||||
|
||||
var updatedDragInfo = dragInfo.CloneWithPosition(startPosition);
|
||||
@@ -289,7 +293,7 @@ public sealed class DragDropService : IDragDropService
|
||||
// Начало перетаскивания
|
||||
bool started;
|
||||
|
||||
if (EnableAsyncOperations && source is Abstractions.IAsyncDragSource asyncSource2)
|
||||
if (EnableAsyncOperations && source is Abstractions.IDragSource asyncSource2)
|
||||
{
|
||||
started = await ExecuteWithTimeoutAsync(
|
||||
asyncSource2.StartDragAsync(updatedDragInfo),
|
||||
@@ -298,7 +302,7 @@ public sealed class DragDropService : IDragDropService
|
||||
}
|
||||
else
|
||||
{
|
||||
started = source.StartDrag(updatedDragInfo);
|
||||
started = await source.StartDragAsync(updatedDragInfo);
|
||||
}
|
||||
|
||||
if (!started)
|
||||
@@ -355,8 +359,8 @@ public sealed class DragDropService : IDragDropService
|
||||
}
|
||||
|
||||
var updatedDragInfo = context.DragInfo.CloneWithPosition(position);
|
||||
context.DragInfo.Dispose();
|
||||
context.DragInfo = updatedDragInfo;
|
||||
context.DragInfo.Dispose();
|
||||
|
||||
// Поиск новой цели сброса
|
||||
var newDropTarget = await FindDropTargetAsync(position, updatedDragInfo);
|
||||
@@ -369,7 +373,7 @@ public sealed class DragDropService : IDragDropService
|
||||
await ExecuteTargetOperationAsync(
|
||||
context.CurrentDropTarget,
|
||||
t => t.DragLeaveAsync(),
|
||||
t => t.DragLeave(),
|
||||
t => t.DragLeaveAsync(),
|
||||
"DragLeave");
|
||||
}
|
||||
|
||||
@@ -395,7 +399,7 @@ public sealed class DragDropService : IDragDropService
|
||||
await ExecuteTargetOperationAsync(
|
||||
context.CurrentDropTarget,
|
||||
t => t.DragOverAsync(dropInfo),
|
||||
t => t.DragOver(dropInfo),
|
||||
t => t.DragOverAsync(dropInfo),
|
||||
"DragOver");
|
||||
}
|
||||
|
||||
@@ -443,7 +447,7 @@ public sealed class DragDropService : IDragDropService
|
||||
await ExecuteTargetOperationAsync(
|
||||
context.CurrentDropTarget,
|
||||
t => t.DropAsync(dropInfo),
|
||||
t => t.Drop(dropInfo),
|
||||
t => t.DropAsync(dropInfo),
|
||||
"Drop");
|
||||
|
||||
if (dropInfo.Handled)
|
||||
@@ -457,7 +461,7 @@ public sealed class DragDropService : IDragDropService
|
||||
await ExecuteSourceOperationAsync(
|
||||
context.Source,
|
||||
s => s.DragCompletedAsync(context.DragInfo, effects),
|
||||
s => s.DragCompleted(context.DragInfo, effects),
|
||||
s => s.DragCompletedAsync(context.DragInfo, effects),
|
||||
"DragCompleted",
|
||||
effects);
|
||||
|
||||
@@ -504,7 +508,7 @@ public sealed class DragDropService : IDragDropService
|
||||
await ExecuteSourceOperationAsync(
|
||||
context.Source,
|
||||
s => s.DragCancelledAsync(context.DragInfo),
|
||||
s => s.DragCancelled(context.DragInfo),
|
||||
s => s.DragCancelledAsync(context.DragInfo),
|
||||
"DragCancelled");
|
||||
|
||||
_dragCancelled?.Invoke(this, new DragCancelledEventArgs(context.DragInfo));
|
||||
@@ -522,30 +526,6 @@ public sealed class DragDropService : IDragDropService
|
||||
|
||||
#endregion
|
||||
|
||||
#region Synchronous Operations (for compatibility)
|
||||
|
||||
public bool StartDrag(Abstractions.IDragSource source, Geometry.Point startPosition)
|
||||
{
|
||||
return Task.Run(() => StartDragAsync(source, startPosition)).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public void UpdateDrag(Geometry.Point position)
|
||||
{
|
||||
Task.Run(() => UpdateDragAsync(position)).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public Enums.DragDropEffects EndDrag(Geometry.Point position)
|
||||
{
|
||||
return Task.Run(() => EndDragAsync(position)).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
public void CancelDrag()
|
||||
{
|
||||
Task.Run(CancelDragAsync).GetAwaiter().GetResult();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Methods
|
||||
|
||||
public void ClearAllDropTargets()
|
||||
@@ -593,39 +573,30 @@ public sealed class DragDropService : IDragDropService
|
||||
_dropTargetsLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
foreach (var info in _dropTargets.Values)
|
||||
{
|
||||
if (!info.Bounds.Contains(position))
|
||||
continue;
|
||||
// Фильтруем цели по границам и сортируем по приоритету
|
||||
var candidates = _dropTargets.Values
|
||||
.Where(info => info.Bounds.Contains(position))
|
||||
.OrderByDescending(info => info.Priority)
|
||||
.ToList();
|
||||
|
||||
foreach (var info in candidates)
|
||||
{
|
||||
var dropInfo = new Models.DropInfo(
|
||||
dragInfo.Data,
|
||||
position,
|
||||
dragInfo.AllowedEffects,
|
||||
info.Target);
|
||||
|
||||
bool canAccept;
|
||||
|
||||
if (EnableAsyncOperations && info.Target is Abstractions.IAsyncDropTarget asyncTarget)
|
||||
{
|
||||
canAccept = await ExecuteWithTimeoutAsync(
|
||||
asyncTarget.CanAcceptDropAsync(dropInfo),
|
||||
"CanAcceptDropAsync",
|
||||
info.Target);
|
||||
}
|
||||
else
|
||||
{
|
||||
canAccept = info.Target.CanAcceptDrop(dropInfo);
|
||||
}
|
||||
bool canAccept = await ExecuteWithTimeoutAsync(
|
||||
info.Target.CanAcceptDropAsync(dropInfo),
|
||||
"CanAcceptDropAsync",
|
||||
info.Target);
|
||||
|
||||
if (canAccept)
|
||||
{
|
||||
info.LastAccessTime = DateTime.UtcNow;
|
||||
|
||||
if (bestTarget == null || info.Priority > bestTarget.Priority)
|
||||
{
|
||||
bestTarget = info;
|
||||
}
|
||||
bestTarget = info;
|
||||
break; // Берем первую подходящую с наивысшим приоритетом
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -639,13 +610,13 @@ public sealed class DragDropService : IDragDropService
|
||||
|
||||
private async Task ExecuteTargetOperationAsync(
|
||||
Abstractions.IDropTarget target,
|
||||
Func<Abstractions.IAsyncDropTarget, Task> asyncOperation,
|
||||
Func<Abstractions.IDropTarget, Task> asyncOperation,
|
||||
Action<Abstractions.IDropTarget> syncOperation,
|
||||
string operationName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (EnableAsyncOperations && target is Abstractions.IAsyncDropTarget asyncTarget)
|
||||
if (EnableAsyncOperations && target is Abstractions.IDropTarget asyncTarget)
|
||||
{
|
||||
await ExecuteWithTimeoutAsync(
|
||||
asyncOperation(asyncTarget),
|
||||
@@ -666,14 +637,14 @@ public sealed class DragDropService : IDragDropService
|
||||
|
||||
private async Task ExecuteSourceOperationAsync(
|
||||
Abstractions.IDragSource source,
|
||||
Func<Abstractions.IAsyncDragSource, Task> asyncOperation,
|
||||
Func<Abstractions.IDragSource, Task> asyncOperation,
|
||||
Action<Abstractions.IDragSource> syncOperation,
|
||||
string operationName,
|
||||
Enums.DragDropEffects effects = Enums.DragDropEffects.None)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (EnableAsyncOperations && source is Abstractions.IAsyncDragSource asyncSource)
|
||||
if (EnableAsyncOperations && source is Abstractions.IDragSource asyncSource)
|
||||
{
|
||||
await ExecuteWithTimeoutAsync(
|
||||
asyncOperation(asyncSource),
|
||||
@@ -745,16 +716,17 @@ public sealed class DragDropService : IDragDropService
|
||||
|
||||
private void CleanupExpiredTargets(object? state)
|
||||
{
|
||||
var expirationTime = DateTime.UtcNow.AddMinutes(-10); // Цели старше 10 минут
|
||||
var expirationTime = DateTime.UtcNow.AddMinutes(-Constants.DragDropConstants.TargetLifetimeMinutes);
|
||||
|
||||
_dropTargetsLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
var idsToRemove = new List<string>();
|
||||
var currentTarget = _currentDragOperation?.CurrentDropTarget;
|
||||
|
||||
foreach (var kvp in _dropTargets)
|
||||
{
|
||||
if (kvp.Value.LastAccessTime < expirationTime)
|
||||
if (kvp.Value.LastAccessTime < expirationTime && !ReferenceEquals(kvp.Value.Target, currentTarget))
|
||||
{
|
||||
idsToRemove.Add(kvp.Key);
|
||||
}
|
||||
@@ -797,6 +769,9 @@ public sealed class DragDropService : IDragDropService
|
||||
{
|
||||
if (_disposed) return;
|
||||
|
||||
var timer = Interlocked.Exchange(ref _cleanupTimer, null);
|
||||
timer?.Dispose();
|
||||
|
||||
_cleanupTimer?.Dispose();
|
||||
_cleanupTimer = null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user