ProgressBarStyles.cs 3.9 KB

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