Progress.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Threading;
  3. using Terminal.Gui;
  4. namespace UICatalog {
  5. //
  6. // This would be a great scenario to show of threading (Issue #471)
  7. //
  8. [ScenarioMetadata (Name: "Progress", Description: "Shows off ProgressBar.")]
  9. [ScenarioCategory ("Controls")]
  10. class Progress : Scenario {
  11. private ProgressBar _activityProgressBar;
  12. private ProgressBar _pulseProgressBar;
  13. private Timer _timer;
  14. private object _timeoutToken;
  15. public override void Setup ()
  16. {
  17. var pulseButton = new Button ("Pulse") {
  18. X = Pos.Center (),
  19. Y = Pos.Center () - 5,
  20. Clicked = () => Pulse ()
  21. };
  22. Win.Add (new Button ("Start Timer") {
  23. X = Pos.Left(pulseButton) - 20,
  24. Y = Pos.Y(pulseButton),
  25. Clicked = () => Start ()
  26. });
  27. Win.Add (new Button ("Stop Timer") {
  28. X = Pos.Right (pulseButton) + 20, // BUGBUG: Right is somehow adding additional width
  29. Y = Pos.Y (pulseButton),
  30. Clicked = () => Stop()
  31. });
  32. Win.Add (pulseButton);
  33. _activityProgressBar = new ProgressBar () {
  34. X = Pos.Center (),
  35. // BUGBUG: If you remove the +1 below the control is drawn at top?!?!
  36. Y = Pos.Center ()+1,
  37. Width = 30,
  38. Fraction = 0.25F,
  39. };
  40. Win.Add (_activityProgressBar);
  41. _pulseProgressBar = new ProgressBar () {
  42. X = Pos.Center (),
  43. // BUGBUG: If you remove the +1 below the control is drawn at top?!?!
  44. Y = Pos.Center () + 3,
  45. Width = 30,
  46. };
  47. Win.Add (_pulseProgressBar);
  48. }
  49. protected override void Dispose (bool disposing)
  50. {
  51. _timer?.Dispose ();
  52. _timer = null;
  53. Application.MainLoop.RemoveTimeout (_timeoutToken);
  54. base.Dispose (disposing);
  55. }
  56. private void Pulse ()
  57. {
  58. if (_activityProgressBar.Fraction + 0.01F >= 1) {
  59. _activityProgressBar.Fraction = 0F;
  60. } else {
  61. _activityProgressBar.Fraction += 0.01F;
  62. }
  63. _pulseProgressBar.Pulse ();
  64. }
  65. private void Start ()
  66. {
  67. _timer?.Dispose ();
  68. _timer = null;
  69. _activityProgressBar.Fraction = 0F;
  70. _pulseProgressBar.Fraction = 0F;
  71. _timer = new Timer ((o) => Application.MainLoop.Invoke (() => Pulse ()), null, 0, 10);
  72. // BUGBUG: This timeout does nothing but return true, however it trigger the Application.MainLoop
  73. // to run the Action. Without this timeout, the display updates are random,
  74. // or triggered by user interaction with the UI. See #155
  75. _timeoutToken = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (10), loop => true);
  76. }
  77. private void Stop ()
  78. {
  79. _timer?.Dispose ();
  80. _timer = null;
  81. Application.MainLoop.RemoveTimeout (_timeoutToken);
  82. _activityProgressBar.Fraction = 1F;
  83. _pulseProgressBar.Fraction = 1F;
  84. }
  85. }
  86. }