ProgressBar.cs 8.5 KB

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