Progress.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using NStack;
  2. using System;
  3. using System.Threading;
  4. using Terminal.Gui;
  5. using System.Linq;
  6. namespace UICatalog.Scenarios {
  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"), ScenarioCategory ("ProgressBar")]
  14. public 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 = null;
  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),
  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.X (lbl),
  46. Y = Pos.Bottom (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. };
  55. startButton.Clicked += () => Start ();
  56. var pulseButton = new Button ("Pulse") {
  57. X = Pos.Right (startButton) + 2,
  58. Y = Pos.Y (startButton),
  59. };
  60. pulseButton.Clicked += () => Pulse ();
  61. var stopbutton = new Button ("Stop Timer") {
  62. X = Pos.Right (pulseButton) + 2,
  63. Y = Pos.Top (pulseButton),
  64. };
  65. stopbutton.Clicked += () => Stop ();
  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. LayoutSubviews ();
  92. // Set height to height of controls + spacing + frame
  93. Height = 2 + _verticalSpace + Dim.Height (startButton) + _verticalSpace + Dim.Height (ActivityProgressBar) + _verticalSpace + Dim.Height (PulseProgressBar) + _verticalSpace;
  94. }
  95. internal void Start ()
  96. {
  97. Started = true;
  98. StartBtnClick?.Invoke ();
  99. }
  100. internal void Stop ()
  101. {
  102. Started = false;
  103. StopBtnClick?.Invoke ();
  104. }
  105. internal void Pulse ()
  106. {
  107. if (PulseBtnClick != null) {
  108. PulseBtnClick?.Invoke ();
  109. } else {
  110. if (ActivityProgressBar.Fraction + 0.01F >= 1) {
  111. ActivityProgressBar.Fraction = 0F;
  112. } else {
  113. ActivityProgressBar.Fraction += 0.01F;
  114. }
  115. PulseProgressBar.Pulse ();
  116. }
  117. }
  118. }
  119. private Timer _systemTimer = null;
  120. private uint _systemTimerTick = 100; // ms
  121. private object _mainLoopTimeout = null;
  122. private uint _mainLooopTimeoutTick = 100; // ms
  123. public override void Setup ()
  124. {
  125. // Demo #1 - Use System.Timer (and threading)
  126. var systemTimerDemo = new ProgressDemo ("System.Timer (threads)") {
  127. X = 0,
  128. Y = 0,
  129. Width = Dim.Percent (100),
  130. };
  131. systemTimerDemo.StartBtnClick = () => {
  132. _systemTimer?.Dispose ();
  133. _systemTimer = null;
  134. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  135. systemTimerDemo.PulseProgressBar.Fraction = 0F;
  136. _systemTimer = new Timer ((o) => {
  137. // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
  138. // This code must be defensive for that.
  139. Application.MainLoop?.Invoke (() => systemTimerDemo.Pulse ());
  140. }, null, 0, _systemTimerTick);
  141. };
  142. systemTimerDemo.StopBtnClick = () => {
  143. _systemTimer?.Dispose ();
  144. _systemTimer = null;
  145. systemTimerDemo.ActivityProgressBar.Fraction = 1F;
  146. systemTimerDemo.PulseProgressBar.Fraction = 1F;
  147. };
  148. systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
  149. systemTimerDemo.Speed.TextChanged += (a) => {
  150. uint result;
  151. if (uint.TryParse (systemTimerDemo.Speed.Text.ToString(), out result)) {
  152. _systemTimerTick = result;
  153. System.Diagnostics.Debug.WriteLine ($"{_systemTimerTick}");
  154. if (systemTimerDemo.Started) {
  155. systemTimerDemo.Start ();
  156. }
  157. } else {
  158. System.Diagnostics.Debug.WriteLine ("bad entry");
  159. }
  160. };
  161. Win.Add (systemTimerDemo);
  162. // Demo #2 - Use Application.MainLoop.AddTimeout (no threads)
  163. var mainLoopTimeoutDemo = new ProgressDemo ("Application.AddTimer (no threads)") {
  164. X = 0,
  165. Y = Pos.Bottom (systemTimerDemo),
  166. Width = Dim.Percent (100),
  167. };
  168. mainLoopTimeoutDemo.StartBtnClick = () => {
  169. mainLoopTimeoutDemo.StopBtnClick ();
  170. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  171. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
  172. _mainLoopTimeout = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (_mainLooopTimeoutTick), (loop) => {
  173. mainLoopTimeoutDemo.Pulse ();
  174. return true;
  175. });
  176. };
  177. mainLoopTimeoutDemo.StopBtnClick = () => {
  178. if (_mainLoopTimeout != null) {
  179. Application.MainLoop.RemoveTimeout (_mainLoopTimeout);
  180. _mainLoopTimeout = null;
  181. }
  182. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
  183. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
  184. };
  185. mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
  186. mainLoopTimeoutDemo.Speed.TextChanged += (a) => {
  187. uint result;
  188. if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text.ToString (), out result)) {
  189. _mainLooopTimeoutTick = result;
  190. if (mainLoopTimeoutDemo.Started) {
  191. mainLoopTimeoutDemo.Start ();
  192. }
  193. }
  194. };
  195. Win.Add (mainLoopTimeoutDemo);
  196. var startBoth = new Button ("Start Both") {
  197. X = Pos.Center (),
  198. Y = Pos.Bottom(mainLoopTimeoutDemo) + 1,
  199. };
  200. startBoth.Clicked += () => {
  201. systemTimerDemo.Start ();
  202. mainLoopTimeoutDemo.Start ();
  203. };
  204. Win.Add (startBoth);
  205. }
  206. protected override void Dispose (bool disposing)
  207. {
  208. foreach (var v in Win.Subviews.OfType<ProgressDemo>()) {
  209. v?.StopBtnClick ();
  210. }
  211. base.Dispose (disposing);
  212. }
  213. }
  214. }