-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathDimEditorView.cs
More file actions
182 lines (148 loc) · 4.85 KB
/
DimEditorView.cs
File metadata and controls
182 lines (148 loc) · 4.85 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NStack;
namespace Terminal.Gui.Designer
{
public class DimEditorView : Dialog
{
private RadioGroup _radioGroup;
private TextField _valueText;
private Dictionary<ustring, DimType> _dimTypes = new Dictionary<ustring, DimType> {
{ "Absolute", DimType.Absolute },
{ "Fill", DimType.Fill },
{ "Percent", DimType.Percent }
};
public DimEditorView(Dim dim) : base("Edit Dimension")
{
Width = 50;
Height = 7;
var ok = new Button("Ok");
var cancel = new Button("Cancel");
var dimInfo = new DimInfo(dim);
ok.Clicked += () =>
{
var dim1 = MakeDim();
if (DimChanged != null && dim != null)
{
DimChanged(this, new DimChangedEventArgs(dim1));
}
Application.RequestStop();
};
cancel.Clicked += () =>
{
Application.RequestStop();
};
var selectedType = _dimTypes.Values.Select((v, i) => new { Value = v, index = i }).First(m => m.Value == dimInfo.Type).index;
_radioGroup = new RadioGroup(_dimTypes.Keys.ToArray(), selectedType);
_radioGroup.DisplayMode = DisplayModeLayout.Horizontal;
Add(_radioGroup);
var valueLabel = new Label(dimInfo.ValueName)
{
X = 0,
Y = Pos.Bottom(_radioGroup) + 1
};
_valueText = new TextField()
{
X = Pos.Right(valueLabel) + 1,
Y = Pos.Bottom(_radioGroup) + 1,
Width = Dim.Fill(),
Text = dimInfo.Value.ToString()
};
Add(valueLabel);
Add(_valueText);
_radioGroup.SelectedItemChanged += (args) =>
{
var dim1 = MakeDim();
var dimInfo1 = new DimInfo(dim1);
_valueText.ReadOnly = dimInfo1.Type == DimType.Fill;
};
AddButton(ok);
AddButton(cancel);
}
private Dim MakeDim()
{
if (!float.TryParse(_valueText.Text.ToString(), out float value))
{
return null;
}
var selectedType = _dimTypes.Values.Select((v, i) => new { Value = v, index = i }).First(m => m.index == _radioGroup.SelectedItem).Value;
switch (selectedType)
{
case DimType.Absolute:
return Dim.Sized((int)value);
case DimType.Fill:
return Dim.Fill();
case DimType.Percent:
return Dim.Percent(value);
}
return null;
}
public event EventHandler<DimChangedEventArgs> DimChanged;
}
public class DimChangedEventArgs : EventArgs
{
public Dim Dim { get; }
public DimChangedEventArgs(Dim dim)
{
Dim = dim;
}
}
public class DimInfo
{
private readonly Regex argumentRegEx = new Regex("\\((.*)\\)");
public DimInfo(Dim dim)
{
var str = dim.ToString();
Match match = argumentRegEx.Match(str);
var arguments = string.Empty;
if (match.Success)
{
arguments = match.Groups[1].Value;
}
if (str.Contains("Absolute"))
{
Type = DimType.Absolute;
if (int.TryParse(arguments, out int val))
{
Value = val;
}
}
if (str.Contains("Fill")) Type = DimType.Fill;
if (str.Contains("DimView")) Type = DimType.Height;
if (str.Contains("Factor"))
{
// factor=0.01, remaining=False
var args = arguments.Split(',');
Value = float.Parse(args[0].Split('=')[1]);
Type = DimType.Percent;
}
}
public DimType Type { get; set; }
public float Value { get; set; }
public string ValueName { get; } = "Value";
public override string ToString()
{
switch (Type)
{
case DimType.Absolute:
return $"[Terminal.Gui.Dim]::Sized({Value})";
case DimType.Fill:
return $"[Terminal.Gui.Dim]::Fill()";
case DimType.Percent:
return $"[Terminal.Gui.Dim]::Percent({Value})";
}
return string.Empty;
}
}
public enum DimType
{
Absolute,
Fill,
Height,
Percent,
Sized,
Width
}
}