2
0

ProgressBarStyles.cs 13 KB

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