ProgressBar.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System.Text;
  2. using System;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// Specifies the style that a <see cref="ProgressBar"/> uses to indicate the progress of an operation.
  6. /// </summary>
  7. public enum ProgressBarStyle {
  8. /// <summary>
  9. /// Indicates progress by increasing the number of segmented blocks in a <see cref="ProgressBar"/>.
  10. /// </summary>
  11. Blocks,
  12. /// <summary>
  13. /// Indicates progress by increasing the size of a smooth, continuous bar in a <see cref="ProgressBar"/>.
  14. /// </summary>
  15. Continuous,
  16. /// <summary>
  17. /// Indicates progress by continuously scrolling a block across a <see cref="ProgressBar"/> in a marquee fashion.
  18. /// </summary>
  19. MarqueeBlocks,
  20. /// <summary>
  21. /// Indicates progress by continuously scrolling a block across a <see cref="ProgressBar"/> in a marquee fashion.
  22. /// </summary>
  23. MarqueeContinuous
  24. }
  25. /// <summary>
  26. ///Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.
  27. /// </summary>
  28. public enum ProgressBarFormat {
  29. /// <summary>
  30. /// A simple visual presentation showing only the progress bar.
  31. /// </summary>
  32. Simple,
  33. /// <summary>
  34. /// A simple visual presentation showing the progress bar overlaid with the percentage.
  35. /// </summary>
  36. SimplePlusPercentage,
  37. }
  38. /// <summary>
  39. /// A Progress Bar view that can indicate progress of an activity visually.
  40. /// </summary>
  41. /// <remarks>
  42. /// <para>
  43. /// <see cref="ProgressBar"/> can operate in two modes, percentage mode, or
  44. /// activity mode. The progress bar starts in percentage mode and
  45. /// setting the Fraction property will reflect on the UI the progress
  46. /// made so far. Activity mode is used when the application has no
  47. /// way of knowing how much time is left, and is started when the <see cref="Pulse"/> method is called.
  48. /// Call <see cref="Pulse"/> repeatedly as progress is made.
  49. /// </para>
  50. /// </remarks>
  51. public class ProgressBar : View {
  52. bool _isActivity;
  53. int [] _activityPos;
  54. int _delta;
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="ProgressBar"/> class, starts in percentage mode and uses relative layout.
  57. /// </summary>
  58. public ProgressBar ()
  59. {
  60. SetInitialProperties ();
  61. }
  62. void SetInitialProperties ()
  63. {
  64. Height = 1; // This will be updated when Bounds is updated in ProgressBar_LayoutStarted
  65. CanFocus = false;
  66. _fraction = 0;
  67. LayoutStarted += ProgressBar_LayoutStarted;
  68. Initialized += ProgressBar_Initialized;
  69. }
  70. void ProgressBar_Initialized (object sender, EventArgs e)
  71. {
  72. ColorScheme = new ColorScheme (ColorScheme ?? SuperView?.ColorScheme ?? Colors.ColorSchemes ["Base"]) {
  73. HotNormal = new Attribute (Color.BrightGreen, Color.Gray)
  74. };
  75. }
  76. void ProgressBar_LayoutStarted (object sender, EventArgs e)
  77. {
  78. // TODO: use Dim.Auto
  79. Height = 1 + GetFramesThickness ().Vertical;
  80. }
  81. float _fraction;
  82. /// <summary>
  83. /// Gets or sets the <see cref="ProgressBar"/> fraction to display, must be a value between 0 and 1.
  84. /// </summary>
  85. /// <value>The fraction representing the progress.</value>
  86. public float Fraction {
  87. get => _fraction;
  88. set {
  89. _fraction = Math.Min (value, 1);
  90. _isActivity = false;
  91. SetNeedsDisplay ();
  92. }
  93. }
  94. ProgressBarStyle _progressBarStyle;
  95. /// <summary>
  96. /// Gets/Sets the progress bar style based on the <see cref="Terminal.Gui.ProgressBarStyle"/>
  97. /// </summary>
  98. public ProgressBarStyle ProgressBarStyle {
  99. get => _progressBarStyle;
  100. set {
  101. _progressBarStyle = value;
  102. switch (value) {
  103. case ProgressBarStyle.Blocks:
  104. SegmentCharacter = CM.Glyphs.BlocksMeterSegment;
  105. break;
  106. case ProgressBarStyle.Continuous:
  107. SegmentCharacter = CM.Glyphs.ContinuousMeterSegment;
  108. break;
  109. case ProgressBarStyle.MarqueeBlocks:
  110. SegmentCharacter = CM.Glyphs.BlocksMeterSegment;
  111. break;
  112. case ProgressBarStyle.MarqueeContinuous:
  113. SegmentCharacter = CM.Glyphs.ContinuousMeterSegment;
  114. break;
  115. }
  116. SetNeedsDisplay ();
  117. }
  118. }
  119. /// <summary>
  120. /// Specifies the format that a <see cref="ProgressBar"/> uses to indicate the visual presentation.
  121. /// </summary>
  122. public ProgressBarFormat ProgressBarFormat { get; set; }
  123. private Rune _segmentCharacter = CM.Glyphs.BlocksMeterSegment;
  124. /// <summary>
  125. /// Segment indicator for meter views.
  126. /// </summary>
  127. public Rune SegmentCharacter {
  128. get => _segmentCharacter;
  129. set {
  130. _segmentCharacter = value;
  131. SetNeedsDisplay ();
  132. }
  133. }
  134. /// <summary>
  135. /// Gets or sets the text displayed on the progress bar. If set to an empty string and <see cref="ProgressBarFormat"/> is
  136. /// <see cref="ProgressBarFormat.SimplePlusPercentage"/> the percentage will be displayed.
  137. /// If <see cref="ProgressBarStyle"/> is a marquee style, the text will be displayed.
  138. /// </summary>
  139. public override string Text {
  140. get => string.IsNullOrEmpty (base.Text) ? $"{_fraction * 100:F0}%" : base.Text;
  141. set {
  142. if (ProgressBarStyle == ProgressBarStyle.MarqueeBlocks || ProgressBarStyle == ProgressBarStyle.MarqueeContinuous) {
  143. base.Text = value;
  144. }
  145. }
  146. }
  147. bool _bidirectionalMarquee = true;
  148. /// <summary>
  149. /// Specifies if the <see cref="ProgressBarStyle.MarqueeBlocks"/> or the
  150. /// <see cref="ProgressBarStyle.MarqueeContinuous"/> styles is unidirectional
  151. /// or bidirectional.
  152. /// </summary>
  153. public bool BidirectionalMarquee {
  154. get => _bidirectionalMarquee;
  155. set {
  156. _bidirectionalMarquee = value;
  157. SetNeedsDisplay ();
  158. }
  159. }
  160. /// <summary>
  161. /// Notifies the <see cref="ProgressBar"/> that some progress has taken place.
  162. /// </summary>
  163. /// <remarks>
  164. /// If the <see cref="ProgressBar"/> is percentage mode, it switches to activity
  165. /// mode. If is in activity mode, the marker is moved.
  166. /// </remarks>
  167. public void Pulse ()
  168. {
  169. if (_activityPos == null || _activityPos.Length == 0) {
  170. PopulateActivityPos ();
  171. }
  172. if (_activityPos!.Length == 0) {
  173. return;
  174. }
  175. if (!_isActivity) {
  176. _isActivity = true;
  177. _delta = 1;
  178. } else {
  179. for (var i = 0; i < _activityPos.Length; i++) {
  180. _activityPos [i] += _delta;
  181. }
  182. if (_activityPos [^1] < 0) {
  183. for (var i = 0; i < _activityPos.Length; i++) {
  184. _activityPos [i] = i - _activityPos.Length + 2;
  185. }
  186. _delta = 1;
  187. } else if (_activityPos [0] >= Bounds.Width) {
  188. if (_bidirectionalMarquee) {
  189. for (var i = 0; i < _activityPos.Length; i++) {
  190. _activityPos [i] = Bounds.Width + i - 2;
  191. }
  192. _delta = -1;
  193. } else {
  194. PopulateActivityPos ();
  195. }
  196. }
  197. }
  198. SetNeedsDisplay ();
  199. }
  200. ///<inheritdoc/>
  201. public override void OnDrawContent (Rect contentArea)
  202. {
  203. Driver.SetAttribute (GetHotNormalColor ());
  204. Move (0, 0);
  205. if (_isActivity) {
  206. for (int i = 0; i < Bounds.Width; i++)
  207. if (Array.IndexOf (_activityPos, i) != -1) {
  208. Driver.AddRune (SegmentCharacter);
  209. } else {
  210. Driver.AddRune ((Rune)' ');
  211. }
  212. } else {
  213. int mid = (int)(_fraction * Bounds.Width);
  214. int i;
  215. for (i = 0; i < mid & i < Bounds.Width; i++) {
  216. Driver.AddRune (SegmentCharacter);
  217. }
  218. for (; i < Bounds.Width; i++) {
  219. Driver.AddRune ((Rune)' ');
  220. }
  221. }
  222. if (ProgressBarFormat != ProgressBarFormat.Simple && !_isActivity) {
  223. var tf = new TextFormatter () {
  224. Alignment = TextAlignment.Centered,
  225. Text = Text
  226. };
  227. Attribute attr = new Attribute (ColorScheme.HotNormal.Foreground, ColorScheme.HotNormal.Background);
  228. if (_fraction > .5) {
  229. attr = new Attribute (ColorScheme.HotNormal.Background, ColorScheme.HotNormal.Foreground);
  230. }
  231. tf?.Draw (BoundsToScreen (Bounds),
  232. attr,
  233. ColorScheme.Normal,
  234. SuperView?.BoundsToScreen (SuperView.Bounds) ?? default,
  235. fillRemaining: false);
  236. }
  237. }
  238. void PopulateActivityPos ()
  239. {
  240. _activityPos = new int [Math.Min (Frame.Width / 3, 5)];
  241. for (var i = 0; i < _activityPos.Length; i++) {
  242. _activityPos [i] = i - _activityPos.Length + 1;
  243. }
  244. }
  245. ///<inheritdoc/>
  246. public override bool OnEnter (View view)
  247. {
  248. Application.Driver.SetCursorVisibility (CursorVisibility.Invisible);
  249. return base.OnEnter (view);
  250. }
  251. }