chr
2026-04-08 53e656200368a983e563550e2cc1acbc6d86b729
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
using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap.Addin;
using OpenTapEditor.Util;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using UILib;
 
namespace OpenTapEditor
{
    public class TestVariableConverter : IValueConverter
    {
        public static TestVariableConverter Instance = new TestVariableConverter();
 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is TestVariable tv)
            {
                if (parameter?.ToString() == "TypeName")
                {
                    return tv.IsArray ? $"{tv.Type}[]" : tv.Type.ToString();
                }
                if (parameter?.ToString() == "CanEdit")
                {
                    return !tv.IsArray && tv.Type != TestVariableType.Container && tv.Type != TestVariableType.Refrence;
                }
            }
            return value;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
 
 
    public partial class TestVariableWrapper : ObservableObject
    {
        [ObservableProperty]
        private TestVariable value;
 
        [ObservableProperty]
        private TestVariable parent;
 
 
        private bool ValueCanEdit => Value?.IsArray == false;
        private bool NameCanEdit => Value?.IsArray == true;
    }
 
    public class TestVariableMenuItem : MenuItem
    {
        public IEnumerable<TestVariable> Source { get; set; }
        public TestVariable Variable { get; set; }
        public TestVariable Parent { get; set; }
 
        public bool TargetIsArray { get; set; }
        public TestVariableType TargetType { get; set; }
    }
 
    /// <summary>
    /// VariableTree.xaml 的交互逻辑
    /// </summary>
    public partial class VariableTree : UserControl
    {
        public VariableTree()
        {
            InitializeComponent();
            BindingOperations.SetBinding(fileGlobalView, TreeListView.ItemsSourceProperty,
            new Binding(nameof(Datasource))
            {
                Source = this,
                Mode = BindingMode.TwoWay,
            });
        }
 
        public static DependencyProperty DatasourceProperty = DependencyProperty.Register(
            "Datasource",
            typeof(ObservableCollection<TestVariable>),
            typeof(VariableTree),
            new PropertyMetadata(null)
        );
 
        public string Prefix { get; set; }
 
        public ObservableCollection<TestVariable> Datasource
        {
            get => (ObservableCollection<TestVariable>)GetValue(DatasourceProperty);
            set
            {
                SetValue(DatasourceProperty, value);
            }
        }
 
        private void OnPreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            DependencyObject source = e.OriginalSource as DependencyObject;
            while (source != null && source.GetType() != typeof(TreeListViewItem))
            {
                source = System.Windows.Media.VisualTreeHelper.GetParent(source);
            }
 
            if (source != null)
            {
                TreeListViewItem item = source as TreeListViewItem;
                item.Focus();
                var variable = item.DataContext;
                CreateContextMenu(item, variable);
            }
            else
            {
                CreateContextMenu(null, null);
            }
        }
 
        public List<object> RefreshMenuItems(TestVariable parent)
        {
            var allSteps = StepTypeLoader.Steps;
            var StepMenuItems = new List<object>(allSteps.Count);
 
            foreach (TestVariableType type in Enum.GetValues(typeof(TestVariableType)))
            {
                TestVariableMenuItem menuItem = new TestVariableMenuItem();
                menuItem.Header = type.ToString();
                menuItem.Click += MenuItem_Click;
                menuItem.TargetIsArray = false;
                menuItem.TargetType = type;
                menuItem.Parent = parent;
                StepMenuItems.Add(menuItem);
            }
 
            var arrItem = new MenuItem();
            arrItem.Header = "Array Of...";
            foreach (TestVariableType type in Enum.GetValues(typeof(TestVariableType)))
            {
                TestVariableMenuItem menuItem = new TestVariableMenuItem();
                menuItem.Header = type.ToString();
                menuItem.Click += MenuItem_Click;
                menuItem.TargetIsArray = true;
                menuItem.TargetType = type;
                menuItem.Parent = parent;
                arrItem.Items.Add(menuItem);
            }
            StepMenuItems.Add(arrItem);
            return StepMenuItems;
        }
 
        /// <summary>
        /// 显示菜单
        /// </summary>
        /// <param name="positionItem"></param>
        private void CreateContextMenu(TreeListViewItem item, object obj)
        {
            var v = obj as TestVariable;
 
            ContextMenu contextMenu = new ContextMenu();
            contextMenu.Closed += ContextMenu_Closed;
            var parent = item?.parent?.DataContext as TestVariable;
 
 
            TestVariableMenuItem menuItem = new TestVariableMenuItem();
            if (v == null)
            {
                menuItem.Header = "插入";
 
                foreach (var i in RefreshMenuItems(null))
                {
                    menuItem.Items.Add(i);
                }
                contextMenu.Items.Add(menuItem);
            }
            else if (v.IsArray)
            {
                menuItem.Header = "插入元素";
                menuItem.Variable = v;
                menuItem.Parent = v;
                menuItem.Click += MenuItem_Click;
                contextMenu.Items.Add(menuItem);
            }
            else if (v.Type == TestVariableType.Container)
            {
                menuItem.Header = "插入";
                foreach (var i in RefreshMenuItems(v))
                {
                    menuItem.Items.Add(i);
                }
                contextMenu.Items.Add(menuItem);
            }
 
            if (v != null)
            {
                menuItem = new TestVariableMenuItem();
                menuItem.Header = "删除";
                menuItem.Click += Delete_Click;
                menuItem.Variable = v;
                menuItem.Tag = item;
                contextMenu.Items.Add(menuItem);
 
                menuItem = new TestVariableMenuItem();
                menuItem.Header = "复制名称";
                menuItem.Click += CopyName_Click;
                menuItem.Tag = item;
                contextMenu.Items.Add(menuItem);
            }
            this.fileGlobalView.ContextMenu = contextMenu;
        }
 
        private void ContextMenu_Closed(object sender, RoutedEventArgs e)
        {
            this.fileGlobalView.ContextMenu = null;
        }
 
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            TestVariableMenuItem item = sender as TestVariableMenuItem;
            var v = item.Parent;
 
            if (v == null)
            {
                Datasource.Add(new TestVariable
                {
                    IsArray = item.TargetIsArray,
                    Type = item.TargetType,
                    Value = item.TargetIsArray ? null : TestVariable.DefaultValue(item.TargetType)
                });
                return;
            }
 
            if (v.IsArray)
            {
                v.Children.Add(new TestVariable
                {
                    Name = $"[{v.Children.Count}]",
                    IsArray = false,
                    Type = v.Type,
                    Value = TestVariable.DefaultValue(v.Type)
                });
                for (int i = 0; i < v.Children.Count; i++)
                {
                    v.Children[i].Name = $"[{i}]";
                }
                return;
            }
 
            if (v.Type == TestVariableType.Container)
            {
                v.Children.Add(new TestVariable
                {
                    IsArray = item.TargetIsArray,
                    Type = item.TargetType,
                    Value = item.TargetIsArray ? null : TestVariable.DefaultValue(item.TargetType)
                });
                return;
            }
        }
 
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            var tv = ((TestVariableMenuItem)sender).Variable;
            var item = ((TestVariableMenuItem)sender).Tag as TreeListViewItem;
            if (item.parent == null)
            {
                Datasource.Remove(tv);
            }
            else
            {
                var parentData = item.parent.DataContext as TestVariable;
                parentData.Children.Remove(tv);
                if (parentData.IsArray)
                {
                    for (int i = 0; i < parentData.Children.Count; i++)
                    {
                        parentData.Children[i].Name = $"[{i}]";
                    }
                }
            }
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var tv = ((Button)sender).Tag as TestVariable;
            MessageBox.Show(tv.Name);
        }
 
        private void CopyName_Click(object sender, RoutedEventArgs e)
        {
            var item = ((TestVariableMenuItem)sender).Tag as TreeListViewItem;
            var tv = item?.DataContext as TestVariable;
 
            string name = "";
            while (tv != null)
            {
                if (tv.IsArray)
                {
                    name = tv.Name + name;
                }
                else
                {
                    name = tv.Name + (string.IsNullOrEmpty(name) ? "" : $".{name}");
                }
                item = item.parent;
                tv = item?.DataContext as TestVariable;
            }
            Clipboard.SetText(string.IsNullOrEmpty(Prefix) ? name : $"{Prefix}.{name}");
        }
    }
}