MainLoopTimeouts.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma warning disable format
  2. #pragma warning restore format
  3. using System;
  4. using System.Collections.Generic;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios {
  7. [ScenarioMetadata (Name: "MainLoopTimeouts", Description: "MainLoop Timeouts")]
  8. [ScenarioCategory ("Tests")]
  9. public class MainLoopTimeouts : Scenario {
  10. static readonly List<string> GlobalList = new () { "1" };
  11. static readonly ListView GlobalListView = new () { Width = Dim.Fill (), Height = Dim.Fill () };
  12. static Label CounterLabel;
  13. static Label BlinkingLabel;
  14. static int Counter = 0;
  15. static object _listToken = null;
  16. static object _blinkToken = null;
  17. static object _countToken = null;
  18. public override void Init (ColorScheme colorScheme)
  19. {
  20. Application.Init ();
  21. var startButton = new Button ("Start");
  22. var stopButton = new Button ("Stop") { Y = 1 };
  23. var container = new View () { X = Pos.Center (), Y = Pos.Center (), Width = 8, Height = 8, ColorScheme = Colors.Error };
  24. CounterLabel = new Label ("0") { X = Pos.X (container), Y = Pos.Y (container) - 2 };
  25. BlinkingLabel = new Label ("Blink") { X = Pos.X (container), Y = Pos.Bottom (container) + 1 };
  26. startButton.Clicked += Start;
  27. stopButton.Clicked += Stop;
  28. GlobalListView.SetSource (GlobalList);
  29. container.Add (GlobalListView);
  30. Application.Top.Add (container, CounterLabel, BlinkingLabel);
  31. Application.Top.Add (startButton, stopButton);
  32. Application.Run ();
  33. Application.Shutdown ();
  34. }
  35. public override void Run ()
  36. {
  37. }
  38. private static void Start ()
  39. {
  40. _listToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), Add);
  41. _blinkToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (1000), Blink);
  42. _countToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (1000), Count);
  43. }
  44. private static void Stop ()
  45. {
  46. Application.MainLoop.RemoveTimeout (_listToken);
  47. Application.MainLoop.RemoveTimeout (_blinkToken);
  48. Application.MainLoop.RemoveTimeout (_countToken);
  49. }
  50. private static bool Add (MainLoop mainLoop)
  51. {
  52. Application.MainLoop.Invoke (() => {
  53. GlobalList.Add (new Random ().Next (100).ToString ());
  54. GlobalListView.MoveDown ();
  55. });
  56. return true;
  57. }
  58. private static bool Blink (MainLoop mainLoop)
  59. {
  60. Application.MainLoop.Invoke (() => {
  61. if (BlinkingLabel.Visible) {
  62. BlinkingLabel.Visible = false;
  63. System.Diagnostics.Debug.WriteLine (BlinkingLabel.Visible);
  64. } else {
  65. BlinkingLabel.Visible = true;
  66. System.Diagnostics.Debug.WriteLine (BlinkingLabel.Visible);
  67. }
  68. });
  69. return true;
  70. }
  71. private static bool Count (MainLoop mainLoop)
  72. {
  73. Application.MainLoop.Invoke (() => {
  74. Counter++;
  75. CounterLabel.Text = Counter.ToString ();
  76. });
  77. return true;
  78. }
  79. }
  80. }