-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.xaml.cs
143 lines (122 loc) · 4.49 KB
/
Main.xaml.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using ScriptCommander.Core;
namespace ScriptCommander
{
/// <summary>
/// Interaction logic for Main.xaml
/// </summary>
public partial class Main : Window
{
readonly App _app;
private ConsoleProcess _console;
public Main()
{
InitializeComponent();
this.Loaded += Main_Loaded;
_app = (App)Application.Current;
}
void Main_Loaded(object sender, RoutedEventArgs e)
{
var traceListener = new TextTraceListener();
System.Diagnostics.Trace.Listeners.Add(traceListener);
traceListener.TextOut.Subscribe(s => Dispatcher.Invoke(() =>
{
UiTraceOut.Text += s;
ScrollParent(UiTraceOut);
}));
Browser.PropertyChanged += (o, args) =>
{
if (args.PropertyName == "SelectedScriptPath")
{
Dispatcher.Invoke(() =>
{
var path = Browser.SelectedScriptPath;
Title = path;
if (_console != null)
{
_console.Close();
}
var procStartInfo = new ProcessStartInfo("cmd");
var app = (App)Application.Current;
procStartInfo.WorkingDirectory = app.AppSettings.AdbDirectory;
_console = new ConsoleProcess(procStartInfo);
_console.Start();
var script = File.ReadAllLines(path);
var api = new Api(_console, script);
var output = new Subject<string>();
var errors = new Subject<string>();
api.Console.OnOutputReceived(output.OnNext);
api.Console.OnErrorReceived(errors.OnNext);
output.Merge(errors).Subscribe(str => System.Diagnostics.Trace.Write(str));
UiScriptCommander.Api = api;
});
}
};
var adbDirectory = _app.AppSettings.AdbDirectory;
if (string.IsNullOrWhiteSpace(adbDirectory))
{
SetAdbPath();
}
}
private void SetAdbPath()
{
const string messageBoxText = "Need to set adb path!";
const string caption = "Adb not found";
const MessageBoxButton button = MessageBoxButton.OKCancel;
const MessageBoxImage icon = MessageBoxImage.Warning;
var boxResult = MessageBox.Show(messageBoxText, caption, button, icon);
if (boxResult == MessageBoxResult.OK)
{
var dlg = new Microsoft.Win32.OpenFileDialog
{
FileName = "adb",
DefaultExt = ".exe",
Filter = "Executive Files(*.exe;*.com)|*.exe;*.com|All files (*.*)|*.*"
};
var result = dlg.ShowDialog();
if (result == true)
{
var filename = dlg.FileName;
var dir = Path.GetDirectoryName(filename);
_app.AppSettings.AdbDirectory = dir;
}
else
{
SetAdbPath();
}
}
else
{
this.Close();
}
}
private static void ScrollParent(FrameworkElement element)
{
if (element == null) return;
var textBoxBase = element.Parent as TextBoxBase;
if (textBoxBase != null)
{
textBoxBase.ScrollToEnd();
}
else
{
var scrollViewer = element.Parent as ScrollViewer;
if (scrollViewer != null)
{
scrollViewer.ScrollToEnd();
}
else
{
ScrollParent(element.Parent as FrameworkElement);
}
}
}
}
}