using CommunityToolkit.Mvvm.ComponentModel;
using OpenTap;
using OpenTap.Addin;
using OpenTap.Addin.Util;
using System.Collections.ObjectModel;
using System.Windows;
namespace OpenTapEditor
{
///
/// VariableSelector.xaml 的交互逻辑
///
[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 variables;
public VariableSelector()
{
DataContext = this;
Variables = new ObservableCollection() {
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);
}
}
}
}