Progress.cs 13 KB

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