ProgressBar.cs 9.5 KB

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