Progress.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 ("MainLoop")]
  13. [ScenarioCategory ("Threading")]
  14. class Progress : Scenario {
  15. class ProgressDemo : FrameView {
  16. const int _verticalSpace = 1;
  17. internal FrameView LeftFrame { get; private set; }
  18. internal TextField Speed { get; private set; }
  19. internal ProgressBar ActivityProgressBar { get; private set; }
  20. internal ProgressBar PulseProgressBar { get; private set; }
  21. internal Action StartBtnClick;
  22. internal Action StopBtnClick;
  23. internal Action PulseBtnClick;
  24. private Label _startedLabel;
  25. internal bool Started {
  26. get {
  27. return _startedLabel.Text == "Started";
  28. }
  29. private set {
  30. _startedLabel.Text = value ? "Started" : "Stopped";
  31. }
  32. }
  33. internal ProgressDemo (ustring title) : base (title)
  34. {
  35. ColorScheme = Colors.Dialog;
  36. LeftFrame = new FrameView ("Settings") {
  37. X = 0,
  38. Y = 0,
  39. Height = Dim.Percent (100) + 1, // BUGBUG: This +1 should not be needed
  40. Width = Dim.Percent (25)
  41. };
  42. var lbl = new Label (1, 1, "Tick every (ms):");
  43. LeftFrame.Add (lbl);
  44. Speed = new TextField ("") {
  45. X = Pos.Right (lbl) + 1,
  46. Y = Pos.Y (lbl),
  47. Width = 7,
  48. };
  49. LeftFrame.Add (Speed);
  50. Add (LeftFrame);
  51. var startButton = new Button ("Start Timer") {
  52. X = Pos.Right (LeftFrame) + 1,
  53. Y = 0,
  54. Clicked = () => Start()
  55. };
  56. var pulseButton = new Button ("Pulse") {
  57. X = Pos.Right (startButton) + 2,
  58. Y = Pos.Y (startButton),
  59. Clicked = () => PulseBtnClick.Invoke ()
  60. };
  61. var stopbutton = new Button ("Stop Timer") {
  62. X = Pos.Right (pulseButton) + 2,
  63. Y = Pos.Top (pulseButton),
  64. Clicked = () => Stop()
  65. };
  66. Add (startButton);
  67. Add (pulseButton);
  68. Add (stopbutton);
  69. ActivityProgressBar = new ProgressBar () {
  70. X = Pos.Right (LeftFrame) + 1,
  71. Y = Pos.Bottom (startButton) + 1,
  72. Width = Dim.Fill (),
  73. Height = 1,
  74. Fraction = 0.25F,
  75. ColorScheme = Colors.Error
  76. };
  77. Add (ActivityProgressBar);
  78. PulseProgressBar = new ProgressBar () {
  79. X = Pos.Right (LeftFrame) + 1,
  80. Y = Pos.Bottom (ActivityProgressBar) + 1,
  81. Width = Dim.Fill (),
  82. Height = 1,
  83. ColorScheme = Colors.Error
  84. };
  85. Add (PulseProgressBar);
  86. _startedLabel = new Label ("Stopped") {
  87. X = Pos.Right (LeftFrame) + 1,
  88. Y = Pos.Bottom (PulseProgressBar),
  89. };
  90. Add (_startedLabel);
  91. // Set height to height of controls + spacing + frame
  92. Height = 2 + _verticalSpace + Dim.Height (startButton) + _verticalSpace + Dim.Height (ActivityProgressBar) + _verticalSpace + Dim.Height (PulseProgressBar) + _verticalSpace;
  93. }
  94. internal void Start ()
  95. {
  96. Started = true;
  97. StartBtnClick?.Invoke ();
  98. }
  99. internal void Stop ()
  100. {
  101. Started = false;
  102. StopBtnClick?.Invoke ();
  103. }
  104. internal void Pulse ()
  105. {
  106. if (PulseBtnClick != null) {
  107. PulseBtnClick?.Invoke ();
  108. } else {
  109. if (ActivityProgressBar.Fraction + 0.01F >= 1) {
  110. ActivityProgressBar.Fraction = 0F;
  111. } else {
  112. ActivityProgressBar.Fraction += 0.01F;
  113. }
  114. PulseProgressBar.Pulse ();
  115. }
  116. }
  117. }
  118. private Timer _systemTimer = null;
  119. private uint _systemTimerTick = 1000; // ms
  120. private object _mainLoopTimeout = null;
  121. private uint _mainLooopTimeoutTick = 1000; // ms
  122. public override void Setup ()
  123. {
  124. // Demo #1 - Use System.Timer (and threading)
  125. var systemTimerDemo = new ProgressDemo ("System.Timer (threads)") {
  126. X = 0,
  127. Y = 0,
  128. Width = Dim.Percent (100),
  129. };
  130. systemTimerDemo.StartBtnClick = () => {
  131. _systemTimer?.Dispose ();
  132. _systemTimer = null;
  133. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  134. systemTimerDemo.PulseProgressBar.Fraction = 0F;
  135. _systemTimer = new Timer ((o) => {
  136. // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
  137. // This code must be defensive for that.
  138. Application.MainLoop?.Invoke (() => systemTimerDemo.Pulse ());
  139. }, null, 0, _systemTimerTick);
  140. };
  141. systemTimerDemo.StopBtnClick = () => {
  142. _systemTimer?.Dispose ();
  143. _systemTimer = null;
  144. systemTimerDemo.ActivityProgressBar.Fraction = 1F;
  145. systemTimerDemo.PulseProgressBar.Fraction = 1F;
  146. };
  147. systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
  148. systemTimerDemo.Speed.Changed += (sender, a) => {
  149. uint result;
  150. if (uint.TryParse (systemTimerDemo.Speed.Text.ToString(), out result)) {
  151. _systemTimerTick = result;
  152. System.Diagnostics.Debug.WriteLine ($"{_systemTimerTick}");
  153. if (systemTimerDemo.Started) {
  154. systemTimerDemo.Start ();
  155. }
  156. } else {
  157. System.Diagnostics.Debug.WriteLine ("bad entry");
  158. }
  159. };
  160. Win.Add (systemTimerDemo);
  161. // Demo #2 - Use Application.MainLoop.AddTimeout (no threads)
  162. var mainLoopTimeoutDemo = new ProgressDemo ("Application.AddTimer (no threads)") {
  163. X = 0,
  164. Y = Pos.Bottom (systemTimerDemo),
  165. Width = Dim.Percent (100),
  166. };
  167. mainLoopTimeoutDemo.StartBtnClick = () => {
  168. mainLoopTimeoutDemo.StopBtnClick ();
  169. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  170. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
  171. _mainLoopTimeout = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (_mainLooopTimeoutTick), (loop) => {
  172. mainLoopTimeoutDemo.Pulse ();
  173. return true;
  174. });
  175. };
  176. mainLoopTimeoutDemo.StopBtnClick = () => {
  177. if (_mainLoopTimeout != null) {
  178. Application.MainLoop.RemoveTimeout (_mainLoopTimeout);
  179. _mainLoopTimeout = null;
  180. }
  181. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
  182. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
  183. };
  184. mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
  185. mainLoopTimeoutDemo.Speed.Changed += (sender, a) => {
  186. uint result;
  187. if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text.ToString (), out result)) {
  188. _mainLooopTimeoutTick = result;
  189. if (mainLoopTimeoutDemo.Started) {
  190. mainLoopTimeoutDemo.Start ();
  191. }
  192. }
  193. };
  194. Win.Add (mainLoopTimeoutDemo);
  195. var startBoth = new Button ("Start Both") {
  196. X = Pos.Center (),
  197. Y = Pos.AnchorEnd () - 1,
  198. };
  199. startBoth.Clicked = () => {
  200. systemTimerDemo.Start ();
  201. mainLoopTimeoutDemo.Start ();
  202. };
  203. Win.Add (startBoth);
  204. }
  205. protected override void Dispose (bool disposing)
  206. {
  207. foreach (var v in Win.Subviews.OfType<ProgressDemo>()) {
  208. v?.StopBtnClick ();
  209. }
  210. base.Dispose (disposing);
  211. }
  212. }
  213. }