-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPosEditorView.cs
More file actions
250 lines (209 loc) · 8.19 KB
/
PosEditorView.cs
File metadata and controls
250 lines (209 loc) · 8.19 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using NStack;
namespace Terminal.Gui.Designer
{
public class PosEditorView : Dialog
{
private RadioGroup _radioGroup;
private TextField _valueText;
private ListView _cmoViews;
private readonly Dictionary<ustring, PosType> _posTypes = new Dictionary<ustring, PosType>();
private readonly DesignerState _state;
public PosEditorView(Pos pos, DesignerState state) : base("Edit Position")
{
_state = state;
Width = 50;
Height = 15;
var ok = new Button("Ok");
var cancel = new Button("Cancel");
ok.Clicked += () =>
{
var pos1 = MakePos();
if (ValueChanged != null && pos1 != null)
{
ValueChanged(this, new PosChangedEventArgs(pos1));
}
Application.RequestStop();
};
cancel.Clicked += () =>
{
Application.RequestStop();
};
foreach (PosType posType in Enum.GetValues(typeof(PosType)))
{
_posTypes.Add(posType.ToString(), posType);
}
var posInfo = new PosInfo(pos);
var selectedType = _posTypes.Values.Select((v, i) => new { Value = v, index = i }).First(m => m.Value == posInfo.Type).index;
var typeLabel = new Label("Type")
{
X = 0,
Y = 0
};
_radioGroup = new RadioGroup(_posTypes.Keys.ToArray(), selectedType)
{
X = 0,
Y = 1
};
Add(typeLabel);
Add(_radioGroup);
var valueLabel = new Label("Value")
{
X = Pos.Right(_radioGroup),
Y = 0
};
_valueText = new TextField()
{
X = Pos.Right(_radioGroup),
Y = 1,
Width = Dim.Fill(),
Visible = posInfo.Type == PosType.At,
Text = posInfo.Value.ToString()
};
_cmoViews = new ListView()
{
X = Pos.Right(_radioGroup),
Y = 1,
Width = Dim.Fill(),
Height = Dim.Fill() - 2,
Visible = posInfo.Type != PosType.At && posInfo.Type != PosType.AnchorEnd
};
_cmoViews.SetSource(_state.Views);
Add(valueLabel);
Add(_cmoViews);
Add(_valueText);
_radioGroup.SelectedItemChanged += (args) =>
{
var pos1 = MakePos();
var posInfo1 = new PosInfo(pos1);
_valueText.Visible = posInfo1.Type == PosType.At;
_cmoViews.Visible = posInfo1.Type != PosType.At && posInfo1.Type != PosType.AnchorEnd;
SetNeedsDisplay();
};
AddButton(ok);
AddButton(cancel);
}
private Pos MakePos()
{
if (!float.TryParse(_valueText.Text.ToString(), out float value))
{
return null;
}
var selectedType = _posTypes.Values.Select((v, i) => new { Value = v, index = i }).First(m => m.index == _radioGroup.SelectedItem).Value;
var selectedView = _state.Views.Select((v, i) => new { Value = v, index = i }).First(m => m.index == _cmoViews.SelectedItem).Value;
switch (selectedType)
{
case PosType.At:
return Pos.At((int)value);
case PosType.AnchorEnd:
return Pos.AnchorEnd();
case PosType.Bottom:
return Pos.Bottom(selectedView);
case PosType.Top:
return Pos.Top(selectedView);
case PosType.Right:
return Pos.Right(selectedView);
case PosType.Left:
return Pos.Left(selectedView);
}
return null;
}
public event EventHandler<PosChangedEventArgs> ValueChanged;
}
public class PosChangedEventArgs : EventArgs
{
public Pos Pos { get; }
public PosChangedEventArgs(Pos pos)
{
Pos = pos;
}
}
public class PosInfo
{
private static readonly Type _posFactorType = typeof(Pos).Assembly.GetType("Terminal.Gui.Pos+PosFactor");
private static readonly Type _posAnchorEndType = typeof(Pos).Assembly.GetType("Terminal.Gui.Pos+PosAnchorEnd");
private static readonly Type _posAbsoluteType = typeof(Pos).Assembly.GetType("Terminal.Gui.Pos+PosAbsolute");
private static readonly Type _posCombineType = typeof(Pos).Assembly.GetType("Terminal.Gui.Pos+PosCombine");
private static readonly Type _posViewType = typeof(Pos).Assembly.GetType("Terminal.Gui.Pos+PosView");
private static readonly FieldInfo _posFactorFactorField = _posFactorType.GetField("factor", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo _posAbsoluteValueField = _posAbsoluteType.GetField("n", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo _posCombineLeftField = _posCombineType.GetField("left", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo _posCombineRightField = _posCombineType.GetField("right", BindingFlags.Instance | BindingFlags.NonPublic);
private static readonly FieldInfo _posViewTargetField = _posViewType.GetField("Target", BindingFlags.Instance | BindingFlags.Public);
private static readonly FieldInfo _posViewSideField = _posViewType.GetField("side", BindingFlags.Instance | BindingFlags.NonPublic);
private readonly Regex argumentRegEx = new Regex("\\((.*)\\)");
private readonly Regex viewArgumentsRegEx = new Regex("Pos\\.View\\(([^\\+]*)");
public PosInfo(Pos pos)
{
if (pos == null) return;
if (pos.GetType() == _posAbsoluteType)
{
Type = PosType.At;
Value = (int)_posAbsoluteValueField.GetValue(pos);
}
if (pos.GetType() == _posAnchorEndType)
{
Type = PosType.AnchorEnd;
}
if (pos.GetType() == _posCombineType)
{
var posView = _posCombineLeftField.GetValue(pos);
View = (View)_posViewTargetField.GetValue(posView);
Value = (int)_posViewSideField.GetValue(posView);
switch (Value)
{
case 0:
Type = PosType.Left;
break;
case 1:
Type = PosType.Top;
break;
case 2:
Type = PosType.Right;
break;
case 3:
Type = PosType.Bottom;
break;
}
}
if (pos.GetType() == _posFactorType)
{
}
}
public PosType Type { get; }
public int Value { get; }
public View View { get; set; }
public override string ToString()
{
switch (Type)
{
case PosType.AnchorEnd:
return $"[Terminal.Gui.Pos]::AnchorEnd()";
case PosType.At:
return $"[Terminal.Gui.Pos]::At({Value})";
case PosType.Bottom:
return $"[Terminal.Gui.Pos]::Bottom(${View.Id})";
case PosType.Top:
return $"[Terminal.Gui.Pos]::Top(${View.Id})";
case PosType.Left:
return $"[Terminal.Gui.Pos]::Left(${View.Id})";
case PosType.Right:
return $"[Terminal.Gui.Pos]::Right(${View.Id})";
}
return string.Empty;
}
}
public enum PosType
{
AnchorEnd,
At,
Bottom,
Left,
Right,
Top
}
}