2
0

ProgressBarStyles.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. public override void Init ()
  21. {
  22. Application.Init ();
  23. ConfigurationManager.Themes.Theme = Theme;
  24. ConfigurationManager.Apply ();
  25. var editor = new AdornmentsEditor
  26. {
  27. Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}", BorderStyle = LineStyle.Single
  28. };
  29. editor.ColorScheme = Colors.ColorSchemes [TopLevelColorScheme];
  30. const float fractionStep = 0.01F;
  31. var pbList = new ListView
  32. {
  33. Title = "Focused ProgressBar",
  34. Y = 0,
  35. X = Pos.Center (),
  36. Width = 30,
  37. Height = 7,
  38. BorderStyle = LineStyle.Single
  39. };
  40. pbList.SelectedItemChanged += (sender, e) =>
  41. {
  42. editor.ViewToEdit = editor.Subviews.First (
  43. v =>
  44. v.GetType () == typeof (ProgressBar)
  45. && v.Title == (string)e.Value
  46. );
  47. };
  48. editor.Add (pbList);
  49. pbList.SelectedItem = 0;
  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.LayoutComplete += (sender, args) =>
  56. {
  57. // TODO: Replace with Dim.Auto
  58. dialog.X = pbList.Frame.X;
  59. dialog.Y = pbList.Frame.Height;
  60. dialog.Viewport = new Rectangle (0, 0, colorPicker.Frame.Width, colorPicker.Frame.Height);
  61. Application.Top.LayoutSubviews ();
  62. };
  63. dialog.Add (colorPicker);
  64. colorPicker.ColorChanged += (s, e) => { dialog.RequestStop (); };
  65. Application.Run (dialog);
  66. ColorName retColor = colorPicker.SelectedColor;
  67. colorPicker.Dispose ();
  68. return retColor;
  69. }
  70. var fgColorPickerBtn = new Button
  71. {
  72. Text = "Foreground HotNormal Color", X = Pos.Center (), Y = Pos.Bottom (pbList)
  73. };
  74. editor.Add (fgColorPickerBtn);
  75. fgColorPickerBtn.Accept += (s, e) =>
  76. {
  77. ColorName newColor = ChooseColor (
  78. fgColorPickerBtn.Text,
  79. editor.ViewToEdit.ColorScheme.HotNormal.Foreground
  80. .GetClosestNamedColor ()
  81. );
  82. var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
  83. {
  84. HotNormal = new Attribute (
  85. newColor,
  86. editor.ViewToEdit.ColorScheme.HotNormal
  87. .Background
  88. )
  89. };
  90. editor.ViewToEdit.ColorScheme = cs;
  91. };
  92. var bgColorPickerBtn = new Button
  93. {
  94. X = Pos.Center (), Y = Pos.Bottom (fgColorPickerBtn), Text = "Background HotNormal Color"
  95. };
  96. editor.Add (bgColorPickerBtn);
  97. bgColorPickerBtn.Accept += (s, e) =>
  98. {
  99. ColorName newColor = ChooseColor (
  100. fgColorPickerBtn.Text,
  101. editor.ViewToEdit.ColorScheme.HotNormal.Background
  102. .GetClosestNamedColor ()
  103. );
  104. var cs = new ColorScheme (editor.ViewToEdit.ColorScheme)
  105. {
  106. HotNormal = new Attribute (
  107. editor.ViewToEdit.ColorScheme.HotNormal
  108. .Foreground,
  109. newColor
  110. )
  111. };
  112. editor.ViewToEdit.ColorScheme = cs;
  113. };
  114. #endregion
  115. List<ProgressBarFormat> pbFormatEnum =
  116. Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
  117. var rbPBFormat = new RadioGroup
  118. {
  119. BorderStyle = LineStyle.Single,
  120. Title = "ProgressBarFormat",
  121. X = Pos.Left (pbList),
  122. Y = Pos.Bottom (bgColorPickerBtn) + 1,
  123. RadioLabels = pbFormatEnum.Select (e => e.ToString ()).ToArray ()
  124. };
  125. editor.Add (rbPBFormat);
  126. var button = new Button { X = Pos.Center (), Y = Pos.Bottom (rbPBFormat) + 1, Text = "Start timer" };
  127. editor.Add (button);
  128. var blocksPB = new ProgressBar
  129. {
  130. Title = "Blocks",
  131. X = Pos.Center (),
  132. Y = Pos.Bottom (button) + 1,
  133. Width = Dim.Width (pbList),
  134. BorderStyle = LineStyle.Single,
  135. CanFocus = true
  136. };
  137. editor.Add (blocksPB);
  138. var continuousPB = new ProgressBar
  139. {
  140. Title = "Continuous",
  141. X = Pos.Center (),
  142. Y = Pos.Bottom (blocksPB) + 1,
  143. Width = Dim.Width (pbList),
  144. ProgressBarStyle = ProgressBarStyle.Continuous,
  145. BorderStyle = LineStyle.Single,
  146. CanFocus = true
  147. };
  148. editor.Add (continuousPB);
  149. button.Accept += (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 (), Y = Pos.Bottom (continuousPB) + 1, Text = "BidirectionalMarquee", Checked = true
  180. };
  181. editor.Add (ckbBidirectional);
  182. var marqueesBlocksPB = new ProgressBar
  183. {
  184. Title = "Marquee Blocks",
  185. X = Pos.Center (),
  186. Y = Pos.Bottom (ckbBidirectional) + 1,
  187. Width = Dim.Width (pbList),
  188. ProgressBarStyle = ProgressBarStyle.MarqueeBlocks,
  189. BorderStyle = LineStyle.Single,
  190. CanFocus = true
  191. };
  192. editor.Add (marqueesBlocksPB);
  193. var marqueesContinuousPB = new ProgressBar
  194. {
  195. Title = "Marquee Continuous",
  196. X = Pos.Center (),
  197. Y = Pos.Bottom (marqueesBlocksPB) + 1,
  198. Width = Dim.Width (pbList),
  199. ProgressBarStyle = ProgressBarStyle.MarqueeContinuous,
  200. BorderStyle = LineStyle.Single,
  201. CanFocus = true
  202. };
  203. editor.Add (marqueesContinuousPB);
  204. pbList.SetSource (
  205. editor.Subviews.Where (v => v.GetType () == typeof (ProgressBar))
  206. .Select (v => v.Title)
  207. .ToList ()
  208. );
  209. pbList.SelectedItem = 0;
  210. rbPBFormat.SelectedItemChanged += (s, e) =>
  211. {
  212. blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  213. continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  214. marqueesBlocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  215. marqueesContinuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  216. };
  217. ckbBidirectional.Toggled += (s, e) =>
  218. {
  219. ckbBidirectional.Checked = marqueesBlocksPB.BidirectionalMarquee =
  220. marqueesContinuousPB.BidirectionalMarquee = (bool)!e.OldValue;
  221. };
  222. _pulseTimer = new Timer (
  223. _ =>
  224. {
  225. marqueesBlocksPB.Text =
  226. marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
  227. marqueesBlocksPB.Pulse ();
  228. marqueesContinuousPB.Pulse ();
  229. Application.Wakeup ();
  230. },
  231. null,
  232. 0,
  233. 300
  234. );
  235. Application.Top.Unloaded += Top_Unloaded;
  236. void Top_Unloaded (object sender, EventArgs args)
  237. {
  238. if (_fractionTimer != null)
  239. {
  240. _fractionTimer.Dispose ();
  241. _fractionTimer = null;
  242. }
  243. if (_pulseTimer != null)
  244. {
  245. _pulseTimer.Dispose ();
  246. _pulseTimer = null;
  247. }
  248. Application.Top.Unloaded -= Top_Unloaded;
  249. }
  250. Application.Run (editor);
  251. Application.Shutdown ();
  252. }
  253. public override void Run () { }
  254. }