-
Notifications
You must be signed in to change notification settings - Fork 673
/
FrameView.cs
286 lines (258 loc) · 7.64 KB
/
FrameView.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
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
// NOTE: FrameView is functionally identical to Window with the following exceptions.
// - Is not a Toplevel
// - Does not support mouse dragging
// - Does not support padding (but should)
// - Does not support IEnumerable
// Any udpates done here should probably be done in Window as well; TODO: Merge these classes
using System.Linq;
using NStack;
namespace Terminal.Gui {
/// <summary>
/// The FrameView is a container frame that draws a frame around the contents. It is similar to
/// a GroupBox in Windows.
/// </summary>
public class FrameView : View {
View contentView;
ustring title;
/// <summary>
/// The title to be displayed for this <see cref="FrameView"/>.
/// </summary>
/// <value>The title.</value>
public ustring Title {
get => title;
set {
title = value;
if (Border != null) {
Border.Title = title;
}
SetNeedsDisplay ();
}
}
/// <inheritdoc/>
public override Border Border {
get => base.Border;
set {
if (base.Border != null && base.Border.Child != null && value.Child == null) {
value.Child = base.Border.Child;
}
base.Border = value;
if (value == null) {
return;
}
Rect frame;
if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
frame = Rect.Empty;
} else {
frame = Frame;
}
AdjustContentView (frame);
Border.BorderChanged += Border_BorderChanged;
}
}
void Border_BorderChanged (Border border)
{
Rect frame;
if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
frame = Rect.Empty;
} else {
frame = Frame;
}
AdjustContentView (frame);
}
/// <summary>
/// ContentView is an internal implementation detail of Window. It is used to host Views added with <see cref="Add(View)"/>.
/// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
/// are actually deflated due to the border.
/// </summary>
class ContentView : View {
public ContentView (Rect frame) : base (frame) { }
public ContentView () : base () { }
}
/// <summary>
/// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Absolute"/> layout.
/// </summary>
/// <param name="frame">Frame.</param>
/// <param name="title">Title.</param>
/// <param name="views">Views.</param>
/// <param name="border">The <see cref="Border"/>.</param>
public FrameView (Rect frame, ustring title = null, View [] views = null, Border border = null) : base (frame)
{
//var cFrame = new Rect (1, 1, Math.Max (frame.Width - 2, 0), Math.Max (frame.Height - 2, 0));
Initialize (frame, title, views, border);
}
/// <summary>
/// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
/// </summary>
/// <param name="title">Title.</param>
/// <param name="border">The <see cref="Border"/>.</param>
public FrameView (ustring title, Border border = null)
{
Initialize (Rect.Empty, title, null, border);
}
/// <summary>
/// Initializes a new instance of the <see cref="Gui.FrameView"/> class using <see cref="LayoutStyle.Computed"/> layout.
/// </summary>
public FrameView () : this (title: string.Empty) { }
void Initialize (Rect frame, ustring title, View [] views = null, Border border = null)
{
if (title == null) title = ustring.Empty;
this.Title = title;
if (border == null) {
Border = new Border () {
BorderStyle = BorderStyle.Single,
Title = title
};
} else {
Border = border;
if (ustring.IsNullOrEmpty (border.Title)) {
border.Title = title;
}
}
AdjustContentView (frame, views);
}
void AdjustContentView (Rect frame, View [] views = null)
{
var borderLength = Border.DrawMarginFrame ? 1 : 0;
var sumPadding = Border.GetSumThickness ();
var wp = new Point ();
var wb = new Size ();
if (frame == Rect.Empty) {
wp.X = borderLength + sumPadding.Left;
wp.Y = borderLength + sumPadding.Top;
wb.Width = borderLength + sumPadding.Right;
wb.Height = borderLength + sumPadding.Bottom;
if (contentView == null) {
contentView = new ContentView () {
X = wp.X,
Y = wp.Y,
Width = Dim.Fill (wb.Width),
Height = Dim.Fill (wb.Height)
};
} else {
contentView.X = wp.X;
contentView.Y = wp.Y;
contentView.Width = Dim.Fill (wb.Width);
contentView.Height = Dim.Fill (wb.Height);
}
} else {
wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
if (contentView == null) {
contentView = new ContentView (cFrame);
} else {
contentView.Frame = cFrame;
}
}
if (views != null) {
foreach (var view in views) {
contentView.Add (view);
}
}
if (Subviews?.Count == 0) {
base.Add (contentView);
contentView.Text = base.Text;
}
Border.Child = contentView;
}
void DrawFrame ()
{
DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
}
/// <summary>
/// Add the specified <see cref="View"/> to this container.
/// </summary>
/// <param name="view"><see cref="View"/> to add to this container</param>
public override void Add (View view)
{
contentView.Add (view);
if (view.CanFocus)
CanFocus = true;
}
/// <summary>
/// Removes a <see cref="View"/> from this container.
/// </summary>
/// <remarks>
/// </remarks>
public override void Remove (View view)
{
if (view == null)
return;
SetNeedsDisplay ();
var touched = view.Frame;
if (view == contentView) {
base.Remove (view);
} else {
contentView.Remove (view);
}
if (contentView.InternalSubviews.Count < 1)
this.CanFocus = false;
}
/// <summary>
/// Removes all <see cref="View"/>s from this container.
/// </summary>
/// <remarks>
/// </remarks>
public override void RemoveAll ()
{
contentView.RemoveAll ();
}
///<inheritdoc/>
public override void Redraw (Rect bounds)
{
if (!NeedDisplay.IsEmpty) {
Driver.SetAttribute (GetNormalColor ());
Clear ();
}
var savedClip = contentView.ClipToBounds ();
contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
Driver.Clip = savedClip;
ClearLayoutNeeded ();
ClearNeedsDisplay ();
Driver.SetAttribute (GetNormalColor ());
Border.DrawContent (this, false);
}
/// <summary>
/// The text displayed by the <see cref="Label"/>.
/// </summary>
public override ustring Text {
get => contentView?.Text;
set {
base.Text = value;
if (contentView != null) {
contentView.Text = value;
}
}
}
/// <summary>
/// Controls the text-alignment property of the label, changing it will redisplay the <see cref="Label"/>.
/// </summary>
/// <value>The text alignment.</value>
public override TextAlignment TextAlignment {
get => contentView.TextAlignment;
set {
base.TextAlignment = contentView.TextAlignment = value;
}
}
///<inheritdoc/>
public override bool OnEnter (View view)
{
if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
}
return base.OnEnter (view);
}
/// <inheritdoc/>
public override void OnCanFocusChanged ()
{
if (contentView != null) {
contentView.CanFocus = CanFocus;
}
base.OnCanFocusChanged ();
}
}
}