ProgressBarStyles.cs 14 KB

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