1
- namespace Terminal . Gui ;
1
+ #nullable enable
2
+ namespace Terminal . Gui ;
2
3
3
4
public partial class View // Adornments
4
5
{
@@ -7,9 +8,10 @@ public partial class View // Adornments
7
8
/// </summary>
8
9
private void SetupAdornments ( )
9
10
{
10
- //// TODO: Move this to Adornment as a static factory method
11
+ // TODO: Move this to Adornment as a static factory method
11
12
if ( this is not Adornment )
12
13
{
14
+ // TODO: Make the Adornments Lazy and only create them when needed
13
15
Margin = new ( this ) ;
14
16
Border = new ( this ) ;
15
17
Padding = new ( this ) ;
@@ -61,7 +63,7 @@ private void DisposeAdornments ()
61
63
/// <see cref="SuperView"/> and its <see cref="Subviews"/>.
62
64
/// </para>
63
65
/// </remarks>
64
- public Margin Margin { get ; private set ; }
66
+ public Margin ? Margin { get ; private set ; }
65
67
66
68
private ShadowStyle _shadowStyle ;
67
69
@@ -117,7 +119,7 @@ public virtual ShadowStyle ShadowStyle
117
119
/// <see cref="SuperView"/> and its <see cref="Subviews"/>.
118
120
/// </para>
119
121
/// </remarks>
120
- public Border Border { get ; private set ; }
122
+ public Border ? Border { get ; private set ; }
121
123
122
124
/// <summary>Gets or sets whether the view has a one row/col thick border.</summary>
123
125
/// <remarks>
@@ -130,6 +132,10 @@ public virtual ShadowStyle ShadowStyle
130
132
/// Setting this property to <see cref="LineStyle.None"/> is equivalent to setting <see cref="Border"/>'s
131
133
/// <see cref="Adornment.Thickness"/> to `0` and <see cref="BorderStyle"/> to <see cref="LineStyle.None"/>.
132
134
/// </para>
135
+ /// <para>
136
+ /// Calls <see cref="OnBorderStyleChanging"/> and raises <see cref="BorderStyleChanging"/>, which allows change
137
+ /// to be cancelled.
138
+ /// </para>
133
139
/// <para>For more advanced customization of the view's border, manipulate see <see cref="Border"/> directly.</para>
134
140
/// </remarks>
135
141
public LineStyle BorderStyle
@@ -150,32 +156,32 @@ public LineStyle BorderStyle
150
156
return ;
151
157
}
152
158
159
+ BorderStyleChanging ? . Invoke ( this , e ) ;
160
+
161
+ if ( e . Cancel )
162
+ {
163
+ return ;
164
+ }
165
+
153
166
SetBorderStyle ( e . NewValue ) ;
154
167
SetAdornmentFrames ( ) ;
155
168
SetNeedsLayout ( ) ;
156
-
157
169
}
158
170
}
159
171
160
172
/// <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.
163
174
/// </summary>
164
175
/// <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.
166
177
/// </remarks>
167
178
/// <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 ; }
176
180
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 ;
179
185
180
186
/// <summary>
181
187
/// Sets the <see cref="BorderStyle"/> of the view to the specified value.
@@ -198,25 +204,19 @@ public virtual void SetBorderStyle (LineStyle value)
198
204
{
199
205
if ( value != LineStyle . None )
200
206
{
201
- if ( Border . Thickness == Thickness . Empty )
207
+ if ( Border ! . Thickness == Thickness . Empty )
202
208
{
203
209
Border . Thickness = new ( 1 ) ;
204
210
}
205
211
}
206
212
else
207
213
{
208
- Border . Thickness = new ( 0 ) ;
214
+ Border ! . Thickness = new ( 0 ) ;
209
215
}
210
216
211
217
Border . LineStyle = value ;
212
218
}
213
219
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
-
220
220
/// <summary>
221
221
/// The <see cref="Adornment"/> inside of the view that offsets the <see cref="Viewport"/>
222
222
/// from the <see cref="Border"/>.
@@ -232,7 +232,7 @@ public virtual void SetBorderStyle (LineStyle value)
232
232
/// <see cref="SuperView"/> and its <see cref="Subviews"/>.
233
233
/// </para>
234
234
/// </remarks>
235
- public Padding Padding { get ; private set ; }
235
+ public Padding ? Padding { get ; private set ; }
236
236
237
237
/// <summary>
238
238
/// <para>Gets the thickness describing the sum of the Adornments' thicknesses.</para>
@@ -245,12 +245,24 @@ public virtual void SetBorderStyle (LineStyle value)
245
245
/// <returns>A thickness that describes the sum of the Adornments' thicknesses.</returns>
246
246
public Thickness GetAdornmentsThickness ( )
247
247
{
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 { } )
249
256
{
250
- return Thickness . Empty ;
257
+ result += Border . Thickness ;
251
258
}
252
259
253
- return Margin . Thickness + Border . Thickness + Padding . Thickness ;
260
+ if ( Padding is { } )
261
+ {
262
+ result += Padding . Thickness ;
263
+ }
264
+
265
+ return result ;
254
266
}
255
267
256
268
/// <summary>Sets the Frame's of the Margin, Border, and Padding.</summary>
@@ -262,13 +274,19 @@ internal void SetAdornmentFrames ()
262
274
return ;
263
275
}
264
276
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 { } )
266
283
{
267
- return ; // CreateAdornments () has not been called yet
284
+ Border ! . Frame = Margin ! . Thickness . GetInside ( Margin ! . Frame ) ;
268
285
}
269
286
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
+ }
273
291
}
274
292
}
0 commit comments