ProgressBarStyles.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Threading;
  7. using static UICatalog.Scenarios.Adornments;
  8. namespace UICatalog.Scenarios;
  9. [ScenarioMetadata ("ProgressBar Styles", "Shows the ProgressBar Styles.")]
  10. [ScenarioCategory ("Controls")]
  11. [ScenarioCategory ("Progress")]
  12. [ScenarioCategory ("Threading")]
  13. // TODO: Add enable/disable to show that that is working
  14. // TODO: Clean up how FramesEditor works
  15. // TODO: Better align rpPBFormat
  16. public class ProgressBarStyles : Scenario
  17. {
  18. private const uint _timerTick = 20;
  19. private Timer _fractionTimer;
  20. private Timer _pulseTimer;
  21. private ListView _pbList;
  22. public override void Main ()
  23. {
  24. Application.Init ();
  25. Window app = new ()
  26. {
  27. Title = GetQuitKeyAndName (), BorderStyle = LineStyle.Single,
  28. };
  29. var editor = new AdornmentsEditor ()
  30. {
  31. AutoSelectViewToEdit = false,
  32. ShowViewIdentifier = true
  33. };
  34. app.Add (editor);
  35. View container = new ()
  36. {
  37. X = Pos.Right (editor),
  38. Y = 0,
  39. Width = Dim.Fill (),
  40. Height = Dim.Fill (),
  41. };
  42. app.Add (container);
  43. const float fractionStep = 0.01F;
  44. _pbList = new ListView
  45. {
  46. Title = "Focused ProgressBar",
  47. Y = Pos.Align (Alignment.Start),
  48. X = Pos.Center (),
  49. Width = Dim.Auto (),
  50. Height = Dim.Auto (),
  51. BorderStyle = LineStyle.Single
  52. };
  53. container.Add (_pbList);
  54. #region ColorPicker
  55. var fgColorPickerBtn = new Button
  56. {
  57. Text = "Foreground HotNormal Color",
  58. X = Pos.Center (),
  59. Y = Pos.Align (Alignment.Start)
  60. };
  61. container.Add (fgColorPickerBtn);
  62. fgColorPickerBtn.Accepting += (s, e) =>
  63. {
  64. if (!LineDrawing.PromptForColor (
  65. fgColorPickerBtn.Text,
  66. editor.ViewToEdit!.GetAttributeForRole (VisualRole.Normal).Foreground,
  67. out var newColor
  68. ))
  69. {
  70. return;
  71. }
  72. var cs = new Scheme (editor.ViewToEdit.GetScheme ())
  73. {
  74. Active = new Attribute (
  75. newColor,
  76. editor.ViewToEdit.GetAttributeForRole (VisualRole.Active)
  77. .Background,
  78. editor.ViewToEdit.GetAttributeForRole (VisualRole.Active).Style
  79. )
  80. };
  81. editor.ViewToEdit.SetScheme (cs);
  82. };
  83. var bgColorPickerBtn = new Button
  84. {
  85. X = Pos.Center (),
  86. Y = Pos.Align (Alignment.Start),
  87. Text = "Background HotNormal Color"
  88. };
  89. container.Add (bgColorPickerBtn);
  90. bgColorPickerBtn.Accepting += (s, e) =>
  91. {
  92. if (!LineDrawing.PromptForColor (
  93. fgColorPickerBtn.Text,
  94. editor.ViewToEdit!.GetAttributeForRole (VisualRole.Active)
  95. .Background
  96. , out var newColor))
  97. {
  98. return;
  99. }
  100. var cs = new Scheme (editor.ViewToEdit.GetScheme ())
  101. {
  102. Active = new Attribute (
  103. editor.ViewToEdit!.GetAttributeForRole (VisualRole.Normal).Foreground,
  104. newColor,
  105. editor.ViewToEdit!.GetAttributeForRole (VisualRole.Normal).Style
  106. )
  107. };
  108. editor.ViewToEdit.SetScheme (cs);
  109. };
  110. #endregion
  111. List<ProgressBarFormat> pbFormatEnum =
  112. Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
  113. var rbPBFormat = new RadioGroup
  114. {
  115. BorderStyle = LineStyle.Single,
  116. Title = "ProgressBarFormat",
  117. X = Pos.Center (),
  118. Y = Pos.Align (Alignment.Start),
  119. RadioLabels = pbFormatEnum.Select (e => e.ToString ()).ToArray ()
  120. };
  121. container.Add (rbPBFormat);
  122. var button = new Button
  123. {
  124. X = Pos.Center (),
  125. Y = Pos.Align (Alignment.Start),
  126. Text = "Start timer"
  127. };
  128. container.Add (button);
  129. var blocksPB = new ProgressBar
  130. {
  131. Title = "Blocks",
  132. X = Pos.Center (),
  133. Y = Pos.Align (Alignment.Start),
  134. Width = Dim.Percent (50),
  135. BorderStyle = LineStyle.Single,
  136. CanFocus = true
  137. };
  138. container.Add (blocksPB);
  139. rbPBFormat.SelectedItem = (int)blocksPB.ProgressBarFormat;
  140. var continuousPB = new ProgressBar
  141. {
  142. Title = "Continuous",
  143. X = Pos.Center (),
  144. Y = Pos.Align (Alignment.Start),
  145. Width = Dim.Percent (50),
  146. ProgressBarStyle = ProgressBarStyle.Continuous,
  147. BorderStyle = LineStyle.Single,
  148. CanFocus = true
  149. };
  150. container.Add (continuousPB);
  151. button.Accepting += (s, e) =>
  152. {
  153. if (_fractionTimer == null)
  154. {
  155. //blocksPB.Enabled = false;
  156. blocksPB.Fraction = 0;
  157. continuousPB.Fraction = 0;
  158. float fractionSum = 0;
  159. _fractionTimer = new Timer (
  160. _ =>
  161. {
  162. fractionSum += fractionStep;
  163. blocksPB.Fraction = fractionSum;
  164. continuousPB.Fraction = fractionSum;
  165. if (fractionSum > 1)
  166. {
  167. _fractionTimer.Dispose ();
  168. _fractionTimer = null;
  169. button.Enabled = true;
  170. }
  171. Application.Wakeup ();
  172. },
  173. null,
  174. 0,
  175. _timerTick
  176. );
  177. }
  178. };
  179. var ckbBidirectional = new CheckBox
  180. {
  181. X = Pos.Center (),
  182. Y = Pos.Bottom (continuousPB),
  183. Text = "BidirectionalMarquee",
  184. CheckedState = CheckState.Checked
  185. };
  186. container.Add (ckbBidirectional);
  187. var marqueesBlocksPB = new ProgressBar
  188. {
  189. Title = "Marquee Blocks",
  190. X = Pos.Center (),
  191. Y = Pos.Align (Alignment.Start),
  192. Width = Dim.Percent (50),
  193. ProgressBarStyle = ProgressBarStyle.MarqueeBlocks,
  194. BorderStyle = LineStyle.Single,
  195. CanFocus = true
  196. };
  197. container.Add (marqueesBlocksPB);
  198. var marqueesContinuousPB = new ProgressBar
  199. {
  200. Title = "Marquee Continuous",
  201. X = Pos.Center (),
  202. Y = Pos.Align (Alignment.Start),
  203. Width = Dim.Percent (50),
  204. ProgressBarStyle = ProgressBarStyle.MarqueeContinuous,
  205. BorderStyle = LineStyle.Single,
  206. CanFocus = true
  207. };
  208. container.Add (marqueesContinuousPB);
  209. _pbList.SetSource (
  210. new ObservableCollection<string> (
  211. container.SubViews.Where (v => v.GetType () == typeof (ProgressBar))
  212. .Select (v => v.Title)
  213. .ToList ())
  214. );
  215. _pbList.SelectedItemChanged += (sender, e) =>
  216. {
  217. editor.ViewToEdit = container.SubViews.First (
  218. v =>
  219. v.GetType () == typeof (ProgressBar)
  220. && v.Title == (string)e.Value
  221. );
  222. };
  223. rbPBFormat.SelectedItemChanged += (s, e) =>
  224. {
  225. blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  226. continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  227. marqueesBlocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  228. marqueesContinuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  229. };
  230. ckbBidirectional.CheckedStateChanging += (s, e) =>
  231. {
  232. marqueesBlocksPB.BidirectionalMarquee =
  233. marqueesContinuousPB.BidirectionalMarquee = e.NewValue == CheckState.Checked;
  234. };
  235. app.Initialized += App_Initialized;
  236. app.Unloaded += App_Unloaded;
  237. _pulseTimer = new Timer (
  238. _ =>
  239. {
  240. marqueesBlocksPB.Text = marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
  241. marqueesBlocksPB.Pulse ();
  242. marqueesContinuousPB.Pulse ();
  243. Application.Wakeup ();
  244. },
  245. null,
  246. 0,
  247. 300
  248. );
  249. Application.Run (app);
  250. app.Dispose ();
  251. Application.Shutdown ();
  252. return;
  253. void App_Unloaded (object sender, EventArgs args)
  254. {
  255. if (_fractionTimer != null)
  256. {
  257. _fractionTimer.Dispose ();
  258. _fractionTimer = null;
  259. }
  260. if (_pulseTimer != null)
  261. {
  262. _pulseTimer.Dispose ();
  263. _pulseTimer = null;
  264. }
  265. app.Unloaded -= App_Unloaded;
  266. }
  267. }
  268. private void App_Initialized (object sender, EventArgs e)
  269. {
  270. _pbList.SelectedItem = 0;
  271. }
  272. }