Progress.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using NStack;
  2. using System;
  3. using System.Threading;
  4. using Terminal.Gui;
  5. using System.Linq;
  6. namespace UICatalog {
  7. //
  8. // This would be a great scenario to show of threading (Issue #471)
  9. //
  10. [ScenarioMetadata (Name: "Progress", Description: "Shows off ProgressBar and Threading")]
  11. [ScenarioCategory ("Controls")]
  12. [ScenarioCategory ("Threading")]
  13. class Progress : Scenario {
  14. class ProgressDemo : FrameView {
  15. const int _verticalSpace = 1;
  16. internal ProgressBar ActivityProgressBar { get; private set; }
  17. internal ProgressBar PulseProgressBar { get; private set; }
  18. internal Action StartBtnClick;
  19. internal Action StopBtnClick;
  20. internal Action PulseBtnClick;
  21. internal ProgressDemo (ustring title) : base (title)
  22. {
  23. ColorScheme = Colors.Dialog;
  24. var leftFrame = new FrameView ("Settings") {
  25. X = 0,
  26. Y = 0,
  27. Height = Dim.Percent (100),
  28. Width = Dim.Percent (30)
  29. };
  30. Add (leftFrame);
  31. var startButton = new Button ("Start Timer") {
  32. X = Pos.Right (leftFrame) + 1,
  33. Y = 0,
  34. Clicked = () => StartBtnClick?.Invoke ()
  35. };
  36. var pulseButton = new Button ("Pulse") {
  37. X = Pos.Right (startButton) + 2,
  38. Y = Pos.Y (startButton),
  39. Clicked = () => PulseBtnClick.Invoke ()
  40. };
  41. var stopbutton = new Button ("Stop Timer") {
  42. X = Pos.Right (pulseButton) + 2,
  43. Y = Pos.Top (pulseButton),
  44. Clicked = () => StopBtnClick.Invoke ()
  45. };
  46. Add (startButton);
  47. Add (pulseButton);
  48. Add (stopbutton);
  49. ActivityProgressBar = new ProgressBar () {
  50. X = Pos.Right (leftFrame) + 1,
  51. Y = Pos.Bottom (startButton) + 1,
  52. Width = Dim.Fill (),
  53. Height = 1,
  54. Fraction = 0.25F,
  55. ColorScheme = Colors.Error
  56. };
  57. Add (ActivityProgressBar);
  58. PulseProgressBar = new ProgressBar () {
  59. X = Pos.Right (leftFrame) + 1,
  60. Y = Pos.Bottom (ActivityProgressBar) + 1,
  61. Width = Dim.Fill (),
  62. Height = 1,
  63. ColorScheme = Colors.Error
  64. };
  65. Add (PulseProgressBar);
  66. // Set height to height of controls + spacing + frame
  67. Height = 2 + _verticalSpace + Dim.Height (startButton) + _verticalSpace + Dim.Height (ActivityProgressBar) + _verticalSpace + Dim.Height (PulseProgressBar) + _verticalSpace;
  68. }
  69. }
  70. private Timer _systemTimer = null;
  71. private int _systemTimerTick = 100; // ms
  72. private object _mainLoopTimeout = null;
  73. private int _mainLooopTimeoutTick = 1000; // ms
  74. public override void Setup ()
  75. {
  76. // Demo #1 - Use System.Timer (and threading)
  77. var systemTimerDemo = new ProgressDemo ("System.Timer (threads)") {
  78. X = 0,
  79. Y = 0,
  80. Width = Dim.Percent (100),
  81. };
  82. systemTimerDemo.StartBtnClick = () => {
  83. _systemTimer?.Dispose ();
  84. _systemTimer = null;
  85. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  86. systemTimerDemo.PulseProgressBar.Fraction = 0F;
  87. _systemTimer = new Timer ((o) => {
  88. // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
  89. // This code must be defensive for that.
  90. Application.MainLoop?.Invoke (() => systemTimerDemo.PulseBtnClick ());
  91. }, null, 0, _systemTimerTick);
  92. };
  93. systemTimerDemo.PulseBtnClick = () => {
  94. if (systemTimerDemo.ActivityProgressBar.Fraction + 0.01F >= 1) {
  95. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  96. } else {
  97. systemTimerDemo.ActivityProgressBar.Fraction += 0.01F;
  98. }
  99. systemTimerDemo.PulseProgressBar.Pulse ();
  100. };
  101. systemTimerDemo.StopBtnClick = () => {
  102. _systemTimer?.Dispose ();
  103. _systemTimer = null;
  104. systemTimerDemo.ActivityProgressBar.Fraction = 1F;
  105. systemTimerDemo.PulseProgressBar.Fraction = 1F;
  106. };
  107. Win.Add (systemTimerDemo);
  108. // Demo #2 - Use Application.MainLoop.AddTimeout (no threads)
  109. var mainLoopTimeoutDemo = new ProgressDemo ("Application.AddTimer (no threads)") {
  110. X = 0,
  111. Y = Pos.Bottom (systemTimerDemo),
  112. Width = Dim.Percent (100),
  113. };
  114. mainLoopTimeoutDemo.StartBtnClick = () => {
  115. mainLoopTimeoutDemo.StopBtnClick ();
  116. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  117. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
  118. _mainLoopTimeout = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (_mainLooopTimeoutTick), (loop) => {
  119. mainLoopTimeoutDemo?.PulseBtnClick ();
  120. return true;
  121. });
  122. };
  123. mainLoopTimeoutDemo.PulseBtnClick = () => {
  124. if (mainLoopTimeoutDemo.ActivityProgressBar.Fraction + 0.01F >= 1) {
  125. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  126. } else {
  127. mainLoopTimeoutDemo.ActivityProgressBar.Fraction += 0.01F;
  128. }
  129. mainLoopTimeoutDemo.PulseProgressBar.Pulse ();
  130. };
  131. mainLoopTimeoutDemo.StopBtnClick = () => {
  132. if (_mainLoopTimeout != null) {
  133. Application.MainLoop.RemoveTimeout (_mainLoopTimeout);
  134. _mainLoopTimeout = null;
  135. }
  136. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
  137. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
  138. };
  139. Win.Add (mainLoopTimeoutDemo);
  140. }
  141. protected override void Dispose (bool disposing)
  142. {
  143. Win.GetEnumerator ().Reset ();
  144. while (Win.GetEnumerator ().MoveNext ()) {
  145. var cur = (ProgressDemo)Win.GetEnumerator ().Current;
  146. cur?.StopBtnClick ();
  147. }
  148. base.Dispose (disposing);
  149. }
  150. }
  151. }