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
using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap;
using OpenTap.Addin;
using OpenTap.Addin.Util;
using System.Collections.ObjectModel;
using System.Windows;
 
namespace OpenTapEditor
{
    /// <summary>
    /// VariableSelector.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class VariableSelector : Window
    {
        public static DependencyProperty ResultProperty = DependencyProperty.Register(
            "Result",
            typeof(string),
            typeof(VariableSelector),
            new PropertyMetadata(null)
        );
 
        public string Result
        {
            get => (string)GetValue(ResultProperty);
            set
            {
                SetValue(ResultProperty, value);
            }
        }
 
        [ObservableProperty]
        private ObservableCollection<TestVariable> variables;
 
        public VariableSelector()
        {
            DataContext = this;
            Variables = new ObservableCollection<TestVariable>() {
                new TestVariable(){
                    Name = ConstNames.LocalsName,
                    NameCanEdit = false,
                    Type = TestVariableType.Container
                },
                new TestVariable(){
                    Name = ConstNames.ParametersName,
                    NameCanEdit = false,
                    Type = TestVariableType.Container
                },
                new TestVariable(){
                    Name = ConstNames.FileGlobalsName,
                    NameCanEdit = false,
                    Type = TestVariableType.Container
                },
                new TestVariable(){
                    Name = ConstNames.StationGlobalsName,
                    Type = TestVariableType.Container,
                    NameCanEdit = false,
                    Children = UIStationGlobalsManager.Instance.Datasource
                },
                new TestVariable(){
                    Name = ConstNames.ThisContextName,
                    NameCanEdit = false,
                    Type = TestVariableType.Refrence,
                }
            };
            InitializeComponent();
            tree.ItemDoubleClicked += Tree_ItemDoubleClicked;
        }
 
        private void Tree_ItemDoubleClicked(object? sender, string e)
        {
            if (string.IsNullOrEmpty(Result))
            {
                Result += e;
            }
            else
            {
                var length = tb.SelectionLength;
                if (length > 0)
                {
                    int index = tb.SelectionStart;
                    tb.Text = tb.Text.Remove(index, length).Insert(index, e);
                }
                else
                {
                    string text = $" {e} ";
                    int p = tb.CaretIndex;
                    if (p == tb.Text.Length)
                    {
                        text = $" {e}";
                    }
 
 
                    tb.Text = tb.Text.Insert(p, text);
                    tb.CaretIndex = p + text.Length;
 
                    tb.Select(p, text.Length);
                }
            }
        }
 
        public bool? Open(TestPlan plan, TestStepList seq)
        {
            Variables[0].Children = seq.Variables;
            Variables[1].Children = seq.Parameters;
            Variables[2].Children = plan.Variables;
            return this.ShowDialog();
        }
 
        private void tb_GotFocus(object sender, RoutedEventArgs e)
        {
            // tb.Select(start, length);
        }
 
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (tree.EditingCell != null)
            {
                e.Cancel = !tree.EditingCell.PreSubmit(tree.EditingCell, tree.EditingCell.editing.Text, tree.EditingCell.DataContext);
            }
        }
    }
}