ProgressBarStyles.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "ProgressBar Styles", Description: "Shows the ProgressBar Styles.")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("ProgressBar")]
  9. [ScenarioCategory ("Threading")]
  10. public class ProgressBarStyles : Scenario {
  11. private Timer _fractionTimer;
  12. private Timer _pulseTimer;
  13. private const uint _timerTick = 100;
  14. public override void Setup ()
  15. {
  16. const float fractionStep = 0.01F;
  17. const int pbWidth = 20;
  18. var pbFormatEnum = Enum.GetValues (typeof (ProgressBarFormat)).Cast<ProgressBarFormat> ().ToList ();
  19. var rbPBFormat = new RadioGroup (pbFormatEnum.Select (e => NStack.ustring.Make (e.ToString ())).ToArray ()) {
  20. X = Pos.Center (),
  21. Y = 1
  22. };
  23. Win.Add (rbPBFormat);
  24. var ckbBidirectional = new CheckBox ("BidirectionalMarquee", true) {
  25. X = Pos.Center (),
  26. Y = Pos.Bottom (rbPBFormat) + 1
  27. };
  28. Win.Add (ckbBidirectional);
  29. var label = new Label ("Blocks") {
  30. X = Pos.Center (),
  31. Y = Pos.Bottom (ckbBidirectional) + 1
  32. };
  33. Win.Add (label);
  34. var blocksPB = new ProgressBar () {
  35. X = Pos.Center (),
  36. Y = Pos.Y (label) + 1,
  37. Width = pbWidth
  38. };
  39. Win.Add (blocksPB);
  40. label = new Label ("Continuous") {
  41. X = Pos.Center (),
  42. Y = Pos.Bottom (blocksPB) + 1
  43. };
  44. Win.Add (label);
  45. var continuousPB = new ProgressBar () {
  46. X = Pos.Center (),
  47. Y = Pos.Y (label) + 1,
  48. Width = pbWidth,
  49. ProgressBarStyle = ProgressBarStyle.Continuous
  50. };
  51. Win.Add (continuousPB);
  52. var button = new Button ("Start timer") {
  53. X = Pos.Center (),
  54. Y = Pos.Bottom (continuousPB) + 1
  55. };
  56. button.Clicked += (s,e) => {
  57. if (_fractionTimer == null) {
  58. button.Enabled = false;
  59. blocksPB.Fraction = 0;
  60. continuousPB.Fraction = 0;
  61. float fractionSum = 0;
  62. _fractionTimer = new Timer ((_) => {
  63. fractionSum += fractionStep;
  64. blocksPB.Fraction = fractionSum;
  65. continuousPB.Fraction = fractionSum;
  66. if (fractionSum > 1) {
  67. _fractionTimer.Dispose ();
  68. _fractionTimer = null;
  69. button.Enabled = true;
  70. }
  71. Application.MainLoop.Driver.Wakeup ();
  72. }, null, 0, _timerTick);
  73. }
  74. };
  75. Win.Add (button);
  76. label = new Label ("Marquee Blocks") {
  77. X = Pos.Center (),
  78. Y = Pos.Y (button) + 3
  79. };
  80. Win.Add (label);
  81. var marqueesBlocksPB = new ProgressBar () {
  82. X = Pos.Center (),
  83. Y = Pos.Y (label) + 1,
  84. Width = pbWidth,
  85. ProgressBarStyle = ProgressBarStyle.MarqueeBlocks
  86. };
  87. Win.Add (marqueesBlocksPB);
  88. label = new Label ("Marquee Continuous") {
  89. X = Pos.Center (),
  90. Y = Pos.Bottom (marqueesBlocksPB) + 1
  91. };
  92. Win.Add (label);
  93. var marqueesContinuousPB = new ProgressBar () {
  94. X = Pos.Center (),
  95. Y = Pos.Y (label) + 1,
  96. Width = pbWidth,
  97. ProgressBarStyle = ProgressBarStyle.MarqueeContinuous
  98. };
  99. Win.Add (marqueesContinuousPB);
  100. rbPBFormat.SelectedItemChanged += (e) => {
  101. blocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  102. continuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  103. marqueesBlocksPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  104. marqueesContinuousPB.ProgressBarFormat = (ProgressBarFormat)e.SelectedItem;
  105. };
  106. ckbBidirectional.Toggled += (s,e) => {
  107. ckbBidirectional.Checked = marqueesBlocksPB.BidirectionalMarquee = marqueesContinuousPB.BidirectionalMarquee = (bool)!e.OldValue;
  108. };
  109. _pulseTimer = new Timer ((_) => {
  110. marqueesBlocksPB.Text = marqueesContinuousPB.Text = DateTime.Now.TimeOfDay.ToString ();
  111. marqueesBlocksPB.Pulse ();
  112. marqueesContinuousPB.Pulse ();
  113. Application.MainLoop.Driver.Wakeup ();
  114. }, null, 0, 300);
  115. Application.Top.Unloaded += Top_Unloaded;
  116. void Top_Unloaded (object sender, EventArgs args)
  117. {
  118. if (_fractionTimer != null) {
  119. _fractionTimer.Dispose ();
  120. _fractionTimer = null;
  121. }
  122. if (_pulseTimer != null) {
  123. _pulseTimer.Dispose ();
  124. _pulseTimer = null;
  125. }
  126. Application.Top.Unloaded -= Top_Unloaded;
  127. }
  128. }
  129. }
  130. }