ProgressBarStyles.cs 13 KB

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