ProgressBarStyles.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. [Scenario.ScenarioMetadata ("ProgressBar Styles", "Shows the ProgressBar Styles.")]
  11. [Scenario.ScenarioCategory ("Controls")]
  12. [Scenario.ScenarioCategory ("Progress")]
  13. [Scenario.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.ColorScheme.HotNormal.Foreground,
  68. out var newColor
  69. ))
  70. {
  71. return;
  72. }
  73. var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
  74. {
  75. HotNormal = new Attribute (
  76. newColor,
  77. editor.ViewToEdit.ColorScheme.HotNormal
  78. .Background
  79. )
  80. };
  81. editor.ViewToEdit.ColorScheme = 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.ColorScheme.HotNormal.Background
  95. , out var newColor))
  96. {
  97. return;
  98. }
  99. var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
  100. {
  101. HotNormal = new Attribute (
  102. editor.ViewToEdit.ColorScheme.HotNormal
  103. .Foreground,
  104. newColor
  105. )
  106. };
  107. editor.ViewToEdit.ColorScheme = cs;
  108. };
  109. #endregion
  110. List<ProgressBarFormat> pbFormatEnum =
  111. Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
  112. var rbPBFormat = new RadioGroup
  113. {
  114. BorderStyle = LineStyle.Single,
  115. Title = "ProgressBarFormat",
  116. X = Pos.Center (),
  117. Y = Pos.Align (Alignment.Start),
  118. RadioLabels = pbFormatEnum.Select (e => e.ToString ()).ToArray ()
  119. };
  120. container.Add (rbPBFormat);
  121. var button = new Button
  122. {
  123. X = Pos.Center (),
  124. Y = Pos.Align (Alignment.Start),
  125. Text = "Start timer"
  126. };
  127. container.Add (button);
  128. var blocksPB = new ProgressBar
  129. {
  130. Title = "Blocks",
  131. X = Pos.Center (),
  132. Y = Pos.Align (Alignment.Start),
  133. Width = Dim.Percent (50),
  134. BorderStyle = LineStyle.Single,
  135. CanFocus = true
  136. };
  137. container.Add (blocksPB);
  138. rbPBFormat.SelectedItem = (int)blocksPB.ProgressBarFormat;
  139. var continuousPB = new ProgressBar
  140. {
  141. Title = "Continuous",
  142. X = Pos.Center (),
  143. Y = Pos.Align (Alignment.Start),
  144. Width = Dim.Percent (50),
  145. ProgressBarStyle = ProgressBarStyle.Continuous,
  146. BorderStyle = LineStyle.Single,
  147. CanFocus = true
  148. };
  149. container.Add (continuousPB);
  150. button.Accepting += (s, e) =>
  151. {
  152. if (_fractionTimer == null)
  153. {
  154. //blocksPB.Enabled = false;
  155. blocksPB.Fraction = 0;
  156. continuousPB.Fraction = 0;
  157. float fractionSum = 0;
  158. _fractionTimer = new Timer (
  159. _ =>
  160. {
  161. fractionSum += fractionStep;
  162. blocksPB.Fraction = fractionSum;
  163. continuousPB.Fraction = fractionSum;
  164. if (fractionSum > 1)
  165. {
  166. _fractionTimer.Dispose ();
  167. _fractionTimer = null;
  168. button.Enabled = true;
  169. }
  170. Application.Wakeup ();
  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. rbPBFormat.SelectedItemChanged += (s, e) =>
  223. {
  224. blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  225. continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  226. marqueesBlocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  227. marqueesContinuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  228. };
  229. ckbBidirectional.CheckedStateChanging += (s, e) =>
  230. {
  231. marqueesBlocksPB.BidirectionalMarquee =
  232. marqueesContinuousPB.BidirectionalMarquee = e.NewValue == CheckState.Checked;
  233. };
  234. app.Initialized += App_Initialized;
  235. app.Unloaded += App_Unloaded;
  236. _pulseTimer = new Timer (
  237. _ =>
  238. {
  239. marqueesBlocksPB.Text = marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
  240. marqueesBlocksPB.Pulse ();
  241. marqueesContinuousPB.Pulse ();
  242. Application.Wakeup ();
  243. },
  244. null,
  245. 0,
  246. 300
  247. );
  248. Application.Run (app);
  249. app.Dispose ();
  250. Application.Shutdown ();
  251. return;
  252. void App_Unloaded (object sender, EventArgs args)
  253. {
  254. if (_fractionTimer != null)
  255. {
  256. _fractionTimer.Dispose ();
  257. _fractionTimer = null;
  258. }
  259. if (_pulseTimer != null)
  260. {
  261. _pulseTimer.Dispose ();
  262. _pulseTimer = null;
  263. }
  264. app.Unloaded -= App_Unloaded;
  265. }
  266. }
  267. private void App_Initialized (object sender, EventArgs e)
  268. {
  269. _pbList.SelectedItem = 0;
  270. }
  271. }