ProgressBar.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. namespace Terminal.Gui.Views;
  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, IDesignable
  32. {
  33. private int []? _activityPos;
  34. private int _delta;
  35. private float _fraction;
  36. private bool _isActivity;
  37. private ProgressBarStyle _progressBarStyle = ProgressBarStyle.Blocks;
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="ProgressBar"/> class, starts in percentage mode and uses relative
  40. /// layout.
  41. /// </summary>
  42. public ProgressBar ()
  43. {
  44. Width = Dim.Auto (DimAutoStyle.Content);
  45. Height = Dim.Auto (DimAutoStyle.Content, 1);
  46. CanFocus = false;
  47. _fraction = 0;
  48. }
  49. /// <summary>
  50. /// Specifies if the <see cref="ProgressBarStyle.MarqueeBlocks"/> or the
  51. /// <see cref="ProgressBarStyle.MarqueeContinuous"/> styles is unidirectional or bidirectional.
  52. /// </summary>
  53. public bool BidirectionalMarquee { get; set; } = true;
  54. /// <summary>Gets or sets the <see cref="ProgressBar"/> fraction to display, must be a value between 0 and 1.</summary>
  55. /// <value>The fraction representing the progress.</value>
  56. public float Fraction
  57. {
  58. get => _fraction;
  59. set
  60. {
  61. _fraction = Math.Min (value, 1);
  62. _isActivity = false;
  63. SetNeedsDraw ();
  64. }
  65. }
  66. /// <summary>Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.</summary>
  67. public ProgressBarFormat ProgressBarFormat { get; set; } = ProgressBarFormat.Simple;
  68. /// <summary>Gets/Sets the progress bar style based on the <see cref="Views.ProgressBarStyle"/></summary>
  69. public ProgressBarStyle ProgressBarStyle
  70. {
  71. get => _progressBarStyle;
  72. set
  73. {
  74. _progressBarStyle = value;
  75. switch (value)
  76. {
  77. case ProgressBarStyle.Blocks:
  78. SegmentCharacter = Glyphs.BlocksMeterSegment;
  79. break;
  80. case ProgressBarStyle.Continuous:
  81. SegmentCharacter = Glyphs.ContinuousMeterSegment;
  82. break;
  83. case ProgressBarStyle.MarqueeBlocks:
  84. SegmentCharacter = Glyphs.BlocksMeterSegment;
  85. break;
  86. case ProgressBarStyle.MarqueeContinuous:
  87. SegmentCharacter = Glyphs.ContinuousMeterSegment;
  88. break;
  89. }
  90. SetNeedsDraw ();
  91. }
  92. }
  93. /// <summary>Segment indicator for meter views.</summary>
  94. public Rune SegmentCharacter { get; set; } = Glyphs.BlocksMeterSegment;
  95. /// <summary>
  96. /// Gets or sets the text displayed on the progress bar. If set to an empty string and
  97. /// <see cref="ProgressBarFormat"/> is <see cref="ProgressBarFormat.SimplePlusPercentage"/> the percentage will be
  98. /// displayed. If <see cref="ProgressBarStyle"/> is a marquee style, the text will be displayed.
  99. /// </summary>
  100. public override string Text
  101. {
  102. get => string.IsNullOrEmpty (base.Text) ? $"{_fraction * 100:F0}%" : base.Text;
  103. set
  104. {
  105. if (ProgressBarStyle is ProgressBarStyle.MarqueeBlocks or ProgressBarStyle.MarqueeContinuous)
  106. {
  107. base.Text = value;
  108. }
  109. }
  110. }
  111. ///<inheritdoc/>
  112. protected override bool OnDrawingContent (DrawContext? context)
  113. {
  114. SetAttribute (GetAttributeForRole (VisualRole.Active));
  115. Move (0, 0);
  116. if (_isActivity)
  117. {
  118. for (var i = 0; i < Viewport.Width; i++)
  119. {
  120. if (Array.IndexOf (_activityPos!, i) != -1)
  121. {
  122. AddRune (SegmentCharacter);
  123. }
  124. else
  125. {
  126. AddRune ((Rune)' ');
  127. }
  128. }
  129. }
  130. else
  131. {
  132. var mid = (int)(_fraction * Viewport.Width);
  133. int i;
  134. for (i = 0; (i < mid) & (i < Viewport.Width); i++)
  135. {
  136. AddRune (SegmentCharacter);
  137. }
  138. for (; i < Viewport.Width; i++)
  139. {
  140. AddRune ((Rune)' ');
  141. }
  142. }
  143. if (ProgressBarFormat != ProgressBarFormat.Simple && !_isActivity)
  144. {
  145. var tf = new TextFormatter { Alignment = Alignment.Center, Text = Text };
  146. var attr = new Attribute (
  147. GetAttributeForRole (VisualRole.Active).Foreground,
  148. GetAttributeForRole (VisualRole.Active).Background,
  149. GetAttributeForRole (VisualRole.Active).Style);
  150. if (_fraction > .5)
  151. {
  152. attr = new (
  153. GetAttributeForRole (VisualRole.Active).Background,
  154. GetAttributeForRole (VisualRole.Active).Foreground,
  155. GetAttributeForRole (VisualRole.Active).Style);
  156. }
  157. tf.Draw (
  158. driver: Driver,
  159. screen: ViewportToScreen (Viewport),
  160. normalColor: attr,
  161. hotColor: GetAttributeForRole (VisualRole.Normal),
  162. maximum: SuperView?.ViewportToScreen (SuperView.Viewport) ?? default (Rectangle));
  163. }
  164. return true;
  165. }
  166. /// <summary>Notifies the <see cref="ProgressBar"/> that some progress has taken place.</summary>
  167. /// <remarks>
  168. /// If the <see cref="ProgressBar"/> is percentage mode, it switches to activity mode. If is in activity mode, the
  169. /// marker is moved.
  170. /// </remarks>
  171. public void Pulse ()
  172. {
  173. if (_activityPos is null || _activityPos.Length == 0)
  174. {
  175. PopulateActivityPos ();
  176. }
  177. if (_activityPos!.Length == 0)
  178. {
  179. return;
  180. }
  181. if (!_isActivity)
  182. {
  183. _isActivity = true;
  184. _delta = 1;
  185. }
  186. else
  187. {
  188. for (var i = 0; i < _activityPos.Length; i++)
  189. {
  190. _activityPos [i] += _delta;
  191. }
  192. if (_activityPos [^1] < 0)
  193. {
  194. for (var i = 0; i < _activityPos.Length; i++)
  195. {
  196. _activityPos [i] = i - _activityPos.Length + 2;
  197. }
  198. _delta = 1;
  199. }
  200. else if (_activityPos [0] >= Viewport.Width)
  201. {
  202. if (BidirectionalMarquee)
  203. {
  204. for (var i = 0; i < _activityPos.Length; i++)
  205. {
  206. _activityPos [i] = Viewport.Width + i - 2;
  207. }
  208. _delta = -1;
  209. }
  210. else
  211. {
  212. PopulateActivityPos ();
  213. }
  214. }
  215. }
  216. SetNeedsDraw ();
  217. }
  218. private void PopulateActivityPos ()
  219. {
  220. _activityPos = new int [Math.Min (Frame.Width / 3, 5)];
  221. for (var i = 0; i < _activityPos.Length; i++)
  222. {
  223. _activityPos [i] = i - _activityPos.Length + 1;
  224. }
  225. }
  226. /// <inheritdoc/>
  227. public bool EnableForDesign ()
  228. {
  229. Width = Dim.Fill ();
  230. Height = Dim.Auto (DimAutoStyle.Text, 1);
  231. Fraction = 0.75f;
  232. return true;
  233. }
  234. }