@@ -305,7 +305,8 @@ internal static bool PositionCursor ()
305
305
/// <returns>The created <see cref="Toplevel"/> object. The caller is responsible for disposing this object.</returns>
306
306
[ RequiresUnreferencedCode ( "AOT" ) ]
307
307
[ RequiresDynamicCode ( "AOT" ) ]
308
- public static Toplevel Run ( Func < Exception , bool > ? errorHandler = null , IConsoleDriver ? driver = null ) { return Run < Toplevel > ( errorHandler , driver ) ; }
308
+ public static Toplevel Run ( Func < Exception , bool > ? errorHandler = null , IConsoleDriver ? driver = null ) =>
309
+ ApplicationImpl . Instance . Run ( errorHandler , driver ) ;
309
310
310
311
/// <summary>
311
312
/// Runs the application by creating a <see cref="Toplevel"/>-derived object of type <c>T</c> and calling
@@ -331,20 +332,7 @@ internal static bool PositionCursor ()
331
332
[ RequiresUnreferencedCode ( "AOT" ) ]
332
333
[ RequiresDynamicCode ( "AOT" ) ]
333
334
public static T Run < T > ( Func < Exception , bool > ? errorHandler = null , IConsoleDriver ? driver = null )
334
- where T : Toplevel , new ( )
335
- {
336
- if ( ! Initialized )
337
- {
338
- // Init() has NOT been called.
339
- InternalInit ( driver , null , true ) ;
340
- }
341
-
342
- var top = new T ( ) ;
343
-
344
- Run ( top , errorHandler ) ;
345
-
346
- return top ;
347
- }
335
+ where T : Toplevel , new ( ) => ApplicationImpl . Instance . Run < T > ( errorHandler , driver ) ;
348
336
349
337
/// <summary>Runs the Application using the provided <see cref="Toplevel"/> view.</summary>
350
338
/// <remarks>
@@ -385,110 +373,31 @@ public static T Run<T> (Func<Exception, bool>? errorHandler = null, IConsoleDriv
385
373
/// rethrows when null).
386
374
/// </param>
387
375
public static void Run ( Toplevel view , Func < Exception , bool > ? errorHandler = null )
388
- {
389
- ArgumentNullException . ThrowIfNull ( view ) ;
390
-
391
- if ( Initialized )
392
- {
393
- if ( Driver is null )
394
- {
395
- // Disposing before throwing
396
- view . Dispose ( ) ;
397
-
398
- // This code path should be impossible because Init(null, null) will select the platform default driver
399
- throw new InvalidOperationException (
400
- "Init() completed without a driver being set (this should be impossible); Run<T>() cannot be called."
401
- ) ;
402
- }
403
- }
404
- else
405
- {
406
- // Init() has NOT been called.
407
- throw new InvalidOperationException (
408
- "Init() has not been called. Only Run() or Run<T>() can be used without calling Init()."
409
- ) ;
410
- }
411
-
412
- var resume = true ;
413
-
414
- while ( resume )
415
- {
416
- #if ! DEBUG
417
- try
418
- {
419
- #endif
420
- resume = false ;
421
- RunState runState = Begin ( view ) ;
422
-
423
- // If EndAfterFirstIteration is true then the user must dispose of the runToken
424
- // by using NotifyStopRunState event.
425
- RunLoop ( runState ) ;
426
-
427
- if ( runState . Toplevel is null )
428
- {
429
- #if DEBUG_IDISPOSABLE
430
- Debug . Assert ( TopLevels . Count == 0 ) ;
431
- #endif
432
- runState . Dispose ( ) ;
433
-
434
- return ;
435
- }
436
-
437
- if ( ! EndAfterFirstIteration )
438
- {
439
- End ( runState ) ;
440
- }
441
- #if ! DEBUG
442
- }
443
- catch ( Exception error )
444
- {
445
- if ( errorHandler is null )
446
- {
447
- throw ;
448
- }
449
-
450
- resume = errorHandler ( error ) ;
451
- }
452
- #endif
453
- }
454
- }
376
+ => ApplicationImpl . Instance . Run ( view , errorHandler ) ;
455
377
456
378
/// <summary>Adds a timeout to the application.</summary>
457
379
/// <remarks>
458
380
/// When time specified passes, the callback will be invoked. If the callback returns true, the timeout will be
459
381
/// reset, repeating the invocation. If it returns false, the timeout will stop and be removed. The returned value is a
460
382
/// token that can be used to stop the timeout by calling <see cref="RemoveTimeout(object)"/>.
461
383
/// </remarks>
462
- public static object ? AddTimeout ( TimeSpan time , Func < bool > callback )
463
- {
464
- return MainLoop ? . AddTimeout ( time , callback ) ?? null ;
465
- }
384
+ public static object ? AddTimeout ( TimeSpan time , Func < bool > callback ) => ApplicationImpl . Instance . AddTimeout ( time , callback ) ;
466
385
467
386
/// <summary>Removes a previously scheduled timeout</summary>
468
387
/// <remarks>The token parameter is the value returned by <see cref="AddTimeout"/>.</remarks>
469
388
/// Returns
470
- /// <c> true</c >
389
+ /// <see langword=" true"/ >
471
390
/// if the timeout is successfully removed; otherwise,
472
- /// <c> false</c >
391
+ /// <see langword=" false"/ >
473
392
/// .
474
393
/// This method also returns
475
- /// <c> false</c >
394
+ /// <see langword=" false"/ >
476
395
/// if the timeout is not found.
477
- public static bool RemoveTimeout ( object token ) { return MainLoop ? . RemoveTimeout ( token ) ?? false ; }
396
+ public static bool RemoveTimeout ( object token ) => ApplicationImpl . Instance . RemoveTimeout ( token ) ;
478
397
479
398
/// <summary>Runs <paramref name="action"/> on the thread that is processing events</summary>
480
399
/// <param name="action">the action to be invoked on the main processing thread.</param>
481
- public static void Invoke ( Action action )
482
- {
483
- MainLoop ? . AddIdle (
484
- ( ) =>
485
- {
486
- action ( ) ;
487
-
488
- return false ;
489
- }
490
- ) ;
491
- }
400
+ public static void Invoke ( Action action ) => ApplicationImpl . Instance . Invoke ( action ) ;
492
401
493
402
// TODO: Determine if this is really needed. The only code that calls WakeUp I can find
494
403
// is ProgressBarStyles, and it's not clear it needs to.
@@ -517,8 +426,7 @@ public static void LayoutAndDraw (bool forceDraw = false)
517
426
518
427
View . SetClipToScreen ( ) ;
519
428
View . Draw ( TopLevels , neededLayout || forceDraw ) ;
520
- View . SetClipToScreen ( ) ;
521
-
429
+ View . SetClipToScreen ( ) ;
522
430
Driver ? . Refresh ( ) ;
523
431
}
524
432
@@ -528,7 +436,7 @@ public static void LayoutAndDraw (bool forceDraw = false)
528
436
529
437
/// <summary>The <see cref="MainLoop"/> driver for the application</summary>
530
438
/// <value>The main loop.</value>
531
- internal static MainLoop ? MainLoop { get ; private set ; }
439
+ internal static MainLoop ? MainLoop { get ; set ; }
532
440
533
441
/// <summary>
534
442
/// Set to true to cause <see cref="End"/> to be called after the first iteration. Set to false (the default) to
@@ -612,31 +520,8 @@ public static bool RunIteration (ref RunState state, bool firstIteration = false
612
520
/// property on the currently running <see cref="Toplevel"/> to false.
613
521
/// </para>
614
522
/// </remarks>
615
- public static void RequestStop ( Toplevel ? top = null )
616
- {
617
- if ( top is null )
618
- {
619
- top = Top ;
620
- }
621
-
622
- if ( ! top ! . Running )
623
- {
624
- return ;
625
- }
626
-
627
- var ev = new ToplevelClosingEventArgs ( top ) ;
628
- top . OnClosing ( ev ) ;
629
-
630
- if ( ev . Cancel )
631
- {
632
- return ;
633
- }
634
-
635
- top . Running = false ;
636
- OnNotifyStopRunState ( top ) ;
637
- }
638
-
639
- private static void OnNotifyStopRunState ( Toplevel top )
523
+ public static void RequestStop ( Toplevel ? top = null ) => ApplicationImpl . Instance . RequestStop ( top ) ;
524
+ internal static void OnNotifyStopRunState ( Toplevel top )
640
525
{
641
526
if ( EndAfterFirstIteration )
642
527
{
0 commit comments