Skip to content

Commit 7656602

Browse files
committed
#nullable enable
1 parent 2f9cf08 commit 7656602

16 files changed

+242
-202
lines changed

Terminal.Gui/Drawing/Thickness.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Numerics;
1+
#nullable enable
2+
using System.Numerics;
23
using System.Text.Json.Serialization;
34

45
namespace Terminal.Gui;
@@ -15,10 +16,7 @@ namespace Terminal.Gui;
1516
/// with the thickness widths subtracted.
1617
/// </para>
1718
/// <para>
18-
/// Use the helper API (<see cref="Draw(Rectangle, string)"/> to draw the frame with the specified thickness.
19-
/// </para>
20-
/// <para>
21-
/// Thickness uses <see langword="float"/> intenrally. As a result, there is a potential precision loss for very
19+
/// Thickness uses <see langword="float"/> internally. As a result, there is a potential precision loss for very
2220
/// large numbers. This is typically not an issue for UI dimensions but could be relevant in other contexts.
2321
/// </para>
2422
/// </remarks>
@@ -91,7 +89,7 @@ public bool Contains (in Rectangle outside, in Point location)
9189
/// <param name="diagnosticFlags"></param>
9290
/// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
9391
/// <returns>The inner rectangle remaining to be drawn.</returns>
94-
public Rectangle Draw (Rectangle rect, ViewDiagnosticFlags diagnosticFlags = ViewDiagnosticFlags.Off, string label = null)
92+
public Rectangle Draw (Rectangle rect, ViewDiagnosticFlags diagnosticFlags = ViewDiagnosticFlags.Off, string? label = null)
9593
{
9694
if (rect.Size.Width < 1 || rect.Size.Height < 1)
9795
{
@@ -134,26 +132,26 @@ public Rectangle Draw (Rectangle rect, ViewDiagnosticFlags diagnosticFlags = Vie
134132
if (Right > 0)
135133
{
136134
Application.Driver?.FillRect (
137-
rect with
138-
{
139-
X = Math.Max (0, rect.X + rect.Width - Right),
140-
Width = Math.Min (rect.Width, Right)
141-
},
142-
rightChar
143-
);
135+
rect with
136+
{
137+
X = Math.Max (0, rect.X + rect.Width - Right),
138+
Width = Math.Min (rect.Width, Right)
139+
},
140+
rightChar
141+
);
144142
}
145143

146144
// Draw the Bottom side
147145
if (Bottom > 0)
148146
{
149147
Application.Driver?.FillRect (
150-
rect with
151-
{
152-
Y = rect.Y + Math.Max (0, rect.Height - Bottom),
153-
Height = Bottom
154-
},
155-
bottomChar
156-
);
148+
rect with
149+
{
150+
Y = rect.Y + Math.Max (0, rect.Height - Bottom),
151+
Height = Bottom
152+
},
153+
bottomChar
154+
);
157155
}
158156

159157
if (diagnosticFlags.HasFlag (ViewDiagnosticFlags.Ruler))
@@ -192,6 +190,7 @@ rect with
192190
{
193191
// Draw the diagnostics label on the bottom
194192
string text = label is null ? string.Empty : $"{label} {this}";
193+
195194
var tf = new TextFormatter
196195
{
197196
Text = text,

Terminal.Gui/View/Adornment/Border.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
483483
Point parentLoc = Parent.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y))
484484
?? mouseEvent.ScreenPosition;
485485

486-
int minHeight = Thickness.Vertical + Parent!.Margin.Thickness.Bottom;
487-
int minWidth = Thickness.Horizontal + Parent!.Margin.Thickness.Right;
486+
int minHeight = Thickness.Vertical + Parent!.Margin!.Thickness.Bottom;
487+
int minWidth = Thickness.Horizontal + Parent!.Margin!.Thickness.Right;
488488

489489
// TODO: This code can be refactored to be more readable and maintainable.
490490
switch (_arranging)
@@ -1072,7 +1072,7 @@ private static void GetAppealingGradientColors (out List<Color> stops, out List<
10721072
NoPadding = true,
10731073
ShadowStyle = ShadowStyle.None,
10741074
Text = $"{Glyphs.SizeVertical}",
1075-
X = Pos.Center () + Parent!.Margin.Thickness.Horizontal,
1075+
X = Pos.Center () + Parent!.Margin!.Thickness.Horizontal,
10761076
Y = 0,
10771077
Visible = false,
10781078
Data = ViewArrangement.TopResizable
@@ -1095,7 +1095,7 @@ private static void GetAppealingGradientColors (out List<Color> stops, out List<
10951095
ShadowStyle = ShadowStyle.None,
10961096
Text = $"{Glyphs.SizeHorizontal}",
10971097
X = Pos.AnchorEnd (),
1098-
Y = Pos.Center () + Parent!.Margin.Thickness.Vertical / 2,
1098+
Y = Pos.Center () + Parent!.Margin!.Thickness.Vertical / 2,
10991099
Visible = false,
11001100
Data = ViewArrangement.RightResizable
11011101
};
@@ -1117,7 +1117,7 @@ private static void GetAppealingGradientColors (out List<Color> stops, out List<
11171117
ShadowStyle = ShadowStyle.None,
11181118
Text = $"{Glyphs.SizeHorizontal}",
11191119
X = 0,
1120-
Y = Pos.Center () + Parent!.Margin.Thickness.Vertical / 2,
1120+
Y = Pos.Center () + Parent!.Margin!.Thickness.Vertical / 2,
11211121
Visible = false,
11221122
Data = ViewArrangement.LeftResizable
11231123
};
@@ -1138,7 +1138,7 @@ private static void GetAppealingGradientColors (out List<Color> stops, out List<
11381138
NoPadding = true,
11391139
ShadowStyle = ShadowStyle.None,
11401140
Text = $"{Glyphs.SizeVertical}",
1141-
X = Pos.Center () + Parent!.Margin.Thickness.Horizontal / 2,
1141+
X = Pos.Center () + Parent!.Margin!.Thickness.Horizontal / 2,
11421142
Y = Pos.AnchorEnd (),
11431143
Visible = false,
11441144
Data = ViewArrangement.BottomResizable

Terminal.Gui/View/Adornment/Margin.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ private void Margin_LayoutStarted (object? sender, LayoutEventArgs e)
275275
{
276276
case ShadowStyle.Transparent:
277277
// BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
278-
_rightShadow.Y = Parent!.Border.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
278+
_rightShadow.Y = Parent!.Border!.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
279279

280280
break;
281281

282282
case ShadowStyle.Opaque:
283283
// BUGBUG: This doesn't work right for all Border.Top sizes - Need an API on Border that gives top-right location of line corner.
284-
_rightShadow.Y = Parent!.Border.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
284+
_rightShadow.Y = Parent!.Border!.Thickness.Top > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).Y + 1 : 0;
285285
_bottomShadow.X = Parent.Border.Thickness.Left > 0 ? ScreenToViewport (Parent.Border.GetBorderRectangle ().Location).X + 1 : 0;
286286

287287
break;

Terminal.Gui/View/DrawEventArgs.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ public DrawEventArgs (Rectangle newViewport, Rectangle oldViewport)
2020
OldViewport = oldViewport;
2121
}
2222

23-
/// <summary>If set to true, the draw operation will be canceled, if applicable.</summary>
24-
public bool Cancel { get; set; }
25-
2623
/// <summary>Gets the Content-relative rectangle describing the old visible viewport into the <see cref="View"/>.</summary>
2724
public Rectangle OldViewport { get; }
2825

Terminal.Gui/View/View.Adornments.cs

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Terminal.Gui;
1+
#nullable enable
2+
namespace Terminal.Gui;
23

34
public partial class View // Adornments
45
{
@@ -7,9 +8,10 @@ public partial class View // Adornments
78
/// </summary>
89
private void SetupAdornments ()
910
{
10-
//// TODO: Move this to Adornment as a static factory method
11+
// TODO: Move this to Adornment as a static factory method
1112
if (this is not Adornment)
1213
{
14+
// TODO: Make the Adornments Lazy and only create them when needed
1315
Margin = new (this);
1416
Border = new (this);
1517
Padding = new (this);
@@ -61,7 +63,7 @@ private void DisposeAdornments ()
6163
/// <see cref="SuperView"/> and its <see cref="Subviews"/>.
6264
/// </para>
6365
/// </remarks>
64-
public Margin Margin { get; private set; }
66+
public Margin? Margin { get; private set; }
6567

6668
private ShadowStyle _shadowStyle;
6769

@@ -117,7 +119,7 @@ public virtual ShadowStyle ShadowStyle
117119
/// <see cref="SuperView"/> and its <see cref="Subviews"/>.
118120
/// </para>
119121
/// </remarks>
120-
public Border Border { get; private set; }
122+
public Border? Border { get; private set; }
121123

122124
/// <summary>Gets or sets whether the view has a one row/col thick border.</summary>
123125
/// <remarks>
@@ -130,6 +132,10 @@ public virtual ShadowStyle ShadowStyle
130132
/// Setting this property to <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
131133
/// <see cref="Adornment.Thickness"/> to `0` and <see cref="BorderStyle"/> to <see cref="LineStyle.None"/>.
132134
/// </para>
135+
/// <para>
136+
/// Calls <see cref="OnBorderStyleChanging"/> and raises <see cref="BorderStyleChanging"/>, which allows change
137+
/// to be cancelled.
138+
/// </para>
133139
/// <para>For more advanced customization of the view's border, manipulate see <see cref="Border"/> directly.</para>
134140
/// </remarks>
135141
public LineStyle BorderStyle
@@ -150,32 +156,32 @@ public LineStyle BorderStyle
150156
return;
151157
}
152158

159+
BorderStyleChanging?.Invoke (this, e);
160+
161+
if (e.Cancel)
162+
{
163+
return;
164+
}
165+
153166
SetBorderStyle (e.NewValue);
154167
SetAdornmentFrames ();
155168
SetNeedsLayout ();
156-
157169
}
158170
}
159171

160172
/// <summary>
161-
/// Called when the <see cref="BorderStyle"/> is changing. Invokes <see cref="BorderStyleChanging"/>, which allows the
162-
/// event to be cancelled.
173+
/// Called when the <see cref="BorderStyle"/> is changing.
163174
/// </summary>
164175
/// <remarks>
165-
/// Override <see cref="SetBorderStyle"/> to prevent the <see cref="BorderStyle"/> from changing.
176+
/// Set e.Cancel to true to prevent the <see cref="BorderStyle"/> from changing.
166177
/// </remarks>
167178
/// <param name="e"></param>
168-
protected virtual bool OnBorderStyleChanging (CancelEventArgs<LineStyle> e)
169-
{
170-
if (Border is null)
171-
{
172-
return false;
173-
}
174-
175-
BorderStyleChanging?.Invoke (this, e);
179+
protected virtual bool OnBorderStyleChanging (CancelEventArgs<LineStyle> e) { return false; }
176180

177-
return e.Cancel;
178-
}
181+
/// <summary>
182+
/// Fired when the <see cref="BorderStyle"/> is changing. Allows the event to be cancelled.
183+
/// </summary>
184+
public event EventHandler<CancelEventArgs<LineStyle>>? BorderStyleChanging;
179185

180186
/// <summary>
181187
/// Sets the <see cref="BorderStyle"/> of the view to the specified value.
@@ -198,25 +204,19 @@ public virtual void SetBorderStyle (LineStyle value)
198204
{
199205
if (value != LineStyle.None)
200206
{
201-
if (Border.Thickness == Thickness.Empty)
207+
if (Border!.Thickness == Thickness.Empty)
202208
{
203209
Border.Thickness = new (1);
204210
}
205211
}
206212
else
207213
{
208-
Border.Thickness = new (0);
214+
Border!.Thickness = new (0);
209215
}
210216

211217
Border.LineStyle = value;
212218
}
213219

214-
/// <summary>
215-
/// Fired when the <see cref="BorderStyle"/> is changing. Allows the event to be cancelled.
216-
/// </summary>
217-
[CanBeNull]
218-
public event EventHandler<CancelEventArgs<LineStyle>> BorderStyleChanging;
219-
220220
/// <summary>
221221
/// The <see cref="Adornment"/> inside of the view that offsets the <see cref="Viewport"/>
222222
/// from the <see cref="Border"/>.
@@ -232,7 +232,7 @@ public virtual void SetBorderStyle (LineStyle value)
232232
/// <see cref="SuperView"/> and its <see cref="Subviews"/>.
233233
/// </para>
234234
/// </remarks>
235-
public Padding Padding { get; private set; }
235+
public Padding? Padding { get; private set; }
236236

237237
/// <summary>
238238
/// <para>Gets the thickness describing the sum of the Adornments' thicknesses.</para>
@@ -245,12 +245,24 @@ public virtual void SetBorderStyle (LineStyle value)
245245
/// <returns>A thickness that describes the sum of the Adornments' thicknesses.</returns>
246246
public Thickness GetAdornmentsThickness ()
247247
{
248-
if (Margin is null)
248+
Thickness result = Thickness.Empty;
249+
250+
if (Margin is { })
251+
{
252+
result += Margin.Thickness;
253+
}
254+
255+
if (Border is { })
249256
{
250-
return Thickness.Empty;
257+
result += Border.Thickness;
251258
}
252259

253-
return Margin.Thickness + Border.Thickness + Padding.Thickness;
260+
if (Padding is { })
261+
{
262+
result += Padding.Thickness;
263+
}
264+
265+
return result;
254266
}
255267

256268
/// <summary>Sets the Frame's of the Margin, Border, and Padding.</summary>
@@ -262,13 +274,19 @@ internal void SetAdornmentFrames ()
262274
return;
263275
}
264276

265-
if (Margin is null)
277+
if (Margin is { })
278+
{
279+
Margin!.Frame = Rectangle.Empty with { Size = Frame.Size };
280+
}
281+
282+
if (Border is { } && Margin is { })
266283
{
267-
return; // CreateAdornments () has not been called yet
284+
Border!.Frame = Margin!.Thickness.GetInside (Margin!.Frame);
268285
}
269286

270-
Margin.Frame = Rectangle.Empty with { Size = Frame.Size };
271-
Border.Frame = Margin.Thickness.GetInside (Margin.Frame);
272-
Padding.Frame = Border.Thickness.GetInside (Border.Frame);
287+
if (Padding is { } && Border is { })
288+
{
289+
Padding!.Frame = Border!.Thickness.GetInside (Border!.Frame);
290+
}
273291
}
274292
}

Terminal.Gui/View/View.Arrangement.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
namespace Terminal.Gui;
1+
#nullable enable
2+
namespace Terminal.Gui;
23

34
public partial class View
45
{

0 commit comments

Comments
 (0)