-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormProperty.cs
129 lines (115 loc) · 5.18 KB
/
FormProperty.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
using Suconbu.Mobile;
using Suconbu.Toolbox;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
namespace Suconbu.Sumacon
{
public partial class FormProperty : FormBase
{
Sumacon sumacon;
ContextMenuStrip menu = new ContextMenuStrip();
public FormProperty(Sumacon sumacon)
{
Trace.TraceInformation(Util.GetCurrentMethodName());
InitializeComponent();
this.sumacon = sumacon;
this.sumacon.DeviceManager.ActiveDeviceChanged += this.DeviceManager_ActiveDeviceChanged;
this.sumacon.DeviceManager.PropertyChanged += this.DeviceManager_PropertyChanged;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
this.sumacon.DeviceManager.ActiveDeviceChanged -= this.DeviceManager_ActiveDeviceChanged;
this.sumacon.DeviceManager.PropertyChanged -= this.DeviceManager_PropertyChanged;
}
void SetupContextMenu()
{
this.menu.Items.Clear();
this.menu.Items.Add("Refresh", this.imageList1.Images["arrow_refresh.png"], (s, e) => RefreshProperties());
this.menu.Items.Add(new ToolStripSeparator());
var resetPropertyMenuItem = this.menu.Items.Add(string.Empty, null, (s, e) =>
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device == null) return;
if (!this.GetSelectedItemProperty(out var category, out var component, out var label, out var property)) return;
property?.ResetAsync(device);
});
var resetCategoryMenuItem = this.menu.Items.Add(string.Empty, null, (s, e) =>
{
var device = this.sumacon.DeviceManager.ActiveDevice;
if (device == null) return;
var category = this.uxPropertyGrid.SelectedGridItem.PropertyDescriptor.Category;
device.GetComponent(category)?.ResetAsync();
});
var resetAllMenuItem = this.menu.Items.Add(string.Empty, null, (s, e) =>
{
var device = this.sumacon.DeviceManager.ActiveDevice;
foreach (var component in device.Components.OrEmptyIfNull())
{
component.ResetAsync();
}
});
this.menu.Opening += (s, e) =>
{
if(!this.GetSelectedItemProperty(out var category, out var component, out var label, out var property))
{
e.Cancel = true;
return;
}
resetPropertyMenuItem.Enabled = (property != null && property.PushCommand != null);
resetPropertyMenuItem.Text = string.Format(Properties.Resources.FormProperty_MenuItemLabel_ResetOne, label);
resetCategoryMenuItem.Enabled = (component != null);
resetCategoryMenuItem.Text = string.Format(
Properties.Resources.FormProperty_MenuItemLabel_ResetGroup, category);
resetAllMenuItem.Text = Properties.Resources.FormProperty_MenuItemLabel_ResetAll;
};
this.uxPropertyGrid.ContextMenuStrip = this.menu;
}
void DeviceManager_ActiveDeviceChanged(object sender, Device previousDevice)
{
this.SafeInvoke(() =>
{
this.uxPropertyGrid.SelectedObject = this.sumacon.DeviceManager.ActiveDevice;
this.SetupContextMenu();
});
}
void DeviceManager_PropertyChanged(object sender, IReadOnlyList<Property> properties)
{
this.SafeInvoke(() =>
{
this.uxPropertyGrid.SelectedObject = this.sumacon.DeviceManager.ActiveDevice;
});
}
bool GetSelectedItemProperty(out string category, out DeviceComponent component, out string label, out Property property)
{
component = null;
label = null;
property = null;
var device = this.sumacon.DeviceManager.ActiveDevice;
category = this.uxPropertyGrid.SelectedGridItem.PropertyDescriptor?.Category;
component = device?.GetComponent(category);
if (component == null) return false;
label = this.uxPropertyGrid.SelectedGridItem.Label;
// 先頭のコンポーネント名は外して探す(例:ScreenSize->Size)
var findLabel = label.StartsWith(component.Name) ? label.Substring(component.Name.Length) : label;
property = component?.Find(findLabel);
return true;
}
void RefreshProperties()
{
var contexts = new List<CommandContext>();
var device = this.sumacon.DeviceManager.ActiveDevice;
foreach (var component in device.Components.OrEmptyIfNull())
{
contexts.Add(component.PullAsync());
}
this.Enabled = false;
CommandContext.StartNew(() =>
{
contexts.ForEach(c => c?.Wait());
this.SafeInvoke(() => this.Enabled = true);
});
}
}
}