ProgressBar.cs 9.3 KB

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