ProgressBar.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// Progress bar can indicate progress of an activity visually.
  5. /// </summary>
  6. /// <remarks>
  7. /// <para>
  8. /// The progressbar can operate in two modes, percentage mode, or
  9. /// activity mode. The progress bar starts in percentage mode and
  10. /// setting the Fraction property will reflect on the UI the progress
  11. /// made so far. Activity mode is used when the application has no
  12. /// way of knowing how much time is left, and is started when you invoke
  13. /// the Pulse() method. You should call the Pulse method repeatedly as
  14. /// your application makes progress.
  15. /// </para>
  16. /// </remarks>
  17. public class ProgressBar : View {
  18. bool isActivity;
  19. int activityPos, delta;
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="T:Terminal.Gui.ProgressBar"/> class, starts in percentage mode with an absolute position and size.
  22. /// </summary>
  23. /// <param name="rect">Rect.</param>
  24. public ProgressBar (Rect rect) : base (rect)
  25. {
  26. CanFocus = false;
  27. fraction = 0;
  28. }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="T:Terminal.Gui.ProgressBar"/> class, starts in percentage mode and uses relative layout.
  31. /// </summary>
  32. public ProgressBar () : base ()
  33. {
  34. CanFocus = false;
  35. fraction = 0;
  36. }
  37. float fraction;
  38. /// <summary>
  39. /// Gets or sets the progress indicator fraction to display, must be a value between 0 and 1.
  40. /// </summary>
  41. /// <value>The fraction representing the progress.</value>
  42. public float Fraction {
  43. get => fraction;
  44. set {
  45. fraction = value;
  46. isActivity = false;
  47. SetNeedsDisplay ();
  48. }
  49. }
  50. /// <summary>
  51. /// Notifies the progress bar that some progress has taken place.
  52. /// </summary>
  53. /// <remarks>
  54. /// If the ProgressBar is is percentage mode, it switches to activity
  55. /// mode. If is in activity mode, the marker is moved.
  56. /// </remarks>
  57. public void Pulse ()
  58. {
  59. if (!isActivity) {
  60. isActivity = true;
  61. activityPos = 0;
  62. delta = 1;
  63. } else {
  64. activityPos += delta;
  65. if (activityPos < 0) {
  66. activityPos = 1;
  67. delta = 1;
  68. } else if (activityPos >= Frame.Width) {
  69. activityPos = Frame.Width - 2;
  70. delta = -1;
  71. }
  72. }
  73. SetNeedsDisplay ();
  74. }
  75. public override void Redraw(Rect region)
  76. {
  77. Driver.SetAttribute (ColorScheme.Normal);
  78. int top = Frame.Width;
  79. if (isActivity) {
  80. Move (0, 0);
  81. for (int i = 0; i < top; i++)
  82. if (i == activityPos)
  83. Driver.AddRune (Driver.Stipple);
  84. else
  85. Driver.AddRune (' ');
  86. } else {
  87. Move (0, 0);
  88. int mid = (int)(fraction * top);
  89. int i;
  90. for (i = 0; i < mid; i++)
  91. Driver.AddRune (Driver.Stipple);
  92. for (; i < top; i++)
  93. Driver.AddRune (' ');
  94. }
  95. }
  96. }
  97. }