ProgressBarStyles.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. };
  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.ColorScheme.HotNormal.Foreground,
  67. out var newColor
  68. ))
  69. {
  70. return;
  71. }
  72. var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
  73. {
  74. HotNormal = new Attribute (
  75. newColor,
  76. editor.ViewToEdit.ColorScheme.HotNormal
  77. .Background
  78. )
  79. };
  80. editor.ViewToEdit.ColorScheme = cs;
  81. };
  82. var bgColorPickerBtn = new Button
  83. {
  84. X = Pos.Center (),
  85. Y = Pos.Align (Alignment.Start),
  86. Text = "Background HotNormal Color"
  87. };
  88. container.Add (bgColorPickerBtn);
  89. bgColorPickerBtn.Accepting += (s, e) =>
  90. {
  91. if (!LineDrawing.PromptForColor (
  92. fgColorPickerBtn.Text,
  93. editor.ViewToEdit.ColorScheme.HotNormal.Background
  94. , out var newColor))
  95. {
  96. return;
  97. }
  98. var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
  99. {
  100. HotNormal = new Attribute (
  101. editor.ViewToEdit.ColorScheme.HotNormal
  102. .Foreground,
  103. newColor
  104. )
  105. };
  106. editor.ViewToEdit.ColorScheme = cs;
  107. };
  108. #endregion
  109. List<ProgressBarFormat> pbFormatEnum =
  110. Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
  111. var rbPBFormat = new RadioGroup
  112. {
  113. BorderStyle = LineStyle.Single,
  114. Title = "ProgressBarFormat",
  115. X = Pos.Center (),
  116. Y = Pos.Align (Alignment.Start),
  117. RadioLabels = pbFormatEnum.Select (e => e.ToString ()).ToArray ()
  118. };
  119. container.Add (rbPBFormat);
  120. var button = new Button
  121. {
  122. X = Pos.Center (),
  123. Y = Pos.Align (Alignment.Start),
  124. Text = "Start timer"
  125. };
  126. container.Add (button);
  127. var blocksPB = new ProgressBar
  128. {
  129. Title = "Blocks",
  130. X = Pos.Center (),
  131. Y = Pos.Align (Alignment.Start),
  132. Width = Dim.Percent (50),
  133. BorderStyle = LineStyle.Single,
  134. CanFocus = true
  135. };
  136. container.Add (blocksPB);
  137. rbPBFormat.SelectedItem = (int)blocksPB.ProgressBarFormat;
  138. var continuousPB = new ProgressBar
  139. {
  140. Title = "Continuous",
  141. X = Pos.Center (),
  142. Y = Pos.Align (Alignment.Start),
  143. Width = Dim.Percent (50),
  144. ProgressBarStyle = ProgressBarStyle.Continuous,
  145. BorderStyle = LineStyle.Single,
  146. CanFocus = true
  147. };
  148. container.Add (continuousPB);
  149. button.Accepting += (s, e) =>
  150. {
  151. if (_fractionTimer == null)
  152. {
  153. //blocksPB.Enabled = false;
  154. blocksPB.Fraction = 0;
  155. continuousPB.Fraction = 0;
  156. float fractionSum = 0;
  157. _fractionTimer = new Timer (
  158. _ =>
  159. {
  160. fractionSum += fractionStep;
  161. blocksPB.Fraction = fractionSum;
  162. continuousPB.Fraction = fractionSum;
  163. if (fractionSum > 1)
  164. {
  165. _fractionTimer.Dispose ();
  166. _fractionTimer = null;
  167. button.Enabled = true;
  168. }
  169. Application.Wakeup ();
  170. },
  171. null,
  172. 0,
  173. _timerTick
  174. );
  175. }
  176. };
  177. var ckbBidirectional = new CheckBox
  178. {
  179. X = Pos.Center (),
  180. Y = Pos.Bottom (continuousPB),
  181. Text = "BidirectionalMarquee",
  182. CheckedState = CheckState.Checked
  183. };
  184. container.Add (ckbBidirectional);
  185. var marqueesBlocksPB = new ProgressBar
  186. {
  187. Title = "Marquee Blocks",
  188. X = Pos.Center (),
  189. Y = Pos.Align (Alignment.Start),
  190. Width = Dim.Percent (50),
  191. ProgressBarStyle = ProgressBarStyle.MarqueeBlocks,
  192. BorderStyle = LineStyle.Single,
  193. CanFocus = true
  194. };
  195. container.Add (marqueesBlocksPB);
  196. var marqueesContinuousPB = new ProgressBar
  197. {
  198. Title = "Marquee Continuous",
  199. X = Pos.Center (),
  200. Y = Pos.Align (Alignment.Start),
  201. Width = Dim.Percent (50),
  202. ProgressBarStyle = ProgressBarStyle.MarqueeContinuous,
  203. BorderStyle = LineStyle.Single,
  204. CanFocus = true
  205. };
  206. container.Add (marqueesContinuousPB);
  207. _pbList.SetSource (
  208. new ObservableCollection<string> (
  209. container.Subviews.Where (v => v.GetType () == typeof (ProgressBar))
  210. .Select (v => v.Title)
  211. .ToList ())
  212. );
  213. _pbList.SelectedItemChanged += (sender, e) =>
  214. {
  215. editor.ViewToEdit = container.Subviews.First (
  216. v =>
  217. v.GetType () == typeof (ProgressBar)
  218. && v.Title == (string)e.Value
  219. );
  220. };
  221. rbPBFormat.SelectedItemChanged += (s, e) =>
  222. {
  223. blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  224. continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  225. marqueesBlocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  226. marqueesContinuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  227. };
  228. ckbBidirectional.CheckedStateChanging += (s, e) =>
  229. {
  230. marqueesBlocksPB.BidirectionalMarquee =
  231. marqueesContinuousPB.BidirectionalMarquee = e.NewValue == CheckState.Checked;
  232. };
  233. app.Initialized += App_Initialized;
  234. app.Unloaded += App_Unloaded;
  235. _pulseTimer = new Timer (
  236. _ =>
  237. {
  238. marqueesBlocksPB.Text = marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
  239. marqueesBlocksPB.Pulse ();
  240. marqueesContinuousPB.Pulse ();
  241. Application.Wakeup ();
  242. },
  243. null,
  244. 0,
  245. 300
  246. );
  247. Application.Run (app);
  248. app.Dispose ();
  249. Application.Shutdown ();
  250. return;
  251. void App_Unloaded (object sender, EventArgs args)
  252. {
  253. if (_fractionTimer != null)
  254. {
  255. _fractionTimer.Dispose ();
  256. _fractionTimer = null;
  257. }
  258. if (_pulseTimer != null)
  259. {
  260. _pulseTimer.Dispose ();
  261. _pulseTimer = null;
  262. }
  263. app.Unloaded -= App_Unloaded;
  264. }
  265. }
  266. private void App_Initialized (object sender, EventArgs e)
  267. {
  268. _pbList.SelectedItem = 0;
  269. }
  270. }