ProgressBar.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. float progress;
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="T:Terminal.Gui.ProgressBar"/> class, starts in percentage mode.
  23. /// </summary>
  24. /// <param name="rect">Rect.</param>
  25. public ProgressBar (Rect rect) : base (rect)
  26. {
  27. CanFocus = false;
  28. progress = 0;
  29. }
  30. float fraction;
  31. /// <summary>
  32. /// Gets or sets the progress indicator fraction to display, must be a value between 0 and 1.
  33. /// </summary>
  34. /// <value>The fraction representing the progress.</value>
  35. public float Fraction {
  36. get => fraction;
  37. set {
  38. fraction = value;
  39. isActivity = false;
  40. SetNeedsDisplay ();
  41. }
  42. }
  43. /// <summary>
  44. /// Notifies the progress bar that some progress has taken place.
  45. /// </summary>
  46. /// <remarks>
  47. /// If the ProgressBar is is percentage mode, it switches to activity
  48. /// mode. If is in activity mode, the marker is moved.
  49. /// </remarks>
  50. public void Pulse ()
  51. {
  52. if (!isActivity) {
  53. isActivity = true;
  54. activityPos = 0;
  55. delta = 1;
  56. } else {
  57. activityPos += delta;
  58. if (activityPos < 0) {
  59. activityPos = 1;
  60. delta = 1;
  61. } else if (activityPos >= Frame.Width) {
  62. activityPos = Frame.Width - 2;
  63. delta = -1;
  64. }
  65. }
  66. SetNeedsDisplay ();
  67. }
  68. public override void Redraw(Rect region)
  69. {
  70. Driver.SetAttribute (ColorScheme.Normal);
  71. int top = Frame.Width;
  72. if (isActivity) {
  73. Move (0, 0);
  74. for (int i = 0; i < top; i++)
  75. if (i == activityPos)
  76. Driver.AddSpecial (SpecialChar.Stipple);
  77. else
  78. Driver.AddRune (' ');
  79. } else {
  80. Move (0, 0);
  81. int mid = (int)(fraction * top);
  82. int i;
  83. for (i = 0; i < mid; i++)
  84. Driver.AddSpecial (SpecialChar.Stipple);
  85. for (; i < top; i++)
  86. Driver.AddRune (' ');
  87. }
  88. }
  89. }
  90. }