alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
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
using GongSolutions.Wpf.DragDrop;
using GongSolutions.Wpf.DragDrop.Utilities;
using OpenTap;
using System.Windows;
 
namespace OpenTapEditor.Util
{
    public class StepTreeDragHandler: DefaultDropHandler
    {
        public bool CanConvert(IDropInfo dropInfo)
        {
            if (!(dropInfo.TargetItem is TestStep target))
            {
                return false;
            }
 
            if (!(dropInfo.Data is TestStep source))
            {
                return false;
            }
 
            if (source == target)
            {
                return false;
            }
            var tType = target.GetType();
            var sType = source.GetType();
            if (!dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter))
            {
                return true;
            }
 
            if (!TestStepList.AllowChild(tType, sType))
            {
                return false;
            }
 
            //if(source.)
 
            //if (target.sequence != source.sequence)
            //{
            //    return false;
            //}
            return true;
        }
 
        public override void DragOver(IDropInfo dropInfo)
        {
            if (CanConvert(dropInfo))
            {
                dropInfo.Effects = DragDropEffects.Move;
 
                if (dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter))
                {
                    dropInfo.DropTargetAdorner = null;
                }
                else
                {
                    dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
                }
 
                //if (dropInfo.DropTargetAdorner == DropTargetAdorners.Highlight) { 
 
                //}
                //dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
                dropInfo.DropTargetHintState = DropHintState.Active;
                dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
            }
            else
            {
                dropInfo.Effects = DragDropEffects.None;
                dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
                dropInfo.DropTargetHintState = DropHintState.Error;
            }
        }
 
        public override void Drop(IDropInfo dropInfo)
        {
            if (dropInfo?.DragInfo == null)
            {
                return;
            }
 
            if (!CanConvert(dropInfo))
            {
                return;
            }
 
            ITestStep target = dropInfo.TargetItem as ITestStep;
 
            var insertIndex = GetInsertIndex(dropInfo);
            var destinationList = dropInfo.TargetCollection.TryGetList();
            var data = ExtractData(dropInfo.Data).OfType<object>().ToList();
            bool isSameCollection = false;
 
            var copyData = ShouldCopyData(dropInfo);
            if (!copyData)
            {
                var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
                if (sourceList != null)
                {
                    isSameCollection = sourceList.IsSameObservableCollection(destinationList);
                    if (!isSameCollection)
                    {
                        foreach (var o in data)
                        {
                            var index = sourceList.IndexOf(o);
                            if (index != -1)
                            {
                                sourceList.RemoveAt(index);
 
                                // If source is destination too fix the insertion index
                                if (destinationList != null && ReferenceEquals(sourceList, destinationList) && index < insertIndex)
                                {
                                    --insertIndex;
                                }
                            }
                        }
                    }
                }
            }
 
            if (destinationList != null)
            {
                var objects2Insert = new List<object>();
 
                // check for cloning
                var cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link);
 
                foreach (var o in data)
                {
                    var obj2Insert = o;
                    if (cloneData)
                    {
                        if (o is ICloneableDragItem cloneableItem)
                        {
                            obj2Insert = cloneableItem.CloneItem(dropInfo);
                        }
                        else if (o is ICloneable cloneable)
                        {
                            obj2Insert = cloneable.Clone();
                        }
                    }
 
                    objects2Insert.Add(obj2Insert);
 
                    if (!cloneData && isSameCollection)
                    {
                        var index = destinationList.IndexOf(o);
                        if (index != -1)
                        {
                            if (insertIndex > index)
                            {
                                insertIndex--;
                            }
 
                            Move(destinationList, index, insertIndex++);
                        }
                    }
                    else
                    {
                        destinationList.Insert(insertIndex++, obj2Insert);
                    }
 
                    if (obj2Insert is IDragItemSource dragItemSource)
                    {
                        dragItemSource.ItemDropped(dropInfo);
                    }
                }
 
                SelectDroppedItems(dropInfo, objects2Insert);
            }
        }
    }
}