1
- //------------------------------------------------------------------------------
1
+ #nullable enable
2
+ //------------------------------------------------------------------------------
2
3
// Windows Terminal supports Unicode and Emoji characters, but by default
3
4
// conhost shells (e.g., PowerShell and cmd.exe) do not. See
4
5
// <https://spectreconsole.net/best-practices>.
5
6
//------------------------------------------------------------------------------
6
7
7
- using System . Diagnostics ;
8
-
9
8
namespace Terminal . Gui ;
10
9
11
10
/// <summary>A <see cref="View"/> which displays (by default) a spinning line character.</summary>
12
11
/// <remarks>
13
- /// By default animation only occurs when you call <see cref="SpinnerView.AdvanceAnimation(bool)"/>. Use
12
+ /// By default, animation only occurs when you call <see cref="SpinnerView.AdvanceAnimation(bool)"/>. Use
14
13
/// <see cref="AutoSpin"/> to make the automate calls to <see cref="SpinnerView.AdvanceAnimation(bool)"/>.
15
14
/// </remarks>
16
15
public class SpinnerView : View , IDesignable
@@ -25,7 +24,7 @@ public class SpinnerView : View, IDesignable
25
24
private DateTime _lastRender = DateTime . MinValue ;
26
25
private string [ ] _sequence = DEFAULT_STYLE . Sequence ;
27
26
private SpinnerStyle _style = DEFAULT_STYLE ;
28
- private object _timeout ;
27
+ private object ? _timeout ;
29
28
30
29
/// <summary>Creates a new instance of the <see cref="SpinnerView"/> class.</summary>
31
30
public SpinnerView ( )
@@ -134,14 +133,7 @@ public void AdvanceAnimation (bool setNeedsDraw = true)
134
133
{
135
134
if ( SpinBounce )
136
135
{
137
- if ( SpinReverse )
138
- {
139
- _bounceReverse = false ;
140
- }
141
- else
142
- {
143
- _bounceReverse = true ;
144
- }
136
+ _bounceReverse = ! SpinReverse ;
145
137
146
138
_currentIdx = Sequence . Length - 1 ;
147
139
}
@@ -155,14 +147,7 @@ public void AdvanceAnimation (bool setNeedsDraw = true)
155
147
{
156
148
if ( SpinBounce )
157
149
{
158
- if ( SpinReverse )
159
- {
160
- _bounceReverse = true ;
161
- }
162
- else
163
- {
164
- _bounceReverse = false ;
165
- }
150
+ _bounceReverse = SpinReverse ;
166
151
167
152
_currentIdx = 1 ;
168
153
}
@@ -182,25 +167,26 @@ public void AdvanceAnimation (bool setNeedsDraw = true)
182
167
}
183
168
}
184
169
185
- /// <inheritdoc />
170
+ /// <inheritdoc/>
186
171
protected override bool OnClearingViewport ( ) { return true ; }
187
172
188
- /// <inheritdoc />
173
+ /// <inheritdoc/>
189
174
protected override bool OnDrawingContent ( )
190
175
{
191
176
Render ( ) ;
177
+
192
178
return true ;
193
179
}
194
180
195
181
/// <summary>
196
- /// Renders the current frame of the spinner.
182
+ /// Renders the current frame of the spinner.
197
183
/// </summary>
198
184
public void Render ( )
199
185
{
200
186
if ( Sequence is { Length : > 0 } && _currentIdx < Sequence . Length )
201
187
{
202
188
Move ( Viewport . X , Viewport . Y ) ;
203
- View . Driver ? . AddStr ( Sequence [ _currentIdx ] ) ;
189
+ Driver ? . AddStr ( Sequence [ _currentIdx ] ) ;
204
190
}
205
191
}
206
192
@@ -214,7 +200,8 @@ protected override void Dispose (bool disposing)
214
200
215
201
private void AddAutoSpinTimeout ( )
216
202
{
217
- if ( _timeout is { } )
203
+ // Only add timeout if we are initialized and not already spinning
204
+ if ( _timeout is { } || ! Application . Initialized )
218
205
{
219
206
return ;
220
207
}
@@ -223,7 +210,7 @@ private void AddAutoSpinTimeout ()
223
210
TimeSpan . FromMilliseconds ( SpinDelay ) ,
224
211
( ) =>
225
212
{
226
- Application . Invoke ( ( ) => AdvanceAnimation ( ) ) ;
213
+ Application . Invoke ( ( ) => AdvanceAnimation ( ) ) ;
227
214
228
215
return true ;
229
216
}
@@ -237,7 +224,7 @@ private bool GetIsAsciiOnly ()
237
224
return false ;
238
225
}
239
226
240
- if ( _sequence is { } && _sequence . Length > 0 )
227
+ if ( _sequence is { Length : > 0 } )
241
228
{
242
229
foreach ( string frame in _sequence )
243
230
{
@@ -260,7 +247,7 @@ private int GetSpinnerWidth ()
260
247
{
261
248
var max = 0 ;
262
249
263
- if ( _sequence is { } && _sequence . Length > 0 )
250
+ if ( _sequence is { Length : > 0 } )
264
251
{
265
252
foreach ( string frame in _sequence )
266
253
{
@@ -295,15 +282,15 @@ private void SetDelay (int delay)
295
282
296
283
private void SetSequence ( string [ ] frames )
297
284
{
298
- if ( frames is { } && frames . Length > 0 )
285
+ if ( frames is { Length : > 0 } )
299
286
{
300
287
_style = new SpinnerStyle . Custom ( ) ;
301
288
_sequence = frames ;
302
289
Width = GetSpinnerWidth ( ) ;
303
290
}
304
291
}
305
292
306
- private void SetStyle ( SpinnerStyle style )
293
+ private void SetStyle ( SpinnerStyle ? style )
307
294
{
308
295
if ( style is { } )
309
296
{
@@ -320,6 +307,7 @@ bool IDesignable.EnableForDesign ()
320
307
Style = new SpinnerStyle . Points ( ) ;
321
308
SpinReverse = true ;
322
309
AutoSpin = true ;
310
+
323
311
return true ;
324
312
}
325
313
}
0 commit comments