ProgressBar.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. namespace Terminal.Gui;
  2. /// <summary>Specifies the style that a <see cref="ProgressBar"/> uses to indicate the progress of an operation.</summary>
  3. public enum ProgressBarStyle
  4. {
  5. /// <summary>Indicates progress by increasing the number of segmented blocks in a <see cref="ProgressBar"/>.</summary>
  6. Blocks,
  7. /// <summary>Indicates progress by increasing the size of a smooth, continuous bar in a <see cref="ProgressBar"/>.</summary>
  8. Continuous,
  9. /// <summary>Indicates progress by continuously scrolling a block across a <see cref="ProgressBar"/> in a marquee fashion.</summary>
  10. MarqueeBlocks,
  11. /// <summary>Indicates progress by continuously scrolling a block across a <see cref="ProgressBar"/> in a marquee fashion.</summary>
  12. MarqueeContinuous
  13. }
  14. /// <summary>Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.</summary>
  15. public enum ProgressBarFormat
  16. {
  17. /// <summary>A simple visual presentation showing only the progress bar.</summary>
  18. Simple,
  19. /// <summary>A simple visual presentation showing the progress bar overlaid with the percentage.</summary>
  20. SimplePlusPercentage
  21. }
  22. /// <summary>A Progress Bar view that can indicate progress of an activity visually.</summary>
  23. /// <remarks>
  24. /// <para>
  25. /// <see cref="ProgressBar"/> can operate in two modes, percentage mode, or activity mode. The progress bar
  26. /// starts in percentage mode and setting the Fraction property will reflect on the UI the progress made so far.
  27. /// Activity mode is used when the application has no way of knowing how much time is left, and is started when the
  28. /// <see cref="Pulse"/> method is called. Call <see cref="Pulse"/> repeatedly as progress is made.
  29. /// </para>
  30. /// </remarks>
  31. public class ProgressBar : View
  32. {
  33. private int [] _activityPos;
  34. private bool _bidirectionalMarquee = true;
  35. private int _delta;
  36. private float _fraction;
  37. private bool _isActivity;
  38. private ProgressBarStyle _progressBarStyle = ProgressBarStyle.Blocks;
  39. private ProgressBarFormat _progressBarFormat = ProgressBarFormat.Simple;
  40. private Rune _segmentCharacter = Glyphs.BlocksMeterSegment;
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="ProgressBar"/> class, starts in percentage mode and uses relative
  43. /// layout.
  44. /// </summary>
  45. public ProgressBar () { SetInitialProperties (); }
  46. /// <summary>
  47. /// Specifies if the <see cref="ProgressBarStyle.MarqueeBlocks"/> or the
  48. /// <see cref="ProgressBarStyle.MarqueeContinuous"/> styles is unidirectional or bidirectional.
  49. /// </summary>
  50. public bool BidirectionalMarquee
  51. {
  52. get => _bidirectionalMarquee;
  53. set => _bidirectionalMarquee = value;
  54. }
  55. /// <summary>Gets or sets the <see cref="ProgressBar"/> fraction to display, must be a value between 0 and 1.</summary>
  56. /// <value>The fraction representing the progress.</value>
  57. public float Fraction
  58. {
  59. get => _fraction;
  60. set
  61. {
  62. _fraction = Math.Min (value, 1);
  63. _isActivity = false;
  64. }
  65. }
  66. /// <summary>Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.</summary>
  67. public ProgressBarFormat ProgressBarFormat
  68. {
  69. get => _progressBarFormat;
  70. set => _progressBarFormat = value;
  71. }
  72. /// <summary>Gets/Sets the progress bar style based on the <see cref="Terminal.Gui.ProgressBarStyle"/></summary>
  73. public ProgressBarStyle ProgressBarStyle
  74. {
  75. get => _progressBarStyle;
  76. set
  77. {
  78. _progressBarStyle = value;
  79. switch (value)
  80. {
  81. case ProgressBarStyle.Blocks:
  82. SegmentCharacter = Glyphs.BlocksMeterSegment;
  83. break;
  84. case ProgressBarStyle.Continuous:
  85. SegmentCharacter = Glyphs.ContinuousMeterSegment;
  86. break;
  87. case ProgressBarStyle.MarqueeBlocks:
  88. SegmentCharacter = Glyphs.BlocksMeterSegment;
  89. break;
  90. case ProgressBarStyle.MarqueeContinuous:
  91. SegmentCharacter = Glyphs.ContinuousMeterSegment;
  92. break;
  93. }
  94. }
  95. }
  96. /// <summary>Segment indicator for meter views.</summary>
  97. public Rune SegmentCharacter
  98. {
  99. get => _segmentCharacter;
  100. set => _segmentCharacter = value;
  101. }
  102. /// <summary>
  103. /// Gets or sets the text displayed on the progress bar. If set to an empty string and
  104. /// <see cref="ProgressBarFormat"/> is <see cref="ProgressBarFormat.SimplePlusPercentage"/> the percentage will be
  105. /// displayed. If <see cref="ProgressBarStyle"/> is a marquee style, the text will be displayed.
  106. /// </summary>
  107. public override string Text
  108. {
  109. get => string.IsNullOrEmpty (base.Text) ? $"{_fraction * 100:F0}%" : base.Text;
  110. set
  111. {
  112. if (ProgressBarStyle == ProgressBarStyle.MarqueeBlocks
  113. || ProgressBarStyle == ProgressBarStyle.MarqueeContinuous)
  114. {
  115. base.Text = value;
  116. }
  117. }
  118. }
  119. ///<inheritdoc/>
  120. public override void OnDrawContent (Rectangle viewport)
  121. {
  122. Driver.SetAttribute (GetHotNormalColor ());
  123. Move (0, 0);
  124. if (_isActivity)
  125. {
  126. for (var i = 0; i < Viewport.Width; i++)
  127. {
  128. if (Array.IndexOf (_activityPos, i) != -1)
  129. {
  130. Driver.AddRune (SegmentCharacter);
  131. }
  132. else
  133. {
  134. Driver.AddRune ((Rune)' ');
  135. }
  136. }
  137. }
  138. else
  139. {
  140. var mid = (int)(_fraction * Viewport.Width);
  141. int i;
  142. for (i = 0; (i < mid) & (i < Viewport.Width); i++)
  143. {
  144. Driver.AddRune (SegmentCharacter);
  145. }
  146. for (; i < Viewport.Width; i++)
  147. {
  148. Driver.AddRune ((Rune)' ');
  149. }
  150. }
  151. if (ProgressBarFormat != ProgressBarFormat.Simple && !_isActivity)
  152. {
  153. var tf = new TextFormatter { Alignment = Alignment.Center, Text = Text, AutoSize = true };
  154. var attr = new Attribute (ColorScheme.HotNormal.Foreground, ColorScheme.HotNormal.Background);
  155. if (_fraction > .5)
  156. {
  157. attr = new Attribute (ColorScheme.HotNormal.Background, ColorScheme.HotNormal.Foreground);
  158. }
  159. tf?.Draw (
  160. ViewportToScreen (Viewport),
  161. attr,
  162. ColorScheme.Normal,
  163. SuperView?.ViewportToScreen (SuperView.Viewport) ?? default (Rectangle)
  164. );
  165. }
  166. }
  167. /// <summary>Notifies the <see cref="ProgressBar"/> that some progress has taken place.</summary>
  168. /// <remarks>
  169. /// If the <see cref="ProgressBar"/> is percentage mode, it switches to activity mode. If is in activity mode, the
  170. /// marker is moved.
  171. /// </remarks>
  172. public void Pulse ()
  173. {
  174. if (_activityPos is null || _activityPos.Length == 0)
  175. {
  176. PopulateActivityPos ();
  177. }
  178. if (_activityPos!.Length == 0)
  179. {
  180. return;
  181. }
  182. if (!_isActivity)
  183. {
  184. _isActivity = true;
  185. _delta = 1;
  186. }
  187. else
  188. {
  189. for (var i = 0; i < _activityPos.Length; i++)
  190. {
  191. _activityPos [i] += _delta;
  192. }
  193. if (_activityPos [^1] < 0)
  194. {
  195. for (var i = 0; i < _activityPos.Length; i++)
  196. {
  197. _activityPos [i] = i - _activityPos.Length + 2;
  198. }
  199. _delta = 1;
  200. }
  201. else if (_activityPos [0] >= Viewport.Width)
  202. {
  203. if (_bidirectionalMarquee)
  204. {
  205. for (var i = 0; i < _activityPos.Length; i++)
  206. {
  207. _activityPos [i] = Viewport.Width + i - 2;
  208. }
  209. _delta = -1;
  210. }
  211. else
  212. {
  213. PopulateActivityPos ();
  214. }
  215. }
  216. }
  217. SetNeedsDisplay ();
  218. }
  219. private void PopulateActivityPos ()
  220. {
  221. _activityPos = new int [Math.Min (Frame.Width / 3, 5)];
  222. for (var i = 0; i < _activityPos.Length; i++)
  223. {
  224. _activityPos [i] = i - _activityPos.Length + 1;
  225. }
  226. }
  227. private void ProgressBar_Initialized (object sender, EventArgs e)
  228. {
  229. ColorScheme = new ColorScheme (ColorScheme ?? SuperView?.ColorScheme ?? Colors.ColorSchemes ["Base"])
  230. {
  231. HotNormal = new Attribute (Color.BrightGreen, Color.Gray)
  232. };
  233. }
  234. private void SetInitialProperties ()
  235. {
  236. Width = Dim.Auto (DimAutoStyle.Content);
  237. Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: 1);
  238. CanFocus = false;
  239. _fraction = 0;
  240. Initialized += ProgressBar_Initialized;
  241. }
  242. }