Progress.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Threading;
  5. namespace UICatalog.Scenarios;
  6. //
  7. // This would be a great scenario to show of threading (Issue #471)
  8. //
  9. [ScenarioMetadata ("Progress", "Shows off ProgressBar and Threading.")]
  10. [ScenarioCategory ("Controls")]
  11. [ScenarioCategory ("Threading")]
  12. [ScenarioCategory ("Progress")]
  13. public class Progress : Scenario
  14. {
  15. private Window win;
  16. private uint _mainLooopTimeoutTick = 100; // ms
  17. private object _mainLoopTimeout;
  18. private Timer _systemTimer;
  19. private uint _systemTimerTick = 100; // ms
  20. public override void Main ()
  21. {
  22. Application.Init ();
  23. win = new Window { Title = GetQuitKeyAndName () };
  24. // Demo #1 - Use System.Timer (and threading)
  25. var systemTimerDemo = new ProgressDemo
  26. {
  27. X = 0, Y = 0, Width = Dim.Percent (100), Title = "System.Timer (threads)"
  28. };
  29. systemTimerDemo.StartBtnClick = () =>
  30. {
  31. _systemTimer?.Dispose ();
  32. _systemTimer = null;
  33. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  34. systemTimerDemo.PulseProgressBar.Fraction = 0F;
  35. _systemTimer = new Timer (
  36. o =>
  37. {
  38. // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
  39. // This code must be defensive for that.
  40. Application.Invoke ((_) => systemTimerDemo.Pulse ());
  41. },
  42. null,
  43. 0,
  44. _systemTimerTick
  45. );
  46. };
  47. systemTimerDemo.StopBtnClick = () =>
  48. {
  49. _systemTimer?.Dispose ();
  50. _systemTimer = null;
  51. systemTimerDemo.ActivityProgressBar.Fraction = 1F;
  52. systemTimerDemo.PulseProgressBar.Fraction = 1F;
  53. };
  54. systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
  55. systemTimerDemo.Speed.TextChanged += (s, a) =>
  56. {
  57. uint result;
  58. if (uint.TryParse (systemTimerDemo.Speed.Text, out result))
  59. {
  60. _systemTimerTick = result;
  61. Debug.WriteLine ($"{_systemTimerTick}");
  62. if (systemTimerDemo.Started)
  63. {
  64. systemTimerDemo.Start ();
  65. }
  66. }
  67. else
  68. {
  69. Debug.WriteLine ("bad entry");
  70. }
  71. };
  72. win.Add (systemTimerDemo);
  73. // Demo #2 - Use Application.AddTimeout (no threads)
  74. var mainLoopTimeoutDemo = new ProgressDemo
  75. {
  76. X = 0,
  77. Y = Pos.Bottom (systemTimerDemo),
  78. Width = Dim.Percent (100),
  79. Title = "Application.AddTimer (no threads)"
  80. };
  81. mainLoopTimeoutDemo.StartBtnClick = () =>
  82. {
  83. mainLoopTimeoutDemo.StopBtnClick ();
  84. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  85. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
  86. _mainLoopTimeout = Application.AddTimeout (
  87. TimeSpan.FromMilliseconds (_mainLooopTimeoutTick),
  88. () =>
  89. {
  90. mainLoopTimeoutDemo.Pulse ();
  91. return true;
  92. }
  93. );
  94. };
  95. mainLoopTimeoutDemo.StopBtnClick = () =>
  96. {
  97. if (_mainLoopTimeout != null)
  98. {
  99. Application.RemoveTimeout (_mainLoopTimeout);
  100. _mainLoopTimeout = null;
  101. }
  102. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
  103. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
  104. };
  105. mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
  106. mainLoopTimeoutDemo.Speed.TextChanged += (s, a) =>
  107. {
  108. uint result;
  109. if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text, out result))
  110. {
  111. _mainLooopTimeoutTick = result;
  112. if (mainLoopTimeoutDemo.Started)
  113. {
  114. mainLoopTimeoutDemo.Start ();
  115. }
  116. }
  117. };
  118. win.Add (mainLoopTimeoutDemo);
  119. var startBoth = new Button { X = Pos.Center (), Y = Pos.Bottom (mainLoopTimeoutDemo) + 1, Text = "Start Both" };
  120. startBoth.Accepting += (s, e) =>
  121. {
  122. systemTimerDemo.Start ();
  123. mainLoopTimeoutDemo.Start ();
  124. };
  125. win.Add (startBoth);
  126. Application.Run (win);
  127. win.Dispose ();
  128. Application.Shutdown ();
  129. }
  130. protected override void Dispose (bool disposing)
  131. {
  132. foreach (ProgressDemo v in win.SubViews.OfType<ProgressDemo> ())
  133. {
  134. v?.StopBtnClick ();
  135. }
  136. base.Dispose (disposing);
  137. }
  138. private class ProgressDemo : FrameView
  139. {
  140. private const int VerticalSpace = 1;
  141. internal readonly Action PulseBtnClick = null;
  142. internal Action StartBtnClick;
  143. internal Action StopBtnClick;
  144. private readonly Label _startedLabel;
  145. internal ProgressDemo ()
  146. {
  147. SchemeName = "Dialog";
  148. LeftFrame = new FrameView
  149. {
  150. X = 0,
  151. Y = 0,
  152. Height = Dim.Percent (100),
  153. Width = Dim.Percent (25),
  154. Title = "Settings"
  155. };
  156. var lbl = new Label { X = 1, Y = 1, Text = "Tick every (ms):" };
  157. LeftFrame.Add (lbl);
  158. Speed = new TextField { X = Pos.X (lbl), Y = Pos.Bottom (lbl), Width = 7 };
  159. LeftFrame.Add (Speed);
  160. Add (LeftFrame);
  161. var startButton = new Button { X = Pos.Right (LeftFrame) + 1, Y = 0, Text = "Start Timer" };
  162. startButton.Accepting += (s, e) => Start ();
  163. var pulseButton = new Button { X = Pos.Right (startButton) + 2, Y = Pos.Y (startButton), Text = "Pulse" };
  164. pulseButton.Accepting += (s, e) => Pulse ();
  165. var stopbutton = new Button
  166. {
  167. X = Pos.Right (pulseButton) + 2, Y = Pos.Top (pulseButton), Text = "Stop Timer"
  168. };
  169. stopbutton.Accepting += (s, e) => Stop ();
  170. Add (startButton);
  171. Add (pulseButton);
  172. Add (stopbutton);
  173. ActivityProgressBar = new ProgressBar
  174. {
  175. X = Pos.Right (LeftFrame) + 1,
  176. Y = Pos.Bottom (startButton) + 1,
  177. Width = Dim.Fill () - 1,
  178. Height = 1,
  179. Fraction = 0.25F,
  180. SchemeName = "Error"
  181. };
  182. Add (ActivityProgressBar);
  183. Spinner = new SpinnerView
  184. {
  185. Style = new SpinnerStyle.Dots2 (), SpinReverse = true, Y = ActivityProgressBar.Y, Visible = false
  186. };
  187. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  188. Spinner.X = Pos.Right (ActivityProgressBar);
  189. Add (Spinner);
  190. PulseProgressBar = new ProgressBar
  191. {
  192. X = Pos.Right (LeftFrame) + 1,
  193. Y = Pos.Bottom (ActivityProgressBar) + 1,
  194. Width = Dim.Fill () - Spinner.Width,
  195. Height = 1,
  196. SchemeName = "Error"
  197. };
  198. Add (PulseProgressBar);
  199. _startedLabel = new Label
  200. {
  201. X = Pos.Right (LeftFrame) + 1, Y = Pos.Bottom (PulseProgressBar), Text = "Stopped"
  202. };
  203. Add (_startedLabel);
  204. // TODO: Great use of Dim.Auto
  205. Initialized += (s, e) =>
  206. {
  207. // Set height to height of controls + spacing + frame
  208. Height = 2
  209. + VerticalSpace
  210. + startButton.Frame.Height
  211. + VerticalSpace
  212. + ActivityProgressBar.Frame.Height
  213. + VerticalSpace
  214. + PulseProgressBar.Frame.Height
  215. + VerticalSpace;
  216. };
  217. }
  218. internal ProgressBar ActivityProgressBar { get; }
  219. internal FrameView LeftFrame { get; }
  220. internal ProgressBar PulseProgressBar { get; }
  221. internal TextField Speed { get; }
  222. internal SpinnerView Spinner { get; }
  223. internal bool Started
  224. {
  225. get => _startedLabel.Text == "Started";
  226. private set => _startedLabel.Text = value ? "Started" : "Stopped";
  227. }
  228. internal void Pulse ()
  229. {
  230. Spinner.Visible = true;
  231. if (PulseBtnClick != null)
  232. {
  233. PulseBtnClick?.Invoke ();
  234. }
  235. else
  236. {
  237. if (ActivityProgressBar.Fraction + 0.01F >= 1)
  238. {
  239. ActivityProgressBar.Fraction = 0F;
  240. }
  241. else
  242. {
  243. ActivityProgressBar.Fraction += 0.01F;
  244. }
  245. PulseProgressBar.Pulse ();
  246. Spinner.AdvanceAnimation ();
  247. }
  248. }
  249. internal void Start ()
  250. {
  251. Started = true;
  252. StartBtnClick?.Invoke ();
  253. Application.Invoke (
  254. () =>
  255. {
  256. Spinner.Visible = true;
  257. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  258. }
  259. );
  260. }
  261. internal void Stop ()
  262. {
  263. Started = false;
  264. StopBtnClick?.Invoke ();
  265. Application.Invoke (
  266. () =>
  267. {
  268. Spinner.Visible = false;
  269. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  270. }
  271. );
  272. }
  273. }
  274. }