ProgressBarStyles.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 win = new ()
  26. {
  27. Title = GetQuitKeyAndName (), BorderStyle = LineStyle.Single,
  28. };
  29. var editor = new AdornmentsEditor ()
  30. {
  31. AutoSelectViewToEdit = false,
  32. ShowViewIdentifier = true
  33. };
  34. win.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. win.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. OptionSelector<ProgressBarFormat> osPbFormat = new ()
  114. {
  115. BorderStyle = LineStyle.Single,
  116. Title = "ProgressBarFormat",
  117. X = Pos.Center (),
  118. Y = Pos.Align (Alignment.Start),
  119. AssignHotKeys = true
  120. };
  121. container.Add (osPbFormat);
  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. osPbFormat.Value = 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. },
  172. null,
  173. 0,
  174. _timerTick
  175. );
  176. }
  177. };
  178. var ckbBidirectional = new CheckBox
  179. {
  180. X = Pos.Center (),
  181. Y = Pos.Bottom (continuousPB),
  182. Text = "BidirectionalMarquee",
  183. CheckedState = CheckState.Checked
  184. };
  185. container.Add (ckbBidirectional);
  186. var marqueesBlocksPB = new ProgressBar
  187. {
  188. Title = "Marquee Blocks",
  189. X = Pos.Center (),
  190. Y = Pos.Align (Alignment.Start),
  191. Width = Dim.Percent (50),
  192. ProgressBarStyle = ProgressBarStyle.MarqueeBlocks,
  193. BorderStyle = LineStyle.Single,
  194. CanFocus = true
  195. };
  196. container.Add (marqueesBlocksPB);
  197. var marqueesContinuousPB = new ProgressBar
  198. {
  199. Title = "Marquee Continuous",
  200. X = Pos.Center (),
  201. Y = Pos.Align (Alignment.Start),
  202. Width = Dim.Percent (50),
  203. ProgressBarStyle = ProgressBarStyle.MarqueeContinuous,
  204. BorderStyle = LineStyle.Single,
  205. CanFocus = true
  206. };
  207. container.Add (marqueesContinuousPB);
  208. _pbList.SetSource (
  209. new ObservableCollection<string> (
  210. container.SubViews.Where (v => v.GetType () == typeof (ProgressBar))
  211. .Select (v => v.Title)
  212. .ToList ())
  213. );
  214. _pbList.SelectedItemChanged += (sender, e) =>
  215. {
  216. editor.ViewToEdit = container.SubViews.First (
  217. v =>
  218. v.GetType () == typeof (ProgressBar)
  219. && v.Title == (string)e.Value
  220. );
  221. };
  222. osPbFormat.ValueChanged += (s, e) =>
  223. {
  224. if (e.Value is null)
  225. {
  226. return;
  227. }
  228. blocksPB.ProgressBarFormat = e.Value.Value;
  229. continuousPB.ProgressBarFormat = e.Value.Value;
  230. marqueesBlocksPB.ProgressBarFormat = e.Value.Value;
  231. marqueesContinuousPB.ProgressBarFormat = e.Value.Value;
  232. };
  233. ckbBidirectional.CheckedStateChanging += (s, e) =>
  234. {
  235. marqueesBlocksPB.BidirectionalMarquee =
  236. marqueesContinuousPB.BidirectionalMarquee = e.Result == CheckState.Checked;
  237. };
  238. win.Initialized += Win_Initialized;
  239. win.IsRunningChanged += Win_IsRunningChanged;
  240. _pulseTimer = new Timer (
  241. _ =>
  242. {
  243. marqueesBlocksPB.Text = marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
  244. marqueesBlocksPB.Pulse ();
  245. marqueesContinuousPB.Pulse ();
  246. },
  247. null,
  248. 0,
  249. 300
  250. );
  251. Application.Run (win);
  252. win.Dispose ();
  253. Application.Shutdown ();
  254. return;
  255. void Win_IsRunningChanged (object sender, EventArgs<bool> args)
  256. {
  257. if (args.Value)
  258. {
  259. return;
  260. }
  261. if (_fractionTimer != null)
  262. {
  263. _fractionTimer.Dispose ();
  264. _fractionTimer = null;
  265. }
  266. if (_pulseTimer != null)
  267. {
  268. _pulseTimer.Dispose ();
  269. _pulseTimer = null;
  270. }
  271. win.IsRunningChanged -= Win_IsRunningChanged;
  272. }
  273. }
  274. private void Win_Initialized (object sender, EventArgs e)
  275. {
  276. _pbList.SelectedItem = 0;
  277. }
  278. }