/
DragDropBehavior.cs
567 lines (484 loc) · 20.9 KB
/
DragDropBehavior.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Media;
using Xenko.Core.Assets.Editor.ViewModel;
using Xenko.Core.Annotations;
using Xenko.Core.Presentation.Adorners;
using Xenko.Core.Presentation.Extensions;
using Xenko.Core.Translation;
namespace Xenko.Core.Assets.Editor.View.Behaviors
{
public static class DragDropBehavior
{
/// <summary>
/// Can't drop here
/// </summary>
public static readonly string InvalidDropAreaMessage = Tr._p("Message", "Can't drop here");
}
/// <summary>
/// Base class for drag and drop behaviors.
/// </summary>
/// <typeparam name="TControl">The type the <see cref="Behavior{TControl}"/> can be attached to.</typeparam>
/// <typeparam name="TContainer"></typeparam>
public abstract class DragDropBehavior<TControl, TContainer> : Behavior<TControl>, IDragDropBehavior
where TControl : FrameworkElement
where TContainer : FrameworkElement
{
public const double InsertThreshold = 4;
/// <summary>
/// Identifies the <see cref="CanDrag"/> dependency property.
/// </summary>
public static readonly DependencyProperty CanDragProperty =
DependencyProperty.Register(nameof(CanDrag), typeof(bool), typeof(DragDropBehavior<TControl, TContainer>), new PropertyMetadata(true, CanDragChanged));
/// <summary>
/// Identifies the <see cref="CanDrop"/> dependency property.
/// </summary>
public static readonly DependencyProperty CanDropProperty =
DependencyProperty.Register(nameof(CanDrop), typeof(bool), typeof(DragDropBehavior<TControl, TContainer>), new PropertyMetadata(true, CanDropChanged));
/// <summary>
/// Identifies the <see cref="CanInsert"/> dependency property.
/// </summary>
public static readonly DependencyProperty CanInsertProperty =
DependencyProperty.Register(nameof(CanInsert), typeof(bool), typeof(DragDropBehavior<TControl, TContainer>), new PropertyMetadata(false, CanInsertChanged));
/// <summary>
/// Identifies the <see cref="DisplayDropAdorner"/> dependency property.
/// </summary>
public static readonly DependencyProperty DisplayDropAdornerProperty =
DependencyProperty.Register(nameof(DisplayDropAdorner), typeof(DisplayDropAdorner), typeof(DragDropBehavior<TControl, TContainer>), new PropertyMetadata(DisplayDropAdorner.Never));
/// <summary>
/// Identifies the <see cref="DisplayInsertAdorner"/> dependency property.
/// </summary>
public static readonly DependencyProperty DisplayInsertAdornerProperty =
DependencyProperty.Register(nameof(DisplayInsertAdorner), typeof(bool), typeof(DragDropBehavior<TControl, TContainer>), new PropertyMetadata(false));
public bool CanDrag { get => (bool)GetValue(CanDragProperty); set => SetValue(CanDragProperty, value); }
public bool CanDrop { get => (bool)GetValue(CanDropProperty); set => SetValue(CanDropProperty, value); }
public bool CanInsert { get => (bool)GetValue(CanInsertProperty); set => SetValue(CanInsertProperty, value); }
public DisplayDropAdorner DisplayDropAdorner { get => (DisplayDropAdorner)GetValue(DisplayDropAdornerProperty); set => SetValue(DisplayDropAdornerProperty, value); }
public bool DisplayInsertAdorner { get => (bool)GetValue(DisplayInsertAdornerProperty); set => SetValue(DisplayInsertAdornerProperty, value); }
public DataTemplate DragVisualTemplate { get; set; }
public bool UsePreviewEvents { get; set; }
protected Point DragStartPoint { get; private set; }
protected object DragStartOriginalSource { get; set; }
protected Window DragWindow { get; private set; }
/// <summary>
/// Indicates whether a dragging operation is in progress.
/// </summary>
protected bool IsDragging { get; private set; }
/// <summary>
/// Called when the <see cref="CanDrag"/> property has changed.
/// </summary>
private static void CanDragChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((DragDropBehavior<TControl, TContainer>)d).CanDragChanged();
}
/// <summary>
/// Called when the <see cref="CanDrop"/> property has changed.
/// </summary>
private static void CanDropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((DragDropBehavior<TControl, TContainer>)d).CanDropChanged();
}
/// <summary>
/// Called when the <see cref="CanInsert"/> property has changed.
/// </summary>
private static void CanInsertChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((DragDropBehavior<TControl, TContainer>)d).CanInsertChanged();
}
/// <inheritdoc/>
protected override void OnAttached()
{
if (CanDrag)
SubscribeToDragEvents();
if (CanDrop || CanInsert)
SubscribeToDropEvents();
DragDropAdornerManager.RegisterElement(AssociatedObject);
base.OnAttached();
}
/// <inheritdoc/>
protected override void OnDetaching()
{
base.OnDetaching();
DragDropAdornerManager.UnregisterElement(AssociatedObject);
UnsubscribeFromDragEvents();
UnsubscribeFromDropEvents();
}
/// <summary>
/// Occurs when the <see cref="CanDrag"/> property changed.
/// </summary>
protected virtual void CanDragChanged()
{
if (AssociatedObject == null)
return;
if (CanDrag)
SubscribeToDragEvents();
else
UnsubscribeFromDragEvents();
}
/// <summary>
/// Occurs when the <see cref="CanDrop"/> property changed.
/// </summary>
protected virtual void CanDropChanged()
{
if (AssociatedObject == null || CanInsert)
return;
if (CanDrop)
SubscribeToDropEvents();
else
UnsubscribeFromDropEvents();
}
/// <summary>
/// Occurs when the <see cref="CanInsert"/> property changed.
/// </summary>
protected virtual void CanInsertChanged()
{
if (AssociatedObject == null || CanDrop)
return;
if (CanInsert)
SubscribeToDropEvents();
else
UnsubscribeFromDropEvents();
}
protected virtual bool CanInitializeDrag(object originalSource)
{
return true;
}
private void SubscribeToDragEvents()
{
AssociatedObject.PreviewMouseLeftButtonDown += PreviewMouseLeftButtonDown;
AssociatedObject.PreviewMouseMove += PreviewMouseMove;
AssociatedObject.PreviewMouseUp += PreviewMouseUp;
if (UsePreviewEvents)
{
AssociatedObject.PreviewDragLeave += OnDragLeave;
}
else
{
AssociatedObject.DragLeave += OnDragLeave;
}
AssociatedObject.GiveFeedback += OnGiveFeedback;
}
private void SubscribeToDropEvents()
{
AssociatedObject.AllowDrop = true;
if (UsePreviewEvents)
{
AssociatedObject.PreviewDrop += OnDrop;
AssociatedObject.PreviewDragOver += OnDragOver;
}
else
{
AssociatedObject.Drop += OnDrop;
AssociatedObject.DragOver += OnDragOver;
}
}
private void UnsubscribeFromDragEvents()
{
AssociatedObject.GiveFeedback -= OnGiveFeedback;
if (UsePreviewEvents)
{
AssociatedObject.PreviewDragLeave -= OnDragLeave;
}
else
{
AssociatedObject.DragLeave -= OnDragLeave;
}
AssociatedObject.PreviewMouseMove -= PreviewMouseMove;
AssociatedObject.PreviewMouseLeftButtonDown -= PreviewMouseLeftButtonDown;
}
private void UnsubscribeFromDropEvents()
{
if (UsePreviewEvents)
{
AssociatedObject.PreviewDragOver -= OnDragOver;
AssociatedObject.PreviewDrop -= OnDrop;
}
else
{
AssociatedObject.DragOver -= OnDragOver;
AssociatedObject.Drop -= OnDrop;
}
}
protected DragDropEffects DoDragDrop(DependencyObject dragSource)
{
var data = InitializeDrag(DragStartOriginalSource);
if (data == null)
return DragDropEffects.None;
DragWindow = new DragWindow
{
Content = new ContentControl { Content = data, ContentTemplate = DragVisualTemplate }
};
DragWindow.Show();
try
{
IsDragging = true;
return DragDrop.DoDragDrop(dragSource, new DataObject(DragContainer.Format, data), DragDropEffects.All);
}
catch (COMException)
{
return DragDropEffects.None;
}
finally
{
IsDragging = false;
if (DragWindow != null)
{
DragWindow.Close();
DragWindow = null;
}
}
}
[CanBeNull]
protected virtual TContainer GetContainer(object source)
{
var frameworkElement = source as FrameworkElement;
var contentElement = source as FrameworkContentElement;
if (contentElement != null)
{
frameworkElement = contentElement.Parent as FrameworkElement;
}
return frameworkElement as TContainer ?? frameworkElement?.FindVisualParentOfType<TContainer>();
}
[CanBeNull]
protected virtual IAddChildViewModel GetDropTargetItem(TContainer container)
{
return container?.DataContext as IAddChildViewModel;
}
[CanBeNull]
protected virtual IInsertChildViewModel GetInsertTargetItem(TContainer container, Point position, out InsertPosition insertPosition)
{
insertPosition = InsertPosition.Before;
return null;
}
[NotNull]
protected abstract IEnumerable<object> GetItemsToDrag(TContainer container);
[CanBeNull]
protected object InitializeDrag(object originalSource)
{
if (!CanDrag)
return null;
if (!CanInitializeDrag(originalSource))
{
return null;
}
object data = null;
var container = GetContainer(originalSource);
var itemsToDrag = GetItemsToDrag(container).ToList();
if (itemsToDrag.Count > 0)
{
var dragContainer = new DragContainer(itemsToDrag);
data = dragContainer;
}
return data;
}
protected bool OnDragLeave(IDataObject data)
{
// Invalidate current drag status
var dragContainer = DragDropHelper.GetDragContainer(data);
if (dragContainer != null)
{
dragContainer.IsAccepted = false;
dragContainer.Message = DragDropBehavior.InvalidDropAreaMessage;
}
var itemsToDrop = DragDropHelper.GetItemsToDrop(dragContainer, data as DataObject);
if (itemsToDrop == null)
return false;
if (DragDropHelper.ShouldDisplayDropAdorner(DisplayDropAdorner, itemsToDrop))
{
var localPos = AssociatedObject.GetCursorRelativePosition();
if (localPos.X <= 0 || localPos.Y <= 0 || localPos.X >= AssociatedObject.ActualWidth || localPos.Y >= AssociatedObject.ActualHeight)
{
DragDropAdornerManager.SetAdornerState(AssociatedObject, HighlightAdornerState.Visible);
}
}
return true;
}
protected bool OnDragOver(TContainer container, Point position, IDataObject data, [NotNull] RoutedEventArgs e)
{
var dragContainer = DragDropHelper.GetDragContainer(data);
DragDropAdornerManager.ClearInsertAdorner();
// Invalidate current drag status
if (dragContainer != null)
{
dragContainer.IsAccepted = false;
dragContainer.Message = DragDropBehavior.InvalidDropAreaMessage;
}
// Check if we can drop and if we have a valid target.
if (container == null)
return false;
var itemsToDrop = DragDropHelper.GetItemsToDrop(dragContainer, data as DataObject);
if (itemsToDrop == null)
return false;
string message;
// Insertion "override" add, so let's check this first
if (CanInsert)
{
InsertPosition insertPosition;
var target = GetInsertTargetItem(container, position, out insertPosition);
if (target != null && target.CanInsertChildren(itemsToDrop, insertPosition, ComputeModifiers(), out message))
{
if (dragContainer != null)
{
dragContainer.IsAccepted = true;
dragContainer.Message = message;
}
// The event must be handled otherwise OnDrop won't be invoked
e.Handled = true;
DragDropAdornerManager.UpdateInsertAdorner(container, insertPosition);
return true;
}
}
if (CanDrop)
{
var target = GetDropTargetItem(container);
if (target == null)
return false;
var isAccepted = target.CanAddChildren(itemsToDrop, ComputeModifiers(), out message);
if (dragContainer != null)
{
dragContainer.IsAccepted = isAccepted;
dragContainer.Message = message;
}
// The event must be handled otherwise OnDrop won't be invoked
if (isAccepted)
e.Handled = true;
if (DragDropHelper.ShouldDisplayDropAdorner(DisplayDropAdorner, itemsToDrop))
{
var adornerState = isAccepted ? HighlightAdornerState.HighlightAccept : HighlightAdornerState.HighlightRefuse;
DragDropAdornerManager.SetAdornerState(AssociatedObject, adornerState);
}
return isAccepted;
}
return false;
}
protected bool OnDrop(TContainer container, Point position, IDataObject data, RoutedEventArgs e)
{
DragDropAdornerManager.ClearInsertAdorner();
if (DragWindow != null)
{
DragWindow.Close();
DragWindow = null;
}
if (container == null)
return false;
if (CanInsert)
{
// Check if we can drop and if we have a valid target.
InsertPosition insertPosition;
var target = GetInsertTargetItem(container, position, out insertPosition);
if (target != null)
{
var dragContainer = DragDropHelper.GetDragContainer(data);
var itemsToDrop = DragDropHelper.GetItemsToDrop(dragContainer, data as DataObject);
string message;
if (itemsToDrop != null && target.CanInsertChildren(itemsToDrop, insertPosition, ComputeModifiers(), out message))
{
target.InsertChildren(itemsToDrop, insertPosition, ComputeModifiers());
if (e != null) e.Handled = true;
return true;
}
}
}
if (CanDrop)
{
// Check if we can drop and if we have a valid target.
var target = GetDropTargetItem(container);
if (target == null)
return false;
var dragContainer = DragDropHelper.GetDragContainer(data);
var itemsToDrop = DragDropHelper.GetItemsToDrop(dragContainer, data as DataObject);
string message;
if (itemsToDrop != null && target.CanAddChildren(itemsToDrop, ComputeModifiers(), out message))
{
target.AddChildren(itemsToDrop, ComputeModifiers());
if (e != null) e.Handled = true;
}
}
return true;
}
private void PreviewMouseUp(object sender, [NotNull] MouseButtonEventArgs e)
{
DragStartPoint = e.GetPosition(AssociatedObject);
DragStartOriginalSource = null;
}
private void PreviewMouseLeftButtonDown(object sender, [NotNull] MouseButtonEventArgs e)
{
DragStartPoint = e.GetPosition(AssociatedObject);
DragStartOriginalSource = e.OriginalSource;
}
private void PreviewMouseMove(object sender, [NotNull] MouseEventArgs e)
{
// Note: it is possible that multiple controls could sent an event during the same frame (e.g. a ContentControl and its content).
// The drag drop operation could be triggered by the first event so we need to prevent any subsequent event from trigerring another operation.
if (IsDragging || e.LeftButton != MouseButtonState.Pressed || DragStartOriginalSource == null)
return;
var delta = e.GetPosition(AssociatedObject) - DragStartPoint;
if (Math.Abs(delta.X) >= SystemParameters.MinimumHorizontalDragDistance || Math.Abs(delta.Y) >= SystemParameters.MinimumVerticalDragDistance)
{
DoDragDrop((DependencyObject)sender);
}
}
private void OnGiveFeedback(object sender, GiveFeedbackEventArgs e)
{
if (DragWindow == null)
return;
// Get root window
var rootWindow = Window.GetWindow((DependencyObject)sender);
if (rootWindow == null)
return;
// Get position in WPF screen coordinates
var point = rootWindow.GetCursorScreenPosition();
// Get the relative DPI between the two windows
var rootDpi = VisualTreeHelper.GetDpi(rootWindow);
var dragDpi = VisualTreeHelper.GetDpi(DragWindow);
// Calculate relative DPI scale
var dpiRatioX = rootDpi.DpiScaleX / dragDpi.DpiScaleX;
var dpiRatioY = rootDpi.DpiScaleY / dragDpi.DpiScaleY;
// Move drag window accordingly
DragWindow.Left = (point.X + 16) * dpiRatioX;
DragWindow.Top = point.Y * dpiRatioY;
}
private void OnDragOver(object sender, [NotNull] DragEventArgs e)
{
e.Effects = DragDropEffects.None;
// Check if we can drop and if we have a valid target.
var container = GetContainer(e.OriginalSource);
if (container == null)
return;
if (OnDragOver(container, e.GetPosition(container), e.Data, e))
{
e.Effects = DragDropEffects.Move;
}
}
private void OnDrop(object sender, [NotNull] DragEventArgs e)
{
var container = GetContainer(e.OriginalSource);
if (container == null)
return;
OnDrop(container, e.GetPosition(container), e.Data, e);
}
private void OnDragLeave(object sender, [NotNull] DragEventArgs e)
{
OnDragLeave(e.Data);
}
private static AddChildModifiers ComputeModifiers()
{
var modifiers = AddChildModifiers.None;
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
modifiers |= AddChildModifiers.Ctrl;
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
modifiers |= AddChildModifiers.Shift;
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
modifiers |= AddChildModifiers.Alt;
return modifiers;
}
}
}