-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMain.cs
370 lines (327 loc) · 14.1 KB
/
FormMain.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
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
using Suconbu.Mobile;
using Suconbu.Toolbox;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Suconbu.Sumacon
{
public partial class FormMain : FormBase
{
DockPanel dockPanel;
FormProperty propertyForm;
FormConsole consoleForm;
FormCapture captureForm;
FormRecord recordForm;
FormLog logForm;
FormPerformance performanceForm;
FormControl controlForm;
FormScript scriptForm;
ToolStripDropDownButton deviceDropDown;
ToolStripLabel deviceInfoLabel = new ToolStripLabel();
ToolStripButton airplaneModeButton = new ToolStripButton();
ToolStripButton showTouchesButton = new ToolStripButton();
ToolStripButton wirelessAdbButton = new ToolStripButton();
//ToolStripLabel memoryInfoLabel = new ToolStripLabel();
Timer memoryTimer = new Timer() { Interval = 1000 };
FormWindowState previousWindowState;
List<IDockContent> previousVisibleContents = new List<IDockContent>();
Sumacon sumacon = new Sumacon();
public FormMain()
{
Trace.TraceInformation(Util.GetCurrentMethodName());
InitializeComponent();
this.Text = $"{Util.GetApplicationName()} - ver{Util.GetVersionString(3)}";
this.Icon = Properties.Resources.sumacon;
this.KeyPreview = true;
this.AllowDrop = true;
this.SetupDeviceManager();
}
protected override void OnLoad(EventArgs e)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
base.OnLoad(e);
this.dockPanel = new DockPanel();
this.dockPanel.Dock = DockStyle.Fill;
this.dockPanel.DocumentStyle = DocumentStyle.DockingWindow;
this.toolStripContainer1.ContentPanel.Controls.Add(this.dockPanel);
this.deviceDropDown = new ToolStripDropDownButton();
this.statusStrip1.Items.Add(this.deviceDropDown);
this.statusStrip1.Items.Add(this.deviceInfoLabel);
this.statusStrip1.Items.Add(this.airplaneModeButton);
this.statusStrip1.Items.Add(this.showTouchesButton);
this.statusStrip1.Items.Add(this.wirelessAdbButton);
this.statusStrip1.Items.Add(new ToolStripStatusLabel() { Spring = true });
//#if DEBUG
// this.statusStrip1.Items.Add(this.memoryInfoLabel);
//#endif
this.consoleForm = new FormConsole(this.sumacon);
this.consoleForm.Text = "Console";
this.consoleForm.Show(this.dockPanel, DockState.DockBottom);
this.propertyForm = new FormProperty(this.sumacon);
this.propertyForm.Text = "Property";
this.propertyForm.Show(this.dockPanel, DockState.DockRight);
this.scriptForm = new FormScript(this.sumacon);
this.scriptForm.Text = "Script";
this.scriptForm.Show(this.dockPanel, DockState.DockRight);
this.captureForm = new FormCapture(this.sumacon);
this.captureForm.Text = "Capture";
this.captureForm.Show(this.dockPanel, DockState.Document);
this.recordForm = new FormRecord(this.sumacon);
this.recordForm.Text = "Record";
this.recordForm.Show(this.dockPanel, DockState.Document);
this.logForm = new FormLog(this.sumacon);
this.logForm.Text = "Log";
this.logForm.Show(this.dockPanel, DockState.Document);
this.performanceForm = new FormPerformance(this.sumacon);
this.performanceForm.Text = "Performace";
this.performanceForm.Show(this.dockPanel, DockState.Document);
this.controlForm = new FormControl(this.sumacon);
this.controlForm.Text = "Control";
this.controlForm.Show(this.dockPanel, DockState.Document);
//this.propertyForm.Activate();
this.controlForm.Activate();
foreach (var form in this.dockPanel.Contents)
{
form.DockHandler.CloseButtonVisible = false;
}
this.airplaneModeButton.Click += this.AirplaneModeButton_Click;
this.showTouchesButton.Click += this.ShowTouchesButton_Click;
this.wirelessAdbButton.Click += this.WirelessAdbButton_Click;
this.LoadSettings();
this.sumacon.SaveCapturedImageRequested += (image) => this.captureForm.SaveCapturedImage(image);
this.sumacon.DeviceManager.StartDeviceDetection();
}
protected override void OnClosing(CancelEventArgs e)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
base.OnClosing(e);
var device = this.sumacon.DeviceManager.ActiveDevice;
if(device != null)
{
this.sumacon.DeviceManager.SuspendPropertyUpdate(device);
foreach (var component in device.Components)
{
component.ResetAsync();
}
}
// 片付けているところが見えないように
this.Visible = false;
var forms = this.dockPanel.Contents.ToArray();
foreach (var form in forms)
{
form.DockHandler.Close();
}
this.SaveSettings();
this.sumacon.Dispose();
}
protected override void OnClosed(EventArgs e)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
base.OnClosed(e);
Properties.Settings.Default.Save();
}
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
if (this.sumacon.DeviceManager.ActiveDevice != null)
{
var path = (e.Data.GetData(DataFormats.FileDrop, false) as string[]).FirstOrDefault();
if (path != null && Path.GetExtension(path).ToLower() == ".apk")
{
e.Effect = DragDropEffects.All;
}
}
}
protected override void OnDragDrop(DragEventArgs e)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
base.OnDragDrop(e);
var path = (e.Data.GetData(DataFormats.FileDrop, false) as string[]).FirstOrDefault();
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device != null)
{
var command = $"install -r {path}";
this.sumacon.WriteConsole(command);
device?.RunCommandAsync(command, output => this.sumacon.WriteConsole(output), error => this.sumacon.WriteConsole(error));
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if(e.KeyCode == Keys.F11)
{
e.Handled = true;
this.ToggleFullscreen();
}
}
void AirplaneModeButton_Click(object sender, EventArgs e)
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device != null)
{
device.AirplaneMode = !device.AirplaneMode;
}
}
void ShowTouchesButton_Click(object sender, EventArgs e)
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device != null)
{
device.ShowTouches = !device.ShowTouches;
}
}
void WirelessAdbButton_Click(object sender, EventArgs e)
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device != null)
{
if (!this.wirelessAdbButton.Checked)
{
this.sumacon.DeviceManager.StartWireless(device);
}
else
{
device.Dispose();
}
}
}
void SetupDeviceManager()
{
this.sumacon.DeviceManager.PropertyChanged += (s, properties) =>
{
this.SafeInvoke(() => this.UpdateControlState());
};
this.sumacon.DeviceManager.ConnectedDevicesChanged += (s, ee) =>
{
this.SafeInvoke(() => this.UpdateDeviceList());
};
this.sumacon.DeviceManager.ActiveDeviceChanged += (s, previousActiveDevice) =>
{
this.SafeInvoke(() => this.UpdateControlState());
};
}
void UpdateDeviceList()
{
this.deviceDropDown.DropDownItems.Clear();
var devices = this.sumacon.DeviceManager.ConnectedDevices.ToArray();
foreach (var device in devices)
{
var label = device.ToString(Properties.Resources.DeviceLabelFormat);
var item = this.deviceDropDown.DropDownItems.Add(label);
item.Image = this.imageList1.Images[device.HasWirelessConnection ? "phone_denpa.png" : "phone.png"];
item.Click += (s, e) => this.sumacon.DeviceManager.ActiveDevice = device;
}
}
void ToggleFullscreen()
{
if (this.FormBorderStyle == FormBorderStyle.None)
{
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = this.previousWindowState;
this.dockPanel.DocumentStyle = DocumentStyle.DockingWindow;
var activeContent = this.dockPanel.ActiveContent;
foreach (var content in this.dockPanel.Contents)
{
content.DockHandler.Show();
}
foreach(var content in this.previousVisibleContents)
{
content.DockHandler.Activate();
}
activeContent.DockHandler.Show();
}
else
{
foreach (var document in this.dockPanel.Documents)
{
if (document.DockHandler.Form.Visible)
{
document.DockHandler.Activate();
}
}
this.previousVisibleContents.Clear();
foreach (var content in this.dockPanel.Contents)
{
if (content.DockHandler.Form.Visible)
{
this.previousVisibleContents.Add(content);
}
}
foreach (var content in this.dockPanel.Contents)
{
if (content != this.dockPanel.ActiveContent)
{
content.DockHandler.Hide();
}
}
this.dockPanel.DocumentStyle = DocumentStyle.DockingSdi; // タブを消すため
this.previousWindowState = this.WindowState;
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
}
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
void UpdateControlState()
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device != null)
{
this.deviceDropDown.Text = device.ToString(Properties.Resources.FormMain_StatusDeviceFormat);
this.deviceDropDown.Image = this.imageList1.Images[device.HasWirelessConnection ? "phone_denpa.png" : "phone.png"];
var color = Util.ColorFromHsv(Math.Abs(device.Model.GetHashCode()) % 360, 1.0f, 1.0f);
this.deviceDropDown.BackColor = color;
this.deviceDropDown.ForeColor = color.GetLuminance() >= 0.5f ? Color.Black : Color.White;
this.deviceInfoLabel.Text =
device.ToString(Properties.Resources.FormMain_StatusScreenFormat) +
" " +
device.ToString(Properties.Resources.FormMain_StatusBatteryFormat);
this.deviceInfoLabel.Visible = true;
this.airplaneModeButton.Text = device.AirplaneMode ? "✈ ON" : "✈ OFF";
this.airplaneModeButton.Checked = device.AirplaneMode;
this.airplaneModeButton.Visible = true;
this.showTouchesButton.Text = device.ShowTouches ? "👆 ON" : "👆 OFF";
this.showTouchesButton.Checked = device.ShowTouches;
this.showTouchesButton.Visible = true;
var wireless = device.HasWirelessConnection;
this.wirelessAdbButton.Text = wireless ? $"WirelessConnect ON ({device.WirelessPort})" : "WirelessConnect OFF";
this.wirelessAdbButton.Checked = wireless;
this.wirelessAdbButton.Visible = true;
}
else
{
this.deviceDropDown.BackColor = SystemColors.Control;
this.deviceDropDown.ForeColor = SystemColors.ControlText;
this.deviceDropDown.Image = this.imageList1.Images["phone.png"];
this.deviceDropDown.Text = "-";
this.deviceInfoLabel.Visible = false;
this.airplaneModeButton.Visible = false;
this.showTouchesButton.Visible = false;
this.wirelessAdbButton.Visible = false;
}
}
void LoadSettings()
{
this.WindowState = Properties.Settings.Default.MainMaximized ? FormWindowState.Maximized : FormWindowState.Normal;
this.Size = Properties.Settings.Default.MainSize;
}
void SaveSettings()
{
Properties.Settings.Default.MainMaximized = (this.WindowState == FormWindowState.Maximized);
Properties.Settings.Default.MainSize = this.Size;
}
//void MemoryTimer_Tick(object sender, EventArgs e)
//{
// this.memoryInfoLabel.Text = $"{GC.GetTotalMemory(false):#,##0} byte";
//}
}
}