chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap;
using OpenTapEditor.Provider;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Serialization;
 
namespace OpenTapEditor
{
    [ObservableObject]
    public partial class SettingNode
    {
        [ObservableProperty]
        private string title;
 
        [ObservableProperty]
        private ObservableCollection<SettingNode> children;
        public List<string> Groups { get; set; }
        public AnnotationCollection Item { get; set; }
 
    }
 
    /// <summary>
    /// StepSettingView.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class StepSettingView : UserControl
    {
        [ObservableProperty]
        private ObservableCollection<SettingNode> list;
 
        private object obj;
        private StackPanel panel = new StackPanel();
 
        private AnnotationCollection annotations { get; set; }
        private List<AnnotationCollection> items { get; set; }
 
        static ITypeData resourceTypeData = TypeData.FromType(typeof(IResource));
        public StepSettingView()
        {
            DataContext = this;
            InitializeComponent();
        }
 
        public Dictionary<string, Expander> Groups;
 
        public static bool FilterMember(IMemberData member)
        {
            if (member.DeclaringType.DescendsTo(resourceTypeData) && member.Name == nameof(Resource.Name))
                return true;
            if (member.GetAttribute<BrowsableAttribute>() is BrowsableAttribute attr)
                return attr.Browsable;
            if (member.HasAttribute<OutputAttribute>())
                return true;
            return member.Attributes.Any(a => a is XmlIgnoreAttribute) == false && member.Writable;
        }
 
        AnnotationCollection[] getMembers()
        {
            return annotations?.Get<IMembersAnnotation>()?.Members
                .Where(x =>
                {
                    var member = x.Get<IMemberAnnotation>().Member;
                    if (member.DeclaringType.DescendsTo(resourceTypeData) && member.Name == nameof(Resource.Name))
                        return true;
 
                    return x.Get<IAccessAnnotation>()?.IsVisible ?? false;
                })
                .Where(x =>
                {
                    var member = x.Get<IMemberAnnotation>()?.Member;
                    if (member == null || (member.GetAttribute<SubmitAttribute>() != null && x.Get<IAvailableValuesAnnotationProxy>() != null)) return false;
                    return FilterMember(member);
                })
                .ToArray();
        }
 
        private SettingNode CreateNode(AnnotationCollection annotations)
        {
            string name = annotations.Get<DisplayAttribute>().Name;
            var node = new SettingNode();
            node.Item = annotations;
            return node;
        }
 
        private object RenderControl(AnnotationCollection x) {
            if (x == null)
                return null;
            var multi = x.Get<IMultiSelectAnnotationProxy>();
 
 
 
            try
            {
                if (x.Get<IObjectValueAnnotation>()?.Value is Action)
                    return new Button() { Content = x.Get<DisplayAttribute>().Name };
 
                var stack = new StackPanel { Orientation = Orientation.Horizontal };
                
 
                var nameBuilder = new StringBuilder();
                string value = ((x.Get<IAvailableValuesAnnotation>() as IStringReadOnlyValueAnnotation)?.Value
                                ?? x.Get<IStringValueAnnotation>()?.Value
                                ?? x.Get<IStringReadOnlyValueAnnotation>()?.Value
                                ?? x.Get<IAvailableValuesAnnotationProxy>()?.SelectedValue?.Source?.ToString()
                                ?? x.Get<IObjectValueAnnotation>()?.Value)?.ToString()
                            ?? "...";
                // replace new lines with spaces for viewing.
                value = value.Replace("\n", " ").Replace("\r", "");
 
                var icons = x.GetAll<IIconAnnotation>().ToArray();
                var icons2 = new HashSet<string>(icons.Select(y => y.IconName));
                bool icon(string name) => icons2.Contains(name);
                nameBuilder.Clear();
                if (icon(IconNames.OutputAssigned))
                    nameBuilder.Append('●'); // 
                else if (icon(IconNames.Output))
                    nameBuilder.Append(" ⃝"); //
                if (icon(IconNames.Input))
                {
                    nameBuilder.Append("●"); // ●
                    nameBuilder.Append("→"); // 
                }
                if (icon(IconNames.Parameterized))
                    nameBuilder.Append("♦");// 
                if (x.Get<IMemberAnnotation>()?.Member is IParameterMemberData)
                    nameBuilder.Append("◊");// 
 
                if (nameBuilder.Length > 0)
                    nameBuilder.Append(" ");
 
                nameBuilder.Append(x.Get<DisplayAttribute>().Name);
 
                stack.Children.Add(new TextBlock { Text = nameBuilder.ToString() });
                stack.Children.Add(new TextBox { Text = value });
                //nameBuilder.Append(": ");
                //nameBuilder.Append(value);
 
 
 
                // Don't show member name if layout is fullrow
                if (x.Get<IMemberAnnotation>()?.Member.GetAttribute<LayoutAttribute>()?.Mode == LayoutMode.FullRow)
                    return stack;
                
 
                // Check validation rules
                var step = x.Source as IValidatingObject;
                var propertyName = x.Get<IMemberAnnotation>()?.Member?.Name;
                var rule = step?.Rules.FirstOrDefault(r => r.PropertyName == propertyName && r?.IsValid() == false);
                if (rule != null)
                    stack.Children.Add(new TextBlock { Text = "!" });
 
                return stack;
            }
            catch
            {
                return null;
            }
        }
 
        string getTitle(AnnotationCollection x)
        {
            if (x == null)
                return "";
 
            try
            {
                var nameBuilder = new StringBuilder();
                string value = ((x.Get<IAvailableValuesAnnotation>() as IStringReadOnlyValueAnnotation)?.Value
                                ?? x.Get<IStringValueAnnotation>()?.Value
                                ?? x.Get<IStringReadOnlyValueAnnotation>()?.Value
                                ?? x.Get<IAvailableValuesAnnotationProxy>()?.SelectedValue?.Source?.ToString()
                                ?? x.Get<IObjectValueAnnotation>()?.Value)?.ToString()
                            ?? "...";
                // replace new lines with spaces for viewing.
                value = value.Replace("\n", " ").Replace("\r", "");
 
                if (x.Get<IObjectValueAnnotation>()?.Value is Action)
                    return $"[ {x.Get<DisplayAttribute>().Name} ]";
 
                // Don't show member name if layout is fullrow
                if (x.Get<IMemberAnnotation>()?.Member.GetAttribute<LayoutAttribute>()?.Mode == LayoutMode.FullRow)
                    return value;
                var icons = x.GetAll<IIconAnnotation>().ToArray();
                var icons2 = new HashSet<string>(icons.Select(y => y.IconName));
                bool icon(string name) => icons2.Contains(name);
                nameBuilder.Clear();
                if (icon(IconNames.OutputAssigned))
                    nameBuilder.Append('●'); // 
                else if (icon(IconNames.Output))
                    nameBuilder.Append(" ⃝"); //
                if (icon(IconNames.Input))
                {
                    nameBuilder.Append("●"); // ●
                    nameBuilder.Append("→"); // 
                }
                if (icon(IconNames.Parameterized))
                    nameBuilder.Append("♦");// 
                if (x.Get<IMemberAnnotation>()?.Member is IParameterMemberData)
                    nameBuilder.Append("◊");// 
 
                if (nameBuilder.Length > 0)
                    nameBuilder.Append(" ");
 
                nameBuilder.Append(x.Get<DisplayAttribute>().Name);
                nameBuilder.Append(": ");
                nameBuilder.Append(value);
 
                // Check validation rules
                var step = x.Source as IValidatingObject;
                var propertyName = x.Get<IMemberAnnotation>()?.Member?.Name;
                var rule = step?.Rules.FirstOrDefault(r => r.PropertyName == propertyName && r?.IsValid() == false);
                if (rule != null)
                    nameBuilder.Append(" !");
 
                return nameBuilder.ToString();
            }
            catch
            {
                return "";
            }
        }
 
        public SettingNode GetNodeFromItem(AnnotationCollection item)
        {
            SettingNode node = CreateNode(item);
            node.Title = getTitle(item);
            //node.Children = getChildren?.Invoke(item).Select(i => new TreeViewNode<T>(i, this)).ToList() ?? new List<TreeViewNode<T>>();
 
            //if (getParent != null)
            //{
            //    var parent = getParent(item);
            //    if (parent != null && nodes.ContainsKey(parent))
            //        node.Parent = nodes[parent];
            //    else
            //        node.Parent = null;
            //}
 
            return node;
        }
 
        private Expander BuildGroupTree(SettingNode node)
        {
            Expander groupNode = null;
            Expander lastGroup = null;
            string groupKey = null;
            foreach (var group in node.Groups)
            {
                groupKey = string.IsNullOrEmpty(groupKey) ? group : (groupKey + "." + group);
                groupNode = null;
                if (Groups.TryGetValue(groupKey, out groupNode) == false)
                {
                    // add the group
                    groupNode = new Expander
                    {
                        Header = groupKey,
                        Content = new StackPanel()
                    };
                    Groups[groupKey] = groupNode;
 
                    if (lastGroup != null)
                    {
                        ((StackPanel)lastGroup.Content).Children.Add(groupNode);
                    }
                    else
                    {
                        panel.Children.Add(groupNode);
                    }
                }                
                lastGroup = groupNode;
            }
            return groupNode;
            //if (lastGroup != null)
            //{
            //    node.Parent = lastGroup;
            //    if (lastGroup.Children.Contains(node) == false)
            //        lastGroup.Children.Add(node);
            //}
        }
 
 
        ObservableCollection<SettingNode> GetItemsToRenderWithGroup(bool noCache)
        {
            SettingNode node;
            var list = new ObservableCollection<SettingNode>();
            // Build groups tree
            foreach (var item in items)
            {
                var _groups = item?.Get<DisplayAttribute>().Group.ToList();
                node = GetNodeFromItem(item);
                node.Groups = _groups ?? new List<string>();
                var expander = BuildGroupTree(node);
                var control = RenderControl(item);
                if (control == null) continue;
                if (expander == null)
                {
                    panel.Children.Add((UIElement)control);
                }
                else {
                    ((StackPanel)expander.Content).Children.Add((UIElement)control);
                }
                    
                list.Add(node);
            }
 
            //foreach (var item in items)
            //{
            //    var node = nodes[item];
            //    if (node.Groups.Any())
            //    {
            //        var firstGroup = groups[node.Groups.First()];
            //        if (list.Contains(firstGroup))
            //            continue;
 
            //        list.Add(firstGroup);
            //        list.AddRange(printGroup(firstGroup));
            //    }
            //    else
            //        list.Add(node);
            //}
 
            return list;
        }
 
        public void RenderTreeView(bool noCache = false)
        {
            if (items == null)
                return;
 
            var list = new ObservableCollection<SettingNode>();
            list = GetItemsToRenderWithGroup(noCache);
            this.List = list;
        }
 
        public void UpdateSource(object obj)
        {
            this.obj = obj ?? new object();
            if (obj is TestStep step) { 
                var sss = BreakConditionProperty.GetBreakCondition(step);
 
                
            }
 
            
 
            var sb = obj.GetType().GetProperties(BindingFlags.Instance|BindingFlags.DeclaredOnly |BindingFlags.Static| BindingFlags.Public | BindingFlags.NonPublic);
 
 
            var sssb = obj.GetType().GetProperties(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic);
            List<PropertyInfo> props = new List<PropertyInfo>();
            List<string> names = new List<string>();
            foreach (var prop in sssb) {
                var attr = prop.GetCustomAttribute<DisplayAttribute>();
                if (attr == null) {
                    continue;
                }
                names.Add(attr.Name ?? prop.Name);
                props.Add(prop);
            }
            annotations = AnnotationCollection.Annotate(obj);
            view.Children.Clear();
            //view.Children.Add(DutControlProvider.GetControl(annotations));
            view.Children.Add(GridControlProvider.GetControl(obj));
        }
 
        //public void UpdateSource(object obj)
        //{
        //    this.obj = obj ?? new object();
        //    annotations = AnnotationCollection.Annotate(obj);
        //    Groups = new Dictionary<string, Expander>();
        //    panel.Children.Clear();
 
        //    this.items = getMembers()?.ToList() ?? new List<AnnotationCollection>();
        //    RenderTreeView();
        //}
    }
}