Progress.cs 7.4 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.ColorSchemes ["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.ColorSchemes ["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.ColorSchemes ["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. // TODO: Great use of Dim.Auto
  101. Initialized += (s, e) => {
  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. }
  106. internal void Start ()
  107. {
  108. Started = true;
  109. StartBtnClick?.Invoke ();
  110. Application.Invoke(()=>{
  111. Spinner.Visible = true;
  112. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  113. this.LayoutSubviews();
  114. });
  115. }
  116. internal void Stop ()
  117. {
  118. Started = false;
  119. StopBtnClick?.Invoke ();
  120. Application.Invoke(()=>{
  121. Spinner.Visible = false;
  122. ActivityProgressBar.Width = Dim.Fill () - Spinner.Width;
  123. this.LayoutSubviews();
  124. });
  125. }
  126. internal void Pulse ()
  127. {
  128. Spinner.Visible = true;
  129. if (PulseBtnClick != null) {
  130. PulseBtnClick?.Invoke ();
  131. } else {
  132. if (ActivityProgressBar.Fraction + 0.01F >= 1) {
  133. ActivityProgressBar.Fraction = 0F;
  134. } else {
  135. ActivityProgressBar.Fraction += 0.01F;
  136. }
  137. PulseProgressBar.Pulse ();
  138. Spinner.AdvanceAnimation ();
  139. }
  140. }
  141. }
  142. private Timer _systemTimer = null;
  143. private uint _systemTimerTick = 100; // ms
  144. private object _mainLoopTimeout = null;
  145. private uint _mainLooopTimeoutTick = 100; // ms
  146. public override void Setup ()
  147. {
  148. // Demo #1 - Use System.Timer (and threading)
  149. var systemTimerDemo = new ProgressDemo ("System.Timer (threads)") {
  150. X = 0,
  151. Y = 0,
  152. Width = Dim.Percent (100),
  153. };
  154. systemTimerDemo.StartBtnClick = () => {
  155. _systemTimer?.Dispose ();
  156. _systemTimer = null;
  157. systemTimerDemo.ActivityProgressBar.Fraction = 0F;
  158. systemTimerDemo.PulseProgressBar.Fraction = 0F;
  159. _systemTimer = new Timer ((o) => {
  160. // Note the check for Mainloop being valid. System.Timers can run after they are Disposed.
  161. // This code must be defensive for that.
  162. Application.Invoke (() => systemTimerDemo.Pulse ());
  163. }, null, 0, _systemTimerTick);
  164. };
  165. systemTimerDemo.StopBtnClick = () => {
  166. _systemTimer?.Dispose ();
  167. _systemTimer = null;
  168. systemTimerDemo.ActivityProgressBar.Fraction = 1F;
  169. systemTimerDemo.PulseProgressBar.Fraction = 1F;
  170. };
  171. systemTimerDemo.Speed.Text = $"{_systemTimerTick}";
  172. systemTimerDemo.Speed.TextChanged += (s, a) => {
  173. uint result;
  174. if (uint.TryParse (systemTimerDemo.Speed.Text.ToString(), out result)) {
  175. _systemTimerTick = result;
  176. System.Diagnostics.Debug.WriteLine ($"{_systemTimerTick}");
  177. if (systemTimerDemo.Started) {
  178. systemTimerDemo.Start ();
  179. }
  180. } else {
  181. System.Diagnostics.Debug.WriteLine ("bad entry");
  182. }
  183. };
  184. Win.Add (systemTimerDemo);
  185. // Demo #2 - Use Application.AddTimeout (no threads)
  186. var mainLoopTimeoutDemo = new ProgressDemo ("Application.AddTimer (no threads)") {
  187. X = 0,
  188. Y = Pos.Bottom (systemTimerDemo),
  189. Width = Dim.Percent (100),
  190. };
  191. mainLoopTimeoutDemo.StartBtnClick = () => {
  192. mainLoopTimeoutDemo.StopBtnClick ();
  193. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 0F;
  194. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 0F;
  195. _mainLoopTimeout = Application.AddTimeout (TimeSpan.FromMilliseconds (_mainLooopTimeoutTick), () => {
  196. mainLoopTimeoutDemo.Pulse ();
  197. return true;
  198. });
  199. };
  200. mainLoopTimeoutDemo.StopBtnClick = () => {
  201. if (_mainLoopTimeout != null) {
  202. Application.RemoveTimeout (_mainLoopTimeout);
  203. _mainLoopTimeout = null;
  204. }
  205. mainLoopTimeoutDemo.ActivityProgressBar.Fraction = 1F;
  206. mainLoopTimeoutDemo.PulseProgressBar.Fraction = 1F;
  207. };
  208. mainLoopTimeoutDemo.Speed.Text = $"{_mainLooopTimeoutTick}";
  209. mainLoopTimeoutDemo.Speed.TextChanged += (s, a) => {
  210. uint result;
  211. if (uint.TryParse (mainLoopTimeoutDemo.Speed.Text, out result)) {
  212. _mainLooopTimeoutTick = result;
  213. if (mainLoopTimeoutDemo.Started) {
  214. mainLoopTimeoutDemo.Start ();
  215. }
  216. }
  217. };
  218. Win.Add (mainLoopTimeoutDemo);
  219. var startBoth = new Button ("Start Both") {
  220. X = Pos.Center (),
  221. Y = Pos.Bottom(mainLoopTimeoutDemo) + 1,
  222. };
  223. startBoth.Clicked += (s,e) => {
  224. systemTimerDemo.Start ();
  225. mainLoopTimeoutDemo.Start ();
  226. };
  227. Win.Add (startBoth);
  228. }
  229. protected override void Dispose (bool disposing)
  230. {
  231. foreach (var v in Win.Subviews.OfType<ProgressDemo>()) {
  232. v?.StopBtnClick ();
  233. }
  234. base.Dispose (disposing);
  235. }
  236. }
  237. }