-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptEditWindow.xaml.cs
More file actions
282 lines (232 loc) · 8.54 KB
/
Copy pathScriptEditWindow.xaml.cs
File metadata and controls
282 lines (232 loc) · 8.54 KB
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
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Microsoft.Win32;
namespace PresentationPrompter
{
public partial class ScriptEditWindow : Window
{
private List<ScriptStep> _steps;
private bool _isUpdating;
private string _filePath;
public string ScriptTitle { get; private set; }
public ScriptStep[] Steps { get { return _steps.ToArray(); } }
public string SavedFilePath { get { return _filePath; } }
public ScriptEditWindow(string title, ScriptStep[] steps, string filePath)
{
InitializeComponent();
_filePath = filePath;
TitleBox.Text = title;
_steps = new List<ScriptStep>();
foreach (var s in steps)
_steps.Add(s.Clone());
RefreshList();
if (_steps.Count > 0)
StepListBox.SelectedIndex = 0;
}
#region List Management
private void RefreshList()
{
int sel = StepListBox.SelectedIndex;
StepListBox.Items.Clear();
for (int i = 0; i < _steps.Count; i++)
{
StepListBox.Items.Add(FormatStepDisplay(i));
}
if (sel >= 0 && sel < _steps.Count)
StepListBox.SelectedIndex = sel;
else if (_steps.Count > 0)
StepListBox.SelectedIndex = 0;
}
private string FormatStepDisplay(int index)
{
var s = _steps[index];
return string.Format("{0}. {1}{2}",
index + 1,
s.Text.Length > 20 ? s.Text.Substring(0, 20) + "…" : s.Text,
string.IsNullOrEmpty(s.Command) ? "" : " ⚡");
}
private void RefreshListItem(int index)
{
if (index >= 0 && index < StepListBox.Items.Count)
StepListBox.Items[index] = FormatStepDisplay(index);
}
private void SaveDetailToList()
{
if (_isUpdating || StepListBox.SelectedIndex < 0) return;
if (StepListBox.SelectedIndex >= _steps.Count) return;
var step = _steps[StepListBox.SelectedIndex];
step.Text = DetailTextBox.Text;
step.Command = DetailCommandBox.Text;
step.AutoRun = DetailAutoRun.IsChecked == true;
int d;
step.Delay = int.TryParse(DetailDelay.Text, out d) ? d : 0;
RefreshListItem(StepListBox.SelectedIndex);
}
#endregion
#region Selection
private void StepListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (StepListBox.SelectedIndex < 0 || StepListBox.SelectedIndex >= _steps.Count)
{
DetailPanel.IsEnabled = false;
return;
}
DetailPanel.IsEnabled = true;
_isUpdating = true;
var step = _steps[StepListBox.SelectedIndex];
DetailTextBox.Text = step.Text;
DetailCommandBox.Text = step.Command;
DetailAutoRun.IsChecked = step.AutoRun;
DetailDelay.Text = step.Delay.ToString();
_isUpdating = false;
}
#endregion
#region Step Operations
private void InsertBefore_Click(object sender, RoutedEventArgs e)
{
int idx = StepListBox.SelectedIndex;
if (idx < 0) idx = 0;
_steps.Insert(idx, new ScriptStep("新步骤"));
RefreshList();
StepListBox.SelectedIndex = idx;
}
private void InsertAfter_Click(object sender, RoutedEventArgs e)
{
int idx = StepListBox.SelectedIndex;
if (idx < 0) idx = _steps.Count - 1;
if (idx < 0) idx = -1;
_steps.Insert(idx + 1, new ScriptStep("新步骤"));
RefreshList();
StepListBox.SelectedIndex = idx + 1;
}
private void RemoveStep_Click(object sender, RoutedEventArgs e)
{
int idx = StepListBox.SelectedIndex;
if (idx < 0 || idx >= _steps.Count) return;
_steps.RemoveAt(idx);
RefreshList();
}
private void MoveUp_Click(object sender, RoutedEventArgs e)
{
int idx = StepListBox.SelectedIndex;
if (idx <= 0) return;
SaveDetailToList();
var temp = _steps[idx];
_steps[idx] = _steps[idx - 1];
_steps[idx - 1] = temp;
RefreshList();
StepListBox.SelectedIndex = idx - 1;
}
private void MoveDown_Click(object sender, RoutedEventArgs e)
{
int idx = StepListBox.SelectedIndex;
if (idx < 0 || idx >= _steps.Count - 1) return;
SaveDetailToList();
var temp = _steps[idx];
_steps[idx] = _steps[idx + 1];
_steps[idx + 1] = temp;
RefreshList();
StepListBox.SelectedIndex = idx + 1;
}
#endregion
#region Command Execution
private void TestRun_Click(object sender, RoutedEventArgs e)
{
SaveDetailToList();
int idx = StepListBox.SelectedIndex;
if (idx < 0) return;
string rawCmd = _steps[idx].Command;
if (string.IsNullOrEmpty(rawCmd)) return;
string title = TitleBox.Text.Trim();
if (string.IsNullOrEmpty(title)) title = "未命名脚本";
string cmd = ScriptFile.ExpandVariables(rawCmd, idx, title, _steps.Count);
try
{
var psi = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c " + cmd,
UseShellExecute = false,
CreateNoWindow = true
};
Process.Start(psi);
}
catch (System.Exception ex)
{
MessageBox.Show("执行失败: " + ex.Message, "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
#endregion
#region Save
private bool DoSave(string path)
{
string title = TitleBox.Text.Trim();
if (string.IsNullOrEmpty(title)) title = "未命名脚本";
try
{
if (path.EndsWith(".json", System.StringComparison.OrdinalIgnoreCase))
ScriptFile.SaveJson(path, title, _steps.ToArray());
else
{
string[] lines = new string[_steps.Count + 1];
lines[0] = title;
for (int i = 0; i < _steps.Count; i++)
lines[i + 1] = _steps[i].Text;
File.WriteAllLines(path, lines);
}
_filePath = path;
return true;
}
catch (System.Exception ex)
{
MessageBox.Show("保存失败: " + ex.Message, "错误",
MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
}
private void SaveFile_Click(object sender, RoutedEventArgs e)
{
SaveDetailToList();
if (!string.IsNullOrEmpty(_filePath))
{
DoSave(_filePath);
}
else
{
SaveAs_Click(sender, e);
}
}
private void SaveAs_Click(object sender, RoutedEventArgs e)
{
SaveDetailToList();
string title = TitleBox.Text.Trim();
if (string.IsNullOrEmpty(title)) title = "未命名脚本";
var dlg = new SaveFileDialog
{
Filter = "JSON 脚本 (*.json)|*.json|文本文件 (*.txt)|*.txt|所有文件 (*.*)|*.*",
DefaultExt = ".json",
FileName = !string.IsNullOrEmpty(_filePath) ? Path.GetFileName(_filePath) : title + ".json"
};
if (dlg.ShowDialog() == true)
DoSave(dlg.FileName);
}
#endregion
#region Dialog Buttons
private void OK_Click(object sender, RoutedEventArgs e)
{
SaveDetailToList();
ScriptTitle = TitleBox.Text.Trim();
if (string.IsNullOrEmpty(ScriptTitle))
ScriptTitle = "未命名脚本";
DialogResult = true;
}
private void Cancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
#endregion
}
}