ProgressBar.cs 9.4 KB

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