Progress.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System.Text;
  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 ("Threading"), ScenarioCategory ("Progress")]
  13. public class Progress : Scenario {
  14. class ProgressDemo : FrameView {
  15. const int _verticalSpace = 1;
  16. internal FrameView LeftFrame { get; private set; }
  17. internal TextField Speed { get; private set; }
  18. internal ProgressBar ActivityProgressBar { get; private set; }
  19. internal ProgressBar PulseProgressBar { get; private set; }
  20. internal SpinnerView Spinner { 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 (string 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 += (s,e) => Start ();
  56. var pulseButton = new Button ("Pulse") {
  57. X = Pos.Right (startButton) + 2,
  58. Y = Pos.Y (startButton),
  59. };
  60. pulseButton.Clicked += (s,e) => Pulse ();
  61. var stopbutton = new Button ("Stop Timer") {
  62. X = Pos.Right (pulseButton) + 2,
  63. Y = Pos.Top (pulseButton),
  64. };
  65. stopbutton.Clicked += (s,e) => 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 () - 1,
  73. Height = 1,
  74. Fraction = 0.25F,
  75. ColorScheme = Colors.Error
  76. };
  77. Add (ActivityProgressBar);
  78. Spinner = new SpinnerView () {
  79. Style = new SpinnerStyle.Dots2 (),
  80. SpinReverse = true,
  81. Y = ActivityProgressBar.Y,
  82. Visible = false
  83. };
  84. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  85. Spinner.X = Pos.Right (ActivityProgressBar);
  86. Add (Spinner);
  87. PulseProgressBar = new ProgressBar () {
  88. X = Pos.Right (LeftFrame) + 1,
  89. Y = Pos.Bottom (ActivityProgressBar) + 1,
  90. Width = Dim.Fill () - Spinner.Width,
  91. Height = 1,
  92. ColorScheme = Colors.Error
  93. };
  94. Add (PulseProgressBar);
  95. _startedLabel = new Label ("Stopped") {
  96. X = Pos.Right (LeftFrame) + 1,
  97. Y = Pos.Bottom (PulseProgressBar),
  98. };
  99. Add (_startedLabel);
  100. // Explictly cause layout so the setting of Height below works
  101. LayoutSubviews ();
  102. // Set height to height of controls + spacing + frame
  103. Height = 2 + _verticalSpace + startButton.Frame.Height + _verticalSpace + ActivityProgressBar.Frame.Height + _verticalSpace + PulseProgressBar.Frame.Height + _verticalSpace;
  104. }
  105. internal void Start ()
  106. {
  107. Started = true;
  108. StartBtnClick?.Invoke ();
  109. Application.MainLoop.Invoke(()=>{
  110. Spinner.Visible = true;
  111. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  112. this.LayoutSubviews();
  113. });
  114. }
  115. internal void Stop ()
  116. {
  117. Started = false;
  118. StopBtnClick?.Invoke ();
  119. Application.MainLoop.Invoke(()=>{
  120. Spinner.Visible = false;
  121. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  122. this.LayoutSubviews();
  123. });
  124. }
  125. internal void Pulse ()
  126. {
  127. Spinner.Visible = true;
  128. if (PulseBtnClick != null) {
  129. PulseBtnClick?.Invoke ();
  130. } else {
  131. if (ActivityProgressBar.Fraction + 0.01F >= 1) {
  132. ActivityProgressBar.Fraction = 0F;
  133. } else {
  134. ActivityProgressBar.Fraction += 0.01F;
  135. }
  136. PulseProgressBar.Pulse ();
  137. Spinner.AdvanceAnimation ();
  138. }
  139. }
  140. }
  141. private Timer _systemTimer = null;
  142. private uint _systemTimerTick = 100; // ms
  143. private object _mainLoopTimeout = null;
  144. private uint _mainLooopTimeoutTick = 100; // ms
  145. public override void Setup ()
  146. {
  147. // Demo #1 - Use System.Timer (and threading)
  148. var systemTimerDemo = new ProgressDemo ("System.Timer (threads)") {
  149. X = 0,
  150. Y = 0,
  151. Width = Dim.Percent (100),
  152. };
  153. systemTimerDemo.StartBtnClick = () => {
  154. _systemTimer?.Dispose ();
  155. _systemTimer = null;
  156. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  157. systemTimerDemo.PulseProgressBar.Fraction = 0F;
  158. _systemTimer = new Timer ((o) => {
  159. // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
  160. // This code must be defensive for that.
  161. Application.MainLoop?.Invoke (() => systemTimerDemo.Pulse ());
  162. }, null, 0, _systemTimerTick);
  163. };
  164. systemTimerDemo.StopBtnClick = () => {
  165. _systemTimer?.Dispose ();
  166. _systemTimer = null;
  167. systemTimerDemo.ActivityProgressBar.Fraction = 1F;
  168. systemTimerDemo.PulseProgressBar.Fraction = 1F;
  169. };
  170. systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
  171. systemTimerDemo.Speed.TextChanged += (s, a) => {
  172. uint result;
  173. if (uint.TryParse (systemTimerDemo.Speed.Text.ToString(), out result)) {
  174. _systemTimerTick = result;
  175. System.Diagnostics.Debug.WriteLine ($"{_systemTimerTick}");
  176. if (systemTimerDemo.Started) {
  177. systemTimerDemo.Start ();
  178. }
  179. } else {
  180. System.Diagnostics.Debug.WriteLine ("bad entry");
  181. }
  182. };
  183. Win.Add (systemTimerDemo);
  184. // Demo #2 - Use Application.MainLoop.AddTimeout (no threads)
  185. var mainLoopTimeoutDemo = new ProgressDemo ("Application.AddTimer (no threads)") {
  186. X = 0,
  187. Y = Pos.Bottom (systemTimerDemo),
  188. Width = Dim.Percent (100),
  189. };
  190. mainLoopTimeoutDemo.StartBtnClick = () => {
  191. mainLoopTimeoutDemo.StopBtnClick ();
  192. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  193. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
  194. _mainLoopTimeout = Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (_mainLooopTimeoutTick), (loop) => {
  195. mainLoopTimeoutDemo.Pulse ();
  196. return true;
  197. });
  198. };
  199. mainLoopTimeoutDemo.StopBtnClick = () => {
  200. if (_mainLoopTimeout != null) {
  201. Application.MainLoop.RemoveTimeout (_mainLoopTimeout);
  202. _mainLoopTimeout = null;
  203. }
  204. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
  205. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
  206. };
  207. mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
  208. mainLoopTimeoutDemo.Speed.TextChanged += (s, a) => {
  209. uint result;
  210. if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text, out result)) {
  211. _mainLooopTimeoutTick = result;
  212. if (mainLoopTimeoutDemo.Started) {
  213. mainLoopTimeoutDemo.Start ();
  214. }
  215. }
  216. };
  217. Win.Add (mainLoopTimeoutDemo);
  218. var startBoth = new Button ("Start Both") {
  219. X = Pos.Center (),
  220. Y = Pos.Bottom(mainLoopTimeoutDemo) + 1,
  221. };
  222. startBoth.Clicked += (s,e) => {
  223. systemTimerDemo.Start ();
  224. mainLoopTimeoutDemo.Start ();
  225. };
  226. Win.Add (startBoth);
  227. }
  228. protected override void Dispose (bool disposing)
  229. {
  230. foreach (var v in Win.Subviews.OfType<ProgressDemo>()) {
  231. v?.StopBtnClick ();
  232. }
  233. base.Dispose (disposing);
  234. }
  235. }
  236. }