ProgressBar.cs 9.4 KB

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