ProgressBarStyles.cs 13 KB

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